浅谈Quartz的Trigger

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengqiuming/article/details/84169886

一 点睛

1 Trigger是什么?

Quartz中的Trigger用来告诉调度程序什么时候触发。即Trigger对象是用来触发Job的。

2 Quartz框架中的Tirgger

3 触发器通用属性

JobKey:表示job实例的标识,触发器被触发时,该指定的job实例会执行。

StartTime:表示触发器的时间表首次触发的时间。它的值类型是Java.util.Date。

EndTime:指定触发器的不再被触发的时间。它的值类型是Java.util.Date。

二 实战

1 启动类

package com.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

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

public class HelloScheduler {

    public static void main(String[] args) throws SchedulerException {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("Current Time is:" + simpleDateFormat.format(date));

        //创建一个JobDetail实例, 将该实例与hellojob class 绑定
        JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
                .withIdentity("myJob").build();  //演示传递参数
        //获取距离当前时间3秒后的时间
        date.setTime(date.getTime()+3000);
        //获取距离当前时间6秒后的时间
        Date endDate = new Date();
        endDate.setTime(endDate.getTime()+6000);
        //创建一个Trigger实例,定义该job立即执行,并且每隔两秒钟重复执行一次
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger","group1")
                .startAt(date)  //定义开始执行时间
                .endAt(endDate)  //定义结束执行时间
                .withSchedule(
                        SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(2).repeatForever()
                ).build();
        //创建Schedule实例
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = schedulerFactory.getScheduler();
        scheduler.start();

        scheduler.scheduleJob(jobDetail,trigger);
    }
}

2 Job类

package com.quartz;

import org.quartz.*;

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

public class HelloJob implements Job{

    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //打印当前执行的时间
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("+++++++++++++++++++++++++++");
        System.out.println("Current Exec Time is:" + simpleDateFormat.format(date));
        Trigger curentTrigger=jobExecutionContext.getTrigger();
        System.out.println("Start time is:"+ simpleDateFormat.format(curentTrigger.getStartTime()));
        System.out.println("End time is:"+simpleDateFormat.format(curentTrigger.getEndTime()));
        JobKey jobKey=curentTrigger.getJobKey();
        System.out.println("JobKey info---"+"jobName:"+jobKey.getName()+"jobGroup:"+jobKey.getGroup());
    }
}

三 执行结果

+++++++++++++++++++++++++++

Current Exec Time is:2018-11-17 09:48:11

Start time is:2018-11-17 09:48:11

End time is:2018-11-17 09:48:14

JobKey info---jobName:myJobjobGroup:DEFAULT

+++++++++++++++++++++++++++

Current Exec Time is:2018-11-17 09:48:13

Start time is:2018-11-17 09:48:11

End time is:2018-11-17 09:48:14

JobKey info---jobName:myJobjobGroup:DEFAULT

猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/84169886
今日推荐