这里主要是用quartz结合ssh框架在特定时间运行相应代码,达到程序自动处理的效果。JDK自带的calendar工具类也要注意,能更加方便的解决特定时间的设置。
1、第一步,引入quartz的jar包和Spring的支持包
<span style="white-space:pre"> </span>quartz-1.8.6.jar
<span style="white-space:pre"> </span>org.springframework.context.support-3.0.2.RELEASE.jar
2、配置spring
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="complainDao" class="cn.buaa.nsfw.complain.dao.impl.ComplainDaoImpl" parent="baseDao">
</bean>
<!-- 扫描service -->
<context:component-scan base-package="cn.buaa.nsfw.complain.service.impl"></context:component-scan>
<!-- 1、制定任务信息信息 -->
<bean id="complainJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 设置执行对象 -->
<property name="targetObject" ref="complainService"></property>
<!-- 设置执行对象中对应的执行方法 -->
<property name="targetMethod" value="autoDeal"></property>
<!-- 是否可以同步执行;不可同步执行 -->
<property name="concurrent" value="false"></property>
</bean>
<!-- 2、制定任务执行时机(任务执行触发器) -->
<bean id="complainCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 设置任务详细信息 -->
<property name="jobDetail" ref="complainJobDetail"></property>
<!-- 设置任务执行时间点,cronExpression: 在每月的月底下午的10点每分钟的第10秒执行任务 -->
<property name="cronExpression" value="10 * 22 L * ?"></property>
</bean>
<!-- 3、设置调度工厂 -->
<bean id="complainScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="complainCronTrigger"/>
</list>
</property>
</bean>
</beans>
3、执行的service方法
package cn.buaa.nsfw.complain.service.impl;
import java.io.Serializable;
import java.util.Calendar;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import cn.buaa.core.dao.BaseDao;
import cn.buaa.core.page.PageResult;
import cn.buaa.core.service.impl.BaseServiceImpl;
import cn.buaa.core.util.QueryHelper;
import cn.buaa.nsfw.complain.dao.ComplainDao;
import cn.buaa.nsfw.complain.entity.Complain;
import cn.buaa.nsfw.complain.service.ComplainService;
import cn.buaa.nsfw.info.entity.Info;
@Service("complainService")
public class ComplainServiceImpl extends BaseServiceImpl<Complain> implements ComplainService {
private ComplainDao complainDao;
@Resource
public void setComplainDao(ComplainDao complainDao) {
super.setBaseDao(complainDao);
this.complainDao = complainDao;
}
@Override
public void autoDeal() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);//设置当前时间的日期为1号
cal.set(Calendar.HOUR_OF_DAY, 0);//设置当前时间的日期为1号,0时
cal.set(Calendar.MINUTE, 0);//设置当前时间的日期为1号,0时,0分
cal.set(Calendar.SECOND, 0);//设置当前时间的日期为1号,0时,0分,0秒
//1、查询本月之前待受理的投诉列表
QueryHelper queryHelper = new QueryHelper(Complain.class, "c");
queryHelper.addCondition("c.state=?",Complain.COMPLAIN_STATE_UNDONE );
queryHelper.addCondition("c.compTime<?", cal.getTime()); //本月一号0时0分0秒
List<Complain> list = findObjects(queryHelper);
if(list != null && list.size()>0){
//2、更新投诉列表的状态为已失效
for(Complain comp:list){
comp.setState(Complain.COMPLAIN_STATE_INVALID);
update(comp);
}
}
}
}
4、注意,service已经注入过,直接引用即可,quartz的配置中直接配置service中要定期定时执行的代码。