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
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.
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.
The Event Hubs Standard tier does allow message retention periods longer than 24 hours, for a maximum of seven days.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With