Handling Exceptions in @Async Methods in Spring Boot
Learn how to catch and report errors from @Async methods in Spring Boot, using CompletableFuture, a custom handler and task executor properties.
2,886 posts about Java: questions with answers, tutorials and articles, newest first.
Learn how to catch and report errors from @Async methods in Spring Boot, using CompletableFuture, a custom handler and task executor properties.
ThreadPoolTaskExecutor runs Spring async work on a bounded thread pool. See how core size, queue and max size interact and how to use @Async.
Tune Boot's applicationTaskExecutor with properties, define named ThreadPoolTaskExecutor beans, or switch @Async to virtual threads on Boot 3.2 and later.
@Async in Spring Boot runs methods on the auto-configured task executor so callers do not wait. Learn setup, pool tuning, custom executors and common mistakes.
Enable @Async in Spring Boot, tune the thread pool with spring.task.execution properties, return CompletableFuture and avoid the proxy and context pitfalls.
TaskScheduler runs tasks at a future time or on a repeating schedule. See how to size its pool and use fixed-rate, fixed-delay and cron triggers.
Enable scheduling with @EnableScheduling and annotate bean methods with @Scheduled. Learn fixedRate, fixedDelay, cron, pool size and running jobs once per cluster.
@Scheduled runs Spring Boot methods at a fixed rate, delay or cron time. See how to tune the auto-configured scheduler, monitor tasks and test them.
Spring annotations do not set expiry; the provider does. Configure TTL with Caffeine or Redis, set per-cache policies and avoid stale or stampeding entries.
@CacheEvict removes one or all entries from a Spring cache when a method runs, so updates and deletes never leave stale data behind.
Add the Redis and cache starters, enable caching and use @Cacheable, @CachePut and @CacheEvict with a TTL and JSON serializer for shared, fast caching.
@ResponseStatus ties an HTTP status code to an exception class or handler method in Spring. See examples, limits and when to use ResponseEntity instead.
Use @RestControllerAdvice and @ExceptionHandler to return consistent JSON errors. Covers validation failures and the RFC 9457 ProblemDetail type in Spring 6.
@ControllerAdvice applies shared exception handling, binding and model logic to all Spring controllers. See examples with @RestControllerAdvice and ProblemDetail.
Learn how to implement custom error handling in Spring Boot with @RestControllerAdvice, ProblemDetail responses and validation error details.
readOnly = true marks a transaction as read-only, letting JDBC, Hibernate and routing layers optimise queries without guaranteeing that writes fail.
Learn which exceptions JPA throws in Spring Boot, why try/catch around save often fails, and how to map errors to 409 and 404 responses.
@Valid runs Jakarta Bean Validation on controller arguments and nested objects in Spring. See DTO examples, error handling and how it differs from @Validated.
Create custom Bean Validation constraints for JPA entities, validate on persist and update, and know where database constraints still matter.
@NotNull rejects null values in Jakarta Bean Validation. See Spring Boot and JPA examples and how it differs from @NotEmpty and @NotBlank.