资讯专栏INFORMATION COLUMN

Quartz初级教程

lncwwn / 3237人阅读

摘要:是一个任务日程管理系统,一个在预先确定被纳入日程的时间到达时,负责执行或者通知其他软件组件的系统。核心接口核心调度器任务任务描述触发器和是同时相互依赖存在的,和触发器一起注册到核心调度器。

一、Quartz简介 1. Quartz

Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以多带带使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的日程序表。Jobs可以做成标准的Java组件或 EJBs。

Quartz是一个任务日程管理系统,一个在预先确定(被纳入日程)的时间到达时,负责执行(或者通知)其他软件组件的系统。

Quartz用一个小Java库发布文件(.jar文件),这个库文件包含了所有Quartz核心功能。这些功能的主要接口(API)是Scheduler接口。

2. Quartz核心接口

Scheduler – 核心调度器

Job – 任务

JobDetail – 任务描述

Trigger -- 触发器

Job和JobDetail是同时相互依赖存在的,和触发器一起注册到核心调度器。
Quartz的执行过程: Scheduler--> Trigger --> JobDetail --> Job
3. Tigger

SimpleTrigger

CronTrigger

SimpleTrigger

SimpleTrigger用来触发只需执行一次或者在给定时间触发并且重复N次且每次执行延迟一定时间的任务。

CronTrigger

像日历那样按日程来触发任务,而不是像SimpleTrigger 那样每隔特定的间隔时间触发,CronTriggers通常比SimpleTrigger更有用。

二、Cron Expressions 1. CronTrigger

CronTriggers往往比SimpleTrigger更有用,如果您需要基于日历的概念,而非SimpleTrigger完全指定的时间间隔,复发的发射工作的时间表。
CronTrigger,你可以指定触发的时间表如“每星期五中午”,或“每个工作日9:30时”,甚至“每5分钟一班9:00和10:00逢星期一上午,星期三星期五“。
即便如此,SimpleTrigger一样,CronTrigger拥有的startTime指定的时间表时生效,指定的时间表时,应停止(可选)结束时间。

2. Cron Expressions

cron的表达式被用来配置CronTrigger实例。 cron的表达式是字符串,实际上是由七子表达式,描述个别细节的时间表。这些子表达式是分开的空白,代表:

1 . 1. Seconds
2 . 2. Minutes
3 . 3. Hours
4 . 4. Day-of-Month
5 . 5. Month
6 . 6. Day-of-Week
7 . 7. Year (可选字段)

例: "0 0 12 ? * WED" 在每星期三下午12:00 执行。

个别子表达式可以包含范围, 例如,在前面的例子里("WED")可以替换成 "MON-FRI", "MON, WED, FRI"甚至"MON-WED,SAT".
“*” 代表整个时间段.
每一个字段都有一套可以指定有效值,如
Seconds (秒):可以用数字0-59 表示,
Minutes(分):可以用数字0-59 表示,
Hours(时):可以用数字0-23表示,
Day-of-Month(天):可以用数字1-31 中的任一一个值,但要注意一些特别的月份
Month(月):可以用0-11 或用字符串 “JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV and DEC” 表示
Day-of-Week(每周):可以用数字1-7表示(1 = 星期日)或用字符口串“SUN, MON, TUE, WED, THU, FRI and SAT”表示

“/”:为特别单位,表示为“每”如“0/15”表示每隔15分钟执行一次,“0”表示为从“0”分开始, “3/20”表示表示每隔20分钟执行一次,“3”表示从第3分钟开始执行
“?”:表示每月的某一天,或第周的某一天
“L”:用于每月,或每周,表示为每月的最后一天,或每个月的最后星期几如“6L”表示“每月的最后一个星期五”
“W”:表示为最近工作日,如“15W”放在每月(day-of-month)字段上表示为“到本月15日最近的工作日”
““#”:是用来指定“的”每月第n个工作日,例 在每周(day-of-week)这个字段中内容为"6#3" or "FRI#3" 则表示“每月第三个星期五”

1). Cron表达式的格式:秒 分 时 日 月 周 年(可选)。
字段名 允许的值 允许的特殊字符
0-59 , - * /
0-59 , - * /
小时 0-23 , - * /
1-31 , - * ? / L W C
1-12 or JAN-DEC , - * /
周几 1-7 or SUN-SAT , - * ? / L C #
年(可选字段) empty, 1970-2099 , - * /
 “?”字符:表示不确定的值
 “,”字符:指定数个值
 “-”字符:指定一个值的范围
 “/”字符:指定一个值的增加幅度。n/m表示从n开始,每次增加m
 “L”字符:用在日表示一个月中的最后一天,用在周表示该月最后一个星期X
 “W”字符:指定离给定日期最近的工作日(周一到周五)
 “#”字符:表示该月第几个周X。6#3表示该月第3个周五
2). Cron表达式范例:

每隔5秒执行一次:/5 * ?
每隔1分钟执行一次:0 /1 ?
每天23点执行一次:0 0 23 ?
每天凌晨1点执行一次:0 0 1 ?
每月1号凌晨1点执行一次:0 0 1 1 * ?
每月最后一天23点执行一次:0 0 23 L * ?
每周星期天凌晨1点实行一次:0 0 1 ? * L
在26分、29分、33分执行一次:0 26,29,33 * ?
每天的0点、13点、18点、21点都执行一次:0 0 0,13,18,21 ?

三、Quartz 触发器Tigger的简单案例
前面对Tigger进行过简单描述,现在用Demo进行演示
Quartz的Maven依赖

          org.quartz-scheduler
          quartz
          2.2.1

1 . 实现一个Job接口

/* 
 * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved. 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
 * use this file except in compliance with the License. You may obtain a copy 
 * of the License at 
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0 
 *   
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 * License for the specific language governing permissions and limitations 
 * under the License.
 * 
 */
 
package org.ouhei.quartz.example;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * 

* This is just a simple job that says "Hello" to the world. *

* * @author Bill Kratzer */ public class HelloJob implements Job { private static Logger _log = LoggerFactory.getLogger(HelloJob.class); /** *

* Empty constructor for job initilization *

*

* Quartz requires a public empty constructor so that the * scheduler can instantiate the class whenever it needs. *

*/ public HelloJob() { } /** *

* Called by the {@link org.quartz.Scheduler} when a * {@link org.quartz.Trigger} fires that is associated with * the Job. *

* * @throws JobExecutionException * if there is an exception while executing the job. */ public void execute(JobExecutionContext context) throws JobExecutionException { // Say Hello to the World and display the date/time _log.info("Hello World! - " + new Date()); } }

2 . SimpleTrigger触发器进行模拟

SimpleExample代码
/* 
 * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved. 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
 * use this file except in compliance with the License. You may obtain a copy 
 * of the License at 
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0 
 *   
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 * License for the specific language governing permissions and limitations 
 * under the License.
 * 
 */

package org.ouhei.quartz.example;

import static org.quartz.DateBuilder.evenMinuteDate;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * This Example will demonstrate how to start and shutdown the Quartz scheduler and how to schedule
 * a job to run in Quartz.
 * 
 * @author Bill Kratzer
 */
public class SimpleExample {

    public void run() throws Exception {
        Logger log = LoggerFactory.getLogger(SimpleExample.class);

        log.info("------- Initializing ----------------------");

        // 定义调度器
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();

        log.info("------- Initialization Complete -----------");

        // 获取当前时间的下一分钟
        Date runTime = evenMinuteDate(new Date());

        log.info("------- Scheduling Job  -------------------");

        // 定义job
        // 在quartz中,有组的概念,组+job名称 唯一的
        JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();

        // 定义触发器,在下一分钟启动
        Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime).build();

        // 将job注册到调度器
        sched.scheduleJob(job, trigger);
        log.info(job.getKey() + " will run at: " + runTime);

        // 启动调度器
        sched.start();

        log.info("------- Started Scheduler -----------------");

        // 等待65秒
        log.info("------- Waiting 65 seconds... -------------");
        try {
            // wait 65 seconds to show job
            Thread.sleep(65L * 1000L);
            // executing...
        } catch (Exception e) {
            //
        }

        // 关闭调度器
        log.info("------- Shutting Down ---------------------");
        sched.shutdown(true);
        log.info("------- Shutdown Complete -----------------");
    }

    public static void main(String[] args) throws Exception {

        SimpleExample example = new SimpleExample();
        example.run();

    }

}

3 . 添加日志文件log4j.properties

log4j.rootLogger=DEBUG,A1
log4j.logger.com.taotao = DEBUG
log4j.logger.org.mybatis = DEBUG

log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n

运行main方法,观察控制台。

如果模拟CronTrigger触发器,把上述的SimpleExample代码用SimpleCronExample代码替换
SimpleCronExample代码:
/* 
 * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved. 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
 * use this file except in compliance with the License. You may obtain a copy 
 * of the License at 
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0 
 *   
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 * License for the specific language governing permissions and limitations 
 * under the License.
 * 
 */

package org.ouhei.quartz.example;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.DateBuilder.evenMinuteDate;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * This Example will demonstrate how to start and shutdown the Quartz scheduler and how to schedule
 * a job to run in Quartz.
 * 
 * @author Bill Kratzer
 */
public class SimpleCronExample {

    public void run() throws Exception {
        Logger log = LoggerFactory.getLogger(SimpleCronExample.class);

        log.info("------- Initializing ----------------------");

        // 定义调度器
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();

        log.info("------- Initialization Complete -----------");

        // 获取当前时间的下一分钟
        Date runTime = evenMinuteDate(new Date());

        log.info("------- Scheduling Job  -------------------");

        // 定义job
        JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();

        // 定义触发器,每2秒执行一次
        Trigger trigger = newTrigger().withIdentity("trigger1", "group1")
                .withSchedule(cronSchedule("0 0/1 * * * ?")).build();

        // 将job注册到调度器
        sched.scheduleJob(job, trigger);
        log.info(job.getKey() + " will run at: " + runTime);

        // 启动调度器
        sched.start();

        log.info("------- Started Scheduler -----------------");

        // 等待1分钟
        log.info("------- Waiting 60 seconds... -------------");
        try {
            Thread.sleep(60L * 1000L);
        } catch (Exception e) {
            //
        }

        // 关闭调度器
        log.info("------- Shutting Down ---------------------");
        sched.shutdown(true);
        log.info("------- Shutdown Complete -----------------");
    }

    public static void main(String[] args) throws Exception {

        SimpleCronExample example = new SimpleCronExample();
        example.run();

    }

}
四、spring整合Quartz

前面用案例介绍过SimpleTrigger和CronTrigger,
下面用spring整合Quartz,分四步:

导入依赖

编写Job

编写spring配置文件

启动spring容器(启动调度器)

1. 导入maven依赖

  4.0.0
  org.ouhei.quartz
  ouhei-quartz
  1.0.0-SNAPSHOT
  
      
          org.springframework
          spring-context-support
          4.0.6.RELEASE
      
      
          org.quartz-scheduler
          quartz
          2.2.1
      
      
          org.slf4j
          slf4j-log4j12
          1.7.7
      
      
          org.springframework
          spring-tx
          4.0.6.RELEASE
      
  
2. 编写Job
package org.ouhei.quartz;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;

/**
 * QuartzJobBean实现了Job接口
 * 
 */
public class MyJob extends QuartzJobBean {
    
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        System.out.println("myJob 执行了............." + context.getTrigger().getKey().getName());
        ApplicationContext applicationContext = (ApplicationContext) context.getJobDetail().getJobDataMap()
                .get("applicationContext");
        System.out.println("获取到的Spring容器是: " + applicationContext);
        
    }

}
3. 编写spring配置文件applicationContext-scheduler.xml



    
    
        
        
        
        
        
        
        
        
        
        
    
    
    
    
        
        
        
    
    
    
    
    
        
        
        
    
    
    
    
        
            
                
                
            
        
    


4. 启动spring容器(启动调度器)
package org.ouhei.quartz;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    
    public static void main(String[] args) {
        new ClassPathXmlApplicationContext("classpath:applicationContext-scheduler.xml");
    }

}
5. 添加日志文件
log4j.rootLogger=DEBUG,A1
log4j.logger.com.taotao = DEBUG
log4j.logger.org.mybatis = DEBUG

log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/69308.html

相关文章

  • Quartz学习之入门学习

    摘要:触发器也可以给予名称和放置在组中,以方便地将它们调度内组织。作业可以被添加到所述调度器一次,而是具有多个触发器注册。调度类链接工作和触发器到一起,并执行它。 简介 Quartz是一个开源的作业调度框架,可以让计划的程序任务一个预定义的日期和时间运行。Quartz可以用来创建简单或复杂的日程安排执行几十,几百,甚至是十万的作业数。官方链接,戳这里 Quartz是什么? 作业调度库 Qua...

    Pandaaa 评论0 收藏0
  • [笔记]springboot集成Quartz简单使用+数据库

    摘要:教程二十一使用定时任务文章目录环境一简单使用二配合数据库环境一简单使用二配合数据库 ​​Spring Boot教程(二十一):Spring Boot使用Quartz定时任务​​ 文章目录​​环境​​​​一、简单使用​​​​二、配合数据库​​环境​​quartz 2.3​​一、简单使用...

    hikui 评论0 收藏0
  • 两个月的Java实习结束,继续努力

    摘要:花了将近两个星期完成了功能,期间我编写的能力也算是有所提升了。所以能看到这篇文章的同学都是大佬如果想看更多的原创技术文章,欢迎大家关注我的微信公众号。可能感兴趣的链接文章的目录导航微信公众号端文章的目录导航端海量精美脑图 前言 只有光头才能变强 2018年8月30日,今天我辞职了。在6月25号入职,到现在也有两个月时间了。 感受: 第一天是期待的:第一次将项目拉到本地上看的时候,代码...

    snifes 评论0 收藏0
  • Quartz学习之多作业、监听

    摘要:多作业例子在这个例子中,我们将介绍如何通过多个作业。在调度框架中,每个作业将被连接到一个唯一的触发,并且由调度器运行它。备注说明在中,一个触发器触发多个作业是不可以的。第一步创建个作业,,和。 多作业例子 在这个例子中,我们将介绍如何通过Quartz API 多个作业。在Quartz调度框架中,每个作业将被连接到一个唯一的触发,并且由调度器运行它。 备注说明:在 Quartz 中,一个...

    miracledan 评论0 收藏0

发表评论

0条评论

lncwwn

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<