求助 quartz启动与暂停问题

隋春羽 2012-08-11 01:09:25
近日由于项目需要用到quartz组件实现调度管理功能,想要实现调度的启动和暂停以及恢复功能,但是暂停遇到问题
暂停后重新启动,会连续多次调用job中的execute方法。如果当前工作的处理时间过长必然会导致问题。代码如下,急求帮助


import java.util.Date;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;

public class TestTwo {

/**
* @param args
*/
public static void main(String[] args) {

try {
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();

JobDetail job = new JobDetail();
job.setJobClass(HelloJob.class);
job.setGroup("jobgourp");
job.setName("jobname");

SimpleTrigger striger=new SimpleTrigger();
striger.setName("strigername");
striger.setStartTime(new Date());
striger.setRepeatInterval(1000);
striger.setRepeatCount(-1);

striger.setJobName("jobname");
striger.setJobGroup("jobgourp");

sched.scheduleJob(job, striger);
sched.start();
//-------------------------------------------------------
Thread.sleep(4000);
System.out.println("暂停");

sched.pauseJobGroup("jobgourp");
sched.pauseTriggerGroup("jobgourp");
Thread.sleep(5000);
System.out.println("暂停结束");

sched.resumeJobGroup("jobgourp");
sched.resumeTriggerGroup("jobgourp");
sched.start();

Thread.sleep(4800);
sched.shutdown(true);
} catch (SchedulerException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}

job 类代码如下

package com.scy.quartz.test;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class HelloJob implements Job {

@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("HelloJob 执行了"+new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));

}

}



HelloJob 执行了2012-08-11 12:57:16
HelloJob 执行了2012-08-11 12:57:17
HelloJob 执行了2012-08-11 12:57:18
HelloJob 执行了2012-08-11 12:57:19
暂停
暂停结束
----------????-----------
HelloJob 执行了2012-08-11 12:57:25
HelloJob 执行了2012-08-11 12:57:25
HelloJob 执行了2012-08-11 12:57:25
HelloJob 执行了2012-08-11 12:57:25
HelloJob 执行了2012-08-11 12:57:25
HelloJob 执行了2012-08-11 12:57:25
----------????-----------
HelloJob 执行了2012-08-11 12:57:26
HelloJob 执行了2012-08-11 12:57:27
HelloJob 执行了2012-08-11 12:57:28
HelloJob 执行了2012-08-11 12:57:29
...全文
121 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
MiceRice 2012-08-11
  • 打赏
  • 举报
回复
resumeJobGroup的API解释说了(错过的触发,将在恢复时重新执行):
If any of the Job'sTrigger s missed one or more fire-times, then the Trigger's misfire instruction will be applied.

所以我想你应该用standby()会更合适(但这个影响是全局性的):
When start() is called (to bring the scheduler out of stand-by mode), trigger misfire instructions will NOT be applied during the execution of the start() method - any misfires will be detected immediately afterward (by the JobStore's normal process).

或者用unscheduleJob()将这个任务撤掉。


顺带说一句:在你这个例子里面,pauseJobGroup() 跟 pauseTriggerGroup() 重复,因为pauseJobGroup实际上也是暂停它的triggers:
Pause all of the JobDetails in the given group - by pausing all of their Triggers.

51,410

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧