I'm working on a Spring boot JMS application that is setup strictly with bean annotations and is reading messages from WebshpereMQ. Everything works except I can't figure out how to safely shutdown this application. Once my JMSListener Method reads all the messages it just stays idle. I make an initial connection to the queue and access the queue depth, so ideally when queue depth is zero, it needs to wrap it up and shut down. My current workaround (and I do not like it at all) is this little method I call (from inside the listener, yikes) when depth is zero:
public void shutDownApplication() {
logger.info("Initiating shutdown of application...");
System.out.println("Terminating application...");
Thread.currentThread().interrupt();
System.exit(0);
}
I do not like this solution. And neither does Spring, because this is obviously interrupted as an error mid-process and before the application dies my JMSListener initiates a rollback and puts the single last remaining message back on the queue.
I tried a few other solutions after looking at the following sources:
How can I Stop/start/Pause a @JmsListener (the clean way)
How to gracefully shut down a Spring JMS MessageListenerAdapter
This was my most recent solution:
public class JMSShutdownService {
public void initiateShutdown() {
JmsListenerEndpointRegistry jmsListenerEndpointRegistry = new JmsListenerEndpointRegistry();
Collection<MessageListenerContainer> col = jmsListenerEndpointRegistry
.getListenerContainers();
for (MessageListenerContainer cont : col) {
cont.stop(Thread.currentThread());
}
System.exit(0);
}
}
This kills the application but still puts the last message back on the queue. There are alot of intricacies of Spring I'm still trying to understand, so it all really comes down to that. I feel like the main issue is that it is inside the listener I signal for shutdown. From what I gather the listener shouldn't be responsible for that. But I'm not sure how to define a way to shutdown the application before the listener starts, or how to pop out of the listener when queue depth is zero.
Any ideas?