Spring 定時任務實現web
1.首先配置好Spring環境,加入定時任務的配置文件,以下:
<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- Configure the sync Service -->
<bean name="randomSyncJob"
class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.iman.wrms.webservice.sync.task.QuartzSyncJob</value>
</property>
<property name="jobDataAsMap">
<map>
<entry key="timeout">
<value>5</value>
</entry>
</map>
</property>
</bean>
<!-- 配置觸發器 -->
<bean id="cronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="randomSyncJob" />
</property>
<!-- 天天0點0分,觸發RandomPriceJob,具體說明見附錄 -->
<property name="cronExpression">
<value>0 0 0 * * ?</value>
</property>
</bean>
<bean autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 添加觸發器 -->
<property name="triggers">
<list>
<ref local="cronTrigger" />
</list>
</property>
</bean>
</beans>
2.編寫實現任務的處理代碼
package com.iman.wrms.webservice.sync.task;
import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import com.zhengxin.wrm.opm.dbutil.JdbcWrmDelegate;
/**
* @author:Jakemanse
* @time :
* <p>description : 定時同步EOMS組織處理類
* <p></p>
*/
public class QuartzSyncJob extends QuartzJobBean {
private final Logger logger = Logger.getLogger(getClass());
/**
* 執行任務
*/
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
logger.info("---------------Job start---------------------");
JdbcWrmDelegate jdbcDelegate;
try{
jdbcDelegate = new JdbcWrmDelegate();
// 執行具體操做
logger.info("正在同步機構信息.....");
jdbcDelegate.SyncOpmOrganInfo();//同步機構信息 success
logger.info("正在同步用戶信息.....");
jdbcDelegate.SyncOpmUserInfo();//同步用戶信息 success
// jdbcDelegate.SyncOpmJobInfo();// 同步job信息 success
// jdbcDelegate.SyncOpmRoleInfo();//同步Role信息
logger.info("正在執行本系統的同步存儲過程.....");
// jdbcDelegate.ExecutePro(); //執行存儲過程
logger.info("全部同步信息執行完畢.");
}catch(Exception e){
logger.error("機構或用戶信息同步過程當中出錯:"+e.getCause());
e.printStackTrace();
}
}
}
實現定時功能,編寫本身的類,須要繼承Spring 提供的
import org.springframework.scheduling.quartz.QuartzJobBean;
而後 覆蓋該類的
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException ; 方法,全部定時任務實現,都在這個方法裏就能夠了。