Spring  定时任务实现

1.首先配置好Spring环境,加入定时任务的配置文件,如下:
xml version="1.0" encoding="GB2312"?>

DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

 

<beans>

 

   

    <bean name="randomSyncJob"

       class="org.springframework.scheduling.quartz.JobDetailBean">

 

       <property name="jobClass">

           <value>com.iman.wrms.webservice.sync.task.QuartzSyncJobvalue>

       property>

 

       <property name="jobDataAsMap">

           <map>

              <entry key="timeout">

                  <value>5value>

              entry>

           map>

       property>

 

    bean>

   

    <bean id="cronTrigger"

       class="org.springframework.scheduling.quartz.CronTriggerBean">

 

       <property name="jobDetail">

           <ref bean="randomSyncJob" />

       property>

      

       <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

 *

description : 定时同步EOMS组织处理类

 *

 */

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 ; 方法,所有定时任务实现,都在这个方法里就可以了。