我是靠谱客的博主 激情八宝粥,最近开发中收集的这篇文章主要介绍Java定时任务调度工具详解(5)— Quartz 之 Trigger,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

五、浅谈Trigger

Trigger是什么

Quartz中的触发器用来告诉调度程序作业什么时候触发
即Trigger对象是用来触发执行Job的

Quartz框架中的Trigger示意图
这里写图片描述

触发器通用属性

  • JobKey:表示job实例的标识,触发器被触发时,该指定的job实例会执行;
  • StartTime:表示触发器的时间表首次被触发的时间,它的值的类型是Java.util.Date;
  • EndTime:指定触发器的不再被触发的时间,它的值的类型是Java.util.Date。

代码演示

源码地址:https://gitee.com/liupeifeng3514/Timer-Quartz

HelloJob类改造

package helloquartz.three;

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

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

/**
 * 自定义任务 触发器通用属性
 */
public class HelloJob implements Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        // 打印当前的执行时间,格式为2017-01-01 00:00:00
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("--------Current Exec Time Is : " + sf.format(date));

        Trigger currentTrigger = context.getTrigger();
        System.out.println("--------Start Time Is : " + sf.format(currentTrigger.getStartTime()));
        System.out.println("--------End Time Is : " + sf.format(currentTrigger.getEndTime()));

        JobKey jobKey = currentTrigger.getJobKey();
        System.out.println("--------JobKey info : " + " jobName : " + jobKey.getName() + " jobGroup : " + jobKey.getGroup());
    }
}

HelloScheduler类改造

package helloquartz.three;

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

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

/**
 * 任务调度类  触发器通用属性
 */
public class HelloScheduler {

    public static void main(String[] args) throws SchedulerException {
        // 打印当前的执行时间,格式为2017-01-01 00:00:00
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("--------Current Time Is : " + sf.format(date));

        // 创建一个 JobDetail 实例,将该实例与 HelloJob 实例绑定
        JobDetail jobDeatil = JobBuilder.newJob(HelloJob.class)
                .withIdentity("myjob", "jobgroup1")// 定义标识符
                .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","trigroup1")// 定义标识符
                .startAt(date)// 定义3秒后执行
                .endAt(endDate)// 定义6秒后结束
                .withSchedule(SimpleScheduleBuilder
                        .simpleSchedule()
                        .withIntervalInSeconds(2)
                        .repeatForever())// 定义执行频度
                .build();

        // 创建 Scheduler 实例
        SchedulerFactory sfact = new StdSchedulerFactory();
        Scheduler scheduler = sfact.getScheduler();

        // 绑定 JobDetail 和 trigger
        scheduler.scheduleJob(jobDeatil, trigger);

        // 执行任务
        scheduler.start();
    }
}

运行结果:

--------Current Time Is : 2018-03-06 19:48:21
2018-03-06 19:48:21,574 [INFO ][main] Using default implementation for ThreadExecutor  (org.quartz.impl.StdSchedulerFactory:StdSchedulerFactory.java:1172) 
2018-03-06 19:48:21,578 [INFO ][main] Job execution threads will use class loader of thread: main  (org.quartz.simpl.SimpleThreadPool:SimpleThreadPool.java:268) 
2018-03-06 19:48:21,592 [INFO ][main] Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl  (org.quartz.core.SchedulerSignalerImpl:SchedulerSignalerImpl.java:61) 
2018-03-06 19:48:21,592 [INFO ][main] Quartz Scheduler v.2.2.3 created.  (org.quartz.core.QuartzScheduler:QuartzScheduler.java:240) 
2018-03-06 19:48:21,593 [INFO ][main] RAMJobStore initialized.  (org.quartz.simpl.RAMJobStore:RAMJobStore.java:155) 
2018-03-06 19:48:21,593 [INFO ][main] Scheduler meta-data: Quartz Scheduler (v2.2.3) 'DefaultQuartzScheduler' with instanceId 'NON_CLUSTERED'
  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  NOT STARTED.
  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
  Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.
  (org.quartz.core.QuartzScheduler:QuartzScheduler.java:305) 
2018-03-06 19:48:21,593 [INFO ][main] Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'  (org.quartz.impl.StdSchedulerFactory:StdSchedulerFactory.java:1327) 
2018-03-06 19:48:21,593 [INFO ][main] Quartz scheduler version: 2.2.3  (org.quartz.impl.StdSchedulerFactory:StdSchedulerFactory.java:1331) 
2018-03-06 19:48:21,594 [INFO ][main] Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.  (org.quartz.core.QuartzScheduler:QuartzScheduler.java:575) 
--------Current Exec Time Is : 2018-03-06 19:48:24
--------Start Time Is : 2018-03-06 19:48:24
--------End Time Is : 2018-03-06 19:48:27
--------JobKey info :  jobName : myjob jobGroup : jobgroup1
--------Current Exec Time Is : 2018-03-06 19:48:26
--------Start Time Is : 2018-03-06 19:48:24
--------End Time Is : 2018-03-06 19:48:27
--------JobKey info :  jobName : myjob jobGroup : jobgroup1

最后

以上就是激情八宝粥为你收集整理的Java定时任务调度工具详解(5)— Quartz 之 Trigger的全部内容,希望文章能够帮你解决Java定时任务调度工具详解(5)— Quartz 之 Trigger所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部