标签:api ring 依赖 als its bug not exe shel
Quartz是一个功能强大的开源任务调度库,几乎可以集成到任何Java应用程序中,无论是超小型的独立应用还是超大型电子商务系统。
它常用于企业级应用中:
Quartz还支持集群模式和对JTA事务。
http://www.quartz-scheduler.org/documentation/quartz-2.2.2/tutorials/
JobExecutionContext
对象,为Job实例提供了丰富的运行时环境信息,比如:scheduler、trigger、jobDataMap、job、calendar、各种time等。Quartz提供了各种各样的Builder类,定义了Domain Specific Language,且都提供了静态的创建方法,我们可以使用
import static
简化书写。
usingJobData(key,value)
在构建Jobdetail的时候传入数据,使用jobDetail.getJobDataMap()
获取map。While developing Quartz, we decided that it made sense to create a separation between the schedule and the work to be performed on that schedule. This has (in our opinion) many benefits.
For example, Jobs can be created and stored in the job scheduler independent of a trigger, and many triggers can be associated with the same job. Another benefit of this loose-coupling is the ability to configure jobs that remain in the scheduler after their associated triggers have expired, so that that it can be rescheduled later, without having to re-define it. It also allows you to modify or replace a trigger without having to re-define its associated job.
隔离schedule和schedule上执行的Job,优点是可见的:
可以独立于触发器创建作业并将其存储在作业调度程序中,并且许多触发器可以与同一作业相关联。这样的松耦合好处是什么?
导入依赖
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.2</version>
</dependency>
简单案例如下
public class QuartzTest {
// 你需要在start和shutdown之间执行你的任务。
public static void main(String[] args) {
try {
// 从工厂中获取Scheduler示例
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
// 开始
scheduler.start();
// 定义Job,并将其绑定到HelloJob类中
JobDetail job = JobBuilder.newJob(HelloJob.class)
.withIdentity("job1", "group1") // name 和 group
.usingJobData("username", "天乔巴夏") // 置入JobDataMap
.usingJobData("age", "20")
.withDescription("desc-demo")
.build();
// 触发Job执行,每40s执行一次
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger1", "group1")
.startNow() // 立即开始
.withSchedule(SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(40)
.repeatForever())
.build();
// 告诉 quartz 使用trigger来调度job
scheduler.scheduleJob(job, trigger);
// 关闭,线程终止
scheduler.shutdown();
} catch (SchedulerException se) {
se.printStackTrace();
}
}
}
@Slf4j
@NoArgsConstructor
public class HelloJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
// 从context中获取属性
JobDetail jobDetail = context.getJobDetail();
JobDataMap jobDataMap = jobDetail.getJobDataMap();
JobKey key = jobDetail.getKey();
Class<? extends Job> jobClass = jobDetail.getJobClass();
String description = jobDetail.getDescription();
String username = jobDataMap.getString("username");
int age = jobDataMap.getIntValue("age");
log.info("\nJobKey : {},\n JobClass : {},\n JobDesc : {},\n username : {},\n age : {}",
key, jobClass.getName(), description, username, age);
}
}
启动测试,打印日志如下:
01:23:12.406 [main] INFO org.quartz.impl.StdSchedulerFactory - Using default implementation for ThreadExecutor
01:23:12.414 [main] INFO org.quartz.simpl.SimpleThreadPool - Job execution threads will use class loader of thread: main
01:23:12.430 [main] INFO org.quartz.core.SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
01:23:12.430 [main] INFO org.quartz.core.QuartzScheduler - Quartz Scheduler v.2.3.2 created.
01:23:12.432 [main] INFO org.quartz.simpl.RAMJobStore - RAMJobStore initialized.
01:23:12.433 [main] INFO 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.
01:23:12.433 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler ‘DefaultQuartzScheduler‘ initialized from default resource file in Quartz package: ‘quartz.properties‘
01:23:12.433 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 2.3.2
01:23:12.434 [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
01:23:12.434 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers
01:23:12.443 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
01:23:12.445 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.simpl.PropertySettingJobFactory - Producing instance of Job ‘group1.job1‘, class=com.hyhwky.HelloJob
01:23:12.451 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers
01:23:12.452 [DefaultQuartzScheduler_Worker-1] DEBUG org.quartz.core.JobRunShell - Calling execute on job group1.job1
01:23:12.452 [DefaultQuartzScheduler_Worker-1] INFO com.hyhwky.HelloJob -
JobKey : group1.job1,
JobClass : com.hyhwky.HelloJob,
JobDesc : desc-demo,
username : 天乔巴夏,
age : 20
我们可以看到quartz已经被初始化了,初始化配置如下,在org\quartz-scheduler\quartz\2.3.2\quartz-2.3.2.jar!\org\quartz\quartz.properties
# 调度器配置
org.quartz.scheduler.instanceName: DefaultQuartzScheduler
org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false
# 线程池配置
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 10
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true
# 存储配置
org.quartz.jobStore.misfireThreshold: 60000 #trigger 容忍时间60s
org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore
更多的配置:Quartz Configuration Reference
Job和JobDetail
这样定义有什么好处呢?举个例子吧,假设现在你定义了一个类实现了Job接口,比如:SendEmailJob。如果你希望根据用户的姓名,选择指定的人发送,那么你可以通过JobDataMap绑定参数传递进JobDetail中,也就是说我们需要创建两个不同的JobDetail,比如:SendEmailToSummerday和SendEmailToTQBX,他们拥有各自独立的JobDataMap,实现更加灵活。
该注解标注在Job类上,意思是不能并发执同一个JobDetail定义的多个实例,但可以同时执行多个不同的JobDetail定义的实例。
拿上面的例子继续举例,假设SendEmailJob标注了此注解,表明同一时间可以同时执行SendEmailToSummerday和SendEmailToTQBX,因为他们是不同的JobDetail,但是不能同时执行多个SendEmailToSummerday。
该注解也标注在Job类上,告诉Scheduler正常执行完Job之后,重新存储更新一下JobDataMap。一般标注此注解的Job类应当考虑也加上@DisallowConcurrentExecution注解,以避免同时执行Job时出现JobDataMap存储的竞争。
更多例子可以查看文档,非常详细:SimpleTrigger 和 CronTrigger
构建一个触发器,该触发器将立即触发,然后每五分钟重复一次,直到小时 22:00:
import static org.quartz.TriggerBuilder.*;
import static org.quartz.SimpleScheduleBuilder.*;
import static org.quartz.DateBuilder.*:
SimpleTrigger trigger = (SimpleTrigger) newTrigger()
.withIdentity("trigger7", "group1")
.withSchedule(simpleSchedule()
.withIntervalInMinutes(5)
.repeatForever())
.endAt(dateOf(22, 0, 0))
.build();
构建一个触发器,该触发器将在周三上午 10:42 触发,在系统默认值以外的时区中:
import static org.quartz.TriggerBuilder.*;
import static org.quartz.CronScheduleBuilder.*;
import static org.quartz.DateBuilder.*:
trigger = newTrigger()
.withIdentity("trigger3", "group1")
.withSchedule(cronSchedule("0 42 10 ? * WED")) // [秒] [分] [时] [月的第几天] [月] [一星期的第几天] [年(可选)]
.inTimeZone(TimeZone.getTimeZone("America/Los_Angeles"))
.forJob(myJobKey)
.build();
Job的持久化是非常重要的,如果Job不能持久化,一旦不再有与之关联的Trigger,他就会自动从调度程序中被删除。
Job存储器的类型:
类型 | 优点 | 缺点 |
---|---|---|
RAMJobStore | 不要外部数据库,配置容易,运行速度快 | 因为调度程序信息是存储在被分配给 JVM 的内存里面,所以,当应用程序停止运行时,所有调度信息将被丢失。另外因为存储到JVM内存里面,所以可以存储多少个 Job 和 Trigger 将会受到限制 |
JDBC 作业存储 | 支持集群,因为所有的任务信息都会保存到数据库中,可以控制事物,还有就是如果应用服务器关闭或者重启,任务信息都不会丢失,并且可以恢复因服务器关闭或者重启而导致执行失败的任务 | 运行速度的快慢取决与连接数据库的快慢 |
具体的使用方法:http://www.quartz-scheduler.org/documentation/quartz-2.2.2/tutorials/tutorial-lesson-09.html
标签:api ring 依赖 als its bug not exe shel
原文地址:https://www.cnblogs.com/summerday152/p/14192845.html