DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Playing with Azure Event Hub

Playing with Azure Event Hub

One developer shares with us his experience in poking around Azure Event Hub and helps in getting started with using it.

Paul Michaels user avatar by
Paul Michaels
·
May. 23, 18 · Presentation
Like (1)
Save
Tweet
Share
11.26K Views

Join the DZone community and get the full member experience.

Join For Free

I've recently been playing with the Azure Event Hub. This is basically a way of transmitting large amounts* of data between systems. In a later post, I may try and test these limits by designing some kind of game based on this.

As a quick disclaimer, it's worth bearing in mind that I am playing with this technology, and so much of the content of this post can be found in the links at the bottom of this post — you won't find anything original here, just a record of my findings. You may find more (and more accurate) information in those.

The first step, as with many Azure services, is to create a namespace:

For a healthy amount of data transference, you'll pay around £10 per month.

Finally, we'll create event hub within the namespace:

When you create the event hub, it asks how many partitions you need. This basically splits the message delivery, and it's clever enough to work out that if you have 3 partitions and two listeners you should have two slots, and one listener needs only one slot:

We'll need an access policy so that we have permission to listen:

We'll need to create two applications: a producer and a consumer.

Let's start with a producer. Create a new console app and add this NuGet library.

Here's the code:

class Program {
 private static EventHubClient eventHubClient;
 private
 const string EhConnectionString = "Endpoint=sb://pcm-testeventhub.servicebus.windows.net/;SharedAccessKeyName=Publisher;SharedAccessKey=key;EntityPath=pcm-eventhub1";
 private
 const string EhEntityPath = "pcm-eventhub1";

 public static async Task Main(string[] args) {
  EventHubsConnectionStringBuilder connectionStringBuilder = new EventHubsConnectionStringBuilder(EhConnectionString) {
   EntityPath = EhEntityPath
  };

  eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());

  while (true) {
   Console.Write("Please enter message to send: ");
   string message = Console.ReadLine();
   if (string.IsNullOrWhiteSpace(message)) break;

   await eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(message)));
  }

  await eventHubClient.CloseAsync();

  Console.WriteLine("Press ENTER to exit.");
  Console.ReadLine();
 }
}

Consumer

Next we'll create a consumer, so the first thing we'll need is to grant permissions for listening:

We'll create a second new console application with this same library and the processor library, too.

class Program {
 private
 const string EhConnectionString = "Endpoint=sb://pcm-testeventhub.servicebus.windows.net/;SharedAccessKeyName=Listener;SharedAccessKey=key;EntityPath=pcm-eventhub1";
 private
 const string EhEntityPath = "pcm-eventhub1";
 private
 const string StorageContainerName = "eventhub";
 private
 const string StorageAccountName = "pcmeventhubstorage";
 private
 const string StorageAccountKey = "key";

 private static readonly string StorageConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", StorageAccountName, StorageAccountKey);

 static async Task Main(string[] args) {
  Console.WriteLine("Registering EventProcessor...");

  var eventProcessorHost = new EventProcessorHost(
   EhEntityPath,
   PartitionReceiver.DefaultConsumerGroupName,
   EhConnectionString,
   StorageConnectionString,
   StorageContainerName);

  // Registers the Event Processor Host and starts receiving messages
  await eventProcessorHost.RegisterEventProcessorAsync < EventsProcessor > ();

  Console.WriteLine("Receiving. Press ENTER to stop worker.");
  Console.ReadLine();

  // Disposes of the Event Processor Host
  await eventProcessorHost.UnregisterEventProcessorAsync();
 }
}

class EventsProcessor: IEventProcessor {
 public Task CloseAsync(PartitionContext context, CloseReason reason) {
  Console.WriteLine($ "Processor Shutting Down. Partition '{context.PartitionId}', Reason: '{reason}'.");
  return Task.CompletedTask;
 }

 public Task OpenAsync(PartitionContext context) {
  Console.WriteLine($ "SimpleEventProcessor initialized. Partition: '{context.PartitionId}'");
  return Task.CompletedTask;
 }

 public Task ProcessErrorAsync(PartitionContext context, Exception error) {
  Console.WriteLine($ "Error on Partition: {context.PartitionId}, Error: {error.Message}");
  return Task.CompletedTask;
 }

 public Task ProcessEventsAsync(PartitionContext context, IEnumerable < EventData > messages) {
  foreach(var eventData in messages) {
   var data = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);
   Console.WriteLine($ "Message received. Partition: '{context.PartitionId}', Data: '{data}'");
  }

  return context.CheckpointAsync();
 }
}

As you can see, we can now transmit data through the Event Hub into client applications:

Footnotes

*Large, in terms of frequency, rather than volume — for example, transmitting a small message twice a second, rather than uploading a petabyte of data.

https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-dotnet-standard-getstarted-send
https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-dotnet-standard-getstarted-receive-eph
Event azure

Published at DZone with permission of Paul Michaels, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 2023 Software Testing Trends: A Look Ahead at the Industry's Future
  • SAST: How Code Analysis Tools Look for Security Flaws
  • Automated Performance Testing With ArgoCD and Iter8
  • Spring Cloud: How To Deal With Microservice Configuration (Part 1)

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: