Azure EventHub
Azure EventHub
PLATEFORME
Azure Event Hub
INTRODUCTION
KEY SERVICE CHARACTERISTICS
KEY CONCEPTS
KEY CONCEPTS
KEY CONCEPTS
KEY CONCEPTS
KEY CONCEPTS
KEY CONCEPTS
KEY CONCEPTS
KEY CONCEPTS
KEY CONCEPTS
KEY CONCEPTS
KEY CONCEPTS
KEY CONCEPTS
using System;
using System.Text;
using System.Threading.Tasks;
using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Producer;
namespace _01_SendEvents
{
class Program
{
private const string connectionString = "<event_hub_connection_string>";
private const string eventHubName = "<event_hub_name>";
static async Task Main()
{
// Create a producer client that you can use to send events to an event hub
await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName))
{
// Create a batch of events
using EventDataBatch eventBatch = await producerClient.CreateBatchAsync();
// Add events to the batch. An event is a represented by a collection of bytes and metadata.
eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("First event")));
eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Second event")));
eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Third event")));
// Use the producer client to send the batch of events to the event hub
await producerClient.SendAsync(eventBatch);
Console.WriteLine("A batch of 3 events has been published.");
}
}
KEY CONCEPTS
KEY CONCEPTS
KEY CONCEPTS
KEY CONCEPTS
KEY CONCEPTS
KEY CONCEPTS
KEY CONCEPTS
KEY CONCEPTS
KEY CONCEPTS
MESSAGE ROUTING
using Azure.Storage.Blobs;
using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Consumer;
using Azure.Messaging.EventHubs.Processor;
private const string connectionString = "<event_hub_connection_string>";
private const string eventHubName = "<event_hub_name>";
private const string blobStorageConnectionString = "<blob_connection_string>";
private const string blobContainerName = "<container_name>";
static async Task Main()
{
string consumerGroup = EventHubConsumerClient.DefaultConsumerGroupName;
// Create a blob container client that the event processor will use
BlobContainerClient storageClient = new BlobContainerClient(blobStorageConnectionString, blobContainerName);
// Register handlers for processing events and handling errors static async Task ProcessEventHandler(ProcessEventArgs eventArgs)
{
processor.ProcessEventAsync += ProcessEventHandler;
// Write the body of the event to the console window
processor.ProcessErrorAsync += ProcessErrorHandler; Console.WriteLine("\tRecevied event: {0}", Encoding.UTF8.GetString(eventArgs.Data.Body.ToArray()));
// Start the processing // Update checkpoint in the blob storage so that the app receives only new events the next time it's run
await processor.StartProcessingAsync(); await eventArgs.UpdateCheckpointAsync(eventArgs.CancellationToken);
}
// Wait for 10 seconds for the events to be processed
await Task.Delay(TimeSpan.FromSeconds(10)); static Task ProcessErrorHandler(ProcessErrorEventArgs eventArgs)
{
// Stop the processing // Write details about the error to the console window
Console.WriteLine($"\tPartition '{ eventArgs.PartitionId}': an unhandled exception was encountered. This was not expected to happen.");
await processor.StopProcessingAsync();
Console.WriteLine(eventArgs.Exception.Message);
} return Task.CompletedTask;
}
}
ADDITIONAL FEATURES
MESSAGE ROUTING