quartz(一)基础篇

简介: quartz(一)基础篇

一、简介



Quartz是一个功能丰富的开源作业调度库,几乎可以集成到任何Java应用程序中——从最小的独立应用程序到最大的电子商务系统。Quartz可以用来创建简单或复杂的时间表,以执行数十个、数百个甚至数万个工作;这些作业的任务被定义为标准Java组件,可以执行您通过编程让它们执行的几乎任何事情。Quartz调度器包括许多企业级特性,比如对JTA事务和集群的支持。


二、应用场景



  1. 定时任务(通过cron表达式)
  2. 订单管理(状态管理)
  3. 系统维护(数据库备份...)
  4. 提醒服务
  5. ...


三、简单应用



1. 基本配置


  1. 引入jar包
<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.2.1</version>
</dependency>


  1. resources目录下添加quartz.properties文件
org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore


2. 简单应用


  1. java代码
public static void main(String[] args) {
        try {
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            scheduler.start();
            scheduler.shutdown();
        }catch (Exception e){
            e.printStackTrace();
        }
        SpringApplication.run(DockerImagesApplication.class, args);
    }


  1. 执行结果
2020-12-26 19:54:22.882  INFO 4904 --- [           main] org.quartz.impl.StdSchedulerFactory      : Using default implementation for ThreadExecutor
2020-12-26 19:54:22.892  INFO 4904 --- [           main] org.quartz.core.SchedulerSignalerImpl    : Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
2020-12-26 19:54:22.893  INFO 4904 --- [           main] org.quartz.core.QuartzScheduler          : Quartz Scheduler v.2.2.1 created.
2020-12-26 19:54:22.894  INFO 4904 --- [           main] org.quartz.simpl.RAMJobStore             : RAMJobStore initialized.
2020-12-26 19:54:22.895  INFO 4904 --- [           main] org.quartz.core.QuartzScheduler          : Scheduler meta-data: Quartz Scheduler (v2.2.1) 'MyScheduler' 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 3 threads.
  Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.
2020-12-26 19:54:22.895  INFO 4904 --- [           main] org.quartz.impl.StdSchedulerFactory      : Quartz scheduler 'MyScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
2020-12-26 19:54:22.895  INFO 4904 --- [           main] org.quartz.impl.StdSchedulerFactory      : Quartz scheduler version: 2.2.1
2020-12-26 19:54:22.896  INFO 4904 --- [           main] org.quartz.core.QuartzScheduler          : Scheduler MyScheduler_$_NON_CLUSTERED started.
2020-12-26 19:54:22.896  INFO 4904 --- [           main] org.quartz.core.QuartzScheduler          : Scheduler MyScheduler_$_NON_CLUSTERED shutting down.
2020-12-26 19:54:22.896  INFO 4904 --- [           main] org.quartz.core.QuartzScheduler          : Scheduler MyScheduler_$_NON_CLUSTERED paused.
2020-12-26 19:54:22.896  INFO 4904 --- [           main] org.quartz.core.QuartzScheduler          : Scheduler MyScheduler_$_NON_CLUSTERED shutdown complete.


3. 立即执行job


  1. 简单job实现
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
@Slf4j
public class HelloJob implements Job {
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("hello world!!!");
    }
}


  1. job调用
public static void main(String[] args) {
        try {
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("test1", "repeat").build();
            jobDetail.getJobDataMap().put("name","ljl");
            Trigger build = newTrigger().withIdentity("test1", "repeat").startNow().build();
            build.getJobDataMap().put("name","lsr");
            scheduler.scheduleJob(jobDetail,build);
            scheduler.start();
        }catch (Exception e){
            e.printStackTrace();
        }
        SpringApplication.run(DockerImagesApplication.class, args);
    }


  1. 执行结果
23:33:31.110 [main] INFO org.quartz.impl.StdSchedulerFactory - Using default implementation for ThreadExecutor
23:33:31.123 [main] INFO org.quartz.core.SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
23:33:31.124 [main] INFO org.quartz.core.QuartzScheduler - Quartz Scheduler v.2.2.1 created.
23:33:31.125 [main] INFO org.quartz.simpl.RAMJobStore - RAMJobStore initialized.
23:33:31.126 [main] INFO org.quartz.core.QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.2.1) 'QuartzScheduler' 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 3 threads.
  Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.
23:33:31.126 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler 'QuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
23:33:31.126 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 2.2.1
23:33:31.131 [main] INFO org.quartz.core.QuartzScheduler - Scheduler QuartzScheduler_$_NON_CLUSTERED started.
23:33:31.132 [QuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
23:33:31.134 [QuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.simpl.PropertySettingJobFactory - Producing instance of Job 'repeat.test1', class=com.example.docker_images.job.HelloJob
23:33:31.139 [QuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers
23:33:31.139 [QuartzScheduler_Worker-1] DEBUG org.quartz.core.JobRunShell - Calling execute on job repeat.test1
ljl
lsr
hello world!!!


4. 未来执行一次


  1. job简单实现
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
@Slf4j
public class HelloJob implements Job {
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //通过jobDetail获取数据
        String name = context.getJobDetail().getJobDataMap().getString("name");
        //通过trigger获取传参
        String nameByTrigger = context.getTrigger().getJobDataMap().getString("name");
        System.out.println(name);
        System.out.println(nameByTrigger);
        System.out.println("hello world!!!");
    }
}


  1. job调用
public static void main(String[] args) {
        try {
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            //利用jobDetail传参
            JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("test1", "repeat").build();
            jobDetail.getJobDataMap().put("name","ljl");
            //利用trigger传参
            Trigger build = newTrigger().withIdentity("test1", "repeat").startAt(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2020-12-26 23:26:00")).build();
            build.getJobDataMap().put("name","lsr");
            scheduler.scheduleJob(jobDetail,build);
            scheduler.start();
        }catch (Exception e){
            e.printStackTrace();
        }
        SpringApplication.run(DockerImagesApplication.class, args);
    }


  1. 执行结果
2020-12-26 23:25:06.309  INFO 20832 --- [           main] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed
2020-12-26 23:25:06.320  INFO 20832 --- [           main] d.s.w.p.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s)
2020-12-26 23:25:06.339  INFO 20832 --- [           main] s.d.s.w.s.ApiListingReferenceScanner     : Scanning for api listing references
2020-12-26 23:25:06.442  INFO 20832 --- [           main] c.e.d.DockerImagesApplication            : Started DockerImagesApplication in 5.418 seconds (JVM running for 6.339)
ljl
lsr
hello world!!!


5. 重复执行


  1. 简单job实现
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
@Slf4j
public class HelloJob implements Job {
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("hello world!!!");
    }
}


  1. job重复调用
public static void main(String[] args) {
        try {
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("test1", "repeat").build();
            CronTrigger build = newTrigger().withIdentity("test1", "repeat").withSchedule(cronSchedule("*/5 * * * * ?")).build();
            scheduler.scheduleJob(jobDetail,build);
            scheduler.start();
        }catch (Exception e){
            e.printStackTrace();
        }
        SpringApplication.run(DockerImagesApplication.class, args);
    }


  1. 执行结果
2020-12-26 23:06:43.122  INFO 21156 --- [           main] s.d.s.w.s.ApiListingReferenceScanner     : Scanning for api listing references
2020-12-26 23:06:43.209  INFO 21156 --- [           main] c.e.d.DockerImagesApplication            : Started DockerImagesApplication in 4.835 seconds (JVM running for 5.715)
hello world!!!
hello world!!!
hello world!!!
hello world!!!
hello world!!!


目录
相关文章
|
调度
Quartz-TriggerListener解读
Quartz-TriggerListener解读
67 0
quartz学习笔记7:trading
quartz学习笔记7:trading
78 0
|
存储 Java 调度
这种方式整合Quartz你见过吗?
`Quartz`是一款优秀的任务调度框架,支持内存、JDBC的形式来存储未执行的任务列表,支持多个任务节点同时执行任务,支持任务漂移到不同的节点执行。
|
Java 应用服务中间件
Quartz - 基础篇(下)
Quartz - 基础篇(下)
173 0
Quartz - 基础篇(下)
Quartz - 基础篇(上)
Quartz - 基础篇(上)
120 0
Quartz - 基础篇(上)
|
存储 安全 Java
Quartz 是什么?一文带你入坑
本文主要介绍 Quartz 的使用
684 0
|
Java 调度 Spring
Spring定时任务的实现:Quartz
Spring定时任务的实现:Quartz
169 0
Spring定时任务的实现:Quartz
|
开发框架 Java 调度
Quartz的初认识(一)
Quartz的初认识(一)
234 0
Quartz的初认识(一)