File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes how to stop/cancel a scheduled task ScheduledFuture/ThreadPoolTaskScheduler Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "how to stop/cancel a scheduled task ScheduledFuture/ThreadPoolTaskScheduler" Watch "how to stop/cancel a scheduled task ScheduledFuture/ThreadPoolTaskScheduler" New topic
Author

how to stop/cancel a scheduled task ScheduledFuture/ThreadPoolTaskScheduler

Angie Kimura
Greenhorn

Joined: Mar 26, 2013
Posts: 1
Hi, I am able to call the start() function to run this QuarterlyMetricReportSchedulingService that implements Runnable to schedule a certain task, but i also need to be able to stop/cancel the scheduled tasks later in case i need to change the schedule time.

i have read many posts and tried several ways to make the stop() function work, but no luck.
i guess my question can also be how can i find/retrieve the scheduled tasks and cancel them?

would someone please help? thanks a lot.

@Configuration
@EnableScheduling
public class QuarterlyMetricReportScheduling{

@Autowired
QuarterlyMetricReportService qmrService;

ScheduledFuture sf;

CronTrigger trigger;

int shutdownTimeout = 1 * 10 * 1000;

@Autowired
QuarterlyMetricReportSchedulingService task;

public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
ThreadPoolTaskScheduler tps = new ThreadPoolTaskScheduler();
tps.setThreadNamePrefix("job");
return tps;
}

public void changeTrigger(String cronExpression){
System.out.println("change trigger to: " + cronExpression);
trigger = new CronTrigger(cronExpression);
start();
}

public void start(){

ThreadPoolTaskScheduler tps = new ThreadPoolTaskScheduler();
tps.initialize();
task = new QuarterlyMetricReportSchedulingService();
tps.schedule(task, trigger);
}

public void stop() {

ThreadPoolTaskScheduler tps = threadPoolTaskScheduler();
//tps = new ThreadPoolTaskScheduler();
//tps.setThreadNamePrefix("job");
tps.initialize();
task = new QuarterlyMetricReportSchedulingService();
trigger = new CronTrigger("0 59 11 26 3 ?");
tps.schedule(task, trigger);
ScheduledExecutorService scheduledExecutorService = tps.getScheduledExecutor();
try {
//ScheduledServiceExecutor service =
//Executors.newSingleThreadScheduledExecutor();
task = new QuarterlyMetricReportSchedulingService();
ScheduledFuture future = scheduledExecutorService.scheduleWithFixedDelay(
task, 1, 1, TimeUnit.MILLISECONDS);

future.cancel(true);
scheduledExecutorService.shutdown();
}


}
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

The schedule() method returns a scheduled future ( http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledFuture.html?is-external=true )
http://static.springsource.org/spring/docs/3.2.2.RELEASE/spring-framework-reference/htmlsingle/#scheduling-task-scheduler

You need to keep a handle to this future (you get it in your start() method) somewhere so that when you need to cancel the job you can then call cancel on the ScheduledFuture.

Also you do not need to shutdown the entire executor that will kill everything.


[How To Ask Questions][Read before you PM me]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to stop/cancel a scheduled task ScheduledFuture/ThreadPoolTaskScheduler