RabbitMQ EventBasicConsumer not working
Asked Answered
M

2

8

BACKGROUND INFO

I have a queue (for emails) in RabbitMQ, and want to build a consumer for it. The queue is used by another .NET app for sending emails to customers. I wanted the emailing logic to sit outside of the .NET app, and also have the benefits of durability ...etc that RabbitMQ offers.

ISSUE

The .NET app is able to publish/push emails onto the queue, but I have difficulty building the consumer! Here's my code for the consumer:

// A console app that would be turned into a service via TopShelf
public void Start()
{
  using (_connection = _connectionFactory.CreateConnection())
  {
    using (var model = _connection.CreateModel())
    {
      model.QueueDeclare(_queueName, true, false, false, null);
      model.BasicQos(0, 1, false);

      var consumer = new EventingBasicConsumer(model);
      consumer.Received += (channelModel, ea) =>
      {
        var message = (Email) ea.Body.DeSerialize(typeof(Email));
        Console.WriteLine("----- Email Processed {0} : {1}", message.To, message.Subject);
        model.BasicAck(ea.DeliveryTag, false);
      };
      var consumerTag = model.BasicConsume(_queueName, false, consumer);
    }
  }
}

The code above should be able to grab messages off the queue and process them (according to this official guide), but this isn't happening.

Megrims answered 3/7, 2017 at 8:24 Comment(3)
Many things can go wrong. How do you publish messages (to which exchange)? Also in your current code - connection will be closed immediately after you create your consumer (after BasicConsume) so you won't be able to get any messages anyway. Do not dispose your connection right after starting consumption.Gnu
@Gnu both the publisher and consumer are targeting the same exchange and queue, of that I am certain. But I think you are right about the connection disposal, I'll fix that, and see if that does anything.Megrims
@Gnu you were right, the disposing of the connection was the issue, please reply and I'll mark as answer :)Megrims
G
20

The problem is premature connection disposal. People often think that BasicConsume is a blocking call, but it is not. It will return almost immediately, and the very next statement is disposing (closing) of channel and connection which of course will cancel your subscription. So to fix - store connection and model in private fields and dispose them only when you are done with queue consumption.

Gnu answered 3/7, 2017 at 14:45 Comment(0)
A
0

You said queue is used by another .Net app, is that another consumer? If that is another consumer then can you please confirm which exchange you are using? If you want multiple consumers to pick up the message then please go ahead with "FanOut" exchange

Accrual answered 3/7, 2017 at 14:26 Comment(1)
It turned out to be what @Gnu suggested.Megrims

© 2022 - 2024 — McMap. All rights reserved.