quartz定时任务

简介: quartz定时任务

介绍

Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,是完全由java开发的一个开源的任务日程管理系统,“任务进度管理器”就是一个在预先确定(被纳入日程)的时间到达时,负责执行(或者通知)其他软件组件的系统。
Quartz用一个小Java库发布文件(.jar文件),这个库文件包含了所有Quartz核心功能。这些功能的主要接口(API)是Scheduler接口。它提供了简单的操作,例如:将任务纳入日程或者从日程中取消,开始/停止/暂停日程进度。

quartz 核心部分

  • job 执行器,用户实现业务逻辑
  • jobdetail 任务,关联具体的job
  • trigger 触发器, 什么时候执行
  • Scheduler 调度器

常用的Trigger有SimpleTrigger和CronTrigger

Job jobDetail

JobDetail绑定指定的Job,每次Scheduler调度执行一个Job的时候,首先会拿到对应的Job,然后创建该Job实例,再去执行Job中的execute()的内容,任务执行结束后,关联的Job对象实例会被释放,且会被JVM GC清除。

为什么设计成JobDetail + Job,不直接使用Job

JobDetail定义的是任务数据,而真正的执行逻辑是在Job中。
这是因为任务是有可能并发执行,如果Scheduler直接使用Job,就会存在对同一个Job实例并发访问的问题。而JobDetail & Job 方式,Sheduler每次执行,都会根据JobDetail创建一个新的Job实例,这样就可以规避并发访问的问题。

JobExecutionContext
JobExecutionContext中包含了Quartz运行时的环境以及Job本身的详细数据信息。
当Schedule调度执行一个Job的时候,就会将JobExecutionContext传递给该Job的execute()中,Job就可以通过JobExecutionContext对象获取信息。

Trigger

SimpleTrigger 可以实现在一个指定时间段内执行一次作业任务或一个时间段内多次执行作业任务。

CronTrigger功能非常强大,是基于日历的作业调度,而SimpleTrigger是精准指定间隔,所以相比SimpleTrigger,CroTrigger更加常用。CroTrigger是基于Cron表达式.

可通过在线生成Cron表达式的工具:http://cron.qqe2.com/ 来生成自己想要的表达式。

代码说明

依赖jar

<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.3.0</version>
</dependency>

调度器Scheduler的使用

public class QuartzTest {
      public static void main(String[] args) {
          try {
              // 从factory创建调度器 Scheduler
              Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

              // 启动
              scheduler.start();
                            ...
              // 关闭
              scheduler.shutdown();
          } catch (SchedulerException se) {
              se.printStackTrace();
          }
      }
  }

JobDetail、Trigger的使用

HelloJob 为实现job接口的类,处理用户业务逻辑的job

// define the job and tie it to our HelloJob class
  JobDetail job = newJob(HelloJob.class)
      .withIdentity("job1", "group1")
      .build();

  // Trigger the job to run now, and then repeat every 40 seconds
  Trigger trigger = newTrigger()
      .withIdentity("trigger1", "group1")
      .startNow()
            .withSchedule(simpleSchedule()
              // 间隔40s
              .withIntervalInSeconds(40)
              .repeatForever())            
      .build();

  // Tell quartz to schedule the job using our trigger
  scheduler.scheduleJob(job, trigger);

一个完整的例子

package org.quartz.examples.example1;

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 ----------------------");

    // First we must get a reference to a scheduler
    SchedulerFactory sf = new StdSchedulerFactory();
    Scheduler sched = sf.getScheduler();

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

    // computer a time that is on the next round minute
    Date runTime = evenMinuteDate(new Date());

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

    // define the job and tie it to our HelloJob class
    JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();

    // Trigger the job to run on the next round minute
    Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime).build();

    // Tell quartz to schedule the job using our trigger
    sched.scheduleJob(job, trigger);
    log.info(job.getKey() + " will run at: " + runTime);

    // Start up the scheduler (nothing can actually run until the
    // scheduler has been started)
    sched.start();

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

    // wait long enough so that the scheduler as an opportunity to
    // run the job!
    log.info("------- Waiting 65 seconds... -------------");
    try {
      // wait 65 seconds to show job
      Thread.sleep(65L * 1000L);
      // executing...
    } catch (Exception e) {
      //
    }

    // shut down the scheduler
    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();

  }

}
    
public class HelloJob implements Job {

    private static Logger _log = LoggerFactory.getLogger(HelloJob.class);

    /**
     * <p>
     * Empty constructor for job initilization
     * </p>
     * <p>
     * Quartz requires a public empty constructor so that the
     * scheduler can instantiate the class whenever it needs.
     * </p>
     */
    public HelloJob() {
    }

    /**
     * <p>
     * Called by the <code>{@link org.quartz.Scheduler}</code> when a
     * <code>{@link org.quartz.Trigger}</code> fires that is associated with
     * the <code>Job</code>.
     * </p>
     * 
     * @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());
    }

}

总结

Quartz包含4部分job、jobdetail、trigger、Scheduler。通过StdSchedulerFactory工厂创建调度器Scheduler,调度器关联trigger与jobDetail,trigger触发任务通知scheduler查找jobDetail 创建job实例执行用户定义的业务任务。

相关文章
|
开发工具
阿里云百炼应用中心--QuickStart
针对阿里云最新推出的大模型平台:百炼,试用其应用中心的功能,通过Step By Step详细演示该产品的使用。
1715 1
|
机器学习/深度学习 人工智能 并行计算
5分钟掌握开源图神经网络框架DGL使用
近几年神经网络在人工智能领域的成功应用,让它备受关注和热捧。但是,它自身依然具有本质上的局限性,以往的神经网络都是限定在欧式空间内,这和大多数实际应用场景并不符合,因此,也阻碍了它在很多领域的实际落地应用。
5分钟掌握开源图神经网络框架DGL使用
|
1月前
阿里云域名优惠口令2026年最新:域名注册、续费和转入使用
阿里云2026年最新域名优惠口令大全:支持com/cn/xin后缀的注册、续费及转入,如【com批量注册更享优惠】【互联网上的中国标识】等,限时限量,不可与代金券等叠加使用。
281 0
|
3月前
|
人工智能 数据可视化 测试技术
面试预约电话如何自动化?|阿里云智能联络中心实操指南
阿里云智能联络中心语音智能体,助力招聘高效邀约:AI自动外呼确认候选人意向与时间,支持自然对话、实时打断,准确率95%,1.8秒低延迟,最快1天上线。
350 1
|
3月前
|
人工智能 API
Codex Claude Qoder +工匠智能体Craftsman Agent的CLIsSkills 打造一站式秒IP潮玩手办3D设计工作站
本博客介绍如何用Codex/Claude等Agent协同工匠智能体(Craftsman Agent)的Toy Generator / 3D Generator的Skills/CLIs,打造一站式IP设计文创潮玩设计智能体工作站,将IP创意一键生成多视角设计图、AI手办、毛绒玩具及3D打印模型(.glb/.obj),并直连1000+匠人工坊实现按需量产。
|
11月前
|
弹性计算 搜索推荐 异构计算
阿里云服务器多少钱一年?整理阿里云最新爆款服务器配置排行榜;2核4G/4核8G/8核16G、可选实例区别与选型指南参考
阿里云服务器多少钱一年?阿里云最新价格表CPU内存带宽系统盘,购买阿里云服务器多少钱一年?阿里云轻量应用服务器200M峰值带宽68元1年(秒杀38元),ECS云服务器2核2G3M带宽99元一年、2核4G、5M带宽、80G系统盘优惠价格199元一年,4核16G服务器10M带宽89元1个月,8核32G服务器10M固定带宽160元一个月,阿里云香港轻量服务器200M带宽25元个月起。
|
安全 Linux 虚拟化
Rocky Linux 10 - RHEL 100% 1:1 兼容免费发行版
Rocky Linux 10 - RHEL 100% 1:1 兼容免费发行版
1279 34
Rocky Linux 10 - RHEL 100% 1:1 兼容免费发行版
|
11月前
|
传感器 数据采集 监控
[开源代码]基于STM32的环境检测与报警系统
本项目成功实现了基于STM32的环境检测与报警系统,能够同时监测水位、温度、湿度和亮度,并对每项参数独立设置安全范围,实现报警提示。
[开源代码]基于STM32的环境检测与报警系统
|
移动开发 小程序 前端开发
小程序开发平台有哪些?小程序开发制作软件推荐
小程序开发方案全解析:5种主流方式与选择指南 小程序开发需根据技术能力、预算、时间及功能需求综合决策。以下为5种主流开发方案及适用场景分析:
26826 0
|
人工智能 自然语言处理 测试技术
DeepSeek V3:DeepSeek 开源的最新多模态 AI 模型,编程能力超越Claude,生成速度提升至 60 TPS
DeepSeek V3 是深度求索公司开源的最新 AI 模型,采用混合专家架构,具备强大的编程和多语言处理能力,性能超越多个竞争对手。
2909 5
DeepSeek V3:DeepSeek 开源的最新多模态 AI 模型,编程能力超越Claude,生成速度提升至 60 TPS

热门文章

最新文章