Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Scheduled and @Async are sharing same threadpool in spring-boot

I have configured two different thread pools, one for @Scheduled and other for @Async. However, I notice that the thread-pool for @Async is not being used.

Here is the Scheduler configuration

@Configuration
@EnableScheduling
public class SchedulerConfig implements SchedulingConfigurer {
    private final int POOL_SIZE = 10;

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
        threadPoolTaskScheduler.setPoolSize(POOL_SIZE);
        threadPoolTaskScheduler.setThreadNamePrefix("my-sched-pool-");
        threadPoolTaskScheduler.initialize();
        scheduledTaskRegistrar.setTaskScheduler(threadPoolTaskScheduler);
    }
}

Here is the Configuration for Async

@Configuration
@EnableAsync
public class AppConfig {

 @Bean(name = "asyncTaskExecutor")
    public TaskExecutor asyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(15);
        executor.setMaxPoolSize(15);
        executor.setQueueCapacity(100);
        executor.setThreadNamePrefix("my-async-pool-");
        executor.initialize();
        return executor;
    }
}

Here is how I invoke them

@Scheduled(fixedRateString = "2000" )
    public void schedule() {
      log.debug("here is the message from schedule");
      asyncMethod();
    }

@Async("asyncTaskExecutor")
public void asyncMethod(){
  log.info("here is the message from async");
}

Here are the logs

{"thread":"my-sched-pool-1","level":"DEBUG","description":"here is the message from schedule"}
{"thread":"my-sched-pool-1","level":"INFO","description":"here is the message from async"}

As you can notice, both logs are having same pool of that scheduler. but I expect to see the second one to come from async

like image 941
brain storm Avatar asked Jan 12 '18 22:01

brain storm


People also ask

What will happen if you specify @async over a public method of a Spring bean?

For a brief, when we annotate a method of a bean @Async annotation, Spring will execute it in a separate thread and the caller of the method will not wait till the method is completed execution.

What is @scheduled in Spring boot?

The fixedDelay element specified in the @Scheduled annotation executes the annotated method at a fixed time period between the end of the previous invocation and the start of the next invocation. It basically waits for the previous task to be complete. The time value specified for this element is in milliseconds.

Is @scheduled asynchronous?

Note that scheduled tasks don't run in parallel by default. So even if we used fixedRate, the next task won't be invoked until the previous one is done. Now this asynchronous task will be invoked each second, even if the previous task isn't done.

What is the use of @async in Spring boot?

The @EnableAsync annotation switches on Spring's ability to run @Async methods in a background thread pool. This class also customizes the Executor by defining a new bean. Here, the method is named taskExecutor , since this is the specific method name for which Spring searches.


1 Answers

If you call @Async methods from the same class they are declared you are effectively bypassing Spring's proxy mechanism and that is why your example is not working. Try calling the method from a separate class annotated with @Service or any of the other @Component types.

@Service
SomeScheduledClass {

  private final SomeAsyncClass someAsyncClass;

  public SomeScheduledClass(SomeAsyncClass someAsyncClass) {
    this.someAsyncClass = someAsyncClass;
  }

  @Scheduled(fixedRateString = "2000" )
  public void schedule() {
    log.debug("here is the message from schedule");
    someAsyncClass.asyncMethod();
  }
}

@Service
SomeAsyncClass {
  @Async("asyncTaskExecutor")
  public void asyncMethod(){
    log.info("here is the message from async");
  }
}
like image 102
Pedro Avatar answered Oct 18 '22 20:10

Pedro