Quartz学习(1)

1 什么是Quartz

Quartz 是一个功能丰富的开源作业调度库,几乎可以集成到任何 Java 应用程序中 - 从最小的独立应用程序到最大的电子商务系统。Quartz可用于创建简单或复杂的计划,以执行数十个,数百个甚至数万个作业;其任务被定义为标准 Java 组件的作业,这些组件几乎可以执行您可以对它们进行编程执行的任何操作。Quartz 调度程序包括许多企业级功能,例如支持 JTA 事务和集群

如果你的应用程序具有需要在给定时刻执行的任务,或者如果你的系统具有重复的维护工作,则Quartz可能是你理想的解决方案。

2 Quartz入门——第一个定时程序

2.1 依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>


        <dependency>
            <groupId>org.quartz-schedulergroupId>
            <artifactId>quartzartifactId>
            <version>2.3.2version>
        dependency>


        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

2.2 Java代码实现

public class HelloJob implements Job {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("hello "+ DateUtil.getDateTime(new Date()));
    }
}
@SpringBootTest
class QuartzApplicationTests {

    @Test
    void contextLoads() {

        try {
            // 获得默认的定时调度器
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

            //开启定时调度器
            scheduler.start();


            //定义需要执行的任务HelloJob与任务描述JobDetail  job
            JobDetail job = newJob(HelloJob.class)
                    .withIdentity("job1", "group1")
                    .build();

            //定义触发器
            Trigger trigger = newTrigger()
                    .withIdentity("trigger1", "group1")
                    //立即启动运行
                    .startNow()
                    //调度策略定义
                    //使用simpleSchedule,每三秒钟运行一次
                    .withSchedule(simpleSchedule()
                            .withIntervalInSeconds(3)
                            //任务用于重复
                            .repeatForever())
                    .build();

            //调度器绑定需要执行的任务以及触发器
            scheduler.scheduleJob(job, trigger);


            //休眠60s,为了在任务调度器关闭前,观察定时任务的执行情况
            Thread.sleep(60000);
            // 关闭定时任务调度器
            scheduler.shutdown();

        } catch (SchedulerException | InterruptedException se) {
            se.printStackTrace();
        }
    }

}

2.3 功能测试

2022-09-24 17:02:21.493  INFO 12008 --- [           main] org.quartz.impl.StdSchedulerFactory      : Using default implementation for ThreadExecutor
2022-09-24 17:02:21.495  INFO 12008 --- [           main] org.quartz.simpl.SimpleThreadPool        : Job execution threads will use class loader of thread: main
2022-09-24 17:02:21.512  INFO 12008 --- [           main] org.quartz.core.SchedulerSignalerImpl    : Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
2022-09-24 17:02:21.512  INFO 12008 --- [           main] org.quartz.core.QuartzScheduler          : Quartz Scheduler v.2.3.2 created.
2022-09-24 17:02:21.513  INFO 12008 --- [           main] org.quartz.simpl.RAMJobStore             : RAMJobStore initialized.
2022-09-24 17:02:21.514  INFO 12008 --- [           main] org.quartz.core.QuartzScheduler          : Scheduler meta-data: Quartz Scheduler (v2.3.2) '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.

2022-09-24 17:02:21.514  INFO 12008 --- [           main] org.quartz.impl.StdSchedulerFactory      : Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
2022-09-24 17:02:21.514  INFO 12008 --- [           main] org.quartz.impl.StdSchedulerFactory      : Quartz scheduler version: 2.3.2
2022-09-24 17:02:21.514  INFO 12008 --- [           main] org.quartz.core.QuartzScheduler          : Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
hello 2022-09-24 17:02:21
hello 2022-09-24 17:02:24
hello 2022-09-24 17:02:27
hello 2022-09-24 17:02:30
hello 2022-09-24 17:02:33
hello 2022-09-24 17:02:36
hello 2022-09-24 17:02:39
hello 2022-09-24 17:02:42
hello 2022-09-24 17:02:45
hello 2022-09-24 17:02:48
hello 2022-09-24 17:02:51
hello 2022-09-24 17:02:54
hello 2022-09-24 17:02:57
hello 2022-09-24 17:03:00
hello 2022-09-24 17:03:03
hello 2022-09-24 17:03:06
hello 2022-09-24 17:03:09
hello 2022-09-24 17:03:12
hello 2022-09-24 17:03:15
hello 2022-09-24 17:03:18
hello 2022-09-24 17:03:21
2022-09-24 17:03:21.521  INFO 12008 --- [           main] org.quartz.core.QuartzScheduler          : Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutting down.
2022-09-24 17:03:21.521  INFO 12008 --- [           main] org.quartz.core.QuartzScheduler          : Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED paused.
2022-09-24 17:03:21.521  INFO 12008 --- [           main] org.quartz.core.QuartzScheduler          : Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutdown complete.

从运行结果可知,定时任务正常执行。在任务调度器关闭前,间隔三秒钟运行了HelloJob 中的execute(JobExecutionContext jobExecutionContext) 方法。

3 Quartz组件介绍

Quartz学习(1)_第1张图片
由上图及演示代码可知,Quartz框架中重要的API与以下接口有关

  • Scheduler - 与调度程序交互.
  • Job - 任务执行单元
  • JobDetail - 定义Job信息
  • Trigger - 定义任务执行的触发时间信息
  • JobBuilder - 用于定于JobDetail实例
  • TriggerBuilder - 用于定于Trigger实例

你可能感兴趣的:(quartz定时框架,学习,java,quartz)