quartz2.2.1 example 13代码分析

package com.quartz.fsl.test.example13;


import java.util.Date;


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


import com.qdwb.mlxc.util.LogSelf;


public class SimpleRecoveryJob implements Job {
private static final String COUNT = "count";


public void execute(JobExecutionContext context)
throws JobExecutionException {
int count;
JobKey jobKey = context.getJobDetail().getKey();


// If the Job is being re-executed because of a 'recovery' situation,
// this method will return true
// 默认false
/*
* RequestsRecovery - 一个"requests recovery"作业,在scheduler“hard
* shutdown”的情况下,或者机器断电的时候,当scheduler重启时,作业重新执行。
* JobExecutionContext.isRecovering()返回true。
*/
if (context.isRecovering())
LogSelf.fslLog.info("SimpleRecoveryJob: " + jobKey
+ " RECOVERING at " + new Date());
else {
LogSelf.fslLog.info("SimpleRecoveryJob: " + jobKey
+ " starting at " + new Date());
}


long delay = 10000L;
try {
Thread.sleep(delay);
} catch (Exception e) {
}
JobDataMap data = context.getJobDetail().getJobDataMap();


if (data.containsKey("count"))
count = data.getInt("count");
else
count = 0;


++count;
data.put("count", count);


LogSelf.fslLog.info("SimpleRecoveryJob: " + jobKey + " done Execution #" + count);
}


}

-----------------------------------------------------------

package com.quartz.fsl.test.example13;


public class SimpleRecoveryStatefulJob extends SimpleRecoveryJob {

}

------------------------------------------------------------

package com.quartz.fsl.test.example13;


import java.util.Date;


import org.quartz.DateBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.SimpleTrigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;


import com.qdwb.mlxc.util.LogSelf;


public class ClusterExample {
public void run(boolean inClearJobs, boolean inScheduleJobs)
throws Exception {
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();


if (inClearJobs) {
LogSelf.fslLog.warn("***** Deleting existing jobs/triggers *****");
sched.clear();
}


if (inScheduleJobs) {
LogSelf.fslLog.info("------- Scheduling Jobs ------------------");


String schedId = sched.getSchedulerInstanceId();


int count = 1;
//第1个SimpleRecoveryJob job
//requestRecovery():  执行中应用发生故障,需要重新执行
JobDetail job = JobBuilder.newJob(SimpleRecoveryJob.class)
.withIdentity("job_" + count, schedId).requestRecovery()
.build();
SimpleTrigger trigger = (SimpleTrigger) TriggerBuilder.newTrigger()
.withIdentity("triger_" + count, schedId).startAt(
DateBuilder.futureDate(5,DateBuilder.IntervalUnit.SECOND))
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withRepeatCount(20).withIntervalInSeconds(
5)).build();

LogSelf.fslLog.info(job.getKey() + " 将会运行时间: " + trigger.getStartTime()+ " 重复次数: "
+ trigger.getRepeatCount() + " 次,每隔"
+ (trigger.getRepeatInterval() / 1000L) + " 秒");

//第2个SimpleRecoveryJob job
++count;


job = JobBuilder.newJob(SimpleRecoveryJob.class).withIdentity(
"job_" + count, schedId).requestRecovery().build();


trigger = (SimpleTrigger) TriggerBuilder.newTrigger().withIdentity(
"triger_" + count, schedId).startAt(
DateBuilder.futureDate(2, DateBuilder.IntervalUnit.SECOND))
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withRepeatCount(20).withIntervalInSeconds(
5)).build();
LogSelf.fslLog.info(job.getKey() + " 将会运行时间: " + trigger.getStartTime()+ " 重复次数: "
+ trigger.getRepeatCount() + " 次,每隔"
+ (trigger.getRepeatInterval() / 1000L) + " 秒");


sched.scheduleJob(job, trigger);



//第3个SimpleRecoveryStatefulJob job
++count;


job=JobBuilder.newJob(SimpleRecoveryStatefulJob.class).withIdentity("job_" + count, schedId).requestRecovery().build();


trigger = (SimpleTrigger) TriggerBuilder.newTrigger().withIdentity(
"triger_" + count, schedId).startAt(
DateBuilder.futureDate(1, DateBuilder.IntervalUnit.SECOND))
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withRepeatCount(20).withIntervalInSeconds(
3)).build();


LogSelf.fslLog.info(job.getKey() + " 将会运行时间: " + trigger.getStartTime()+ " 重复次数: "
+ trigger.getRepeatCount() + " 次,每隔"
+ (trigger.getRepeatInterval() / 1000L) + " 秒");


sched.scheduleJob(job, trigger);


//第4个SimpleRecoveryJob job
++count;


job = JobBuilder.newJob(SimpleRecoveryJob.class).withIdentity(
"job_" + count, schedId).requestRecovery().build();


trigger = (SimpleTrigger) TriggerBuilder.newTrigger().withIdentity(
"triger_" + count, schedId).startAt(
DateBuilder.futureDate(1, DateBuilder.IntervalUnit.SECOND))
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withRepeatCount(20).withIntervalInSeconds(
4)).build();


LogSelf.fslLog.info(job.getKey() + " 将会运行时间: " + trigger.getStartTime()+ " 重复次数: "
+ trigger.getRepeatCount() + " 次,每隔"
+ (trigger.getRepeatInterval() / 1000L) + " 秒");


sched.scheduleJob(job, trigger);


//第5个SimpleRecoveryJob job
++count;


job = JobBuilder.newJob(SimpleRecoveryJob.class).withIdentity(
"job_" + count, schedId).requestRecovery().build();


trigger = (SimpleTrigger) TriggerBuilder.newTrigger().withIdentity(
"triger_" + count, schedId).startAt(
DateBuilder.futureDate(1, DateBuilder.IntervalUnit.SECOND))
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withRepeatCount(20)
.withIntervalInMilliseconds(4500L)).build();


LogSelf.fslLog.info(job.getKey() + " 将会运行时间: " + trigger.getStartTime()+ " 重复次数: "
+ trigger.getRepeatCount() + " 次,每隔"
+ (trigger.getRepeatInterval() / 1000L) + " 秒");


sched.scheduleJob(job, trigger);
}


LogSelf.fslLog.info("-------手工触发Scheduler ---------------");
sched.start();


LogSelf.fslLog.info("------- Waiting for 5 minutes... ----------");
try {
Thread.sleep(300L * 1000L);
} catch (Exception e) {
}
LogSelf.fslLog.info("------- Shutting Down --------------------");
sched.shutdown();
}


public static void main(String[] args) throws Exception {
boolean clearJobs = false;//默认不清除所有job
boolean scheduleJobs = true;


String[] arr$ = args;
int len$ = arr$.length;
for (int i$ = 0; i$ < len$; ++i$) {
String arg = arr$[i$];
if (arg.equalsIgnoreCase("clearJobs"))
clearJobs = true;
else if (arg.equalsIgnoreCase("dontScheduleJobs"))
scheduleJobs = false;
}


ClusterExample example = new ClusterExample();
example.run(clearJobs, scheduleJobs);
}
}

---------测试结果:

见图 result01 02 03 06





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值