Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting Spring <tasks:scheduled-tasks>

Tags:

spring

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>

like image 669
Kevin Avatar asked Feb 22 '12 12:02

Kevin


1 Answers

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.

like image 61
mrembisz Avatar answered Oct 27 '22 15:10

mrembisz