Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RabbitMQ-- selectively retrieving messages from a queue

Tags:

I'm new to RabbitMQ and was wondering of a good approach to this problem I'm mulling over. I want to create a service that subscribes to a queue and only pulls messages that meet a specific criteria; for instance, if a specific subject header is in the message.

I'm still learning about RabbitMQ, and was looking for tips on how to approach this. My questions include: how can the consumer pull only specific messages from the queue? How can the producer set a subject header in the message (if that's even the right term?)

like image 471
larryq Avatar asked Jun 21 '12 15:06

larryq


People also ask

How do you consume messages from RabbitMQ?

In order to consume messages there has to be a queue. When a new consumer is added, assuming there are already messages ready in the queue, deliveries will start immediately. The target queue can be empty at the time of consumer registration. In that case first deliveries will happen when new messages are enqueued.

Is RabbitMQ pull or push?

RabbitMQ uses a push-based model with a smart producer, which means the producer decides when to push data. A prefetch limit is defined on the consumer to stop the producer from overwhelming consumers. Such a push-based approach is suited for low latency messaging.

Can RabbitMQ replay messages?

Messages in RabbitMQ can not be replayed, since they are removed once they are ack:ed. However, a RabbitMQ client can nack (negative acknowledgement) a message when it fails to handle the message; this is useful in case of a temporary failure on the consumer side. The message will simply be added back to the queue.


1 Answers

RabbitMQ is perfect for this situation. You have a number of options to do what you want. I suggest reading the documentation to get a better understanding. I would suggest that you use a topic or direct exchange. Topic is more flexible. It goes like this.

Producer code connects to the RabbitMQ Broker and creates and Exchange with a specific name.

Producer publishes to exchange. Each message published will be published with a routing key.

Consumer connects to RabbitMQ broker.

Consumer creates Queue

Consumer binds Queue to the exchange, the same exchange defined in the producer. The binding also includes the routing keys for each message require for this particular consumer.

Lets say you were publishing log messages. The routing key might be something like "log.info", "log.warn", "log.error". Each message published by the producer will have the relevant routing key attached. You will then have a consumer that sends and email for all the error messages and another one that writes all the error messages to a file. So the emailer will define the binding from its queue to the exchange with the routing key "log.error". This way though the exchange receives all messages, the queue defined for the emailer will only contain the error messages. The filelogger will define a new separate queue bound to the same exchange and set up a different routing key. You could do three separate bindings for the three different routing keys require or just use the wildcard "log.*" to request all messages from the exchange starting with log.

This is a simple example that shows how you can achieve what you want to do.

look here for code examples specifically number tutorial number 5.

like image 107
robthewolf Avatar answered Oct 26 '22 05:10

robthewolf