2012-02-22 14 views
6

Sto tentando di utilizzare la pianificazione Spring con 'scheduled-tasks'. Posso caricare il contesto di primavera usando XmlBeanFactory e ottenere il bean schedulatore. Ma non sono sicuro del prossimo passo. I documenti implicano che le attività dovrebbero avviarsi automaticamente - forse è solo quando carico il contesto in un contenitore come Tomcat? È possibile avviare le attività durante il caricamento con XmlBeanFactory?Inizio delle attività <Spring: operazioni pianificate>

Di seguito è riportata la configurazione della molla java & semplificata.

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> 

risposta

6

Bean Factory offre solo un sottoinsieme delle funzionalità ApplicationContext. Gestire il ciclo di vita dei bean è una di quelle funzionalità mancanti che penso. Prova a creare ApplicationContext:

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

Mi aspetto che le attività pianificate vengano avviate automaticamente.

Problemi correlati