Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I shutdown Spring task executor/scheduler pools before all other beans in the web app are destroyed?
    text
    copied!<p>In a Spring web application I have several DAO and service layer beans. One service layer bean has annotated @Async / @Scheduled methods. These methods depend on other (autowired) beans. I have configured two thread pools in XML:</p> <pre><code>&lt;bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"&gt; &lt;property name="corePoolSize" value="2" /&gt; &lt;property name="maxPoolSize" value="5" /&gt; &lt;property name="queueCapacity" value="5" /&gt; &lt;property name="waitForTasksToCompleteOnShutdown" value="true" /&gt; &lt;property name="rejectedExecutionHandler"&gt; &lt;bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy"/&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean id="taskScheduler" class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler"&gt; &lt;property name="poolSize" value="10" /&gt; &lt;property name="waitForTasksToCompleteOnShutdown" value="true" /&gt; &lt;property name="rejectedExecutionHandler"&gt; &lt;bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy"/&gt; &lt;/property&gt; &lt;/bean&gt; &lt;task:annotation-driven executor="taskExecutor" scheduler="taskScheduler"/&gt; </code></pre> <p>Everything works as expected. My problem is that I cannot get a clean shutdown of the task pools to work. The tasks operate on the database and on the file system. When I stop the web application it takes some time until it is stopped. This indicates that the <code>waitForTasksToCompleteOnShutdown</code> property works. However, I get IllegalStateExceptions in the log indicating that some beans are already destroyed but some worker task threads are still executing and they fail because their dependencies are destroyed.</p> <p>There is a JIRA issue which might be relevant: <a href="https://jira.springsource.org/browse/SPR-5387" rel="noreferrer" title="SPR-5387">SPR-5387 </a></p> <p><strong>My question is: Is there a way to tell Spring to initialize the task executor/scheduler beans last or is there a way to tell Spring to destroy them first?</strong></p> <p>My understanding is that destruction takes place in reversed init order. Therefore the bean init'ed last will be destroyed first. If the thread pool beans are destroyed first, all currently executing tasks would finish and could still access dependent beans.</p> <p>I have also tried using the depends-on attribute on the thread pools referring to my service bean which has the @Async and @Scheduled annotations. Seems like they are never executed then and I do not get context initialization errors. I assume the annotated service bean somehow needs these thread pools initialized first and if I use depends-on I reverse the order and make them non-functional.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload