I need to create task on the fly in my app. How can I do that? I can get scheduler with @autowired
annotation, but scheduler takes Runnable
objects. I need to give Spring objects, so that my tasks can use @autowired
annotation too.
@Autowired private TaskScheduler taskScheduler;
You just need to wrap your target object in a Runnable
, and submit that:
private Target target; // this is a Spring bean of some kind
@Autowired private TaskScheduler taskScheduler;
public void scheduleSomething() {
Runnable task = new Runnable() {
public void run() {
target.doTheWork();
}
};
taskScheduler.scheduleWithFixedDelay(task, delay);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With