我想通过spring.xml设置计时器任务,并且只要加载了applicationContext,计时器就应该启动。
我正在阅读Spring的这个教程: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>test@springframework.org</value>
<value>foo@bar.com</value>
<value>john@doe.net</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>
答案 0 :(得分:1)
org.springframework.scheduling.timer.TimerFactoryBean
将在初始化应用程序上下文后设置计时器。