quartz的简单运用
添加依赖
<!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz定时任务 -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.2</version>
</dependency>
配置xml文件spring-context-jobs.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:websocket="http://www.springframework.org/schema/websocket"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/websocket
http://www.springframework.org/schema/websocket/spring-websocket.xsd">
<bean name="MyScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobFactory" ref="autowireFactory">
</property>
</bean>
<bean id="autowireFactory" class="com.eastcom.quartz.factory.AutowireJobFactory"></bean>
<!-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>任务调度配置<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< -->
<!-- 实例化bean
<bean id="demo" class="com.eastcom.quartz.job.ScOrderAutoCloseByOutTimeJob" /> 定时任务要执行的类
配置MethodInvokingJobDetailFactoryBean
<bean id="testTaskMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="demo" />
<property name="targetMethod" value="execute" /> 定时任务要执行的方法
<property name="concurrent" value="false" />设置为false时,定时任务 job结束,才会执行下一个定时任务;
</bean>
============= 调度触发器 =============
<bean id="articleBatchRunTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="testTaskMethod" />
<property name="cronExpression" value="0 55 11 * * ?" /> 每天 11:55 执行一次
<property name="cronExpression" value="0 0/1 * * * ?" />每隔1分钟执行一次
<property name="cronExpression" value="0/5 * * * * ?" /> 每5秒执行一次
</bean>
其他执行执行规则可以自行查找
============= 调度工厂 =============
<bean id="schedulerBean"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
lazy-init="false">
<property name="triggers">
<list>
<ref bean="articleBatchRunTrigger" />
</list>
</property>
</bean> -->
</beans>
添加包quartz
编写则中对应的类
在工具类中写出启动任务的开始方法也就是spring容器初始化之后就会执行启动任务方法
`
spring的工具类InstantiationTracingBeanPostProcessor
package com.eastcom.common.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import com.eastcom.quartz.service.SysTaskService;
public class InstantiationTracingBeanPostProcessor implements ApplicationListener<ContextRefreshedEvent> {
@Autowired
private SysTaskService sysTaskService;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
// if (event.getApplicationContext().getParent() == null)
// {// root application context 没有parent,他就是老大.
// 需要执行的逻辑代码,当spring容器初始化完成后就会执行该方法。
// FlowUICacheBean.recacheAll();
// }
sysTaskService.runAllEnableTask();
}
}
`