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();
    }
}

棘丶
12 声望1 粉丝

引用和评论

0 条评论