channel.basicPublish - Routing key

83 views
Skip to first unread message

cw storm

unread,
May 16, 2016, 9:52:54 AM5/16/16
to rabbitm...@googlegroups.com
Hello,

Have a curious question. I have a try/catch block around
channel.basicPublish method. I was hoping that if I provide an
invalid routing key that isn't bind to the exchange that it'll
exception out. It doesn't appear that way. Could this be true?

Regards,
Phil

Michael Klishin

unread,
May 16, 2016, 9:55:25 AM5/16/16
to rabbitm...@googlegroups.com, cw storm
If you publish messages as mandatory, they will be returned to you (you need to register a returned
message handler first). Channel#basicPublish will not throw exceptions about
unroutable messages: there is no response for basic.publish in the protocol.  
> --
> You received this message because you are subscribed to the Google Groups "rabbitmq-users"
> group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-user...@googlegroups.com.
> To post to this group, send an email to rabbitm...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

--
MK

Staff Software Engineer, Pivotal/RabbitMQ


cw storm

unread,
May 16, 2016, 10:17:57 AM5/16/16
to Michael Klishin, rabbitm...@googlegroups.com
Thanks MK. Just so I follow, can you please provide an example?

cw storm

unread,
May 16, 2016, 10:27:22 AM5/16/16
to Michael Klishin, rabbitm...@googlegroups.com
MK,

I'm assuming you're referring to "clients can implement the
ReturnListener interface and callChannel.setReturnListener." on
https://www.rabbitmq.com/api-guide.html (Handling unroutable
messages).

Michael Klishin

unread,
May 16, 2016, 10:37:21 AM5/16/16
to cw storm, rabbitm...@googlegroups.com
That is correct.  

On 16 May 2016 at 17:27:19, cw storm (cws...@gmail.com) wrote:
> MK,
>
> I'm assuming you're referring to "clients can implement the
> ReturnListener interface and callChannel.setReturnListener." on
> https://www.rabbitmq.com/api-guide.html (Handling unroutable
> messages).
>

cw storm

unread,
May 16, 2016, 1:03:18 PM5/16/16
to Michael Klishin, rabbitm...@googlegroups.com
MK,

I'm trying to structure the ReturnListener call. Do I have to code it this:

channel.basicPublish(exchange,routingkey,properties,byte)

channel.setReturnListener(new ReturnListener() {
public void handleBasicReturn(int replyCode,
String replyText,
String exchange,
String routingKey,
AMQP.BasicProperties properties,
byte[] body)
throws IOException {
...
}
});

cw storm

unread,
May 18, 2016, 1:05:39 AM5/18/16
to Michael Klishin, rabbitm...@googlegroups.com
Not sure why during compilation, method setReturnListener cannot be
found. Curious, isn't this in all version of rabbitmq-client.jar?

cw storm

unread,
May 18, 2016, 1:21:18 AM5/18/16
to Michael Klishin, rabbitm...@googlegroups.com
Hmm...there is a "addReturnListener" in my copy of the
rabbitmq-client.jar Is that the same as setReturnListener? I'm
assuming maybe setReturnListener is in the JDK 6? Unfortunately, my
server is still operating under JDK 5.

Michael Klishin

unread,
May 18, 2016, 1:34:11 AM5/18/16
to cw storm, rabbitm...@googlegroups.com
A number of feature releases ago you could only set one return listener. Now you can set many.
addReturnListener is the method you want.

cw storm

unread,
May 18, 2016, 1:53:03 AM5/18/16
to Michael Klishin, rabbitm...@googlegroups.com
Thanks MK. I was able to compile successfully. I've coded something
below. I've provided an invalid routing key so I'm assuming it should
be listening and doing the "handleReturn" but I don't feel it's doing
that. Going to do more analysis and testing to be for certain.

channel.addReturnListener(new ReturnListener()
{
public void handleReturn(int replyCode, String replyText, String
exchange, String routingKey, AMQP.BasicProperties properties, byte[]
body) throws IOException
{
String str_successflag = "false";
String detailMsg = "Could not publish the message using
exchange:"+exchange+" "+"and "+"routing key:"+routingKey;

}
});

channel.basicPublish(strExchangeName, strRoutingKey, props, byteMessage);

Michael Klishin

unread,
May 18, 2016, 1:58:38 AM5/18/16
to cw storm, rabbitm...@googlegroups.com
For messages to be returned they need to be published as mandatory.

cw storm

unread,
May 18, 2016, 3:05:56 AM5/18/16
to Michael Klishin, rabbitm...@googlegroups.com
That was it.

Thanks,
Phil
Reply all
Reply to author
Forward
0 new messages