Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if message was published to a queue using rabbitmq routing features

I've been working on a project which uses rabbitmq to communicate. Recently we discovered that it would be much scalable if we used rabbit routing feature. So basically we bind the queue to several routing keys and use an exchange with type direct.

It's working like publish/subscribe. So it's possible to bind and unbind the queue to different events so consumers/subscribers only receive messages to which they're interested.

Of course, the producer/publisher now uses the binding key (event name) as routing_key to pass it to pika implementation. However, when it publishes something for a binding that doesn't exist the message is lost, i.e. when nobody bound a queue for event foo, but some publisher calls pika.basic_publish(..., routing_key='foo').

So my question is:

Is it possible to know if the message was actually published in a queue?

What I've tried:

  • Checking the return value of pika.basic_publish. It always returns None.

  • Check if there's an exception when we try to publish for a binding that doesn't exist. There is none.

  • Having an additional queue to make out of band control (since all subscribers are run by the same process). This approach doesn't feel ideal to me.

Additional info

  • Since I'm using this routing feature, the queue names are generated by rabbit. I don't have any problem if the new approach has to name the queue itself.

  • If a new approach is suggested which requires binding to exchanges instead of queues, I would like to hear them, but I would prefer to avoid them as they're not actually AMQP and are an extension implemented by rabbitmq.

  • pika version is 0.9.5

  • rabbitmq version is 2.8

Thanks a lot

like image 215
Fred Avatar asked Sep 10 '12 10:09

Fred


People also ask

How do I check my RabbitMQ queue status?

sudo rabbitmqctl list_queues lists the queues.

Is RabbitMQ message in queue?

RabbitMQ is an open-source message broker software written in Erlang. It is commonly called message-oriented middleware that implements the AMQP (Advanced Message Queuing Protocol).

Why RabbitMQ shows activity on message rates but not on queued messages?

convertAndSend was not set properly -- the message was not sent to the correct queue -- the Queued messages was empty all time. however, Message rates is not zero, it does show there are message get sent. Which correspond to what the other answer is saying: In case RabbitMQ receive non-routable message it drop it.


1 Answers

I believe the answer to your problem is the Mandatory flag in RabbitMQ:

This flag tells the server how to react if a message cannot be routed to a queue. Specifically, if mandatory is set and after running the bindings the message was placed on zero queues then the message is returned to the sender (with a basic.return). If mandatory had not been set under the same circumstances the server would silently drop the message.

This basically means, enqueue a message and if it can't be routed then return it to me. Take a look at basic_publish in the specification to turn it on.

like image 183
kzhen Avatar answered Sep 20 '22 04:09

kzhen