最全任务调度框架Quartz快速入门(1),高薪程序员必会

最后

这份清华大牛整理的进大厂必备的redis视频、面试题和技术文档

祝大家早日进入大厂,拿到满意的薪资和职级~~~加油!!

感谢大家的支持!!

image.png

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

// 触发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和JobDetail

  • Job是正在执行的作业实例,JobDetail是作业定义。

  • 一个Job可以创建多个JobDetail,拥有不同的JobDataMap。

这样定义有什么好处呢?举个例子吧,假设现在你定义了一个类实现了Job接口,比如:SendEmailJob。如果你希望根据用户的姓名,选择指定的人发送,那么你可以通过JobDataMap绑定参数传递进JobDetail中,也就是说我们需要创建两个不同的JobDetail,比如:SendEmailToSummerday和SendEmailToTQBX,他们拥有各自独立的JobDataMap,实现更加灵活。

Job的State和Concurrency

=========================

https://blog.csdn.net/fly_captain/article/details/83029440

@DisallowConcurrentExecution

================================

该注解标注在Job类上,意思是不能并发从同一个JobDetail定义的多个实例,但可以同时执行多个不同的JobDetail定义的实例。

拿上面的例子继续举例,假设SendEmailJob标注了此注解,表明同一时间可以同时执行SendEmailToSummerday和SendEmailToTQBX,因为他们是不同的JobDetail,但是不能同时执行多个SendEmailToSummerday。

@PersistJobDataAfterExecution

=================================

该注解也标注在Job类上,告诉Scheduler正常执行完Job之后,重新存储更新一下JobDataMap。一般标注此注解的Job类应当考虑也加上@DisallowConcurrentExecution注解,以避免同时执行Job时出现JobDataMap存储的竞争。

Trigger常见使用

===============

更多例子可以查看文档,非常详细: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()

最后

光给面试题不给答案不是我的风格。这里面的面试题也只是凤毛麟角,还有答案的话会极大的增加文章的篇幅,减少文章的可读性

Java面试宝典2021版

最常见Java面试题解析(2021最新版)

2021企业Java面试题精选

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

cB0cY-1715593752519)]

2021企业Java面试题精选

[外链图片转存中…(img-W6GaVDrF-1715593752520)]

[外链图片转存中…(img-E0xBlbyD-1715593752520)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值