Why this matters
- Sending order confirmation emails or generating PDF receipts from a REST endpoint blocks the HTTP response until the slow work finishes — async moves that work off the request thread.
- Scheduled jobs power recurring BookStore tasks: nightly inventory syncs, abandoned-cart reminders, and stale reservation cleanup.
- Misconfigured thread pools are a common production incident: too few threads create backlogs, too many threads exhaust memory.
@Async
L1 · FresherRun work on a thread pool after the caller returns — good for emails and analytics, not for work that must finish before HTTP response.
@EnableAsync configuration
L2 · IntermediateDefine TaskExecutor beans with pool sizes and queue limits; unbounded pools hide overload until the JVM runs out of memory.
@Scheduled cron
L3 · Advanced@Scheduled(cron = "0 0 2 * * *") runs nightly catalog sync — use ShedLock or leader election if multiple BookStore instances run.
Virtual threads for @Async
L4 · ExpertOn Java 21, configure async to use virtual threads for I/O-heavy tasks with simpler scaling than large platform thread pools.
Blocked virtual threads release their carrier — other virtual threads continue
Core annotations and configuration
@EnableAsync— activates Spring's async proxy infrastructure; add it to a@Configurationclass or your main application class.@Async— marks a method to run on the task executor instead of the caller's thread; must be on a public method of a Spring-managed bean.@EnableScheduling— turns on the scheduler that evaluates@Scheduledexpressions.@Scheduled— runs a method on a fixed rate, fixed delay, or cron expression.TaskExecutor/ThreadPoolTaskExecutor— the bean behind@Async; you define pool size, queue capacity, and thread naming.
Enable async and scheduling
// learn:fragment
@Configuration
@EnableAsync
@EnableScheduling
public class AsyncConfig {
@Bean(name = "bookstoreTaskExecutor")
public Executor bookstoreTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(4);
executor.setMaxPoolSize(16);
executor.setQueueCapacity(200);
executor.setThreadNamePrefix("bookstore-async-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
}
The rest of this guide is part of Pro
Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.
Still to come · 3 sections
- Fire-and-forget email after checkout
- Cron jobs for housekeeping
- Interview follow-ups
Test yourself
Answer these before moving on — recall is what makes it stick.