说明:所需jar包:
(注:单单是在后台执行需要的jar包,若是经过tomcat执行,需额外添加一个jar包——jta-1.1.jar)
不同版本需要依赖的jar:
quartz-all-1.6.0.jar版本需要的jar包:
commons-collections-3.2.jar
commons-logging-1.1.1.jar
log4j-1.2.16.jar
spring.jar
quartz-1.8.4.jar版本需要的jar包:
commons-collections-3.2.jar
commons-logging-1.1.1.jar
log4j-1.2.16.jar
quartz-1.8.4.jar
slf4j-api-1.6.1.jar
slf4j-log4j12-1.6.1.jar
spring.jar
一:在applicationContext.xml配置文件中的配置:
<!-- 。。。。。。。。。。。。。。。。。。。。。。。。。以下spring定时器 。。。。。。。。。。。。。。。。。。。。。。。。。 --> <!--统计用户当天登陆退出Task,在TimerService中的dao层和service都在这里注入--> <bean id="timerService" class="com.test.ydzf.service.TimerService"> <property name="trffService" ref="trffService"></property> <property name="timeDao" ref="timeDao"></property> </bean> <bean id="updateYyjgDataTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 调用的类 --> <property name="targetObject" ref="timerService"/> <!-- 调用类中的方法 --> <property name="targetMethod" value="runTime"/> <!-- false,证明不执行并发任务 --> <property name="concurrent" value="false"/> </bean> <!-- 3、配置触发器,定义触发时间,可以根据不同的时间对同一个任务定义多个触发器--> <!-- cron表达式 --> <bean id="updateYyjgDataTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="updateYyjgDataTask"/> <property name="cronExpression" value="0 0/5 15 * * ?"/><!--设置时间:这里代表从下午3点开始每5分钟执行一次定时器执行到3:55分结束--> </bean> <!-- 配置调度器 ,容器启动就会执行调度程序 --> <!-- 总管理类,如果lazy-init='false',则容器启动时就会执行调度程序--> <!-- 如果lazy-init='true',则需要实例化该bean才能执行调度程序 --> <bean id="schdulerFactory" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="updateYyjgDataTrigger"/> <!----> <!-- <ref bean="yjxxFdxTrigger"/>--> </list> </property> </bean>
二:在web.xml中的配置
<!-- 配置Spring监听器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
三:TimerService定时器类
package com.test.ydzf.service; import java.util.List; import org.cjzframework.beans.support.IDomain; import com.test.ydzf.beans.TimeTranceQueue; import com.test.ydzf.dao.support.ITimeDao; import com.test.ydzf.service.support.IWebService; /** * 定时 * @author 2012-10-10 * */ public class TimerService { private IWebService trffService;//综合平台调用接口 private ITimeDao timeDao; private TimeTranceQueue timeTranceQueue=new TimeTranceQueue(); /** * 上传数据 晚上11:30执行 */ public synchronized void runTime() { try { List<IDomain> list=this.timeDao.queryForList(timeTranceQueue); System.out.println("定时器开始 Start:>>>>>>>>>>>>>>>>>>>>>>>>>:"+ list.size()); for(IDomain idomain:list){ TimeTranceQueue timequeue=(TimeTranceQueue)idomain; if(timequeue.getJklx().equals("0")){ System.out.println("---------------->>>>写入接口"); System.out.println("xtlb写入=========>>>"+timequeue.getXtlb()+" "+timequeue.getJkid()+" "+timequeue.getJklx()); trffService.writeObjectOut(timequeue.getXtlb(), timequeue.getJkxh(), timequeue.getJkid(), timequeue.getXmldac()); } if(timequeue.getJklx().equals("1")){ System.out.println("---------------->>>>查询接口"); System.out.println("xtlb查询=========>>>"+timequeue.getXtlb()+" "+timequeue.getJkid()+" "+timequeue.getJklx()); trffService.queryObjectOut(timequeue.getXtlb(), timequeue.getJkxh(), timequeue.getJkid(), timequeue.getXmldac()); } } }catch (Exception e) { e.printStackTrace(); } } public IWebService getTrffService() { return trffService; } public void setTrffService(IWebService trffService) { this.trffService = trffService; } public ITimeDao getTimeDao() { return timeDao; } public void setTimeDao(ITimeDao timeDao) { this.timeDao = timeDao; } }
三:最终得出的结果: