[rabbitmq-discuss] Synchronous confirmation of delivery to the queue

Matthew Sackman matthew at rabbitmq.com
Mon Jul 5 22:14:54 BST 2010


On Mon, Jul 05, 2010 at 03:39:54PM +0300, Andrius Norkaitis wrote:
> I tried it, but BasicPublish and TxCommit completes event if there are no
> queues to route.
> public bool Publish() {
>           try{
> 	ch.TxSelect();
> 	bool Mandatory = true; Immediate = false;
> 	ch.BasicPublish("direct", routingKey,Mandatory, Immediate,
> basicProperties, body);
> 	ch.TxCommit();
> 	return true;
>           }
> 	catch { return false; }
> }
> I always get TRUE event I there is no consumer/query available.

Hmm, we do the basic.return first - the following is a transcript, using
the erlang client:

6> Conn = amqp_connection:start_network_link().

=INFO REPORT==== 5-Jul-2010::14:09:15 ===
Negotiated maximums: (Channel = 0, Frame= 131072, Heartbeat=0)
7> Chan = amqp_connection:open_channel(Conn).  
8> amqp_channel:call(Chan, #'tx.select'{}).    
9> amqp_channel:call(Chan, #'basic.publish'{ routing_key =
<<"no_such_queue">>, mandatory = true }, #amqp_msg{ payload = <<>>,
props = #'P_basic'{} }).
=ERROR REPORT==== 5-Jul-2010::14:10:28 ===
Channel (<0.63.0>): received {{'basic.return',312,<<"NO_ROUTE">>,<<>>,
                                  <<"no_such_queue">>}, {amqp_msg,
                                                         <<>>}} but
there is no return handler registered
10> amqp_channel:call(Chan, #'tx.commit'{}).                                                                                                            

The commit comes back ok because the message never got stored as part of
the commit because we detected the missing queue early and returned it.
Thus the txn ended up being empty, which is why the commit happened
without error.

> How to detect that message wasn't published to the queue in same method
> calling BasicPublish? I need to return bool flag after trying to publish.

You should install a return handler on the Channel:
ChannelN.setReturnListener. I think you can depend on that being called
before the result of the commit comes back. You'll need to be careful
with different threads, but you should effectively be able to have the
return listener set some sort of boolean, and then only check that
boolean after the commit has returned.


More information about the rabbitmq-discuss mailing list