Master Spring Boot Task Execution: Configure Executors, Schedulers, and Async
Learn how Spring Boot automatically configures ThreadPoolTaskExecutor and ThreadPoolTaskScheduler, customize them via spring.task.execution and spring.task.scheduling properties, and use @EnableAsync, @Scheduled, and @Async annotations—including advanced options like cron expressions, time units, executor qualifiers, and exception handling—to efficiently manage asynchronous and scheduled tasks.
Spring Boot Configuration
When no Executor bean is present, Spring Boot automatically configures a ThreadPoolTaskExecutor with sensible defaults, which is used for @EnableAsync and Spring MVC async request handling.
If you define a custom Executor, regular async tasks (e.g., @EnableAsync) will transparently use it, but Spring MVC async support requires an AsyncTaskExecutor named applicationTaskExecutor . You can either change your Executor to a ThreadPoolTaskExecutor or define both a ThreadPoolTaskExecutor and an AsyncConfigurer that wraps your custom executor.
The default thread pool has 8 core threads and can grow based on load. These defaults can be fine‑tuned via the spring.task.execution namespace, for example:
<code>spring:
task:
execution:
pool:
max-size: 16
queue-capacity: 100
keep-alive: "10s"
</code>This configuration increases the pool to a maximum of 16 threads, uses a bounded queue of 100 tasks, and reduces the idle‑thread keep‑alive time to 10 seconds.
For scheduled tasks you can also auto‑configure a ThreadPoolTaskScheduler (e.g., with @EnableScheduling). Its defaults can be adjusted via spring.task.scheduling :
<code>spring:
task:
scheduling:
thread-name-prefix: "scheduling-"
pool:
size: 2
</code>To create custom executors or schedulers, you can define TaskExecutorBuilder and TaskSchedulerBuilder beans in the context.
Spring Configuration
<code>@Configuration
@EnableAsync
@EnableScheduling
public class AppConfig {
}
</code>Use the @Scheduled annotation to define periodic tasks. For example, a fixed‑delay of 5 seconds:
<code>@Scheduled(fixedDelay = 5000)
public void doSomething() {
}
</code>By default the time unit is milliseconds; you can change it with the timeUnit attribute, e.g., @Scheduled(fixedDelay = 5, timeUnit = TimeUnit.SECONDS) .
Other scheduling options include fixedRate , initialDelay , and cron expressions such as @Scheduled(cron="*/5 * * * * MON-FRI") . All bean scopes support @Scheduled methods since Spring 4.3, but avoid registering multiple instances of the same @Scheduled class.
The @Async annotation enables asynchronous method execution. The method runs in a task submitted to the configured TaskExecutor . Simple void methods can be annotated as:
<code>@Async
void doSomething() {
}
</code>Methods can also accept parameters and return Future or CompletableFuture results for richer async handling.
@Async methods can return Spring’s ListenableFuture or Java 8’s CompletableFuture for advanced composition.
Do not combine @Async with @PostConstruct; instead, invoke an @Async method from a separate bean after construction.
<code>public class SampleBeanImpl implements SampleBean {
@Async
void doSomething() { }
}
public class SampleBeanInitializer {
private final SampleBean bean;
public SampleBeanInitializer(SampleBean bean) { this.bean = bean; }
@PostConstruct
public void initialize() {
bean.doSomething();
}
}
</code>You can specify a particular executor with @Async("otherExecutor") and provide an AsyncUncaughtExceptionHandler to handle exceptions from void‑returning async methods.
<code>public class MyAsyncUncaughtExceptionHandler implements AsyncUncaughtExceptionHandler {
@Override
public void handleUncaughtException(Throwable ex, Method method, Object... params) {
}
}
</code>That concludes the guide.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.