I currently have a JMSListener as shown below. It uses a selector of a value in a properties file. This works fine.
@JmsListener(destination = "myQueueDest",
selector = MyHeaders.SELECTOR_KEY + " = '${myapp.selector_val}'")
private void consumeData(MyCustomObj mycustomObj) { }
I have a need now to use a dynamic selector with a value in memory, rather than the spring property. Is there a way to use JMSListener (or some other listener mechnaism) to do a selection off the ActiveMQ queue?
Update:
It may be possible to assign an ID to my @JMSListener
, and then retrieve it from my JmsListenerEndpointRegistry
bean. Get the listener container by ID, cast it to DefaultMessageListenerContainer
, and call setMessageSelector()
, although I'm not entirely sure if this will work yet.
This requires setting my DefaultJmsListenerContainerFactory bean to have the cache level of CACHE_SESSION
.
But this doesn't seem to work, as the listener picks up all messages, regardless of what I set the message selector to be.
JMS specification says the selection string must be provided while creating a consumer. So the answer is NO. Consumer must be closed and recreated with a different selection string to receive messages that match a different selection criteria.
If using JMS API is not a must for your project, then you could explore using Active MQ's native APIs. I am sure the API will have a way to specify a different selection string every time a receive is called. IBM MQ's native API provides such a functionality.
As stated in one of the comments:
This example explains how to setup at startup but doing it dynamically should be possible with a few more tricks:
And the CustomJmsListenerContainerFactory