0

I am a complete newbie to rabbitmq messaging, apologies if this question is silly or my setup completely pear-shaped.

My setup where I use rabbitmq is sending messages from certain probes. Each probe has a unique name. I have then a centralised server where I process the data - if there is a need.

I use a direct exchange and routing keys that correspond to probe names.

I declare my consumer (server) as follows (this is more or less from rabbitmq tutorials):

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.exchange_declare(exchange="foo", type="direct")
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue

If at some point I become interested in what a probe is reporting, I issue

channel.queue_bind(exchange="foo", queue=queue_name, routing_key="XXX")

where XXX is the name of the probe.

My publishers at the probes are declared as follows:

connection = pika.BlockingConnection(pika.ConnectionParameters(host="foo.bar.com"))
channel = connection.channel()
channel.exchange_declare(exchange="foo", type="direct")

and when I send a message, I use

channel.basic_publish(exchange="foo", routing_key="XXX", body=data)

where XXX is the name of the probe.

This all works fine. But how do I make it so that messages to routing keys that no one is listening to get discarded immediately? Now if my consumer stops listening to a routing key or is not running at all, messages sent by probes start piling up. When I start my consumer or have it listen to a routing key is has not been listening to in a while, I might have tens of thousands of messages backlog there. This is not what I need, and that backlog is bound to cause a resource exhaustion somewhere.

Is there a way to modify this so that messages get discarded instead of queued if there is no one listening to them when they arrive at the exchange? I would assume there is a way but Google and pika documents did not help.

Thanks in advance.

Hannu
  • 11,685
  • 4
  • 35
  • 51

1 Answers1

1

But how do I make it so that messages to routing keys that no one is listening to get discarded immediately?

By default, Rabbitmq has implemented this.You just need to make sure that there is no queue which is binded to that routing key.

Now if my consumer stops listening to a routing key or is not running at all, messages sent by probes start piling up

If there is no queue for that routing key, all messages will be discarded.

Is there a way to modify this so that messages get discarded instead of queued if there is no one listening to them when they arrive at the exchange?

Rabbitmq defualt behavior itself support this(for Direct Exchange)

Go through page at https://www.rabbitmq.com/tutorials/tutorial-four-python.html

Girdhar Sojitra
  • 648
  • 4
  • 14
  • Ok, so as long as I close the listening queue, messages should be discarded. I will accept this as an answer. Is there a way with rabbitmqctl to check if there are queued messages there, to verify it works? – Hannu Apr 13 '17 at 08:36
  • 1
    To see queued messages you can use rabbitmq management plugin.More details at http://stackoverflow.com/questions/10709533/is-it-possible-to-view-rabbitmq-message-contents-directly-from-the-command-line – Girdhar Sojitra Apr 13 '17 at 08:55