3

We have a web application where our end-users will be provided with an option to schedule jobs dynamically to send some kind of reports to group of people. i.e., In UI they select date, time and timezone. Using Spring Boot Scheduler to dynamically schedule the jobs.

Present we're using a single thread-pool to asynchronously send report to group of people part of a scheduled job.

But there is a possibility that multiple people schedule jobs at same time, i.e., for instance 5 users scheduled jobs to execute tomorrow at 9AM. In this scenario, since i'm using the single thread-pool for all scheduled jobs in my application, once the first job executed then execution going to second.

So, Any possibility to use separate thread-pool for each scheduled job dynamically? it's like for each scheduled job having a thread-pool bean with request-scope. so that each job will be executed independently. or any other alternatives to achieve independent task execution.

@Scheduled(cron="0 0 1 * * *")<br>
public void schedulerTask() {<br>
  &nbsp;&nbsp;List<User<User>> users = userDao.getProfiles(); // pull users from db<br>
&nbsp;&nbsp;for(User user : users) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;callToAsyncMethod(user); // call to async method in scheduler<br>
}<br>

since we've multiple users pulled from db, using threadpool for asynchronous execution.<br><br>

@Async("customTaskExecutor")<br>
public void callToAsyncMethod(User user) {<br>
&nbsp;&nbsp;System.out.println("Hello " + user.getName() + ", Welcome to Scheduler Job");<br>
}<br>

@Bean("customTaskExecutor")<br>
public ThreadPoolTaskExecutor customTaskExecutor() {<br>
&nbsp;&nbsp;ThreadPoolTaskExecutor customTaskExecutor= new ThreadPoolTaskExecutor();<br>
&nbsp;&nbsp;        customTaskExecutor.setCorePoolSize(5);<br>
&nbsp;&nbsp;        customTaskExecutor.setMaxPoolSize(10);<br>
&nbsp;&nbsp;        customTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);<br>
&nbsp;&nbsp;        customTaskExecutor.initialize();<br>
&nbsp;&nbsp;        return customTaskExecutor;<br>
}

here all scheduler tasks indirectly referring to same threadpool for async method execution. where the bottleneck issue comes.

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • Maybe see: https://stackoverflow.com/questions/57419242/how-to-create-a-different-threadpooltaskexecutor-in-spring-boot – BillMan May 07 '21 at 17:47
  • It doesn't look like there's a way to do this at this time of speaking. Spring boot's scheduling framework only uses 1 executor to handle the scheduled tasks. I've tried multiple implementations of SchedulingConfigurer, but only 1 thread pool will be used for all scheduled tasks. – louis1204 Mar 31 '23 at 22:59

1 Answers1

0

You can configure the pool size in the application.properties. This will allow you to have multiple schedulers being executed at same time.

spring.task.scheduling.pool.size=10
Ezequiel
  • 136
  • 3
  • thanks for your reply. like you said, with the above configuration we can achieve independent execution for each scheduler task. But in the above mentioned scenario, the bottleneck is with Async method execution in scheduler task. – Naraparaju Mruduladevi May 11 '21 at 16:56