1

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?

Community
  • 1
  • 1
eidolon1138
  • 434
  • 5
  • 9
  • 2
    The simplest approach would be to define a service for your library that has a start and stop method. In start you would initialize all necessary classes along with the executor service and stop would shutdown the executor service. This way you can plug it into most containers. – Nagesh Susarla Jan 22 '15 at 21:37
  • @NageshSusarla, you are spot on. I was thinking about the problem from the wrong direction. It isn't the responsibility of the library to understand all possible shutdown scenarios, it is the responsibility of the client to inform the library when it is time to shutdown. – eidolon1138 Jan 23 '15 at 23:06

0 Answers0