This questions is related to: Shutdown ExecutorService gracefully in webapp?
I have a library that is used by multiple clients. One client is a plain "java -jar ..." invocation, one is a Tomcat server, and one a Spring container.
This library publishes messages to a queue and some clients publish so many messages per minute that the act of publishing synchronously degrades performance. I implemented an ExecutorService
to offload the actual publishing to a worker thread. Performance is excellent with this strategy, however, that client's Spring container no longer shuts down cleanly. Yes, my threads are daemon threads already, however, they in turn use a library which doesn't use daemon threads.
The linked article shows that you need a different strategy when running inside a servlet. I have only encountered a few client containers so far and I'm already having to implement specialized shutdown procedures.
Rather than getting to specific use cases, my question is more general. How do you write library code which uses an ExecutorService while remaining agnostic to the threading model of the container in which the code runs?