I am trying to use Spring Scheduling with 'scheduled-tasks'. I can load the spring context using XmlBeanFactory, and get the scheduler bean. But I'm not sure about the next step. The docs imply that the tasks should auto start - by maybe that is only when I load the context in a container like Tomcat ? Is it possible get the tasks to kick off when loading with XmlBeanFactory?
Below is the simplified java & spring config.
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>
Bean factory offers only a subset of ApplicationContext functionality. Handling bean lifecycle is one of those missing features I think. Try to create ApplicationContext:
ApplicationContext ctx = new FileSystemXmlApplicationContext("\\my_spring_file.xml");
I expect the scheduled tasks to be started automatically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With