10
votes

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

4
In addition to the answers below: do realize that, in terms of performance, it is generally better to send messages in a batch using .SendBatch or .SendBatchAsync instead of sending messages one by one. Especially when the rate of messages becomes higher. It does depend on how fast you want these messages to be delivered to the EventHub.Peter Bons
@PeterBons I agree with you. Using batches of messages is always a good choice. In my case I limit the batch to the maximum allowed batch size and then when it's full I send the batch (I've a huge messages amount).Andrea Cattaneo

4 Answers

4
votes

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

3
votes

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.

1
votes

Here is an example of a client creation using factory:

            var endpointUri = ServiceBusEnvironment.CreateServiceUri("sb", eventHubNamespace, string.Empty);

            MessagingFactory factory = MessagingFactory.Create(endpointUri, new MessagingFactorySettings
            {
                TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(sasToken),
                TransportType = TransportType.Amqp
            });

            factory.RetryPolicy = new RetryExponential(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(30), 3);

            client = factory.CreateEventHubClient(eventHubName);
-2
votes

Yes its is correct, you could create a new EventHubClient each time, it would be fine.

Internally, it reuses the same underlying TCP connection if the connection string is the same. EventHubClient caches results of the MessageFactory.Create(...) methods it uses.