diff --git a/sdk/eventhub/event-hubs/migrationguide.md b/sdk/eventhub/event-hubs/migrationguide.md index 4cf6e61413fa..2bad228155d8 100644 --- a/sdk/eventhub/event-hubs/migrationguide.md +++ b/sdk/eventhub/event-hubs/migrationguide.md @@ -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 @@ -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. @@ -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