I'm using Azure ServiceBus Topics to send messages from one application (WorkerRole) to another (WorkerRole). It's important that this adding to the message topic queue is done as quickly as possible however I'm finding that the Send()
method of a TopicClient
varies anywhere from 1second to 1min at times.
The setup I have on my Azure Dashboard:
- 1 namespace
- 1 Topic
- 4 Subscriptions
I send to the topic from 2 different clients however the amount of messages I send is fairly small at the moment i.e. every few seconds I'll send a messages to 2 different subscriptions.
What I'm noticing is that the messages being sent to the subscriptions seem to vary in time taken to send. Below is my code for sending.
private void SendToSubscription(ReceivedQueue record, ServiceBusTopicFilter topicFilter)
{
var connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus." + topicFilter.Topic);
var factory = MessagingFactory.CreateFromConnectionString(connectionString);
var topicClient = factory.CreateTopicClient(topicFilter.Topic);
try
{
// The Filter is so that different handlers of messages can receive only the ones they are desiring
var message = AsBrokeredMessage(record, topicFilter.Filter);
_traceMonitor.TraceInformation(record.Uid, "Sending packet to topic " + "[" + record.PacketId + "] [" + topicFilter.Filter + "]");
// NOTE: From my logging this part seems to take a while to action sometimes
topicClient.Send(message);
}
catch (MessagingException e)
{
// TODO: Handle transient errors properly to ensure we don't missing packet ??
Trace.TraceError("SendToSubscription.MessagingException :: " + e.Message);
}
catch (Exception ex)
{
Trace.TraceError("SendToSubscription.Exception :: " + ex);
}
}
Would the processing of the messages effect this sending? I can post that code if required.
Thanks to Gaurav Mantri for pointing me in a possible direction, I found a method which appears to be faster.
It appears that I was creating a new MessageFactory on every request. As indicated by MSDN it is more efficient to re-use factories where we can.
So what I ended up doing was creating a message factory pool so that in general I create new Message factory Connections rarely.
This meant that I'm now seeing immediate message sending onto the Service Bus queue and the only time I see the initial 5sec delay is when I'm first creating the MessageFactory object and then using it for the first time.