Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it correct to create EventHubClient for every message I send?

I'm creating an EventHub client application that sends message to an Event Hub on Azure. Now when I have a message to send (every 5-6 seconds) I create a new instance of the class EventHubClient, then I use it to send data:

    public async static void SendListOfMeasurements(string eventHubName, string connectionString, List<Measurement> measurementsList, int maxMessageSize)
    {
        // Create EventHubClient
        EventHubClient client = EventHubClient.CreateFromConnectionString(
            connectionString,
            eventHubName);
        ...

Is it a good practice? Or is it better to create it only at startup and then use only the .Send method? What is the best scenario in terms of performance? In the future the ammount of Sends could increase and also the quantity of messages

like image 915
Andrea Cattaneo Avatar asked Sep 25 '15 12:09

Andrea Cattaneo


People also ask

How do I send data to event hub?

Sign in to the Azure Portal. On the portal, click +New > Internet of Things > Event Hubs. In the "Create Namespace" blade, enter the name of your Event Hub in the name field, then choose the Standard Pricing Tier, and choose the desired subscription to create the Event Hub under it.

Where does event hub store data?

Event Hubs Capture enables you to specify your own Azure Blob storage account and container, or Azure Data Lake Storage account, which are used to store the captured data.

What is message retention in event hub?

The Event Hubs Standard tier does allow message retention periods longer than 24 hours, for a maximum of seven days.


2 Answers

Creating a client directly using the EventHubClient.CreateFromConnectionString(...) will create a new tcp connection for each client. However, when creating the client using a shared MessagingFactory

var factory = MessagingFactory.CreateFromConnectionString("your_connection_string");
var client = factory.CreateEventHubClient("MyEventHub");

the client will reuse the underlying tcp connection. From the docs: Create an Event Hubs client

like image 179
lindstromhenrik Avatar answered Oct 15 '22 06:10

lindstromhenrik


No, It's necessary to create a new EventHubClient using the MessageFactory only the first time. If I create a Client for every message I send, then a new connection is created and the old one remain opened (checked using netstat) resulting in a lot of "ESTABILISHED" connections.

Using a single client created for the first message, then recycling the client for the other messages, if the internet connection is lost then comes back, the tcp connection is automatically re-created.

like image 22
Andrea Cattaneo Avatar answered Oct 15 '22 07:10

Andrea Cattaneo