Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RabbitMQ EventBasicConsumer not working

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.

like image 915
J86 Avatar asked Jul 03 '17 08:07

J86


People also ask

What happens when RabbitMQ is down?

In detail: The messages that RabbitMQ publish are persistent. When the message broker goes down, the events are not published and I am planning to store these events in a database and publish them again to RabbitMQ when it is up.

When RabbitMQ quits or crashes What's the default Behaviour for queues & messages?

When RabbitMQ quits or crashes it will forget the queues and messages unless you tell it not to. Two things are required to make sure that messages aren't lost: we need to mark both the queue and messages as durable.

What is auto ack in RabbitMQ?

Acknowledgement is basically a notification for RabbitMQ broker that a particular message has been processed and the broker doesn't need to worry about it anymore. In particular it shouldn't redeliver to message to this or some other consumer.


1 Answers

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.

like image 105
Evk Avatar answered Sep 30 '22 03:09

Evk