I have a spring-boot application with ActiveMQ JMS. I have a queue in the application which will get messages with a string property say color. Value of color can be red, green or blue. Application has a Rest service where it will get list of color(s) { one or more } which should be used as a SELECTOR when listening for the messages on the queue. Over the lifetime of the application, this might change, so the value of SELECTOR can look like "color='red'", "color='blue' OR color='red'" or "color='green'".
@Bean
MessageListenerAdapter adapter() {
return new MessageListenerAdapter(new Object() {
// message handler
});
}
@Bean
DefaultMessageListenerContainer container(ConnectionFactory cf) throws Exception {
DefaultMessageListenerContainer c = new DefaultMessageListenerContainer();
c.setMessageListener(adapter());
c.setConcurrency(this.concurrency);
c.setMessageSelector(this.selector);
c.setConnectionFactory(cf);
c.setDestinationName(this.q);
return c;
}
Was planning to use above code to achieve this; code works fine to start with initial selector, however when selector needs to change following code does not work.
c.stop();
// modify value of selector
c.setMessageSelector(this.selector);
c.start();
Looks like, I have a working solution. I put @Scope("prototype") on top of method container() and have a method which instantiates a new DefaultMessageListenerContainer whenever selector changes.
public void xx(String selector) {
this.selector = selector;
DefaultMessageListenerContainer c =
context.getBean("container", DefaultMessageListenerContainer.class);
c.start();
}
Is this the right way to go about this? Also, when selector changes and I instantiate a new DefaultMessageListenerContainer, what's the correct way to shutdown/stop existing DefaultMessageListenerContainer?
regards, Yogi