2012-02-22 20 views
6

Estoy tratando de usar Spring Scheduling con 'scheduled-tasks'. Puedo cargar el contexto de primavera usando XmlBeanFactory, y obtener el bean del programador. Pero no estoy seguro del siguiente paso. Los documentos implican que las tareas deberían iniciarse automáticamente, tal vez solo cuando cargué el contexto en un contenedor como Tomcat. ¿Es posible iniciar las tareas al cargar con XmlBeanFactory?Starting Spring <tasks: scheduled-tasks>

A continuación se muestra la configuración simplificada 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> 

Respuesta

6

fábrica de beans ofrece sólo un subconjunto de la funcionalidad Application Context. El manejo del ciclo de vida del frijol es una de esas características que faltan, creo. Intente crear ApplicationContext:

ApplicationContext ctx = new FileSystemXmlApplicationContext("\\my_spring_file.xml"); 

Espero que las tareas programadas se inicien automáticamente.

Cuestiones relacionadas