将定时器用到的quartz.jar放在lip文件下
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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <!--映射--> <bean id= "TimedTask" class ="com.blue.yanxishe.service.timer.TimedTask"/> <!--自动推送跟踪日志3天未跟新提醒--> <bean id="autoTailLogRemind" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="TimedTask"/> <property name="targetMethod" value="autoTailLogRemind"/> </bean> <bean id="autoTailLogRemindTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="autoTailLogRemind"/> <property name="cronExpression" value="0 0 0 * * ?"/> </bean> <!--自动推送排期逾期提醒--> <bean id="autoScheduOverdueRemind" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> //定义JobDetail,即定时任务
<property name="targetObject" ref="TimedTask"/> //目标类是TimeTask <property name="targetMethod" value="autoScheduOverdueRemind"/> //目标方法时TimeTask类下的autoScheduOverueRemind方法 </bean> <bean id="autoScheduOverdueRemindTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> //配置触发条件 <property name="jobDetail" ref="autoScheduOverdueRemind"/> //关联需要执行的任务 <property name="cronExpression" value="0 0 0 * * ?"/> //设置定义条件,这里"0 0 0 * * ?",表示每天00:00执行一次 </bean> <!-- 启动触发器的配置开始--> <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="autoTailLogRemindTrigger" /> //关联需要执行的任务设置 <ref bean="autoScheduOverdueRemindTrigger" /> </list> </property> </bean> </beans>
web.xml(部分)
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/context.xml
classpath:spring/context-shiro.xml
classpath:spring/context-redis.xml
classpath:spring/quartz.xml
</param-value>
</context-param>
TimedTask.java(完整)
package com.blue.yanxishe.service.timer; import java.io.Serializable; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.blue.common.exception.ServerSqlErrorException; import com.blue.yanxishe.service.shop.ShopMessageService; @Service public class TimedTask implements Serializable{ @Autowired private ShopMessageService shopMessageService; public void autoTailLogRemind() throws ServerSqlErrorException{ shopMessageService.getTailLogRemind(); System.out.println(" getTailLogRemind() was running!!"); } public void autoScheduOverdueRemind() throws ServerSqlErrorException{ shopMessageService.getScheduOverdueRemind(); System.out.println(" getScheduOverdueRemind() was running!!"); } }