1.applicationContext.xml文件配置
2.pplication-quartz-cluster.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- JobFactory定义 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="ddJobTrigger" />
</list>
</property>
</bean>
<!-- JobTeigger定义 -->
<bean id="ddJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="ddJobDetail" />
</property>
<property name="cronExpression">
<value>59 59 23 * * ? </value>
</property>
</bean>
<!-- JobDetail定义 -->
<bean name="ddJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.chw.task.ddTask" />
<property name="jobDataAsMap">
<map>
<entry key="lptDdSpService" value-ref="lptDdSpServiceImpl" />
</map>
</property>
</bean>
</beans>
3.任务类编写
package com.chw.task;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import com.chw.framework.log.ChwLogUtil;
import com.chw.model.lpt.LptDpDdSp;
import com.chw.service.lpt.ILptDdSpService;
import com.chw.system.util.DateUtils;
/**
* @ClassName: ddTask
* @Description: 更新订单状态接口
* @author wangjintao
* @date 2017年11月14日 下午5:48:45
*
*/
public class ddTask extends QuartzJobBean{
private ILptDdSpService lptDdSpService;
private final static Long ONE_WEEK_TIMES = 7 * 24 * 60 * 60 * 1000L;
private final static String DATE_FORMAT_STR = "yyyy-MM-dd HH:mm:ss";
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
ChwLogUtil.getLogger().info(DateUtils.getCurrentTimeToStr(DATE_FORMAT_STR)+" 订单定时任务开始执行....");
lptDdSpService = (ILptDdSpService) context.getMergedJobDataMap().get("lptDdSpService");
String sdsj = this.timesToDateStr(new Date().getTime() - ONE_WEEK_TIMES,DATE_FORMAT_STR);
//获取一周前订单数据(订单状态为已送达)
List<LptDpDdSp> ddList = lptDdSpService.findTaskList(sdsj);
if(null!=ddList){
//更新订单状态为已确认
for(LptDpDdSp ddInfo:ddList){
ddInfo.setDdzt(3);
int i = lptDdSpService.update(ddInfo);
if(1!=i){
ChwLogUtil.getLogger().info(DateUtils.getCurrentTimeToStr(DATE_FORMAT_STR)+" 订单id="+ddInfo.getDdid()+"更新失败!");
}else{
ChwLogUtil.getLogger().info(DateUtils.getCurrentTimeToStr(DATE_FORMAT_STR)+" 订单id="+ddInfo.getDdid()+"更新成功!");
}
}
}else{
ChwLogUtil.getLogger().info(DateUtils.getCurrentTimeToStr(DATE_FORMAT_STR)+"暂无更新订单!");
}
ChwLogUtil.getLogger().info(DateUtils.getCurrentTimeToStr(DATE_FORMAT_STR)+" 订单定时任务执行结束....");
}
private String timesToDateStr(long oneWeekBeforeTimes, String dateFormatStr) {
Date date = new Date(oneWeekBeforeTimes);
SimpleDateFormat sdf = new SimpleDateFormat(dateFormatStr);
return sdf.format(date);
}
}