[rabbitmq-discuss] How to determine if my message was publish successfully

Michael Klishin mklishin at gopivotal.com
Wed Oct 30 21:12:16 GMT 2013


On 31 Oct 2013, at 01:01, cw storm <cwstorm at gmail.com> wrote:

> I've tried that but it appear the block never get executed as I don't see the "this is an error".
> 
> channel.confirmSelect();
> 
> 	channel.addReturnListener(new ReturnListener()
> 	{
> 		public void handleReturn(int replyCode,
> 						String replyText, 
> 						String exchange, 
> 						String routingKey, 
> 						AMQP.BasicProperties props, 
> 						byte[] msgContent)
> 		throws IOException {
> 			throw new RuntimeException("this is an error");
> 			
> 		}
> 	});

If you expect a RuntimeException to be thrown in the thread calling basicPublish, it won't be.
basic.return handlers will be executed in a different thread and the exceptions
they may throw need to be handled with an exception handler (com.rabbitmq.client.impl.ExceptionHandler).

You can do something like

Exception e;
ch.addReturnListener(
  // initialize e here from message attributes 
);
ch.basicPublish(…);
ch.waitForConfirms();

if(e != null) {
  throw e;
}

but since both basic.publish and basic.return are inherently asynchronous in the protocol,
it may be a better idea to handle returned messages in an asynchronous manner.

MK

Software Engineer, Pivotal/RabbitMQ




More information about the rabbitmq-discuss mailing list