Tin Ng
Tin Ng

Reputation: 1047

@Scheduled with ThreadPoolTaskScheduler behaviour

I've got this as configuration

 @Bean
    public ThreadPoolTaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(3);
        taskScheduler.setThreadNamePrefix("worker-thread-");
        return taskScheduler;
    }

Poller.java that polls every 100 milliseconds

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 that do something that takes 5 seconds or so an an example

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);
    }
}

When I run it, this is the output

      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

As you can see, there are no multi threading happening, it seems like thread 1 polls and process, and either itself starts polling and processing again or another thread, but at any given time only one is executing.

I would have thought that at least 3 threads are executing in parallel, because it'd take only 300 milliseconds to trigger 3 threads. Each takes 5 seconds to complete, so nothing else is scheduled until at least one of the thread finishes.

Did i misunderstand/misconfigure somehow so that 3 of the threads are running in parallel?

Thanks

Upvotes: 0

Views: 1179

Answers (1)

Tin Ng
Tin Ng

Reputation: 1047

So in order for it to execute tasks in parallel I needed to do 2 things 1. @EnableAsync on the configuration 2. Annotate the processMessage above with @Async as below

@Async
public void processMessages() {
  ...
}

Upvotes: 1

Related Questions