6
我想用'scheduled-tasks'來使用Spring計劃。我可以使用XmlBeanFactory加載spring上下文,並獲取調度器bean。但我不確定下一步。文檔暗示任務應該自動啓動 - 也許只有當我在像Tomcat這樣的容器中加載上下文時纔會啓動這些任務?加載XmlBeanFactory時是否有可能啓動任務?開始春天<任務:計劃任務>
以下是簡化的java & spring配置。
public class SchedulingTest {
public static void main(String[] args) throws Exception {
Resource resource = new FileSystemResource("\\my_spring_file.xml");
BeanFactory factory = new XmlBeanFactory(resource);
ThreadPoolTaskScheduler scheduler = (ThreadPoolTaskScheduler) factory.getBean("myScheduler");
// -=-=-=-=-=
// NOW WHAT ?
// -=-=-=-=-=
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<task:scheduler id="myScheduler" pool-size="10" />
<task:scheduled-tasks scheduler="myScheduler">
<task:scheduled ref="EmailPollingTask" method="readAndProcessEmails"
fixed-delay="30000" />
</task:scheduled-tasks>