简体   繁体   English

Windows 服务上的 RabbitMQ 多个唯一消费者

[英]RabbitMQ Multiple Unique Consumers on Windows Service

I've searched StackOverflow and I don't think the other issues around multiple consumers match what I am trying.我已经搜索了 StackOverflow,但我认为围绕多个消费者的其他问题与我正在尝试的问题不符。

I have a Windows service that starts and registers to monitor two different RabbitMQ queues.我有一个 Windows 服务,它启动并注册以监视两个不同的RabbitMQ 队列。 If I only register it to listen to 1 queue, it works fine.如果我只注册它来监听 1 个队列,它就可以正常工作。 When I try to get it to listen to both queues, only the 2nd listener works.当我尝试让它收听两个队列时,只有第二个侦听器有效。

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

The other consumer looks just like this one, but it listens to a different queue, using the same connection, with a different channel.另一个消费者看起来就像这个,但它使用不同的通道使用相同的连接侦听不同的队列。

Here is the initialization for both:这是两者的初始化:

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

The problem ended up being that the service was running on a single thread and it could not monitor both queues at the same time.问题最终是该服务在单个线程上运行,并且无法同时监视两个队列。

I just created two different Threads and started each consumer on a different Thread.我刚刚创建了两个不同的线程,并在不同的线程上启动了每个使用者。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM