简体   繁体   English

Spring Task Scheduler 连续任务

[英]Spring Task Scheduler consecutive tasks

I am new to using the Spring Task Scheduler for executing tasks, so this may be a basic question.我是使用 Spring Task Scheduler 执行任务的新手,所以这可能是一个基本问题。 I have a list of items that I would like to process within a class that implements Runnable .我有一个要在实现Runnable的类中处理的项目列表。 Here is my task class:这是我的任务类:

public class ProcessTask<T> implements Runnable {

private String item;

public ProcessTask(String item) {
    System.out.println("Starting process for " + item);
    this.item = item;
}

@Override
public void run() {
    System.out.println("Finishing task for " + item);
}

I would like to process a list of items, each one starting 10 seconds after the previous task started.我想处理一个项目列表,每个项目在上一个任务开始后 10 秒开始。 I understand I could just set each one to run 10 seconds after the previous one was scheduled, however I don't want to rely on that as other processes may cause a task to run prior to 10 seconds elapsing.我知道我可以将每个进程设置为在前一个进程计划后 10 秒运行,但是我不想依赖它,因为其他进程可能会导致任务在 10 秒过去之前运行。

So in my main class, I have the following:所以在我的主要课程中,我有以下内容:

        Date end = new Date(cal.getTimeInMillis() + 10000); // this is the time that the task should be fired off, the first one being 10 seconds after the current time

    for(String item : items) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(end);
        System.out.println("Next task fires at " + cal.getTime());
        ProcessTask task = new ProcessTask(item);
        ScheduledFuture<?> future = taskScheduler.schedule(task, end);

        end = new Date(Calendar.getInstance().getTimeInMillis() + 10000);
    }

The first task fires off 10 seconds after the code runs, which is great.第一个任务在代码运行 10 秒后触发,这很好。 But then the rest of the items get scheduled immediately, not waiting 10 seconds.但是其余的项目会立即得到安排,而不是等待 10 秒。 I do understand why that happens - because taskScheduler.schedule is asynchronous so the for loop just continues and the rest of the items get scheduled for 10 seconds later.我确实理解为什么会发生这种情况 - 因为taskScheduler.schedule是异步的,所以 for 循环只会继续,其余的项目会在 10 秒后被安排。

I tried having the main thread sleep for a second and check if the ScheduledFuture has completed before scheduling the next task, such as:我尝试让主线程休眠一秒钟,并在调度下一个任务之前检查ScheduledFuture是否已完成,例如:

while(!future.isDone()) {
     Thread.sleep(1000);
     System.out.println("is future done: " + future.isDone());
}

If I add this block immediately after ScheduledFuture<?> future = taskScheduler.schedule(task, end);如果我在ScheduledFuture<?> future = taskScheduler.schedule(task, end);之后立即添加这个块ScheduledFuture<?> future = taskScheduler.schedule(task, end); in the above block, then future.isDone() always returns false, and the ProcessTask run() method never gets called.在上面的块中, future.isDone()总是返回 false,并且ProcessTask run()方法永远不会被调用。

Is there any way I can use the ScheduledFuture to determine if the previous task has ended, but if it hasn't, continue to wait?有没有什么办法可以使用ScheduledFuture来确定上一个任务是否已经结束,但如果还没有,继续等待? Is there a better way overall to do this?有没有更好的方法来做到这一点? Thanks in advance.提前致谢。

So you don't know when the task will end, but 10 seconds later, you want the next task to run.所以你不知道任务什么时候结束,但 10 秒后,你希望下一个任务运行。 So planning it can only be done when that task is done.因此,只有在完成该任务后才能进行规划。 So, have a base abstract class, which does the plumbing.所以,有一个基础抽象类,它做管道。

public abstract class ScheduleTaskAfterRun<T> implements Runnable {
    protected void executeContent();
    private Runnable nextTask;
    private taskScheduler; // init somehow, probably by constructor...

    public void setNextTask(Runnable r) {
        nextTask = r;
    }

    @Override
    public void run() {
        executeContent();
        scheduleNextTask();
    }

    private void scheduleNextTask() {
        if(nextTask == null) {
            System.out.println("No task to handle, finished!");
            return;
        }
        Date end = new Date(Calendar.getInstance().getTimeInMillis() + 10000);
        ScheduledFuture<?> future = taskScheduler.schedule(nextTask, end);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM