Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have added below code to terminate tasks you can use it. You may change the retry numbers.</p> <pre><code>package com.xxx.test.schedulers; import java.util.Map; import java.util.concurrent.TimeUnit; import org.apache.log4j.Logger; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextClosedEvent; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.stereotype.Component; import com.xxx.core.XProvLogger; @Component class ContextClosedHandler implements ApplicationListener&lt;ContextClosedEvent&gt; , ApplicationContextAware,BeanPostProcessor{ private ApplicationContext context; public Logger logger = XProvLogger.getInstance().x; public void onApplicationEvent(ContextClosedEvent event) { Map&lt;String, ThreadPoolTaskScheduler&gt; schedulers = context.getBeansOfType(ThreadPoolTaskScheduler.class); for (ThreadPoolTaskScheduler scheduler : schedulers.values()) { scheduler.getScheduledExecutor().shutdown(); try { scheduler.getScheduledExecutor().awaitTermination(20000, TimeUnit.MILLISECONDS); if(scheduler.getScheduledExecutor().isTerminated() || scheduler.getScheduledExecutor().isShutdown()) logger.info("Scheduler "+scheduler.getThreadNamePrefix() + " has stoped"); else{ logger.info("Scheduler "+scheduler.getThreadNamePrefix() + " has not stoped normally and will be shut down immediately"); scheduler.getScheduledExecutor().shutdownNow(); logger.info("Scheduler "+scheduler.getThreadNamePrefix() + " has shut down immediately"); } } catch (IllegalStateException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } Map&lt;String, ThreadPoolTaskExecutor&gt; executers = context.getBeansOfType(ThreadPoolTaskExecutor.class); for (ThreadPoolTaskExecutor executor: executers.values()) { int retryCount = 0; while(executor.getActiveCount()&gt;0 &amp;&amp; ++retryCount&lt;51){ try { logger.info("Executer "+executor.getThreadNamePrefix()+" is still working with active " + executor.getActiveCount()+" work. Retry count is "+retryCount); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } if(!(retryCount&lt;51)) logger.info("Executer "+executor.getThreadNamePrefix()+" is still working.Since Retry count exceeded max value "+retryCount+", will be killed immediately"); executor.shutdown(); logger.info("Executer "+executor.getThreadNamePrefix()+" with active " + executor.getActiveCount()+" work has killed"); } } @Override public void setApplicationContext(ApplicationContext context) throws BeansException { this.context = context; } @Override public Object postProcessAfterInitialization(Object object, String arg1) throws BeansException { return object; } @Override public Object postProcessBeforeInitialization(Object object, String arg1) throws BeansException { if(object instanceof ThreadPoolTaskScheduler) ((ThreadPoolTaskScheduler)object).setWaitForTasksToCompleteOnShutdown(true); if(object instanceof ThreadPoolTaskExecutor) ((ThreadPoolTaskExecutor)object).setWaitForTasksToCompleteOnShutdown(true); return object; } </code></pre> <p>}</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