manash
manash

Reputation: 7106

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!

Upvotes: 10

Views: 15256

Answers (1)

prem kumar
prem kumar

Reputation: 5877

@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

Upvotes: 15

Related Questions