Springタスクスケジュール実戦のTimer


この記事のアドレス:http://blog.csdn.net/kongxx/article/details/6751151
springではタスクスケジュールに関する統合機能を提供しています.一番簡単なのはJDKが持参するTimerとTimeTask類を利用して簡単なタスクスケジュールを実現することです.以下の例を見てください.
単純なTaskクラスは、このtaskを実行するためのrun方法を含む任意のインターフェースを実装していません.
package org.garbagecan.springstudy.schedule.timer;

public class MyTask {
	private String name;
	
	public void run() {
		System.out.println("Run task: " + name + ".");
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
spring.xmlなどの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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
       default-lazy-init="true">

	<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean" lazy-init="false">
		<property name="scheduledTimerTasks">
			<list>
				<ref local="scheduledTask1"/>
				<ref local="scheduledTask2"/>
			</list>
		</property>
	</bean>

	<bean id="scheduledTask1" class="org.springframework.scheduling.timer.ScheduledTimerTask">
		<property name="delay" value="0" />
		<property name="period" value="10000" />
		<property name="timerTask">
			<ref bean="methodInvokingTask1"/>
		</property>
	</bean>

	<bean id="scheduledTask2" class="org.springframework.scheduling.timer.ScheduledTimerTask">
		<property name="delay" value="0" />
		<property name="period" value="10000" />
		<property name="timerTask">
			<ref bean="methodInvokingTask2"/>
		</property>
	</bean>
	
	<bean id="methodInvokingTask1" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
		<property name="targetObject" ref="myTask1"/>
		<property name="targetMethod" value="run"/>
	</bean>

	<bean id="methodInvokingTask2" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
		<property name="targetObject" ref="myTask2"/>
		<property name="targetMethod" value="run"/>
	</bean>

	<bean id="myTask1" class="org.garbagecan.springstudy.schedule.timer.MyTask">
		<property name="name" value="task1"/>
	</bean>
	
	<bean id="myTask2" class="org.garbagecan.springstudy.schedule.timer.MyTask">
		<property name="name" value="task2"/>
	</bean>
</beans>
1.2つのtask、task 1、およびtask 2が定義されている.
2.springが提供するMethodInvokingTimerg Tasmark FactoryBern類を利用して、task類と方法に対する声明を実現し、ターゲットと方法を声明して、springにその種類の方法を実行することを知ってもらう.
3.ScheduledTimerTaskクラスを利用して、各taskの起動時間遅延を配置し、毎回起動間の間隔はもちろん、そのオブジェクトを実行する必要があります.ここで使っているMethodInvokingTimerTaskyBen類の例です.
4.最終的にTimerFactoryBeanクラスを定義し、ScheduledTimeTaskクラスの例をスケジュールを必要とするtaskとする.
最後に、テストクラスを書いて、上のコードと配置をテストします.
package org.garbagecan.springstudy.schedule.timer;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	public static void main(String[] args) throws Exception {
		new ClassPathXmlApplicationContext("/org/garbagecan/springstudy/schedule/timer/spring.xml");
	}
}
はTestクラスを実行し、2つのtaskが起動し、同じ10秒を各動作間の間隔として使用することができる.