Azure Documentation

Event Processors

Event processors are a crucial component for consuming events from Azure Event Hubs. They provide a scalable and fault-tolerant way to read and process events from one or more partitions within an Event Hub.

What are Event Processors?

An event processor is an abstraction that simplifies reading events from Event Hubs. It handles the complexities of connecting to Event Hubs, managing partition ownership, tracking checkpoints (the progress of event consumption), and receiving events. You implement the logic for how to process each received event.

Key Concepts of Event Processors

Event Processor Host

In the context of the older Event Processor Host library (for .NET), this component managed the lifecycle of event processors, handling partition ownership, checkpointing, and event receiving. While still functional, modern development often leverages the Azure SDK's event processor capabilities directly or through libraries like the BlobCheckpointStore for Java.

Note: Modern Azure SDKs (like for .NET, Java, Python, Node.js) provide built-in event processor abstractions that are more streamlined and integrated with best practices for cloud-native applications.

Implementing Event Processing Logic

When you create an event processor, you typically provide handlers for:

Example (Conceptual - .NET SDK)


using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Consumer;
using Azure.Messaging.EventHubs.Processor;
using Azure.Storage.Blobs;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

// ...

string eventHubsConnectionString = "...";
string eventHubName = "my-event-hub";
string blobStorageConnectionString = "...";
string blobContainerName = "eventhub-checkpoints";

BlobServiceClient blobServiceClient = new BlobServiceClient(blobStorageConnectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(blobContainerName);

EventProcessorClient processor = new EventProcessorClient(containerClient, EventHubConsumerClient.DefaultConsumerGroupName, eventHubsConnectionString, eventHubName);

processor.ProcessEventAsync += ProcessEventHandler;
processor.ProcessErrorAsync += ProcessErrorHandler;

await processor.StartProcessingAsync();

// ... later ...

await processor.StopProcessingAsync();

async Task ProcessEventHandler(ProcessEventArgs eventArgs)
{
    Console.WriteLine($"Received event: {Encoding.UTF8.GetString(eventArgs.Data.EventBody.ToArray())}");
    // Your custom event processing logic here
    await eventArgs.UpdateCheckpointAsync(eventArgs.CancellationToken); // Mark as processed
}

Task ProcessErrorHandler(ProcessErrorEventArgs eventArgs)
{
    Console.WriteLine($"Error in processor: {eventArgs.Exception.Message}");
    return Task.CompletedTask;
}
            
Tip: For robust production scenarios, always use a shared checkpoint store (like Azure Blob Storage or Azure Table Storage) and consider implementing retry policies for transient errors.

Event processors are the backbone of reliable event-driven architectures using Azure Event Hubs, enabling you to build scalable and resilient applications that react to incoming data streams.