了解了什么是任务调度和任务调度框架的基本使用,我们需要将任务调度框架整合到Spring工厂,简化开发流程,避免复杂的关联关系和依赖关系,降低类和类之间的耦合度.
前文推荐
面试官问:订单的定时提醒支付功能是怎么实现的?我的回答让我拿到了offer!
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>4.2.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>4.2.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.quartz-schedulergroupId>
<artifactId>quartzartifactId>
<version>2.2.1version>
dependency>
package per.leiyu.job;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.util.Date;
/**
* @author 雷雨
* @date 2020/6/28 8:32
*/
public class MyJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("Spring 整合Quartz"+new Date());
}
}
- JobDetail:
JobDetailFactoryBean
- 触发器:
CronTriggerFactoryBean
- 调度器:
SchedulerFactoryBean
Spring 整合Quartz进行配置遵循下面的步骤:
- 定义工作任务的job
- 定义触发器Trigger,并将触发器和工作任务绑定
- 定义调度器,并将Trigger注册到Scheduler
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="lxJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="name" value="job1">property>
<property name="group" value="job_group1">property>
<property name="jobClass" value="per.leiyu.job.MyJob">property>
bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="name" value="trigger1"/>
<property name="group" value="trigger_group1"/>
<property name="jobDetail" ref="lxJob"/>
<property name="cronExpression" value="*/2 * * * * ?"/>
bean>
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger">ref>
list>
property>
<property name="quartzProperties">
<value>
#名为:quartz.properties ,放置在classpath下,如果没有此配置则按默认配置启动
# 指定调度器名称,非实现类
org.quarz.scheduler.instanceName=DefaultQuartzScheduler
#指定线程池实现类
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
#线程池线程数量
org.quartz.threadPool.threadCount=10
#优先级,默认5
org.quartz,threadPool.threadPriority= 5
#非持久化job
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
value>
property>
bean>
beans>
工厂启动,调度器启动,任务调度开始
工厂关闭,任务停止
package per.leiyu;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author 雷雨
* @date 2020/6/28 8:49
*/
public class TestMyJob {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("/springcontext.xml");
}
}
结果 |
---|
Job任务还是两秒执行一次,睡眠5秒之后想要将其删除(删除任务)
- deleteJob(Jobkey.jobkey(任务名,任务组别))
public class TestMyJob {
public static void main(String[] args) throws InterruptedException, SchedulerException {
ApplicationContext context = new ClassPathXmlApplicationContext("/springcontext.xml");
Thread.sleep(5000);
Scheduler scheduler = (Scheduler) context.getBean("scheduler");
scheduler.deleteJob(JobKey.jobKey("job1","job_group1"));
}
}
删除任何执行结果 |
---|
public class TestMyJob {
public static void main(String[] args) throws InterruptedException, SchedulerException {
ApplicationContext context = new ClassPathXmlApplicationContext("/springcontext.xml");
Thread.sleep(5000);
Scheduler scheduler = (Scheduler) context.getBean("scheduler");
//scheduler.deleteJob(JobKey.jobKey("job1","job_group1"));
//暂停
scheduler.pauseJob(JobKey.jobKey("job1","job_group1"));
Thread.sleep(5000);
//恢复任务
scheduler.resumeJob(JobKey.jobKey("job1","job_group1"));
}
}
暂停和恢复执行结果 |
---|
因为Job任务是有分组的,可以通过操作同一个组中的任务达到批量操作的目的
- 注意暂停和恢复支持批量操作
- 但是删除不支持批量操作(删除操作比较敏感)
public class TestMyJob {
public static void main(String[] args) throws InterruptedException, SchedulerException {
ApplicationContext context = new ClassPathXmlApplicationContext("/springcontext.xml");
Thread.sleep(5000);
StdScheduler scheduler = (StdScheduler) context.getBean("scheduler");
scheduler.pauseJobs(GroupMatcher.groupEquals("job_group1"));
Thread.sleep(5000);
scheduler.resumeJobs(GroupMatcher.groupEquals("job_group1"));
}
}
批量操作执行结果 |
---|
我是雷雨,一个
普本科
的学生,主要专注于Java后端和大数据开发
如果这篇文章有帮助到你,希望你给我一个
大大的赞
如果有什么问题,希望你能留言
和我一起研究
,学习靠自觉,分享靠自愿
转载注明出处
https://blog.csdn.net/qq_40742223