将 Spring 的 @Scheduled 注释与特定的执行程序一起使用 [英] Using Spring's @Scheduled annotation with a specific executor

查看:108
本文介绍了将 Spring 的 @Scheduled 注释与特定的执行程序一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何告诉我的 Spring 计划方法使用特定的执行程序运行?

How do I tell my Spring scheduled method to run using a specific executor?

例如,这是我的spring scheduler方法之一,

For example, this is one of my spring scheduler method,

@Scheduled(fixedRate=1000)
public void scheduleJobs(){
    doThese();
}

这里是我的 Java 配置中定义的 2 个执行程序:

And here are the 2 executors defined in my Java config:

@Bean
public Executor taskScheduler() {
    ThreadPoolTaskScheduler t = new ThreadPoolTaskScheduler();
    t.setPoolSize(2);
    t.setThreadNamePrefix("taskScheduler - ");
    t.initialize();
    return t;
}

@Bean
public Executor newTaskScheduler() {
    ThreadPoolTaskScheduler t = new ThreadPoolTaskScheduler();
    t.setPoolSize(2);
    t.setThreadNamePrefix("newTaskScheduler - ");
    t.initialize();
    return t;
}

当计划方法运行时,我可以看到它正在使用 taskScheduler 执行程序.如何使用 newTaskScheduler 执行器告诉它运行?

When the scheduled method is running I can see it is using taskScheduler executor. How to tell it to run using newTaskScheduler executor?

推荐答案

@EnableScheduling 的 Javadoc 在这方面非常详尽.

The Javadoc of @EnableScheduling is pretty exhaustive in that area.

您需要实现一个 SchedulingConfigurer 来微调需要使用哪个 Executor.

You need to implement a SchedulingConfigurer to fine-tune which Executor needs to be used.

这篇关于将 Spring 的 @Scheduled 注释与特定的执行程序一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆