共计 1615 个字符,预计需要花费 5 分钟才能阅读完成。
1. 实现 Job 接口 – 你要做什么事?
public class myJob implements Job {
@Autowired
private PileStateManager pileStateManager;
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
// 判断以后工夫发送指定费率报文
int hours = new Date().getHours();
if (hours >= 7 && hours < 11) {System.out.println("我是尖");
// buf.writeBytes(ByteConvertUtil.hourChargeTo2Bytes((int) cachePile.getOneRate()));
} else if (hours >= 19 && hours < 23) {System.out.println("我是峰");
// buf.writeBytes(ByteConvertUtil.hourChargeTo2Bytes((int) cachePile.getTwoRate()));
} else if (hours >= 11 && hours < 19) {System.out.println("我是平");
pileStateManager.UpdateChargeRate();
//buf.writeBytes(ByteConvertUtil.hourChargeTo2Bytes((int) cachePile.getThreeRate()));
} else {System.out.println("我是谷");
// buf.writeBytes(ByteConvertUtil.hourChargeTo2Bytes((int) cachePile.getFourRate()));
}
}
2.Tirgger - 触发器 – 你什么时候去做
3.Scheduler - 任务调度器 – 你什么时候须要去做什么事
public class Test {public void go() throws Exception {
// 首先,必须要获得一个 Scheduler 的援用
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
//jobs 能够在 scheduled 的 sched.start() 办法前被调用
//job 1 将每隔 20 秒执行一次
JobDetail job = newJob(myJob.class).withIdentity("job1", "group1").build();
CronTrigger trigger = newTrigger().withIdentity("trigger1", "group1").withSchedule(cronSchedule("0/20 * * * * ?")).build();
sched.scheduleJob(job, trigger);
// job 2 将每 2 分钟执行一次(在该分钟的第 15 秒 )
job = newJob(myJob.class).withIdentity("job2", "group1").build();
trigger = newTrigger().withIdentity("trigger2", "group1").withSchedule(cronSchedule("15 0/2 * * * ?")).build();
sched.scheduleJob(job, trigger);
// 开始执行,start() 办法被调用后,计时器就开始工作,计时调度中容许放入 N 个 Job
sched.start();}
public static void main(String[] args) throws Exception {Test test = new Test();
test.go();}
}
正文完