AsyncHandlerInterceptor.java 3.8 KB
Newer Older
1
/*
J
Juergen Hoeller 已提交
2
 * Copyright 2002-2016 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

22 23
import org.springframework.web.method.HandlerMethod;

24
/**
25 26
 * Extends {@code HandlerInterceptor} with a callback method invoked after the
 * start of asynchronous request handling.
27
 *
28
 * <p>When a handler starts an asynchronous request, the {@link DispatcherServlet}
29
 * exits without invoking {@code postHandle} and {@code afterCompletion} as it
30 31 32 33 34
 * normally does for a synchronous request, since the result of request handling
 * (e.g. ModelAndView) is likely not yet ready and will be produced concurrently
 * from another thread. In such scenarios, {@link #afterConcurrentHandlingStarted}
 * is invoked instead, allowing implementations to perform tasks such as cleaning
 * up thread-bound attributes before releasing the thread to the Servlet container.
35
 *
36
 * <p>When asynchronous handling completes, the request is dispatched to the
37 38
 * container for further processing. At this stage the {@code DispatcherServlet}
 * invokes {@code preHandle}, {@code postHandle}, and {@code afterCompletion}.
39 40 41
 * To distinguish between the initial request and the subsequent dispatch
 * after asynchronous handling completes, interceptors can check whether the
 * {@code javax.servlet.DispatcherType} of {@link javax.servlet.ServletRequest}
42
 * is {@code "REQUEST"} or {@code "ASYNC"}.
43
 *
44
 * <p>Note that {@code HandlerInterceptor} implementations may need to do work
45 46 47
 * when an async request times out or completes with a network error. For such
 * cases the Servlet container does not dispatch and therefore the
 * {@code postHandle} and {@code afterCompletion} methods will not be invoked.
48
 * Instead, interceptors can register to track an asynchronous request through
49 50 51 52
 * the {@code registerCallbackInterceptor} and {@code registerDeferredResultInterceptor}
 * methods on {@link org.springframework.web.context.request.async.WebAsyncManager
 * WebAsyncManager}. This can be done proactively on every request from
 * {@code preHandle} regardless of whether async request processing will start.
53 54 55
 *
 * @author Rossen Stoyanchev
 * @since 3.2
56
 * @see org.springframework.web.context.request.async.WebAsyncManager
57 58
 * @see org.springframework.web.context.request.async.CallableProcessingInterceptor
 * @see org.springframework.web.context.request.async.DeferredResultProcessingInterceptor
59 60 61 62
 */
public interface AsyncHandlerInterceptor extends HandlerInterceptor {

	/**
63
	 * Called instead of {@code postHandle} and {@code afterCompletion}, when
64 65 66 67 68
	 * the a handler is being executed concurrently.
	 * <p>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.
69 70
	 * @param request the current request
	 * @param response the current response
71
	 * @param handler the handler (or {@link HandlerMethod}) that started async
72 73
	 * execution, for type and/or instance examination
	 * @throws Exception in case of errors
74
	 */
J
Juergen Hoeller 已提交
75
	void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler)
76
			throws Exception;
77 78

}