spring timer的整合

1  定时任务的spring 配置文件配置

 <?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
	http://www.springframework.org/schema/tx
	 http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
	 http://www.springframework.org/schema/aop 
	 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
	 http://www.springframework.org/schema/jee 
	 http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
	 http://www.springframework.org/schema/context 
	 http://www.springframework.org/schema/context/spring-context-2.5.xsd"
	default-autowire="byName" default-lazy-init="false">
	<!-- 执行的任务类 -->
	<bean id="task" class="com.ambow.TimerTaskTest.MyTask">
	<!-- 
		<property name="myService">
			<ref bean="myService" />
		</property>
		 -->
	</bean>
	<!--  触发器  以及任务的执行规则  -->
	<bean id="repeatingTrigger" class="org.springframework.scheduling.timer.ScheduledTimerTask">
		<!--启动一秒后执行 -->
		<property name="delay">
			<value>4000</value>
		</property>
		<!--每隔一小时执行一次 3600000-->
		<property name="period">
			<value>5000</value>
		</property>
		<!--注入要监控的javaBean -->
		<property name="timerTask">
			<ref bean="task" />
		</property>
		<!--类型是否为fixedRate型,默认为fixedDelay-->
		<property name="fixedRate">
			<value>true</value>
		</property>
	</bean>
	<!-- 注册触发器  -->
	<bean id="scheduler" class="org.springframework.scheduling.timer.TimerFactoryBean">
		<property name="scheduledTimerTasks">
			<list>
				<ref bean="repeatingTrigger" />
			</list>
		</property>
	</bean>
</beans>

 2 timer的任务类 注意要继承 TimerTask 

import java.util.Calendar;
import java.util.TimerTask;
/**
 * 可以再这个类中加入 service 执行类中的方法
 */

public class MyTask extends TimerTask {
	private static boolean isRunning = false;
	private static boolean flag = true;
	private static final int C_SCHEDULE_HOUR = 23;// 每天执行的时间,定为每晚11点
	// private MyService
	// myService;//在这里加上你要用到的service,到时候和spring的controller一样注入使用即可

	public void run() {
		System.out.println("开始执行指定任务");
		// TODO 添加自定义的详细任务
		try {
			Calendar cal = Calendar.getInstance();
			if (!isRunning) {
				System.out.println( "定时器执行了 。。。。");
//				if (C_SCHEDULE_HOUR == cal.get(Calendar.HOUR_OF_DAY) && flag) {
					isRunning = true; //一旦有任务执行 就锁定 这个方法
//					// 在这里加入相关业务,比如myService.notice();
					isRunning = false; //任务执行结束就 打开执行的锁定
					flag = false;
//					System.out.println("指定任务执行结束");
//				}
			} else {
				System.out.println("上一次任务执行还未结束");
			}
			if (C_SCHEDULE_HOUR != cal.get(Calendar.HOUR_OF_DAY)) {
				flag = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			
		}
	}
	// public MyService getMyService() {
	// return myService;
	// }
	// public void setMyService(MyService myService) {
	// this.myService = myService;
	// }
}

 

3 把配置文件配置到spring的配置文件中

猜你喜欢

转载自username2.iteye.com/blog/1825359
今日推荐