1
我可以調用start()
函數來運行此Quarterly Metric Report Scheduling Service,該服務實現了Runnable來調度某個任務,但是我還需要能夠在以後的情況下停止/取消計劃任務需要更改計劃時間。stop/cancel ScheduledFuture
我已閱讀了很多帖子,並嘗試了幾種方法使stop()
函數正常工作,但沒有運氣。 我想我的問題也可以是我如何找到/檢索計劃任務並取消它們?
有人請幫忙嗎?非常感謝。
@Configuration
@EnableScheduling
public class QuarterlyMetricReportScheduling{
@Autowired
QuarterlyMetricReportService qmrService;
ScheduledFuture sf;
CronTrigger trigger;
int shutdownTimeout = 1 * 60 * 1000;//10 sec
@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();
}
}
'10秒'爲'10 * 1000'而不是'10 * 60 * 1000'。 – 2013-03-26 16:48:34