Skip to content

Commit

Permalink
[event-hubs] add checkkpointing to migration guide sample (#13016)
Browse files Browse the repository at this point in the history
  • Loading branch information
chradek authored Dec 24, 2020
1 parent a951f24 commit 107a514
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions sdk/eventhub/event-hubs/migrationguide.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ This and the need to support improvements to the algorithm used for managing par
Other noteworthy changes:

- The `send` method on the client that allowed sending single events in each call is removed
in favor of the `sendBatch` to encourage sending events in batches for better throughput.
in favor of the `sendBatch` to encourage sending events in batches for better throughput.
- The `sendBatch` method on the client has two overloads. One takes an array of events. The other takes an
object of type `EventDataBatch` that should be created using the `createBatch` method on the client. This object represents the batch and can be safely filled until the maximum size allowed.
object of type `EventDataBatch` that should be created using the `createBatch` method on the client. This object represents the batch and can be safely filled until the maximum size allowed.

#### Migrating from `EventHubClient` to `EventHubProducerClient` for sending events

Expand Down Expand Up @@ -257,7 +257,13 @@ const eph = EventProcessorHost.createFromConnectionString(
// In V2, you get a single event passed to your callback. If you had asynchronous code running in your callback,
// it is not awaited before the callback is called for the next event.
const onMessage = (context, event) => {
/** your code here **/
/** Your code to process the event here **/

// Note: EventProcessorHost can invoke the onMessage handler
// before the previous onMessage handler invocation has completed.
// Special care needs to be taken to ensure that checkpointing happens
// on events in order.
context.checkpointFromEventData(event);
};

// This is your error handler for errors occuring when receiving events.
Expand Down Expand Up @@ -290,7 +296,16 @@ const subscription = eventHubConsumerClient.subscribe(partitionId, {
// If your callback is an async function or returns a promise, it will be awaited before the
// callback is called for the next batch of events.
processEvents: (events, context) => {
/** your code here **/
/** Your code to process events here **/

// The events array could be empty, so only checkpoint if it contained events.
if (events.length) {
// Save a checkpoint for the last event now that we've processed this batch.
// Note: EventHubConsumerClient will wait for `processEvents` to return before
// calling it again for the same partition id.
// This allows you to safely update your checkpoints while receiving events.
await context.updateCheckpoint(events[events.length - 1]);
}
},

// Prior to V5, errors were handled by separate callbacks depending
Expand Down

0 comments on commit 107a514

Please sign in to comment.