定时任务框架Quartz学习笔记(一)

编程

Quartz 简介

    Quartz 是一个功能丰富且开源的作业调度库,从最小的独立应用程序到最大的电子商务系统,它几乎可以集成到任何 Java 应用中。

    Quartz 既可以创建简单的作业,也可以创建数以万计的复杂作业,它的作业定义符合标准 Java 组件规范,几乎可以执行所编写的所有内容。此外,它还包含许多企业级的功能,例如对 JTA 事务和集群的支持。

作业调度这个名词听着很高大上,通俗理解就是需要程序在固定时间做的某些事情,而 Quartz 这个框架就可以帮我们做这个事情,而且它可以很方便的和我们的应用进行集成使用。

Quartz 快速开始指南

1.下载与安装

  • 官网下载地址:http://www.quartz-scheduler.org/downloads,选择稳定版本(Stable Releases)下载
  • 安装

    • 将下载的稳定版本(例如:quartz-2.3.0-distribution.tar.gz)进行解压缩操作。获取解压文件夹 lib 下的 jar 包。
    • 新建一个 Java 项目,在项目根目录下新建 lib 文件夹,将 Quartz 所需要的 jar 包全部复制过来。
    • 配置 Quartz 所需要的 jar 为依赖项(不同IDE操作方式不一样,以IDEA为例)

    • 新建 HelloJob.java、QuartzTest.java、quartz.properties、log4j.properties。

HelloJob.java

package org.demo.quickstart;  

import org.quartz.Job;

import org.quartz.JobExecutionContext;

import org.quartz.JobExecutionException;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import java.util.Date;

public class HelloJob implements Job {

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

public HelloJob() {

}

@Override

public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

_log.info("Hello World! - " + new Date());

}

}`

QuartzTest.java

package org.demo.quickstart;  

import org.quartz.JobBuilder;

import org.quartz.JobDetail;

import org.quartz.Scheduler;

import org.quartz.SchedulerException;

import org.quartz.SimpleScheduleBuilder;

import org.quartz.Trigger;

import org.quartz.TriggerBuilder;

import org.quartz.impl.StdSchedulerFactory;

public class QuartzTest {

public static void main(String[] args) {

try {

// Grab the Scheduler instance from the Factory

Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

// and start it off

scheduler.start();

JobDetail job = JobBuilder.newJob(HelloJob.class)

.withIdentity("job1", "group1")

.build();

// Trigger the job to run now, and then repeat every 3 seconds

Trigger trigger = TriggerBuilder.newTrigger()

.withIdentity("trigger1", "group1")

.startNow()

.withSchedule(SimpleScheduleBuilder.simpleSchedule()

.withIntervalInSeconds(3)

.repeatForever())

.build();

// Tell quartz to schedule the job using our trigger

scheduler.scheduleJob(job, trigger);

Thread.sleep(12000);

scheduler.shutdown();

} catch (SchedulerException | InterruptedException se) {

se.printStackTrace();

}

}

}

log4j.properties

log4j.rootLogger=debug,console

# 控制台(console)

log4j.appender.console=org.apache.log4j.ConsoleAppender

log4j.appender.console.Threshold=debug

log4j.appender.console.ImmediateFlush=true

log4j.appender.console.Target=System.out

log4j.appender.console.layout=org.apache.log4j.PatternLayout

log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%p] [%l]: %m%n

quartz.properties

org.quartz.scheduler.instanceName = MyScheduler  

org.quartz.threadPool.threadCount = 3

org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

2.启动与效果预览

  • 启动:运行 QuartzTest.java 中的 main 方法。
  • 预览(运行日志)

"C:Program FilesJavajdk1.8.0_202injava.exe" "-javaagent:D:developerIntelliJ IDEA 2019.3.1libidea_rt.jar=50796:D:developerIntelliJ IDEA 2019.3.1in" -Dfile.encoding=UTF-8 -classpath "C:Program FilesJavajdk1.8.0_202jrelibcharsets.jar;C:Program FilesJavajdk1.8.0_202jrelibdeploy.jar;C:Program FilesJavajdk1.8.0_202jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.8.0_202jrelibextcldrdata.jar;C:Program FilesJavajdk1.8.0_202jrelibextdnsns.jar;C:Program FilesJavajdk1.8.0_202jrelibextjaccess.jar;C:Program FilesJavajdk1.8.0_202jrelibextjfxrt.jar;C:Program FilesJavajdk1.8.0_202jrelibextlocaledata.jar;C:Program FilesJavajdk1.8.0_202jrelibext

ashorn.jar;C:Program FilesJavajdk1.8.0_202jrelibextsunec.jar;C:Program FilesJavajdk1.8.0_202jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.8.0_202jrelibextsunmscapi.jar;C:Program FilesJavajdk1.8.0_202jrelibextsunpkcs11.jar;C:Program FilesJavajdk1.8.0_202jrelibextzipfs.jar;C:Program FilesJavajdk1.8.0_202jrelibjavaws.jar;C:Program FilesJavajdk1.8.0_202jrelibjce.jar;C:Program FilesJavajdk1.8.0_202jrelibjfr.jar;C:Program FilesJavajdk1.8.0_202jrelibjfxswt.jar;C:Program FilesJavajdk1.8.0_202jrelibjsse.jar;C:Program FilesJavajdk1.8.0_202jrelibmanagement-agent.jar;C:Program FilesJavajdk1.8.0_202jrelibplugin.jar;C:Program FilesJavajdk1.8.0_202jrelibesources.jar;C:Program FilesJavajdk1.8.0_202jrelibt.jar;D:developerDataideaProjectsframeworkquartzoutproductionquartz;D:developerDataideaProjectsframeworkquartzlibc3p0-0.9.5.2.jar;D:developerDataideaProjectsframeworkquartzliblog4j-1.2.16.jar;D:developerDataideaProjectsframeworkquartzlibslf4j-api-1.7.7.jar;D:developerDataideaProjectsframeworkquartzlibslf4j-log4j12-1.7.7.jar;D:developerDataideaProjectsframeworkquartzlibquartz-2.3.0-SNAPSHOT.jar;D:developerDataideaProjectsframeworkquartzlibquartz-jobs-2.3.0-SNAPSHOT.jar;D:developerDataideaProjectsframeworkquartzexamplegf.javax.transaction-1.1.jar;D:developerDataideaProjectsframeworkquartzexamplequartz-examples-2.3.0-SNAPSHOT.jar" org.demo.quickstart.QuartzTest

[INFO] 24 二月 11:31:22.308 下午 main [org.quartz.impl.StdSchedulerFactory]

Using default implementation for ThreadExecutor

[INFO] 24 二月 11:31:22.321 下午 main [org.quartz.core.SchedulerSignalerImpl]

Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl

[INFO] 24 二月 11:31:22.322 下午 main [org.quartz.core.QuartzScheduler]

Quartz Scheduler v.2.3.0-SNAPSHOT created.

[INFO] 24 二月 11:31:22.322 下午 main [org.quartz.simpl.RAMJobStore]

RAMJobStore initialized.

[INFO] 24 二月 11:31:22.323 下午 main [org.quartz.core.QuartzScheduler]

Scheduler meta-data: Quartz Scheduler (v2.3.0-SNAPSHOT) "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.

[INFO] 24 二月 11:31:22.323 下午 main [org.quartz.impl.StdSchedulerFactory]

Quartz scheduler "MyScheduler" initialized from default resource file in Quartz package: "quartz.properties"

[INFO] 24 二月 11:31:22.323 下午 main [org.quartz.impl.StdSchedulerFactory]

Quartz scheduler version: 2.3.0-SNAPSHOT

[INFO] 24 二月 11:31:22.323 下午 main [org.quartz.core.QuartzScheduler]

Scheduler MyScheduler_$_NON_CLUSTERED started.

[INFO] 24 二月 11:31:22.342 下午 MyScheduler_Worker-1 [org.demo.quickstart.HelloJob]

Hello World! - Mon Feb 24 23:31:22 GMT+08:00 2020

[INFO] 24 二月 11:31:25.327 下午 MyScheduler_Worker-2 [org.demo.quickstart.HelloJob]

Hello World! - Mon Feb 24 23:31:25 GMT+08:00 2020

[INFO] 24 二月 11:31:28.326 下午 MyScheduler_Worker-3 [org.demo.quickstart.HelloJob]

Hello World! - Mon Feb 24 23:31:28 GMT+08:00 2020

[INFO] 24 二月 11:31:31.326 下午 MyScheduler_Worker-1 [org.demo.quickstart.HelloJob]

Hello World! - Mon Feb 24 23:31:31 GMT+08:00 2020

[INFO] 24 二月 11:31:34.326 下午 MyScheduler_Worker-2 [org.demo.quickstart.HelloJob]

Hello World! - Mon Feb 24 23:31:34 GMT+08:00 2020

[INFO] 24 二月 11:31:34.329 下午 main [org.quartz.core.QuartzScheduler]

Scheduler MyScheduler_$_NON_CLUSTERED shutting down.

[INFO] 24 二月 11:31:34.329 下午 main [org.quartz.core.QuartzScheduler]

Scheduler MyScheduler_$_NON_CLUSTERED paused.

[INFO] 24 二月 11:31:34.330 下午 main [org.quartz.core.QuartzScheduler]

Scheduler MyScheduler_$_NON_CLUSTERED shutdown complete.

Process finished with exit code 0

可以看到,Hello World! 每隔 3s 打印一次,打印 12s (4次) 后,执行了 scheduler.shutdown(); 方法后程序结束。

END

以上是 定时任务框架Quartz学习笔记(一) 的全部内容, 来源链接: utcz.com/z/513802.html

回到顶部