0

In our code we have attached RabbitTemplate.ReturnCallback() to get notification of not routed messages from consumer and it is working fine for the fixed queue.

But in case of synchronous publish by using rabbitTemplate.sendAndReceive, after receiving message when consumer tries to send response through dynamic queue , ReturnCallback is called always.

Is it the expected behavior? Do we have any way to handle not routed message for dynamic queue.

Below are the beans created in my project , here we have attached one consumer to the queue "test", which receive the published "Message" from queue "test" then put a response to "reply-to" queue.

@Bean
SimpleMessageListenerContainer container( ConnectionFactory connectionFactory ) {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory( connectionFactory );
    container.setQueueNames( "test" );
    container.setMessageListener( exampleListener() );
    return container;
}

@Bean
public ConnectionFactory rabbitConnectionFactory() {
    return new CachingConnectionFactory( "localhost" );
}

@Bean
public RabbitTemplate rabbitTemplate( ConnectionFactory connectionFactory ) {
    RabbitTemplate rabbitTemplate = new RabbitTemplate( connectionFactory );
    rabbitTemplate.setMandatory( true );
    ((CachingConnectionFactory)rabbitTemplate.getConnectionFactory()).setPublisherReturns( true );
    rabbitTemplate.setReturnCallback( new RabbitTemplate.ReturnCallback() {
        @Override
        public void returnedMessage( Message arg0, int arg1, String arg2, String arg3, String arg4 ) {
            System.out.println( "Returned message  " + arg0 );
            System.out.println( "Returned replyText  " + arg2 );
           }
    } );
    return rabbitTemplate;
}
@Bean
public RabbitAdmin rabbitAdmin( ConnectionFactory connectionFactory ){
    return  new RabbitAdmin( connectionFactory );
}


@Bean
public MessageListener exampleListener() {
    return new MessageListener() {
        public void onMessage( Message message ) {
            System.out.println( "received: " + message );
            RabbitTemplate rabbitTemplate = rabbitTemplate( rabbitConnectionFactory() );

            // sending message to reply-to queue( dynamic queue in case of synchronous publish)
            // even though the queue exist and messaage successfully published  , RabbitTemplate.ReturnCallback() is called 
            rabbitTemplate.convertAndSend( message.getMessageProperties().getReplyTo(), "response from consumer" );

        }
    };
}

Rest end point to publish message synchronously to queue "test"

/**

 * publish  Message to Queue.
 *
 * @return response 

 */
@RequestMapping ( value = "/publish", method = RequestMethod.POST )
public String sendMsgToQueue( ) {
    MessageProperties messageProperties = new MessageProperties();
    messageProperties.setCorrelationId("101".getBytes());
    rabbitTemplate.setReplyTimeout( 30000 ); // timeout
    return rabbitTemplate.sendAndReceive( "test", new SimpleMessageConverter().toMessage( "message to test", messageProperties ) ).toString();

}
Prabhat
  • 21
  • 5
  • You need to describe your problem more clearly; you imply the message is returned even if it's routed; the message will only be returned if it can't be routed, regardless of the route. You need to include configuration and a lot more details before anyone can help. – Gary Russell Jun 10 '16 at 14:13
  • Yes Garry, message is being returned with replyText NO_ROUTE even if it is routed to the dynamic queue. – Prabhat Jun 11 '16 at 09:56
  • Yes Garry, message is being returned with replyText NO_ROUTE even if it is routed to the dynamic queue. Scenario : Client is publishing the message to queue synchronously by using the api RabbitTemplate.sendAndReceive(). Then after receiving the published message consumer sends the response through dynamic queue. Issue here is even consumer successfully sends the message through dynamic queue and same message is received as well by client, ReturnCallback gets triggered . – Prabhat Jun 11 '16 at 10:10
  • As described, that makes no sense to me. As I previously asked, please provide example configuration, client and server, that exhibits this strange behavior. – Gary Russell Jun 11 '16 at 17:24
  • @Gary Russel : I have updated the question with example . You can see i am publishing message synchronously by calling the endpoint /publish to the queue "test", which exist in rmq. The attached consumer receive the message and then submit the response to the reply-to queue, which get received by the client , but ReturnCallback is also called with reply text NO_ROUTE – Prabhat Jun 12 '16 at 20:30
  • I don't see how it's possible. Something else must be in the picture. Turn on TRACE logging for `org.springframework.amqp`. If you can't figure it out from that, post the log someplace like github gist or pastebin and maybe someone can help you – Gary Russell Jun 12 '16 at 23:28

0 Answers0