c# - 来自多个线程的安慰 session SendRequest() 正在阻塞

标签 c# multithreading solace

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

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(异步),那么它会按预期工作,但我需要请求在同一线程内同步。

是否可以使用同一 session 同时发送请求?

最佳答案

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

正在配置 sendRequest()执行阻塞发送会导致整个 session 等待当前 sendRequest()在另一个之前完成 sendRequest()可以启动。

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

关于c# - 来自多个线程的安慰 session SendRequest() 正在阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49196393/

相关文章:

c# - 插入自定义实体状态代码

c# - 谓词的委托(delegate)不起作用

windows - 为什么我的 ActivePerl 程序报告 'Sorry. Ran out of threads' ?

android - AsyncTask 的 doInBackground 方法阻塞 UI 线程

c# - 使用对象数据填充 GridView

c# - WPF、MVVM中U盘检测放哪里

multithreading - 在 Octave 中使用线程运行代码

docker - 从 Solace 8.4 迁移到 8.5 - Docker 启动时出错

java - Solace 不确认之前的消息是否违反了 JMS 规范?

solace - 能否在 Solace 中检测端点订阅者的存在