這裏主要是用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中要定期定時執行的代碼。