Running Spring <Tasks: Scheduled Tasks>

I am trying to use Spring Planning with 'scheduled tasks . I can load the Spring context using XmlBeanFactory and get the bean scheduler. But I'm not sure about the next step. The docs imply that tasks should start automatically - maybe this is only when I load a context into a container like Tomcat? Is it possible to run tasks on boot using XmlBeanFactory?

The following is a simplified configuration of java and 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> 

+6
source share
1 answer

Bean factory offers only part of the ApplicationContext functionality. Lifecycle bean handling is one of those missing features that I think. Try creating an ApplicationContext:

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

I expect the scheduled tasks to be started automatically.

+6
source

Source: https://habr.com/ru/post/909075/


All Articles