How to Handle Exceptions in Asynchronous Methods in Spring
Exceptions in @Async methods do not reach the caller. Learn AsyncUncaughtExceptionHandler, CompletableFuture error handling and common pitfalls.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
Exceptions in @Async methods do not reach the caller. Learn AsyncUncaughtExceptionHandler, CompletableFuture error handling and common pitfalls.
CompletableFuture lets you run tasks asynchronously and chain, combine and recover from errors without blocking. It extends Future with a non-blocking pipeline API.
Define a ThreadPoolTaskExecutor bean with bounded queue and rejection policy, use it with @Async, or tune Boot properties and virtual threads.
Spring builds on the JDK Executor with TaskExecutor and ThreadPoolTaskExecutor to run async, scheduled and background work in managed thread pools.
Add @EnableAsync and annotate public methods with @Async to run them on a task executor, with a custom pool, error handling and virtual threads.
@Async runs a Spring bean method on a separate executor thread through a proxy. Learn how to enable it, pick an executor, handle errors and avoid common traps.
Use fixedRate for a steady start-to-start cadence and fixedDelay for a gap after each run finishes. Includes externalised intervals, thread pools and multi-instance caveats.
@EnableScheduling lets Spring run @Scheduled methods. See fixedRate, fixedDelay, cron, the single-thread default and multi-instance pitfalls.
Use @Scheduled in Spring Boot with fixedRate, fixedDelay and cron, configure the scheduler pool, and avoid duplicate runs across instances.
@Scheduled runs Spring bean methods at fixed intervals or on cron expressions. Learn the options, cron fields, threading behaviour and how to avoid duplicate runs.
Add the Redis and cache starters, enable caching and annotate methods with @Cacheable to share a cache across instances. Includes TTL, serialisation and failure handling.
@CachePut runs a method every time and stores the return value in a Spring cache, keeping entries fresh after updates. It differs from @Cacheable, which can skip the call.
Build a custom Spring CacheManager with a TTL cache, register it as a bean and learn when Caffeine or Redis are the better choice.
@CacheEvict removes stale entries from a Spring cache when data changes. See its attributes, working examples and the pitfalls that stop it from working.
Add the cache starter, enable caching, annotate methods with @Cacheable, and pick a provider such as Caffeine or Redis through application.yml.
@Cacheable stores a method's return value in a Spring cache so later calls with the same arguments skip the method. See setup, attributes and pitfalls.
Understand the proxy rules, transaction timing and key design behind Spring caching so annotations like @Cacheable behave correctly in real applications.
Cross-cutting concerns such as logging, security and transactions touch many modules. See how AOP, interceptors and decorators separate them from business logic.
Wrap methods with an @Around aspect to time them accurately, scope it with a custom annotation, flag slow calls and publish timers to Micrometer.
@Pointcut defines a named, reusable expression in Spring AOP that selects which method executions advice applies to. Learn syntax, designators and limits.