Background:
I need to use java.util.concurrent.Executor
to serialize the execution of some legacy code inside a WebService.
I have added an member variable, executor, to the WebService class. It is injected from the outside by the springframework.
The executor bean is defined as such:
<bean id="riskValueEstimateUpdateExecutor" scope="singleton"
class="java.util.concurrent.Executors"
factory-method="newSingleThreadExecutor" />
Problem:
The WS works as expected. We rolled it out to a linux server. Then we realised the tomcat stop script could no longer stop the service.
I use kill -3 to the tomcat instance. In the thread dump I find these lines:
"pool-2-thread-1" prio=10 tid=0xad637c00 nid=0xf37 waiting on condition [0xae882000..0xae883020]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0xb453b710> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:158)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:1925)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:358)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:946)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:906)
at java.lang.Thread.run(Thread.java:619)
How can I fix this problem?
If you need more information, please suggest.
It's neither Tomcat's nor Spring's fault. You just have to shutdown ExecutorService
properly. Add destroy-method
in bean definition:
<bean id="riskValueEstimateUpdateExecutor" scope="singleton"
class="java.util.concurrent.Executors"
factory-method="newSingleThreadExecutor" destroy-method="shutdown"/>
BTW singleton
scope is default. You might also consider using shutdownNow()
method. Also note that Spring provides powerful task scheduling mechanism.
You need to find a way to call shutdown when the Spring context is shutdown: try using the bean destroy-method
property and set that to shutdown
. Something like this:
<bean id="riskValueEstimateUpdateExecutor" scope="singleton"
class="java.util.concurrent.Executors"
factory-method="newSingleThreadExecutor" destroy-method="shutdown" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With