2

Hey so i'm using spring integration's jms:outbound-channel-adapterand need to set a priority on a message before i push it through to the messaging system.

Now in plain JMS i had two ways of doing it.

Either set the priority on the MessageProducer:

this.producer.setPriority(i);

Or on the send method itself:

channel.send(message, DeliveryMode.PERSISTENT, 5, 1000);

Neither of these options are available for me anymore since the channel adapter abstracts me away from these details.

Setting the priority on the message itself works only in the in memory channels of spring integration and loses effect soon as i put it into an actual queue. And turns out setting the priority on the message isn't an option at all: JMS message priority not working on Message

There's an attribute on the channel adapter where i can set the priority, but this is static.

    <jms:outbound-channel-adapter id="101Out" 
                              channel="101MessageChannel"
                              connection-factory="101Factory"
                              destination="QUEUE_NAME"
                              priority="1" />

The max i can do i read it from a property file. (Or so i think. I'm not sure). I can use the destination-expression attribute to inspect the incoming message and dynamically route it to different destinations, but there's no priority-expression counter part for me to do the same with the priority.

I have a work around of sorts, but it's not a very good one:

    <jms:outbound-channel-adapter id="101HighPriorityOut" 
                              channel="101HighPriorityChannel"
                              connection-factory="101Factory"
                              destination-expression="headers.QUEUE_NAME"
                              priority="1"
                              explicit-qos-enabled="true" />

    <jms:outbound-channel-adapter id="101LowPriorityOut" 
                              channel="101LowPriorityChannel"
                              connection-factory="101Factory"
                              destination-expression="headers.QUEUE_NAME"
                              priority="0"
                              explicit-qos-enabled="true" />

I just route the messages to the appropriate outbound adapter once i determine what the priority needs to be. But if the number of priorities increases i'll be in trouble. Even if it doesn't, having two outbound adapters instead of one just coz i couldn't dynamically assign a priority is kinda clumsy i thought.

Appreciate the help :-)

Oh and i'm using Websphere MQ as my message broker. I don't know if this has anything to do with the message broker though.

Community
  • 1
  • 1
alokraop
  • 853
  • 1
  • 11
  • 29

1 Answers1

4

Simply set the priority header in the message...

<int:header-enricher ...>
    <int:priority value="2" />
</int:heaer-enricher>

The priority in the adapter configuration is a default which is used when there's no priority header (you can use a property placeholder to set it from a properties file).

Or, use an expression...

<int:header-enricher ...>
    <int:priority expression="payload.foo == 'bar' ? 1 : 2" />
</int:heaer-enricher>

<int:header-enricher ...>
    <int:priority expression="payload.priority" />
</int:heaer-enricher>

<int:header-enricher ...>
    <int:priority expression="@someBean.calculatePriority(payload)" />
</int:heaer-enricher>
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • I'd still be doing a static lookup right? Instead of two channel adapters, i now have two header enrichers and one channel adapter. I kinda need a way to lookup data from the payload itself and assign it to the priority. Without that i'll just end up writing extra enrichers for every new priority that gets added. – alokraop Oct 21 '15 at 12:00
  • Use an expression in header enricher: `` etc. – Gary Russell Oct 21 '15 at 13:11
  • Oh awesome. That totally simplifies things. Thanks a lot! – alokraop Oct 21 '15 at 13:25
  • While this answer is correct, this has been broken since spring-integration 4.1.0.RC1 (at least until 5.2.3.RELEASE and depending on the underlying JMS provider) See https://github.com/spring-projects/spring-integration/issues/3179 – Michel Jung Feb 13 '20 at 10:39