spring定时任务配置
1.在资源文件夹resources下,新建定时任务配置文件service-quartz.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:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
default-destroy-method="destroy" default-init-method="init"
default-autowire="byName">
<!-- Quartz -->
<bean name="quartzJobObj" class="com.huawei.csb.analysis.dispatch.common.QuartzJob" />
<bean id="quartzJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!--ref="quartzJobObj"为类名 -->
<!--value="autoRun"为加载类名中的定时任务方法入口 -->
<property name="targetObject" ref="quartzJobObj"></property>
<property name="targetMethod" value="autoRun"></property>
<property name="concurrent" value="false"></property>
</bean>
<bean id="quartzTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="quartzJob" />
<!-- 配置时间,此处为一分钟一次 -->
<property name="cronExpression" value="0 */1 * * * ?" />
</bean>
<bean name="quartzScheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
autowire="no">
<property name="triggers">
<list>
<ref local="quartzTrigger" />
</list>
</property>
</bean>
</beans>
2.spring的applicationContext.xml文件中引入定时任务
<import resource="classpath:service-quartz.xml" />
3.编写定时任务入口,java类(QuartzJob.java)
package com.huawei.csb.analysis.dispatch.common;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import org.quartz.CronExpression;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.huawei.csb.analysis.dispatch.domain.Dispatch;
import com.huawei.csb.analysis.dispatch.service.DispatchService;
public class QuartzJob {
@Autowired
private DispatchService dispatchService;
@Autowired
private DiExistence diExistence;
//1表示任务处于开启的状态
public static int STATUS = 1;
private static final Logger LOGGER = LoggerFactory.getLogger(QuartzJob.class);
/**
* 定时任务:分钟调用一次,
* 根据查询匹配到的调度任务,进行校验。如果满足当前的调度任务计划则开始执行一次调度.
* @throws ParseException
*/
public void autoRun() throws ParseException {
LOGGER.info("QuartzJob is start......");
List<Dispatch> list = dispatchService.selectAll();
for (Dispatch dispatch : list) {
try {
//当前时间匹配corn表达式
CronExpression cronExpression = new CronExpression(dispatch.getCron());
boolean satisfiedBy = cronExpression.isSatisfiedBy(new Date());
if (satisfiedBy) {
if (dispatch.getStatus() == STATUS) {
diExistence.ExecuteDispatch(dispatch);
}
}
} catch (ParseException e) {
e.printStackTrace();
}finally{
}
}
LOGGER.info("QuartzJob is end......");
}
}