3

I'm trying to send multiple concurrent requests using the same session instance, and it looks like the session sends them one by one instead of sending them in parallel. It awaits for a reply before sending the next message.

using (var client = new SolaceClient())
{
    for (int i = 0; i < 10; i++)
        Task.Factory.StartNew((s) => SendRequest(TOPIC, $"Hello Solace! + {s}", s), i);

    Console.ReadLine();    
}

   ...

public void SendRequest(string topic, string content, object index)
{
    using (IMessage message = ContextFactory.Instance.CreateMessage())
    {
        message.Destination = ContextFactory.Instance.CreateTopic(topic);
        message.BinaryAttachment = Encoding.ASCII.GetBytes(content);
        message.DeliveryMode = MessageDeliveryMode.Direct;

        IMessage replyMessage = null;
        Console.WriteLine($"Sending message....{index}");
        ReturnCode returnCode = _session.SendRequest(message, out replyMessage, 4000);

        if (returnCode == ReturnCode.SOLCLIENT_OK)
            Console.WriteLine(Encoding.ASCII.GetString(replyMessage.BinaryAttachment));
        else
            Console.WriteLine("Request failed, return code: {0}", returnCode);
    }
}

If i set the timeout to 0 (async) then it works as expected, but i need the requests to be synchronous within the same thread.

Is it possible to send simultaneous requests using the same session?

e-master
  • 66
  • 5

1 Answers1

2

Is it possible to send simultaneous requests using the same session?

Configuring sendRequest() to execute a blocking send causes the entire session to wait for the current sendRequest() to complete before another sendRequest() can be initiated.

The best way to do this is to set sendRequest() to non-blocking and have your thread wait for the message receive callback to be triggered. You can use features such as the CorrelationId property on the messages to help correlate the requests with the replies.

Russell Sim
  • 1,693
  • 2
  • 14
  • 22
  • 1
    I appreciate the reply. I think that this behavior is very dangerous and unexpected by most developers. For example Tibco allows sending parallel requests with a timeout, and your own Java wrapper seems to allow this too. I'd like to urge you to change this behavior and not the least update the documentation to make it clear the session queues up requests from multiple threads. – e-master Mar 19 '18 at 13:08