我正在尝试使用同一个 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/