我開始使用RabbitMQ,並且在跟隨教程後,我現在試圖按照我需要的方式工作,並且遇到困難。我的設置是我需要能夠首先創建一個RPC,然後根據客戶端將(或不會)發送另一條消息到工作隊列的響應(我不需要響應客戶端)。不幸的是,我努力讓這一切合作似乎沒有按照我想要的方式工作。在服務器端,我有這樣的事情(我已經嘗試了許多變化都具有相同的問題):讓工作隊列和RPC一起工作
var factory = new ConnectionFactory() { HostName = "localhost" };
connection = factory.CreateConnection();
channel = connection.CreateModel();
channel.ExchangeDeclare(exchange: "jobs", type: "direct", durable: true);
// I started with a named queue, not sure if that's better or worse for this
var queueName = channel.QueueDeclare().QueueName;
channel.QueueBind(queue: queueName,
exchange: "jobs",
routingKey: "saveJob_queue");
channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
// save stuff that was sent with the saveJob_queue routingKey
}
channel.BasicConsume(queue: queueName,
noAck: false,
consumer: consumer);
// set up channel for RPC
// Not sure if this has to have another channel, but it wasn't working on the same channel either
rpcChannel = connection.CreateModel();
var rpcQueueName = rpcChannel.QueueDeclare().QueueName;
rpcChannel.QueueBind(queue: rpcQueueName,
exchange: "jobs",
routingKey: "rpc_CheckJob_queue");
var rpcConsumer = new EventingBasicConsumer(rpcChannel);
rpcConsumer.Received += (model, ea) =>
{
// do my remote call and send back a response
}
我的問題是,發送到jobs
交換與路由鍵rpc_CheckJob_queue
仍然消息儘管事實上它應該只接收saveJob_queue
路由,但在第一個信道上結束觸發Recieved
事件。我可以在該處理程序中檢查ea.RoutingKey
,並忽略這些消息,但我不明白他們是如何以及爲什麼最終出現在那裏?
設置連接的正確方法是什麼?以便它可以接收工作隊列消息和RPC消息並正確處理它們?
您是否缺少類似'rpcChannel .BasicConsume(rpcQueueName:queueName,'?,如果您可以發送您的代碼發送郵件 – cantSleepNow