Spring
定时任务实现
1.
首先配置好
Spring
环境,加入定时任务的配置文件,如下:
<? xml version = "1.0" encoding = "GB2312" ?>
<? 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 :2008-1-25
* <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 ;
方法
,
所有定时任务实现,都在这个方法里就可以了。