Package org.springframework.web.servlet

# Interface AsyncHandlerInterceptor

All Superinterfaces:
  `HandlerInterceptor`

All Known Implementing Classes:
  `WebRequestHandlerInterceptorAdapter`

public interface AsyncHandlerInterceptor extends HandlerInterceptor
Extends `HandlerInterceptor` with a callback method invoked after the start of asynchronous request handling.

When a handler starts an asynchronous request, the `DispatcherServlet` exits without invoking `postHandle` and `afterCompletion` as it normally does for a synchronous request, since the result of request handling (for example, ModelAndView) is likely not yet ready and will be produced concurrently from another thread. In such scenarios, `afterConcurrentHandlingStarted(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, java.lang.Object)` is invoked instead, allowing implementations to perform tasks such as cleaning up thread-bound attributes before releasing the thread to the Servlet container.

When asynchronous handling completes, the request is dispatched to the container for further processing. At this stage the `DispatcherServlet` invokes `preHandle`, `postHandle`, and `afterCompletion`. To distinguish between the initial request and the subsequent dispatch after asynchronous handling completes, interceptors can check whether the `jakarta.servlet.DispatcherType` of `ServletRequest` is `"REQUEST"` or `"ASYNC"`.

Note that `HandlerInterceptor` implementations may need to do work when an async request times out or completes with a network error. For such cases the Servlet container does not dispatch and therefore the `postHandle` and `afterCompletion` methods will not be invoked. Instead, interceptors can register to track an asynchronous request through the `registerCallbackInterceptor` and `registerDeferredResultInterceptor` methods on `WebAsyncManager`. This can be done proactively on every request from `preHandle` regardless of whether async request processing will start.

Since:
  3.2
Author:
  Rossen Stoyanchev
See Also:
  - `WebAsyncManager`
  - `CallableProcessingInterceptor`
  - `DeferredResultProcessingInterceptor`

## Method Summary

Modifier and Type
Method
Description
`default void`
`afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler)`
Called instead of `postHandle` and `afterCompletion` when the handler is being executed concurrently.

### Methods inherited from interface org.springframework.web.servlet.HandlerInterceptor

`afterCompletion, postHandle, preHandle`

## Method Details

### afterConcurrentHandlingStarted

default void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
Called instead of `postHandle` and `afterCompletion` when the handler is being executed concurrently.

Implementations may use the provided request and response but should avoid modifying them in ways that would conflict with the concurrent execution of the handler. A typical use of this method would be to clean up thread-local variables.

Parameters:
  `request` - the current request
  `response` - the current response
  `handler` - the handler (or `HandlerMethod`) that started async execution, for type and/or instance examination
Throws:
  `Exception` - in case of errors