說明:所需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;
}
}
三:最終得出的結果: