spring定时任务配置

nan_feng_wushi 2018-12-14 10:40:10
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......");
}
}

...全文
114 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Insist_on_progress 2019-01-08
  • 打赏
  • 举报
回复
看不太明白你在说啥?先看看这篇博客把https://blog.csdn.net/qq_41988504/article/details/86063980
十八道胡同 2018-12-14
  • 打赏
  • 举报
回复
问题是???

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧