Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting the TaskScheduler with Spring

Is it possible to inject the TaskScheduler instance created by Spring?

I would like to schedule tasks programatically and for that, I guess I need to access the TaskScheduler but for some reason, it's not found by Spring for autowiring.

@Configuration
@EnableScheduling
public class MySpringConfig {

}

@Component
public class MyClass implements InitializingBean {

    @Autowired
    private TaskScheduler taskScheduler;

    @Override
    public void afterPropertiesSet() throws Exception {
        ...
    }
}

Any idea?

Thanks!

like image 594
manash Avatar asked Dec 23 '15 12:12

manash


1 Answers

@Configuration
@EnableScheduling
public class MySpringConfig {

 @Bean
 public TaskScheduler taskScheduler() {
     //org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler
     return new ThreadPoolTaskScheduler();
 }
}

You can choose which ever implementation you like. ThreadPoolTaskScheduler is the simpler one as mentioned in this link.

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-task-scheduler-implementations

like image 200
prem kumar Avatar answered Oct 25 '22 16:10

prem kumar