加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 00:40:19 所属栏目:Java 来源:网络整理
导读:我正在尝试使用 java spring中的@Scheduled注释以固定速率执行任务.但是,默认情况下,如果任务比速率慢,spring将不会以固定速率执行fixedRate任务.是否有一些设置我可以添加到我的弹簧配置来改变这种行为? 例: @Servicepublic class MyTask{ @Scheduled(fix
我正在尝试使用 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读