Announcement Announcement Module
Collapse
No announcement yet.
How to extract routing key from message in MessageListenerAdapter Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to extract routing key from message in MessageListenerAdapter

    Hello,
    This is porbably an elementary question.

    How can I extract the routing key from a message when the delegate property of the MessageListenerAdapter is set to my custom POJO. Should my POJO process a Message instead of a byte[] in its defaultListenerMethod()?

    Additionaly is the routing key that is set by a Rabbit Template when sending the message to be consumed, a message property or should an explicit message property with name ex: 'routingKey' => 'some value' be set on the message prior to publish.

    Reason why I am asking this, is I need to parse the routing key(delimited string that has an id as one of its sub string) to extract the ID and pass it along from the custom POJO to another publish component downstream.

    What is best way to go about this.

    Appreciate suggestions/comments.

    Thanks

  • #2
    Yes, if your POJO takes a Message instead of byte[] you will have access to the routing key, exchange etc.

    In fact, instead of using the adapter, you can simply implement MessageListener instead.

    No, you don't set the routing key as a property, it is passed by the template to the rabbit API as an argument.

    As a general note, having the downstream component coupled to the mechanism by which it received the message (routing key) is not necessarily a good idea. It's better to make it more abstract, perhaps using a custom property instead.

    Comment


    • #3
      Hi!

      Let me guess: you provide to the MessageListenerAdapter SimpleMessageConverter, who converts your Message to the byte[] on the MessageListenerAdapter.extractMessage(). So, if you are interested in receivedRoutingKey, you can implement your own MessageConverter:
      Code:
      public class ExtractRoutingKeyMessageConverter extends SimpleMessageConverter {
        public Object fromMessage(Message message) throws MessageConversionException {
            return new Object[] {message.getMessageProperties().getReceivedRoutingKey(), super.fromMessage(message)};
        }
      }
      Then your POJO should take two parameter:
      Code:
        public void handle(String routingKey, byte[] payload) {
       ...
      }
      However, Gary is right: it's bad idea to rely on receivedRoutingKey as some further destination. How about to use replyTo?
      And, of course, take a look into Spring Integration AMQP support: http://static.springsource.org/sprin...mlsingle/#amqp.
      It helps you to have much flexibility as from POJO invocation as from AMQP.

      Take care,
      Artem

      Comment


      • #4
        Thank you both Gary, Artem for your quick response and insight. I will implement as you suggest.
        Thanks again for the advice.

        suraj

        Comment

        Working...
        X