Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Spring Framework task programmatically?

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;
like image 843
newbie Avatar asked Dec 21 '10 12:12

newbie


1 Answers

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);
}
like image 95
skaffman Avatar answered Oct 17 '22 07:10

skaffman