1

in my scenario i need to schedule a chain of tasks. e.g. task a, b and c should start running at 1 o'clock but in the order ive inserted them. task a should start at 1 o'clock and task b should start after task a is finished, whenever that may be. Task c also starts only after task b has finished.

I would have hoped that springs Taskscheduler could just schedule a list of runnables, but i can only schedule on Runnable :

taskScheduler.schedule(task, cronTrigger()));

How can i do something like this :

taskScheduler.schedule(taskList, cronTrigger()));

Any idea?

user4035
  • 22,508
  • 11
  • 59
  • 94
user817795
  • 165
  • 1
  • 11
  • These might help http://stackoverflow.com/questions/19246310/queuing-schedular-tasks http://stackoverflow.com/questions/13486607/how-to-do-sequential-job-scheduling-quartz – Karthik Nov 09 '13 at 15:53

2 Answers2

3

A reasonable approach would probably be to create a basic implementation of a Runnable that runs a list of Runnables, and then to schedule that as your task, e.g.:

public class RunnableList implements Runnable {
    private final List<Runnable> delegates;

    public RunnableList(List<Runnable> delegates) {
        this.delegates = new ArrayList<Runnable>(delegates);
    }

    @Override
    public void run() {
        for (Runnable job : delegates) {
            job.run();
        }
    }
}
Mark Elliot
  • 75,278
  • 22
  • 140
  • 160
  • that seems to be a solution, but will job2 wait until job1 is finished or do i have to let them wait explicitly? if so how would you do that? – user817795 Nov 10 '13 at 11:03
  • @user817795 This will run the first item in the Runnable list to completion, followed by the next, just as described in your question. – Mark Elliot Nov 10 '13 at 16:27
0

If you use an ExecutorService with only 1 Thread, you can use invokeAll on a list of Callables. The way the Executor is designed and since there is only one Thread to process those tasks, those tasks are going to be processed in the given order.

If you must use Runnables, you need to loop-add them in the correct order.

TwoThe
  • 13,879
  • 6
  • 30
  • 54