我已经将此作为配置
@Bean
public ThreadPoolTaskScheduler taskScheduler() {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(3);
taskScheduler.setThreadNamePrefix("worker-thread-");
return taskScheduler;
}
每100毫秒轮询一次的Poller.java
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class Poller {
private Processor processor
@Autowired
public SqsPoller(Processor processor) {
this.processor = processor;
}
@Scheduled(fixedRate = 100)
public void poll() throws InterruptedException {
log.info("polling");
processor.processMessages();
}
}
Processor.java做一个耗时5秒钟左右的示例
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class Processor {
public void processMessages() throws InterruptedException {
log.info("processing message and sleep 5 seconds");
Thread.sleep(5000);
}
}
当我运行它时,这就是输出
2018-11-02 21:13:43.029 INFO 6642 --- [ worker-thread-1]: polling
2018-11-02 21:13:43.029 INFO 6642 --- [ worker-thread-1]: processing message and sleep 5 seconds
2018-11-02 21:13:48.034 INFO 6642 --- [ worker-thread-2]: polling
2018-11-02 21:13:48.035 INFO 6642 --- [ worker-thread-2]: processing message and sleep 5 seconds
2018-11-02 21:13:53.037 INFO 6642 --- [ worker-thread-2]: polling
2018-11-02 21:13:53.037 INFO 6642 --- [ worker-thread-2]: processing message and sleep 5 seconds
....
2018-11-02 21:14:53.078 INFO 6642 --- [ worker-thread-1]: polling
2018-11-02 21:14:53.078 INFO 6642 --- [ worker-thread-1]: processing message and sleep 5 seconds
2018-11-02 21:14:58.081 INFO 6642 --- [ worker-thread-1]: polling
2018-11-02 21:14:58.082 INFO 6642 --- [ worker-thread-1]: processing message and sleep 5 seconds
2018-11-02 21:15:03.086 INFO 6642 --- [ worker-thread-1]: polling
2018-11-02 21:15:03.087 INFO 6642 --- [ worker-thread-1]: processing message and sleep 5 seconds
....
2018-11-02 21:16:38.148 INFO 6642 --- [ worker-thread-1]: polling
2018-11-02 21:16:38.148 INFO 6642 --- [ worker-thread-1]: processing message and sleep 5 seconds
2018-11-02 21:16:43.153 INFO 6642 --- [ worker-thread-2]: polling
2018-11-02 21:16:43.153 INFO 6642 --- [ worker-thread-2]: processing message and sleep 5 seconds
....
2018-11-02 21:21:48.339 INFO 6642 --- [ worker-thread-3]: polling
2018-11-02 21:21:48.339 INFO 6642 --- [ worker-thread-3]: processing message and sleep 5 seconds
2018-11-02 21:21:53.345 INFO 6642 --- [ worker-thread-3]: polling
2018-11-02 21:21:53.345 INFO 6642 --- [ worker-thread-3]: processing message and sleep 5 seconds
如您所见,没有多线程发生,似乎线程1正在轮询和处理,或者它本身开始再次轮询和处理,或者另一个线程,但是在任何给定时间,只有一个正在执行。
我会认为至少有3个线程正在并行执行,因为触发3个线程只需要300毫秒。每个过程都需要5秒钟才能完成,因此,直到至少一个线程完成为止,没有安排其他任何事情。
我是否以某种方式误解了/错误地配置了3个线程以并行方式运行?
谢谢
答案 0 :(得分:0)
因此,要使其并行执行任务,我需要做两件事 1.在配置上@EnableAsync 2.使用以下@Async注释上面的processMessage
@Async
public void processMessages() {
...
}