Windows服务上的RabbitMQ多个唯一使用者

时间:2019-05-06 18:28:41

标签: c# rabbitmq windows-services

我已经搜索了StackOverflow,但我认为围绕多个消费者的其他问题与我的尝试不符。

我有一个Windows服务,该服务会启动并注册以监视两个不同的 RabbitMQ队列。如果我仅将其注册以侦听1个队列,则可以正常工作。当我尝试让它监听两个队列时,只有第二个监听器起作用。

var responseConsumer = new EventingBasicConsumer(_responseChannel);

responseConsumer.Received += (model, ea) =>
{
    var message = Encoding.UTF8.GetString(ea.Body);
    // PROCESS MESSAGE
    IssuerProcessor processor = new IssuerProcessor("Processed Message(" + pause + "ms) : ");

    Thread.Sleep(pause);

    var ret = processor.ProcessResponseMessage();
    Console.WriteLine("Processed message: " + ret);
    ulong deliveryTag = ea.DeliveryTag;

    _responseChannel.BasicAck(deliveryTag, false);
    Console.WriteLine("ACK RESPONSE Delivery Tag (" + deliveryTag.ToString() + "): " + ret);
}; // end requestConsumer.Received

_responseChannel.BasicConsume(queue: RESPONSE_QUEUE, autoAck: autoAck, consumer: responseConsumer);

另一个使用者看起来像这个使用者,但是它使用相同的连接和不同的频道监听不同的队列。

这是两者的初始化:

_requestChannel = _connection.CreateModel();
_responseChannel = _connection.CreateModel();

_requestChannel.ExchangeDeclare(EXCHANGE, ExchangeType.Direct, true, false, null);
_requestChannel.QueueDeclare(queue: REQUEST_QUEUE, durable: true, exclusive: false, autoDelete: false, arguments: null);
_requestChannel.BasicQos(0, 1, false);
_requestChannel.QueueBind(REQUEST_QUEUE, EXCHANGE, REQUEST_QUEUE, null);

_responseChannel.ExchangeDeclare(EXCHANGE, ExchangeType.Direct, true, false, null);
_responseChannel.QueueDeclare(RESPONSE_QUEUE, durable: true, exclusive: false, autoDelete: false, arguments: null);
_responseChannel.BasicQos(0, 1, false);
_responseChannel.QueueBind(RESPONSE_QUEUE, EXCHANGE, RESPONSE_QUEUE, null);

1 个答案:

答案 0 :(得分:0)

问题最终导致该服务在单个线程上运行,并且无法同时监视两个队列。

我刚刚创建了两个不同的线程,并在不同的线程上启动了每个使用者。

public void StartWorkerThreads()
{
    Thread receivingConsumerThread = new Thread(StartListenReceivingQueue);
    receivingConsumerThread.Start();

    Thread respondingConsumerThread = new Thread(StartListenRespondingQueue);
    respondingConsumerThread.Start();
}