Results 1 to 4 of 4

Thread: How to extract routing key from message in MessageListenerAdapter

  1. #1
    Join Date
    Sep 2008
    Posts
    12

    Question 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. #2
    Join Date
    Mar 2010
    Location
    Gtr Philadelphia, PA
    Posts
    2,037

    Default

    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.
    Gary P. Russell
    Spring Integration Team
    SpringSource, a division of VMware

  3. #3
    Join Date
    Jan 2009
    Location
    Ukraine, Kharkov
    Posts
    645

    Default

    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

  4. #4
    Join Date
    Sep 2008
    Posts
    12

    Default

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

    suraj

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •