user1052610
user1052610

Reputation: 4719

Spring Integration: getting the name of the underlying Queue from a Message or MessageChannel

Given a MessageChannel or Message object, how is it possible to get from one of them the name of the underlying JMS Queue which the message was received on ?

Here is the scenario: Several jms:message-driven-channel-adapter instances are defined in the xml. The destination-name of each adapter uses SEL to receive from different queues. This SEL is dynamic, and is not possible to know these queue names ahead of time. All channel adapters output to the same internal Spring Integration channel.

I want to add the actual underlying queue name which the message was received on to the header of the message.

The idea is to setup a ChannelInterceptor for either the channel-adapters or the internal channel. The postReceive() method has both the Message and MessageChannel as arguments. Using either of these, is it possible to get the name of the underlying Queue name which the message came in on? Thanks

Upvotes: 0

Views: 1299

Answers (2)

Daniel Steinmann
Daniel Steinmann

Reputation: 2199

This is how we did it:

  <int:header-enricher>
     <int:header name="JMS_DESTINATION" expression="payload.JMSDestination.queueName"/>
  </int:header-enricher>

It requires extract-payload="false" in your <jms:message-driven-channel-adapter>.

P.S. The answer of Artem is missing the return statement (I do not have enough reputations to comment).

Upvotes: 0

Artem Bilan
Artem Bilan

Reputation: 121550

Looks like you need to extend a bit DefaultJmsHeaderMapper:

class DestinationJmsHeaderMapper extends DefaultJmsHeaderMapper {
    public Map<String, Object> toHeaders(javax.jms.Message jmsMessage) {
        Map<String, Object> headers = super.toHeaders(jmsMessage);
        headers.put("JMS_DESTINATION", ((Queue) jmsMessage.getJMSDestination()).getQueueName());
    }
}

And inject it to your <jms:message-driven-channel-adapter>s

Upvotes: 1

Related Questions