stop Spring Scheduled execution if it hangs after some fixed time
<p>I have used Spring Framework's <code>Scheduled</code> to schedule my job to run at every 5 mins using cron. But sometime my job waits infinitely for an external resource and I can't put timeout there. I can't use <code>fixedDelay</code> as previous process sometime goes in wait infinitely mode and I have to refresh data at every 5 mins.</p>

<p>So I was looking any option in Spring Framework's <code>Scheduled</code> to stop that process/thread after a <code>fixed-time</code> either it run successfully or not.</p>

<p>I have found below setting which initialized <code>ThreadPoolExecutor</code> with 120 seconds for <code>keepAliveTime</code> which I put in <code>@Configuration</code> class. Can anybody tell me will this work as I expected.</p>

<pre><code>@Bean(destroyMethod="shutdown")
public Executor taskExecutor() {
    int coreThreads = 8;
    int maxThreads = 20;
    final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
            coreThreads, maxThreads, 120L, 
            TimeUnit.SECONDS, new LinkedBlockingQueue&lt;Runnable&gt;()
    );
    threadPoolExecutor.allowCoreThreadTimeOut(true);

    return threadPoolExecutor;
}
</code></pre>