标签:
我们上次使用quartz任务调度框架创建了一个在后台按照时间间隔不停运行的任务,我们使用的是simpleTrigger简单触发器,为了实现我们的月末自动回复的功能,我们要设置触发器能够在某个精确时间去自动执行任务,那么使用simpleTrigger简单触发器就远远不够了,我们需要使用CronTrigger任务触发器来实现这个功能。package cn.edu.hpu.tax.complain;
import java.text.SimpleDateFormat;
import java.util.Date;
public class QuartzTask {
public void doSimpleTriggerTask() {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("doing simpleTrigger task..."+sdf.format(new Date()));
}
public void doCronTriggerTask() {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("doing cronTrigger task..."+sdf.format(new Date()));
}
}
<?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 -->
<bean id="quartzTask" class="cn.edu.hpu.tax.complain.QuartzTask"></bean>
<!-- 1.指定任务详细信息 -->
<bean id="jobDetial1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- ① 设置执行对象 -->
<property name="targetObject" ref="quartzTask"></property>
<!-- ② 设置执行对象中对应的执行方法 -->
<property name="targetMethod" value="doSimpleTriggerTask"></property>
<!-- ③ 是否可以同步执行(这里设置不同步执行) -->
<property name="concurrent" value="false"></property>
</bean>
<bean id="jobDetial2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- ① 设置执行对象 -->
<property name="targetObject" ref="quartzTask"></property>
<!-- ② 设置执行对象中对应的执行方法 -->
<property name="targetMethod" value="doCronTriggerTask"></property>
<!-- ③ 是否可以同步执行(这里设置不同步执行) -->
<property name="concurrent" value="false"></property>
</bean>
<!-- 2.制定任务执行时机(任务执行触发器) -->
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<!-- ① 设置任务详细 -->
<property name="jobDetail" ref="jobDetial1"></property>
<!-- ② 设置任务延迟执行时间(延迟2秒) -->
<property name="startDelay" value="2000"></property>
<!-- ③ 设置任务执行频率(执行频率为每2秒执行一下) -->
<property name="repeatInterval" value="2000"></property>
</bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 设置任务详细 -->
<property name="jobDetail" ref="jobDetial2"></property>
<!-- 设置任务执行时机,cron表达式 -->
<property name="cronExpression" value="0/3 * * * * ?"></property>
</bean>
<!-- 3.设置调度工厂 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!-- 第一个触发器 -->
<ref bean="simpleTrigger"/>
<!-- 第二个触发器 -->
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
</beans>
...... 信息: Server startup in 16932 ms doing simpleTrigger task...2015-12-07 09:09:44 doing cronTrigger task...2015-12-07 09:09:45 doing simpleTrigger task...2015-12-07 09:09:46 doing cronTrigger task...2015-12-07 09:09:48 doing simpleTrigger task...2015-12-07 09:09:48 doing simpleTrigger task...2015-12-07 09:09:50 doing cronTrigger task...2015-12-07 09:09:51 doing simpleTrigger task...2015-12-07 09:09:52 doing cronTrigger task...2015-12-07 09:09:54 doing simpleTrigger task...2015-12-07 09:09:54 doing simpleTrigger task...2015-12-07 09:09:56 doing cronTrigger task...2015-12-07 09:09:57 doing simpleTrigger task...2015-12-07 09:09:58 ......
<?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">
<!-- 继承了注入sessionFactory的抽象类,不用反复出入sessionFactory -->
<bean id="complainDao" class="cn.edu.hpu.tax.complain.dao.impl.ComplainDaoImpl" parent="xDao"></bean>
<!-- 扫描Service -->
<context:component-scan base-package="cn.edu.hpu.tax.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>
<!-- 设置任务执行时机,cron表达式 -->
<property name="cronExpression" value="10 10 2 L * ?"></property>
</bean>
<!-- 3.设置调度工厂 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!-- 触发器 -->
<ref bean="complainCronTrigger"/>
</list>
</property>
</bean>
</beans>package cn.edu.hpu.tax.complain.service;
import cn.edu.hpu.tax.complain.entity.Complain;
import cn.edu.hpu.tax.core.service.BaseService;
public interface ComplainService extends BaseService<Complain> {
//自动受理投诉
public void autoDeal();
}
package cn.edu.hpu.tax.complain.service.impl;
import java.util.Calendar;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import cn.edu.hpu.tax.complain.dao.ComplainDao;
import cn.edu.hpu.tax.complain.entity.Complain;
import cn.edu.hpu.tax.complain.service.ComplainService;
import cn.edu.hpu.tax.core.service.impl.BaseServiceImpl;
import cn.edu.hpu.tax.core.util.QueryHelper;
@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() {
//1、查询本月之前的待受理的投诉列表
QueryHelper queryHelper=new QueryHelper(Complain.class,"c");
queryHelper.addCondition("c.state=?",Complain.COMPLAIN_STATE_UNDONE);
Calendar cal=Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);//设置当前时间的日期为1号
cal.set(Calendar.HOUR_OF_DAY, 0);//设置当前时间的日期为0时
cal.set(Calendar.MINUTE, 0);//设置当前时间的日期为0分
cal.set(Calendar.SECOND, 0);//设置当前时间的日期为0秒
queryHelper.addCondition("c.compTime < ?", cal.getTime());
List<Complain> list=findObjects(queryHelper);
if(list != null && list.size()>0){
//2、更新投诉信息的状态为已失效
for(Complain comp:list){
comp.setState(Complain.COMPLAIN_STATE_INVALID);
update(comp);
}
}
}
}
<td align="center">
<s:if test="state != 2">
<a href="javascript:doDeal('<s:property value='compId'/>')">受理</a>
</s:if>
</td>效果:至此,我们的quartz与simpleTrigger、CronTrigger学习完毕,月底自动回复功能也已经实现。
转载请注明出处:http://blog.csdn.net/acmman/article/details/50205893
【SSH项目实战】国税协同平台-34.quartz&CronTrigger
标签:
原文地址:http://blog.csdn.net/acmman/article/details/50205893