Giter Club home page Giter Club logo

azureeventhub's Introduction

โ† Return to AZ-204

.NET

Azure Event Hub

In this example I am Sending and Receiving messages with Azure Event Hub. For this I used the: Azure.Messaging.EventHubs, Azure.Messaging.EventHubs.Processor, Azure.Storage.Blobs NuGet packages. I used Blob storage with the EventProccesoorClient, which will come up later on. I also used dotnet secrets with my joerivanarkel.UserSecrets package.

Before writing any code i have created am Azure Event Hub namespace and a Storage Account in the Azure Portal. Furthermore i created an Event Hub in the Namespace. In this example is how to work with the Event Hub.

Sending Data to an Event Hub

I firstly create a Batch of events I will be sending to the Event Hub, with the EventDataBatch object. I used the EventHubProducerClient i created earlier. Then i add 5 events to this batch. I send these to the Event Hub with the SendAsync() method. Finally i close the connection.

using EventDataBatch eventBatch = await producerClient.CreateBatchAsync();

for (int i = 1; i <= 5; i++)
{
  var result = eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes($"Event {i}")));
  if (!result)
  {
    throw new Exception($"Event {i} is too large for the batch and cannot be sent.");
  }
}

await producerClient.SendAsync(eventBatch);
Console.WriteLine($"A batch of 5 events has been published.");
await producerClient.DisposeAsync();
return true;

Recieving Data from an Event Hub

Using the EventProcessorClient class, I assign the Event and Error events to the methods which will handle them. Then I start the processor, wait and close the connection. This method on maintains a connection and sends the events to the right handler.

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

await processor.StartProcessingAsync();
await Task.Delay(TimeSpan.FromSeconds(30));
await processor.StopProcessingAsync();

Task processEventHandler(ProcessEventArgs eventArgs) => Task.CompletedTask;
Task processErrorHandler(ProcessErrorEventArgs eventArgs) => Task.CompletedTask;
return true;

In these handlers I take the arguments given and display the received or return the exception.

protected static async Task ProcessEventHandler(ProcessEventArgs eventArgs)
{
  Console.WriteLine($"Received event: {Encoding.UTF8.GetString(eventArgs.Data.Body.ToArray())}");
  await eventArgs.UpdateCheckpointAsync(eventArgs.CancellationToken);
}

protected static Task ProcessErrorHandler(ProcessErrorEventArgs eventArgs)
{
  Console.WriteLine($"Partition '{eventArgs.PartitionId}': an unhandled exception was encountered. This was not expected to happen.");
  Console.WriteLine(eventArgs.Exception.Message);
  return Task.CompletedTask;
        }
    }

azureeventhub's People

Contributors

dependabot[bot] avatar joerivanarkel avatar

Stargazers

 avatar

Watchers

 avatar

azureeventhub's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.