1 下载和安装
在这里Download Quartz可以下载最新版本的压缩包,及查看maven配置:
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
如果是下载使用,要注意相关的jar包及依赖的jar包要在classpath路径下。这里推荐使用maven或者gradle等工具。
2 配置编辑quartz.properties。Quartz有大量可配置的属性,具体可以查看这个页面:Quartz Configuration Reference。
maven项目可以配置在resources文件夹下
一个基本的配置文件内容大概是这样:
# 调度名称
org.quartz.scheduler.instanceName = MyScheduler
# 线程数量,意味着最大可以同时跑的Job数量
org.quartz.threadPool.threadCount = 3
# 所有的数据都是放在内存里,包括jobs和triggers
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
一个简单的Demo
启动报错:
public class QuickStart {
public static void main(String[] args) {
try {
// Grab the Scheduler instance from the Factory
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
// and start it off
scheduler.start();
scheduler.shutdown();
} catch (SchedulerException se) {
se.printStackTrace();
}
}
}
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
配置一个依赖即可解决
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.25</version>
</dependency>
稍做修改:
// 定义Job
public class MyJob implements Job {
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
Logger.getAnonymousLogger().info("MyJob");
}
}
// 任务
public class QuickStart {
public static void main(String[] args) {
try {
// Grab the Scheduler instance from the Factory
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
// and start it off
scheduler.start();
// define the job and tie it to our MyJob class
JobDetail job = newJob(MyJob.class)
.withIdentity("myJob", "group1")
.build();
// Trigger the job to run now, and then every 5 seconds
Trigger trigger = newTrigger()
.withIdentity("myTrigger", "group1")
.startNow()
.withSchedule(simpleSchedule()
.withIntervalInSeconds(5)
.repeatForever())
.build();
// Tell quartz to schedule the job using our trigger
scheduler.scheduleJob(job, trigger);
} catch (SchedulerException se) {
se.printStackTrace();
}
}
}
日志:
五月 23, 2018 8:52:19 下午 org.quartz.impl.StdSchedulerFactory instantiate
信息: Using default implementation for ThreadExecutor
五月 23, 2018 8:52:19 下午 org.quartz.core.SchedulerSignalerImpl <init>
信息: Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
五月 23, 2018 8:52:19 下午 org.quartz.core.QuartzScheduler <init>
信息: Quartz Scheduler v.2.2.1 created.
五月 23, 2018 8:52:19 下午 org.quartz.simpl.RAMJobStore initialize
信息: RAMJobStore initialized.
五月 23, 2018 8:52:19 下午 org.quartz.core.QuartzScheduler initialize
信息: 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.
五月 23, 2018 8:52:19 下午 org.quartz.impl.StdSchedulerFactory instantiate
信息: Quartz scheduler 'MyScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
五月 23, 2018 8:52:19 下午 org.quartz.impl.StdSchedulerFactory instantiate
信息: Quartz scheduler version: 2.2.1
五月 23, 2018 8:52:19 下午 org.quartz.core.QuartzScheduler start
信息: Scheduler MyScheduler_$_NON_CLUSTERED started.
五月 23, 2018 8:52:19 下午 start.MyJob execute
信息: MyJob
五月 23, 2018 8:52:24 下午 start.MyJob execute
信息: MyJob
五月 23, 2018 8:52:29 下午 start.MyJob execute
信息: MyJob
参考文档:
Quartz Quick Start Guide
感谢您的阅读,若您喜欢,可以点击下方的的“推荐”支持我。谢谢!
也可以关注我的慕课账号,会经常更新Java、算法、Vue开发方面的文章哦~
点击查看更多内容
4人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦