我想通過spring.xml設置計時器任務,並且應該在加載applicationContext時啓動計時器。Spring TimerFactoryBean如何啓動計時器任務
我從春閱讀本教程:http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/scheduling.html
但我沒有找到有關春天將如何啓動計時器開始執行的任何信息。例如,使用Timer類我們可以這樣說:new Timer(new Task).schedule()然後它會馬上開始執行任務,這將如何在以下配置中工作?
public class CheckEmailAddresses extends TimerTask {
private List emailAddresses;
public void setEmailAddresses(List emailAddresses) {
this.emailAddresses = emailAddresses;
}
public void run() {
// iterate over all email addresses and archive them
}
}
Spring配置文件:應用程序上下文初始化後
<bean id="checkEmail" class="examples.CheckEmailAddress">
<property name="emailAddresses">
<list>
<value>[email protected]</value>
<value>[email protected]</value>
<value>[email protected]</value>
</list>
</property>
</bean>
<bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<!-- wait 10 seconds before starting repeated execution -->
<property name="delay" value="10000" />
<!-- run every 50 seconds -->
<property name="period" value="50000" />
<property name="timerTask" ref="checkEmail" />
</bean>
<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<!-- see the example above -->
<ref bean="scheduledTask" />
</list>
</property>
</bean>