Spring @Scheduler并行运行 [英] Spring @Scheduler parallel running
问题描述
我有以下3个班级:
ComponantA
package mytest.spring.test.spring;
import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ComponentA {
Logger log = Logger.getLogger(ComponentB.class);
@Scheduled(fixedRate=2000)
public void sayHello() {
for(int i=1 ; i<=5 ; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
log.info("Hello from ComponentA " + i);
}
}
}
ComponentB
package mytest.spring.test.spring;
import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ComponentB {
Logger log = Logger.getLogger(ComponentB.class);
@Scheduled(fixedRate=2000)
public void sayHello() {
for(int i=1 ; i<=3 ; i++) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
log.info("Hello from ComponentB " + i);
}
}
}
MyApplication
package mytest.spring.test.spring;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
当我执行它时,我得到以下内容输出:
When I execute it, I'm getting the following output:
Hello from ComponentA 1
Hello from ComponentA 2
Hello from ComponentA 3
Hello from ComponentA 4
Hello from ComponentA 5
Hello from ComponentB 1
Hello from ComponentB 2
Hello from ComponentB 3
Hello from ComponentA 1
Hello from ComponentA 2
Hello from ComponentA 3
Hello from ComponentA 4
Hello from ComponentA 5
Hello from ComponentB 1
Hello from ComponentB 2
Hello from ComponentB 3
...
我需要2个预定方法并行运行,这显然是根据我得到的输出而不是cae。我读到应该可以使用自定义的TaskExecutor提供@Schedule注释,使用它可以定义我们想要多少线程...
I need the 2 Scheduled methods to run in parallel, which is clearly not the cae according to the output I'm getting. I read that it should be possible to provide the @Schedule annotation with a custom TaskExecutor, with which it should be possible to define how many thread we want ...
Am我对吗?我找不到如何提供这些信息。
Am I right ? I can't find how to provide this information.
推荐答案
文档明确指出:
默认情况下,将搜索关联的调度程序定义:
上下文中的唯一TaskScheduler
bean,或者aTaskScheduler
另外名为taskScheduler的
bean;对于ScheduledExecutorService
bean,同样的查找也将是
。如果两个
都不可解析,则本地单线程默认调度程序将在注册商中创建并使用
。
By default, will be searching for an associated scheduler definition: either a unique
TaskScheduler
bean in the context, or aTaskScheduler
bean named "taskScheduler" otherwise; the same lookup will also be performed for aScheduledExecutorService
bean. If neither of the two is resolvable, a local single-threaded default scheduler will be created and used within the registrar.
当更多控制时需要一个@Configuration类可以实现
SchedulingConfigurer。这允许访问底层的
ScheduledTaskRegistrar实例。例如,以下示例
演示了如何自定义用于执行预定
任务的执行程序:
When more control is desired, a @Configuration class may implement SchedulingConfigurer. This allows access to the underlying ScheduledTaskRegistrar instance. For example, the following example demonstrates how to customize the Executor used to execute scheduled tasks:
@Configuration
@EnableScheduling
public class AppConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskExecutor());
}
@Bean(destroyMethod="shutdown")
public Executor taskExecutor() {
return Executors.newScheduledThreadPool(100);
}
}
这篇关于Spring @Scheduler并行运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!