Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop jobs scheduled using spring task

Tags:

java

spring

I have implemented a sample spring scheduled task, with an applicationContext as follows,

<task:scheduled-tasks scheduler="myScheduler">
    <task:scheduled ref="cron" method="show" cron="0/10 * * * * ?"/>
    <task:scheduled ref="cron" method="show2" cron="0/15 * * * * ?"/>
</task:scheduled-tasks>

<task:scheduler id="myScheduler" pool-size="10"/>

How can I stop this schedule method?

like image 262
witek010 Avatar asked Oct 02 '12 14:10

witek010


People also ask

How do you stop a scheduled job in spring?

Another way to stop the scheduler would be manually canceling its Future. In the cases with multiple scheduler tasks, then we can maintain the Future map inside of the custom scheduler pool but cancel the corresponding scheduled Future based on scheduler class.

How do you stop a running job in Spring Batch?

The method is stop(long executionId) You would have to use JobExplorer to find the correct executionId to stop. Also from within a job flow config you can configure a job to stop after a steps execution based on exit status (see https://docs.spring.io/spring-batch/trunk/reference/html/configureStep.html#stopElement).

How do you stop a scheduled job in Java?

In order to cancel the Timer Task in Java, we use the java. util. TimerTask. cancel() method.

How do I enable and disable scheduler in Spring Boot?

Scheduling is not enabled by default. We explicitly enable scheduling by adding the @EnableScheduling annotation to a Spring configuration class. We can make the scheduling conditional on a property so that we can enable and disable scheduling by setting the property.


2 Answers

Inject the ThreadPoolTaskScheduler into another bean, and invoke shutdown(). If that isn't acceptable, you could configure the cron bean to accept a flag. For example:

public class Job() {
    private final AtomicBoolean stop = new AtomicBoolean(false);

    public void show() {
        if (stop.get()) {
            return;
        }
        ...
    }

    public void stop() {
        stop.set(true);
    }
}

Note that this won't remove the job from the scheduler. The only way to prevent that would be to obtain a reference to the ScheduledFuture and call cancel().

like image 103
David Grant Avatar answered Oct 06 '22 01:10

David Grant


Depends on what you mean by "stop".

  1. Business Condition Stop: Stop as result of a business condition, you should have those conditions evaluated in your methods and just simply not execute the code. This way you can stop unwanted execution at runtime, run your logic to handle the condition fail (logging,notification,etc) as a result.

  2. Non Business Condition: Externalize the chron expression to properties file or as I prefer a system variable in the JVM. Then you can just change the property value to a 9999 scenario to stop any execution.

System Variable Example.

<task:scheduled-tasks scheduler="myScheduler">
<task:scheduled ref="cron" method="show" cron="#{systemProperties['chron1']}"/>
<task:scheduled ref="cron" method="show2" cron="#{systemProperties['chron2']}"/>

like image 38
haju Avatar answered Oct 06 '22 00:10

haju