4

I have wrote server-client application.
Server Side
server will initilise a queue queue1 with routing key key1 on direct exchange.
After initilise and declaration it consume data whenever someone write on it.

Client Side

client will publish some data on that exchange using routing key key1 .
Also i have set mandotory flag to true before i publish.

Problem

everything is fine when i start server first .but i got problem when i start client first and it publish data with routing key. When client published data there is no exception from broker.

Requirement
I want exception or error when i published data on non existing queue.

Chintan Patel
  • 173
  • 3
  • 14

2 Answers2

6

If you will publish messages with mandatory flag set to true, then that message will returned back in case it cannot be routed to any queue.

As to nonexistent exchanges, it is forbidden to publish messages to non-existent exchanges, so you'll have to get an error about that, something like NOT_FOUND - no exchange 'nonexistent_exchange' in vhost '/'.

You can declare exchanges an queues and bind them as you need on client side too. These operations are idempotent.

Note, that creating and binding exchanges and queues on every publish may have negative performance impact, so do that on client start, not every publish.

P.S.: if you use rabbitmq-c, then it is worth to cite basic_publish documentation

Note that at the AMQ protocol level basic.publish is an async method:

this means error conditions that occur on the broker (such as publishing to a non-existent exchange) will not be reflected in the return value of this function.

pinepain
  • 12,453
  • 3
  • 60
  • 65
  • How to handle unrouted message from client ? – Chintan Patel Oct 01 '15 at 09:33
  • If you mean how to know that message was returned after published with `basic_publish` in rabbitmq-c, then ask it in rabbitmq-c github issues - https://github.com/alanxz/rabbitmq-c/issues, that would be a nice question. – pinepain Oct 01 '15 at 11:38
  • @Zac I have use **amqp_simple_wait_frame_noblock** method to get AMQP_BASIC_RETURN_METHOD . It gives reply_code is **312** and reply_text is **NO_ROUTE** – Chintan Patel Oct 01 '15 at 11:46
2

I spend a lot time to find do that. I have a example code in python using pika lib to show how to send messsage with delivery mode to prevent waiting response when send message to noneexist queue(broker will ignore meessage so that do not need receive response message)

    import pika

    # Open a connection to RabbitMQ on localhost using all default parameters
    connection = pika.BlockingConnection()

    # Open the channel
    channel = connection.channel()

    # Declare the queue
    channel.queue_declare(queue="test", durable=True, exclusive=False, auto_delete=False)

    # Enabled delivery confirmations
    channel.confirm_delivery()

    # Send a message
    if channel.basic_publish(exchange='test',
                     routing_key='test',
                     body='Hello World!',
                     properties=pika.BasicProperties(content_type='text/plain',
                                                     delivery_mode=1),
                     mandatory=True):
            print('Message was published')
    else:
            print('Message was returned')

Reference: http://pika.readthedocs.org/en/latest/examples/blocking_publish_mandatory.html

Son Lam
  • 1,229
  • 11
  • 16