本篇内容:
本篇只做简介及效果预览项目搭建,不做代码分析。
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 Files\Java\jdk1.8.0_202\bin\java.exe" "-javaagent:D:\developer\IntelliJ IDEA 2019.3.1\lib\idea_rt.jar=50796:D:\developer\IntelliJ IDEA 2019.3.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_202\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\rt.jar;D:\developerData\ideaProjects\framework\quartz\out\production\quartz;D:\developerData\ideaProjects\framework\quartz\lib\c3p0-0.9.5.2.jar;D:\developerData\ideaProjects\framework\quartz\lib\log4j-1.2.16.jar;D:\developerData\ideaProjects\framework\quartz\lib\slf4j-api-1.7.7.jar;D:\developerData\ideaProjects\framework\quartz\lib\slf4j-log4j12-1.7.7.jar;D:\developerData\ideaProjects\framework\quartz\lib\quartz-2.3.0-SNAPSHOT.jar;D:\developerData\ideaProjects\framework\quartz\lib\quartz-jobs-2.3.0-SNAPSHOT.jar;D:\developerData\ideaProjects\framework\quartz\example\gf.javax.transaction-1.1.jar;D:\developerData\ideaProjects\framework\quartz\example\quartz-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
来源:oschina
链接:https://my.oschina.net/sharpever/blog/3171854