问题
Spring provides @JMSListener annotation to listen to messages from a particular queue. There is also an alternative to implement JmsListenerConfigurer and register a MessageListener.
In both the cases, the name of the queue has to be known at the time when the application starts, either by hardcoding in the code or via properties.
Is there a way to listen to a queue whose name is known to the application much later after it started?
回答1:
This should work.
- Configure the
@JmsListenerwith a dummy queue name and anid. - Configure the listener container factory with
autoStartupfalse. - When you are ready, get a reference to the listener container using it's
idfrom theJmsListenerEndpointRegistry(auto wire the registry into your app and callgetListenerContainer(id)). - Cast the container to an
AbstractMessageListenerContainerand callsetDestinationName(orsetDestination) with the desired queue. start()it.
来源:https://stackoverflow.com/questions/40791567/spring-jms-start-listening-to-jms-queues-on-request