Quartz提供了一些Scheduler(调度策略),以便我们管理和执行Job(任务)。
官网介绍:
二:源码下载Quartz是一个功能丰富、开源的任务调度库,它可以被集成到所有的Java程序,无论是很小的单节点还是规模庞大的商业系统。Quartz可以被用来创建简单或者复杂的调度策略,以执行成千上万的任务。任务一般是指一个标准的Java组件,实际上可以是你写代码指定的任何逻辑。Quartz Scheduler还包括很多企业级的特性,例如JTA事务控制和集群。
下载地址
三:怎么使用Quartz 大致步骤:
1. 创建SchedulerFactory
2. 创建Scheduler
3. 创建JobDetail
4. 创建Trigger
5. 注册到Scheduler:scheduler.scheduleJob(jobDetail, trigger)
6. 启动Scheduler:scheduler.start()
实例代码
以非集群的RamJob为例
- RAMJob.java
package org.quartz.entity;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.util.Date;
/**
* @author lizhangyu
* @date 2022/4/25 11:19
*/
public class RAMJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("Hello Quartz: " + new Date() + " " + Thread.currentThread().getName());
}
}
- RAMJobTest.java
package org.quartz.entity;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author lizhangyu
* @date 2022/4/25 11:23
*/
public class RAMJobTest {
public static void main(String[] args) throws SchedulerException{
/**
* JobDetail:用来绑定Job,并且在job执行的时候携带一些执行的信息
*/
//创建一个JobDetail实例,将该实例与HelloJob Class绑定
JobDetail jobDetail = JobBuilder.newJob(RAMJob.class)
.withIdentity("rmaJob", "rmaGroup").build();
/**
* Trigger:用来触发job去执行的,包括定义了什么时候去执行,
* 第一次执行,是否会一直重复地执行下去,执行几次等
*/
//创建一个Trigger实例,定义该job立即执行,并且每隔2秒钟重复执行一次,直到程序停止
/**
* trigger通过builder模式来创建,TriggerBuilder.newTrigger()
* withIdentity():定义一个标识符,定义了组
* startNow():定义现在开始执行,
* withSchedule(SimpleScheduleBuilder.simpleSchedule():withSchedule也是builder模式创建
*.withIntervalInSeconds(2).repeatForever()):定义了执行频度:每2秒钟执行一次,不间断地重复执行
* build():创建trigger
*/
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("rmaTrigger", "rmaTriggerGroup").startNow()
.withSchedule(CronScheduleBuilder.cronSchedule("0/3 * * * * ?")).build();
//创建scheduler实例:
/**
* scheduler区别于trigger和jobDetail,是通过factory模式创建的
*/
//创建一个ScheduleFactory
SchedulerFactory sfact = new StdSchedulerFactory();
Scheduler scheduler = sfact.getScheduler();
scheduler.start();
//打印当前时间
Date date = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("当前的时间为(HelloScheduler):" + sf.format(date));
//需要将jobDetail和trigger传进去,并将jobDetail和trigger绑定在一起。
scheduler.scheduleJob(jobDetail, trigger);
}
}
- 程序主要输出
D:\java\jdk1.8\bin\java.exe "-javaagent:D:\idea\IntelliJ IDEA 2018.1.6\lib\idea_rt.jar=60579:D:\idea\IntelliJ IDEA 2018.1.6\bin" -Dfile.encoding=UTF-8 -classpath D:\java\jdk1.8\jre\lib\charsets.jar;D:\java\jdk1.8\jre\lib\deploy.jar;D:\java\jdk1.8\jre\lib\ext\access-bridge-64.jar;D:\java\jdk1.8\jre\lib\ext\cldrdata.jar;D:\java\jdk1.8\jre\lib\ext\dnsns.jar;D:\java\jdk1.8\jre\lib\ext\jaccess.jar;D:\java\jdk1.8\jre\lib\ext\jfxrt.jar;D:\java\jdk1.8\jre\lib\ext\localedata.jar;D:\java\jdk1.8\jre\lib\ext\nashorn.jar;D:\java\jdk1.8\jre\lib\ext\sunec.jar;D:\java\jdk1.8\jre\lib\ext\sunjce_provider.jar;D:\java\jdk1.8\jre\lib\ext\sunmscapi.jar;D:\java\jdk1.8\jre\lib\ext\sunpkcs11.jar;D:\java\jdk1.8\jre\lib\ext\zipfs.jar;D:\java\jdk1.8\jre\lib\javaws.jar;D:\java\jdk1.8\jre\lib\jce.jar;D:\java\jdk1.8\jre\lib\jfr.jar;D:\java\jdk1.8\jre\lib\jfxswt.jar;D:\java\jdk1.8\jre\lib\jsse.jar;D:\java\jdk1.8\jre\lib\management-agent.jar;D:\java\jdk1.8\jre\lib\plugin.jar;D:\java\jdk1.8\jre\lib\resources.jar;D:\java\jdk1.8\jre\lib\rt.jar;F:\idea_resource_code\quartz\quartz-core\target\test-classes;F:\idea_resource_code\quartz\quartz-core\target\classes;D:\Maven\repository\com\mchange\c3p0.9.5.5\c3p0-0.9.5.5.jar;D:\Maven\repository\com\mchange\mchange-commons-java.2.19\mchange-commons-java-0.2.19.jar;D:\Maven\repository\com\zaxxer\HikariCP.4.5\HikariCP-3.4.5.jar;D:\Maven\repository\org\slf4j\slf4j-api.7.36\slf4j-api-1.7.36.jar;D:\Maven\repository\javax\xml\bind\jaxb-api.3.1\jaxb-api-2.3.1.jar;D:\Maven\repository\javax\activation\javax.activation-api.2.0\javax.activation-api-1.2.0.jar;D:\Maven\repository\org\apache\openejb\javaee-api.0-2\javaee-api-5.0-2.jar;D:\Maven\repository\org\slf4j\slf4j-reload4j.7.36\slf4j-reload4j-1.7.36.jar;D:\Maven\repository\ch\qos\reload4j\reload4j.2.19\reload4j-1.2.19.jar;D:\Maven\repository\log4j\log4j.2.17\log4j-1.2.17.jar;D:\Maven\repository\org\hamcrest\hamcrest-library.2\hamcrest-library-1.2.jar;D:\Maven\repository\org\hamcrest\hamcrest-core.2\hamcrest-core-1.2.jar;D:\Maven\repository\org\mockito\mockito-all.9.5\mockito-all-1.9.5.jar;D:\Maven\repository\junit\junit-dep.8.2\junit-dep-4.8.2.jar;D:\Maven\repository\asm\asm.2\asm-3.2.jar;D:\Maven\repository\org\apache\derby\derby.8.2.2\derby-10.8.2.2.jar;D:\Maven\repository\org\apache\derby\derbynet.8.2.2\derbynet-10.8.2.2.jar;D:\Maven\repository\org\apache\derby\derbyclient.8.2.2\derbyclient-10.8.2.2.jar org.quartz.entity.RAMJobTest
2022-04-26 22:20:34,724 INFO [org.quartz.impl.StdSchedulerFactory] - Using default implementation for ThreadExecutor
2022-04-26 22:20:34,768 INFO [org.quartz.simpl.SimpleThreadPool] - Job execution threads will use class loader of thread: main
2022-04-26 22:20:34,821 INFO [org.quartz.core.SchedulerSignalerImpl] - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
2022-04-26 22:20:34,821 INFO [org.quartz.core.QuartzScheduler] - Quartz Scheduler v2.4.0-SNAPSHOT created.
2022-04-26 22:20:34,822 INFO [org.quartz.simpl.RAMJobStore] - RAMJobStore initialized.
2022-04-26 22:20:34,832 INFO [org.quartz.core.QuartzScheduler] - Scheduler meta-data: Quartz Scheduler (v2.4.0-SNAPSHOT) '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-04-26 22:20:34,832 INFO [org.quartz.impl.StdSchedulerFactory] - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
2022-04-26 22:20:34,832 INFO [org.quartz.impl.StdSchedulerFactory] - Quartz scheduler version: 2.4.0-SNAPSHOT
2022-04-26 22:20:34,833 INFO [org.quartz.core.QuartzScheduler] - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
当前的时间为(HelloScheduler):2022-04-26 22:20:34
Hello Quartz: Tue Apr 26 22:20:36 CST 2022 DefaultQuartzScheduler_Worker-1
Hello Quartz: Tue Apr 26 22:20:39 CST 2022 DefaultQuartzScheduler_Worker-2
Hello Quartz: Tue Apr 26 22:20:42 CST 2022 DefaultQuartzScheduler_Worker-3
Hello Quartz: Tue Apr 26 22:20:45 CST 2022 DefaultQuartzScheduler_Worker-4
Hello Quartz: Tue Apr 26 22:20:48 CST 2022 DefaultQuartzScheduler_Worker-5
Hello Quartz: Tue Apr 26 22:20:51 CST 2022 DefaultQuartzScheduler_Worker-6
Hello Quartz: Tue Apr 26 22:20:54 CST 2022 DefaultQuartzScheduler_Worker-7
Hello Quartz: Tue Apr 26 22:20:57 CST 2022 DefaultQuartzScheduler_Worker-8
Hello Quartz: Tue Apr 26 22:21:00 CST 2022 DefaultQuartzScheduler_Worker-9
Hello Quartz: Tue Apr 26 22:21:03 CST 2022 DefaultQuartzScheduler_Worker-10
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)