java – 如何配置spring来执行重叠的fixedRate任务?

我正在尝试使用 java spring中的@Scheduled注释以固定速率执行任务.但是,认情况下,如果任务比速率慢,spring将不会以固定速率执行fixedrate任务.是否有一些设置我可以添加到我的弹簧配置来改变这种行为?

例:

@Service
public class MyTask{
    @Scheduled(fixedrate = 1000)
    public void doIt(){
        // this sometimes takes >1000ms,in which case the next execution is late
        ...
    }
}

我有一个解决方案,但似乎不太理想.基本上,我只是用线程池替换认的单线程执行器,然后我有一个调度方法调用异步方法,因为@Async注释允许并发执行:

@Service
public class MyTask{
    @Async
    public void doIt(){
        // this sometimes takes >1000ms,but the next execution is on time
        ...
    }
}

@Service
public class MyTaskScheduler{
    ...
    @Scheduled(fixedrate = 1000)
    public void doIt(){
        myTask.doIt();
    }
}

@Configuration
@EnableScheduling
@EnableAsync
public class MySpringJavaConfig{
    @Bean(destroyMethod = "shutdown")
    public Executor taskScheduler() {
        return Executors.newScheduledThreadPool(5);
    }
}

我真实场景的无聊细节:在我的生产代码中,我的任务需要10毫秒到10分钟,具体取决于当前的工作负载.理想情况下,我想每隔1000毫秒从池中捕获一个新线程,以便并发线程数随着工作量的增加增加.显然我有一个上限(在其他控件之中),以防止事情失控.

解决方法

TaskScheduler API(支持一些Spring Scheduling行为)似乎被定义为阻止您请求的行为

Schedule the given Runnable,invoking it at the specified execution
time and subsequently with the given period.

Parameters

  • period the interval between successive executions of the task (in milliseconds)

随后和连续似乎表明下一次执行只会在当前执行完成后发生.

更重要的是,ScheduledExecutorService#scheduleAtFixedRate(..)(内置的TaskScheduler实现使用)也说

If any execution of this task takes longer than its period,then
subsequent executions may start late,but will not concurrently
execute
.

因此,实现的另一层可以阻止您想要的行为.

一个可能的解决方案,我不建议使用,因为API似乎不是围绕它构建的,是定义并提供自己的TaskScheduler,它同时运行任务.查看@EnableSchedulingSchedulingConfigurer,了解如何注册TaskScheduler.

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...