Spring Quartz 调度与Jdk Timer

,最近经历的几个项目大多里面都使用到Spring Quartz,及JDKTimer 去实现任务调度的一些功能(比比如说,检查每天定时检查订单是否发送成功,或者每天具体的某个时间点根据当前的时间去更新一些状态值等等),在此贴点代码出来。
一 Quartz MethodInvokingJobDetailFactoryBean:
(使用MethodInvokingJobDetailFactoryBean的好处就是 你能够不用编写一个单独的TimerTask或QuartzJobBean类就可以调度单次方法调用)

1 代码简要如下就可以实现:UpdateAgentContractStatusJob.java

package com.kingdee.ais.ibmp.business.task;

import java.util.Date;

import org.apache.log4j.Logger;
import com.kingdee.ais.ibmp.business.advert.service.AdvAgentContractService;

/**
 *  task类,针对(MethodInvokingJobDetailFactoryBean這种bean)
 * @author yong_jiang
 *
 */

public class UpdateAgentContractStatusJob{
	
	
	private static Logger log = Logger.getLogger(UpdateAgentContractStatusJob.class);
	
	private AdvAgentContractService advAgentContractService;
	
	public AdvAgentContractService getAdvAgentContractService() {
		return advAgentContractService;
	}

	public void setAdvAgentContractService(
			AdvAgentContractService advAgentContractService) {
		this.advAgentContractService = advAgentContractService;
	}
	
	public void run(){
		
		long p1 = System.currentTimeMillis();
		log.info("updatestatus task start......"+new Date());
		
		/**
		 * 执行顺序 1 initOverriteDates(),2 updateforContractStaus(),3 updateforContractStaus;
		 */
			boolean res1=advAgentContractService.initOverriteDates();
			boolean res2=advAgentContractService.updateforContractStaus();
			boolean res3=advAgentContractService.updateforContractStaus2();
			System.out.println(res1);
			System.out.println(res2);
			System.out.println(res3);
			log.info("updatestatus task end...."+new Date());
			System.out.println("本轮调度时间:"+(System.currentTimeMillis() - p1));

		
		
	}

}


2 配置文件 sb-daemontask-context.xml

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
    
	<!-- 声明调度的Job-->
    <bean id="quartzJob" class="com.kingdee.ais.ibmp.business.task.UpdateAgentContractStatusJob">
  		<property name="advAgentContractService" ref="advAgentContractService" />
    </bean>
    
    <bean id="updateAgentContractStatusjobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <ref bean="quartzJob"/>
        </property>
        <property name="targetMethod"><value>run</value> </property>
       
    </bean>
    
    <bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail">
            <ref bean="updateAgentContractStatusjobtask"/>
        </property>
        <!-- 在每天中午12点触发 -->
        <property name="cronExpression">
        	<value>0 27 10 * * ? *</value>
			</property>
    </bean>
    
    <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序  -->
    <bean id="startQuertz" lazy-init="true" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <!--可以加入多个调度 -->
            <list>
                <ref bean="doTime"/>
            </list>
        </property>
    </bean>
    
    
</beans>




二:JDK Timer任务调度, 使用jDK TIMER对象,你可以创建定制的timer或者调用某些方法的timer,包装的timer的工作由TimerFactoryBean完成
package com.gpdi.infores.rc.task;

import java.util.TimerTask;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gpdi.infores.global.client.GlobalVars;

public class OnlineUserSynTask extends TimerTask {
	
	private static final Log LOG = LogFactory.getLog(OnlineUserSynTask.class);
	/**
	 * 定时任务是否开始工作,解决IDE拖Swing界面时自动把定时任务拉起来 而影响开发效率。
	 */
	private static boolean isStartWork = false;
	@Override
	public void run() {		
		if(!isStartWork) 
		{
			return;
		}
		LOG.debug("开始执行定时任务:OnlineUserSynTask");		 	
		try
		{
			//添加用户
			GlobalVars.getInstance().addUpdateOnlineUser();
			//检查版本
			ClientVerCheck.getInstance().checkVersion();
			//接收公告
			BulletinIncept.getInstance().incept();
		}catch(Exception ex)
		{
			LOG.error(ex.getMessage());
		};		
		LOG.debug("退出定时任务:OnlineUserSynTask");
	}
	public static void setStartWork(boolean isStartWork) {
		OnlineUserSynTask.isStartWork = isStartWork;
	}

}



 <bean id="onlineUserSynTask"class="com.gpdi.infores.rc.task.OnlineUserSynTask" />
    
	<bean id="scheduleReportTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
	   <property name="timerTask" ref="onlineUserSynTask" />
	   每600000运行一次;
	   <property name="period">
	       <value>600000</value>
	   </property>
	   开启服务之后,延迟60000start
	   <property name="delay">
	       <value>60000</value>
	   </property>
	</bean>
	
	//最好使用TimerFactoryBean来设置任务
	<bean class="org.springframework.scheduling.timer.TimerFactoryBean">
		<property name="scheduledTimerTasks">
		   <list><ref bean="scheduleReportTask"/></list>		   
		</property>
	</bean>	



三 quartz与maven 集成:

<dependency>
	<groupId>org.quartz-scheduler</groupId>
	<artifactId>quartz</artifactId>
          <version>1.8.4</version>
	 </dependency>


	  <org.quartz-scheduler>1.8.4</org.quartz-scheduler>

           <dependency>
		<groupId>org.quartz-scheduler</groupId>
		<artifactId>quartz</artifactId>
		<version>${org.quartz-scheduler}</version>
	</dependency>


你可能感兴趣的:(spring,timer,quartz,集成:\,quartz与maven)