【问题标题】:Solace session SendRequest() from multiple threads is blocking来自多个线程的 Solace 会话 SendRequest() 被阻塞
【发布时间】:2018-03-15 15:25:22
【问题描述】:

我正在尝试使用同一个会话实例发送多个并发请求,看起来会话一个接一个地发送它们,而不是并行发送它们。它在发送下一条消息之前等待回复。

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);
    }
}

如果我将超时设置为 0(异步),那么它会按预期工作,但我需要在同一个线程中同步请求。

是否可以使用同一个会话同时发送请求?

【问题讨论】:

    标签: c# multithreading solace


    【解决方案1】:

    是否可以使用同一个会话同时发送请求?

    sendRequest() 配置为执行阻塞发送会导致整个会话等待当前sendRequest() 完成,然后才能启动另一个sendRequest()

    最好的方法是将sendRequest() 设置为非阻塞并让你的线程等待消息接收回调被触发。您可以使用消息上的 CorrelationId 属性等功能来帮助将请求与回复关联起来。

    【讨论】:

    • 感谢您的回复。我认为这种行为对于大多数开发人员来说是非常危险且出乎意料的。例如,Tibco 允许发送带有超时的并行请求,而您自己的 Java 包装器似乎也允许这样做。我想敦促您改变这种行为,而不是至少更新文档以明确会话队列来自多个线程的请求。
    猜你喜欢
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    相关资源
    最近更新 更多