Skip to content

Commit fae9dfb

Browse files
authored
example code cleanup (#632)
* example code cleanup * added note about auto.offset.reset * added OnError handler to example
1 parent 0e827e8 commit fae9dfb

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,8 @@ optimizing communication with the Kafka brokers for you, batching requests as ap
8181

8282
```csharp
8383
using System;
84-
using System.Collections.Generic;
85-
using System.Text;
8684
using System.Threading.Tasks;
8785
using Confluent.Kafka;
88-
using Confluent.Kafka.Serialization;
8986

9087
class Program
9188
{
@@ -118,10 +115,7 @@ make use the `BeginProduce` method instead:
118115

119116
```csharp
120117
using System;
121-
using System.Collections.Generic;
122-
using System.Text;
123118
using Confluent.Kafka;
124-
using Confluent.Kafka.Serialization;
125119

126120
class Program
127121
{
@@ -152,11 +146,7 @@ class Program
152146

153147
```csharp
154148
using System;
155-
using System.Collections.Generic;
156-
using System.Text;
157-
using System.Threading.Tasks;
158149
using Confluent.Kafka;
159-
using Confluent.Kafka.Serialization;
160150

161151
class Program
162152
{
@@ -166,14 +156,24 @@ class Program
166156
{
167157
GroupId = "test-consumer-group",
168158
BootstrapServers = "localhost:9092",
159+
// Note: The AutoOffsetReset property determines the start offset in the event
160+
// there are not yet any committed offsets for the consumer group for the
161+
// topic/partitions of interest. By default, offsets are committed
162+
// automatically, so in this example, consumption will only start from the
163+
// eariest message in the topic 'my-topic' the first time you run the program.
169164
AutoOffsetReset = AutoOffsetResetType.Earliest
170165
};
171166

172167
using (var c = new Consumer<Ignore, string>(conf))
173168
{
174169
c.Subscribe("my-topic");
175170

176-
while (true)
171+
bool consuming = true;
172+
// The client will automatically recover from non-fatal errors. You typically
173+
// don't need to take any action unless an error is marked as fatal.
174+
c.OnError += (_, e) => consuming = !e.IsFatal;
175+
176+
while (consuming)
177177
{
178178
try
179179
{
@@ -186,6 +186,7 @@ class Program
186186
}
187187
}
188188

189+
// Ensure the consumer leaves the group cleanly and final offsets are committed.
189190
c.Close();
190191
}
191192
}

0 commit comments

Comments
 (0)