Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of java.util.concurrent.Executor stops tomcat from stopping

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" />
  • Tomcat version: 6.0.22
  • Java version: 1.6
  • spring framework: 2.5.5

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.

like image 720
Anthony Kong Avatar asked Sep 15 '11 07:09

Anthony Kong


2 Answers

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.

like image 154
Tomasz Nurkiewicz Avatar answered Nov 11 '22 01:11

Tomasz Nurkiewicz


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" />
like image 5
Femi Avatar answered Nov 11 '22 00:11

Femi