Java Code Examples for org.springframework.web.context.request.async.AsyncRequestTimeoutException

The following examples show how to use org.springframework.web.context.request.async.AsyncRequestTimeoutException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Customize the response for AsyncRequestTimeoutException.
 * <p>This method delegates to {@link #handleExceptionInternal}.
 * @param ex the exception
 * @param headers the headers to be written to the response
 * @param status the selected response status
 * @param webRequest the current request
 * @return a {@code ResponseEntity} instance
 * @since 4.2.8
 */
@Nullable
protected ResponseEntity<Object> handleAsyncRequestTimeoutException(
		AsyncRequestTimeoutException ex, HttpHeaders headers, HttpStatus status, WebRequest webRequest) {

	if (webRequest instanceof ServletWebRequest) {
		ServletWebRequest servletWebRequest = (ServletWebRequest) webRequest;
		HttpServletResponse response = servletWebRequest.getResponse();
		if (response != null && response.isCommitted()) {
			if (logger.isWarnEnabled()) {
				logger.warn("Async request timed out");
			}
			return null;
		}
	}

	return handleExceptionInternal(ex, null, headers, status, webRequest);
}
 
Example 2
/**
 * Customize the response for NoHandlerFoundException.
 * <p>This method delegates to {@link #handleExceptionInternal}.
 * @param ex the exception
 * @param headers the headers to be written to the response
 * @param status the selected response status
 * @param webRequest the current request
 * @return a {@code ResponseEntity} instance
 * @since 4.2.8
 */
@Nullable
protected ResponseEntity<Object> handleAsyncRequestTimeoutException(
		AsyncRequestTimeoutException ex, HttpHeaders headers, HttpStatus status, WebRequest webRequest) {

	if (webRequest instanceof ServletWebRequest) {
		ServletWebRequest servletWebRequest = (ServletWebRequest) webRequest;
		HttpServletResponse response = servletWebRequest.getResponse();
		if (response != null && response.isCommitted()) {
			if (logger.isWarnEnabled()) {
				logger.warn("Async request timed out");
			}
			return null;
		}
	}

	return handleExceptionInternal(ex, null, headers, status, webRequest);
}
 
Example 3
/**
 * Customize the response for NoHandlerFoundException.
 * <p>This method delegates to {@link #handleExceptionInternal}.
 * @param ex the exception
 * @param headers the headers to be written to the response
 * @param status the selected response status
 * @param webRequest the current request
 * @return a {@code ResponseEntity} instance
 * @since 4.2.8
 */
protected ResponseEntity<Object> handleAsyncRequestTimeoutException(
		AsyncRequestTimeoutException ex, HttpHeaders headers, HttpStatus status, WebRequest webRequest) {

	if (webRequest instanceof ServletWebRequest) {
		ServletWebRequest servletRequest = (ServletWebRequest) webRequest;
		HttpServletRequest request = servletRequest.getNativeRequest(HttpServletRequest.class);
		HttpServletResponse response = servletRequest.getNativeResponse(HttpServletResponse.class);
		if (response.isCommitted()) {
			if (logger.isErrorEnabled()) {
				logger.error("Async timeout for " + request.getMethod() + " [" + request.getRequestURI() + "]");
			}
			return null;
		}
	}

	return handleExceptionInternal(ex, null, headers, status, webRequest);
}
 
Example 4
@Test  // SPR-14669
public void handleAsyncRequestTimeoutException() throws Exception {
	Exception ex = new AsyncRequestTimeoutException();
	ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
	assertNotNull("No ModelAndView returned", mav);
	assertTrue("No Empty ModelAndView returned", mav.isEmpty());
	assertEquals("Invalid status code", 503, response.getStatus());
}
 
Example 5
@Test  // SPR-14669
public void handleAsyncRequestTimeoutException() throws Exception {
	Exception ex = new AsyncRequestTimeoutException();
	ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
	assertNotNull("No ModelAndView returned", mav);
	assertTrue("No Empty ModelAndView returned", mav.isEmpty());
	assertEquals("Invalid status code", 503, response.getStatus());
}
 
Example 6
@ExceptionHandler(value = AsyncRequestTimeoutException.class)
public ModelAndView handleTimeout(HttpServletResponse rsp) throws IOException {
   if (!rsp.isCommitted()) {
      rsp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
   }
   return new ModelAndView();
}
 
Example 7
@ExceptionHandler(value = AsyncRequestTimeoutException.class)
public ModelAndView handleTimeout(HttpServletResponse rsp) throws IOException {
   if (!rsp.isCommitted()) {
      rsp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
   }
   return new ModelAndView();
}
 
Example 8
@Test
public void shouldHandleException_returns_TEMPORARY_SERVICE_PROBLEM_for_AsyncRequestTimeoutException() {
    // given
    AsyncRequestTimeoutException ex = new AsyncRequestTimeoutException();
    assertThat(ex.getClass().getName())
        .isEqualTo("org.springframework.web.context.request.async.AsyncRequestTimeoutException");

    // when
    ApiExceptionHandlerListenerResult result = listener.shouldHandleException(ex);

    // then
    validateResponse(result, true, singletonList(testProjectApiErrors.getTemporaryServiceProblemApiError()));
}
 
Example 9
Source Project: moon-api-gateway   Source File: ExceptionAdvice.java    License: MIT License 5 votes vote down vote up
@ExceptionHandler(AsyncRequestTimeoutException.class)
public ResponseEntity asyncRequestTimeoutException(Exception e, HttpServletRequest request) {
    log.error("{}", getStackTrace(e));
    ExceptionType exceptionType = ExceptionType.E_1104_OUTBOUND_SERVICE_REQUEST_TIME_OUT;
    setHttpResponseErrorCode(request, ExceptionType.E_1104_OUTBOUND_SERVICE_REQUEST_TIME_OUT.getCode());
    CommonResponseEntity response = CommonResponseEntity.generateException(exceptionType.getCode(), messageManager.getProperty(exceptionType.getCode()));
    return HttpHelper.newResponseEntityWithId(HttpStatus.REQUEST_TIMEOUT, response);
}
 
Example 10
@Override
@Nullable
protected ModelAndView doResolveException(
		HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {

	try {
		if (ex instanceof HttpRequestMethodNotSupportedException) {
			return handleHttpRequestMethodNotSupported(
					(HttpRequestMethodNotSupportedException) ex, request, response, handler);
		}
		else if (ex instanceof HttpMediaTypeNotSupportedException) {
			return handleHttpMediaTypeNotSupported(
					(HttpMediaTypeNotSupportedException) ex, request, response, handler);
		}
		else if (ex instanceof HttpMediaTypeNotAcceptableException) {
			return handleHttpMediaTypeNotAcceptable(
					(HttpMediaTypeNotAcceptableException) ex, request, response, handler);
		}
		else if (ex instanceof MissingPathVariableException) {
			return handleMissingPathVariable(
					(MissingPathVariableException) ex, request, response, handler);
		}
		else if (ex instanceof MissingServletRequestParameterException) {
			return handleMissingServletRequestParameter(
					(MissingServletRequestParameterException) ex, request, response, handler);
		}
		else if (ex instanceof ServletRequestBindingException) {
			return handleServletRequestBindingException(
					(ServletRequestBindingException) ex, request, response, handler);
		}
		else if (ex instanceof ConversionNotSupportedException) {
			return handleConversionNotSupported(
					(ConversionNotSupportedException) ex, request, response, handler);
		}
		else if (ex instanceof TypeMismatchException) {
			return handleTypeMismatch(
					(TypeMismatchException) ex, request, response, handler);
		}
		else if (ex instanceof HttpMessageNotReadableException) {
			return handleHttpMessageNotReadable(
					(HttpMessageNotReadableException) ex, request, response, handler);
		}
		else if (ex instanceof HttpMessageNotWritableException) {
			return handleHttpMessageNotWritable(
					(HttpMessageNotWritableException) ex, request, response, handler);
		}
		else if (ex instanceof MethodArgumentNotValidException) {
			return handleMethodArgumentNotValidException(
					(MethodArgumentNotValidException) ex, request, response, handler);
		}
		else if (ex instanceof MissingServletRequestPartException) {
			return handleMissingServletRequestPartException(
					(MissingServletRequestPartException) ex, request, response, handler);
		}
		else if (ex instanceof BindException) {
			return handleBindException((BindException) ex, request, response, handler);
		}
		else if (ex instanceof NoHandlerFoundException) {
			return handleNoHandlerFoundException(
					(NoHandlerFoundException) ex, request, response, handler);
		}
		else if (ex instanceof AsyncRequestTimeoutException) {
			return handleAsyncRequestTimeoutException(
					(AsyncRequestTimeoutException) ex, request, response, handler);
		}
	}
	catch (Exception handlerEx) {
		if (logger.isWarnEnabled()) {
			logger.warn("Failure while trying to resolve exception [" + ex.getClass().getName() + "]", handlerEx);
		}
	}
	return null;
}
 
Example 11
@Test
public void asyncRequestTimeoutException() {
	testException(new AsyncRequestTimeoutException());
}
 
Example 12
@Override
@Nullable
protected ModelAndView doResolveException(
		HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {

	try {
		if (ex instanceof HttpRequestMethodNotSupportedException) {
			return handleHttpRequestMethodNotSupported(
					(HttpRequestMethodNotSupportedException) ex, request, response, handler);
		}
		else if (ex instanceof HttpMediaTypeNotSupportedException) {
			return handleHttpMediaTypeNotSupported(
					(HttpMediaTypeNotSupportedException) ex, request, response, handler);
		}
		else if (ex instanceof HttpMediaTypeNotAcceptableException) {
			return handleHttpMediaTypeNotAcceptable(
					(HttpMediaTypeNotAcceptableException) ex, request, response, handler);
		}
		else if (ex instanceof MissingPathVariableException) {
			return handleMissingPathVariable(
					(MissingPathVariableException) ex, request, response, handler);
		}
		else if (ex instanceof MissingServletRequestParameterException) {
			return handleMissingServletRequestParameter(
					(MissingServletRequestParameterException) ex, request, response, handler);
		}
		else if (ex instanceof ServletRequestBindingException) {
			return handleServletRequestBindingException(
					(ServletRequestBindingException) ex, request, response, handler);
		}
		else if (ex instanceof ConversionNotSupportedException) {
			return handleConversionNotSupported(
					(ConversionNotSupportedException) ex, request, response, handler);
		}
		else if (ex instanceof TypeMismatchException) {
			return handleTypeMismatch(
					(TypeMismatchException) ex, request, response, handler);
		}
		else if (ex instanceof HttpMessageNotReadableException) {
			return handleHttpMessageNotReadable(
					(HttpMessageNotReadableException) ex, request, response, handler);
		}
		else if (ex instanceof HttpMessageNotWritableException) {
			return handleHttpMessageNotWritable(
					(HttpMessageNotWritableException) ex, request, response, handler);
		}
		else if (ex instanceof MethodArgumentNotValidException) {
			return handleMethodArgumentNotValidException(
					(MethodArgumentNotValidException) ex, request, response, handler);
		}
		else if (ex instanceof MissingServletRequestPartException) {
			return handleMissingServletRequestPartException(
					(MissingServletRequestPartException) ex, request, response, handler);
		}
		else if (ex instanceof BindException) {
			return handleBindException((BindException) ex, request, response, handler);
		}
		else if (ex instanceof NoHandlerFoundException) {
			return handleNoHandlerFoundException(
					(NoHandlerFoundException) ex, request, response, handler);
		}
		else if (ex instanceof AsyncRequestTimeoutException) {
			return handleAsyncRequestTimeoutException(
					(AsyncRequestTimeoutException) ex, request, response, handler);
		}
	}
	catch (Exception handlerEx) {
		if (logger.isWarnEnabled()) {
			logger.warn("Failure while trying to resolve exception [" + ex.getClass().getName() + "]", handlerEx);
		}
	}
	return null;
}
 
Example 13
@Test
public void asyncRequestTimeoutException() {
	testException(new AsyncRequestTimeoutException());
}
 
Example 14
@ExceptionHandler(value = AsyncRequestTimeoutException.class)
public ResponseEntity<Result> handleAsyncRequestTimeoutException(AsyncRequestTimeoutException e, HttpServletRequest request) {
    System.out.println(request.getRequestURI());
    System.out.println("请求超时");
    return ResponseEntity.status(HttpStatus.REQUEST_TIMEOUT).body(new Result("请求超时"));
}
 
Example 15
@Override
@SuppressWarnings("deprecation")
protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response,
		Object handler, Exception ex) {

	try {
		if (ex instanceof org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException) {
			return handleNoSuchRequestHandlingMethod((org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException) ex,
					request, response, handler);
		}
		else if (ex instanceof HttpRequestMethodNotSupportedException) {
			return handleHttpRequestMethodNotSupported((HttpRequestMethodNotSupportedException) ex, request,
					response, handler);
		}
		else if (ex instanceof HttpMediaTypeNotSupportedException) {
			return handleHttpMediaTypeNotSupported((HttpMediaTypeNotSupportedException) ex, request, response,
					handler);
		}
		else if (ex instanceof HttpMediaTypeNotAcceptableException) {
			return handleHttpMediaTypeNotAcceptable((HttpMediaTypeNotAcceptableException) ex, request, response,
					handler);
		}
		else if (ex instanceof MissingPathVariableException) {
			return handleMissingPathVariable((MissingPathVariableException) ex, request,
					response, handler);
		}
		else if (ex instanceof MissingServletRequestParameterException) {
			return handleMissingServletRequestParameter((MissingServletRequestParameterException) ex, request,
					response, handler);
		}
		else if (ex instanceof ServletRequestBindingException) {
			return handleServletRequestBindingException((ServletRequestBindingException) ex, request, response,
					handler);
		}
		else if (ex instanceof ConversionNotSupportedException) {
			return handleConversionNotSupported((ConversionNotSupportedException) ex, request, response, handler);
		}
		else if (ex instanceof TypeMismatchException) {
			return handleTypeMismatch((TypeMismatchException) ex, request, response, handler);
		}
		else if (ex instanceof HttpMessageNotReadableException) {
			return handleHttpMessageNotReadable((HttpMessageNotReadableException) ex, request, response, handler);
		}
		else if (ex instanceof HttpMessageNotWritableException) {
			return handleHttpMessageNotWritable((HttpMessageNotWritableException) ex, request, response, handler);
		}
		else if (ex instanceof MethodArgumentNotValidException) {
			return handleMethodArgumentNotValidException((MethodArgumentNotValidException) ex, request, response,
					handler);
		}
		else if (ex instanceof MissingServletRequestPartException) {
			return handleMissingServletRequestPartException((MissingServletRequestPartException) ex, request,
					response, handler);
		}
		else if (ex instanceof BindException) {
			return handleBindException((BindException) ex, request, response, handler);
		}
		else if (ex instanceof NoHandlerFoundException) {
			return handleNoHandlerFoundException((NoHandlerFoundException) ex, request, response, handler);
		}
		else if (ex instanceof AsyncRequestTimeoutException) {
			return handleAsyncRequestTimeoutException(
					(AsyncRequestTimeoutException) ex, request, response, handler);
		}
	}
	catch (Exception handlerException) {
		if (logger.isWarnEnabled()) {
			logger.warn("Handling of [" + ex.getClass().getName() + "] resulted in Exception", handlerException);
		}
	}
	return null;
}
 
Example 16
Source Project: opentracing-toolbox   Source File: ExampleController.java    License: MIT License 4 votes vote down vote up
@ExceptionHandler(AsyncRequestTimeoutException.class)
public String onTimeout() {
    return "Interrupted";
}
 
Example 17
@ExceptionHandler(AsyncRequestTimeoutException.class)
public final Object handleSpringServiceUnavailableException(
    Exception e, HandlerMethod handlerMethod) {
  return logAndHandleException(e, SERVICE_UNAVAILABLE, handlerMethod);
}
 
Example 18
/**
 * Handle the case where an async request timed out.
 * <p>The default implementation sends an HTTP 503 error.
 * @param ex the {@link AsyncRequestTimeoutException }to be handled
 * @param request current HTTP request
 * @param response current HTTP response
 * @param handler the executed handler, or {@code null} if none chosen
 * at the time of the exception (for example, if multipart resolution failed)
 * @return an empty ModelAndView indicating the exception was handled
 * @throws IOException potentially thrown from {@link HttpServletResponse#sendError}
 * @since 4.2.8
 */
protected ModelAndView handleAsyncRequestTimeoutException(AsyncRequestTimeoutException ex,
		HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {

	if (!response.isCommitted()) {
		response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
	}
	else {
		logger.warn("Async request timed out");
	}
	return new ModelAndView();
}
 
Example 19
/**
 * Handle the case where an async request timed out.
 * <p>The default implementation sends an HTTP 503 error.
 * @param ex the {@link AsyncRequestTimeoutException }to be handled
 * @param request current HTTP request
 * @param response current HTTP response
 * @param handler the executed handler, or {@code null} if none chosen
 * at the time of the exception (for example, if multipart resolution failed)
 * @return an empty ModelAndView indicating the exception was handled
 * @throws IOException potentially thrown from {@link HttpServletResponse#sendError}
 * @since 4.2.8
 */
protected ModelAndView handleAsyncRequestTimeoutException(AsyncRequestTimeoutException ex,
		HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {

	if (!response.isCommitted()) {
		response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
	}
	else {
		logger.warn("Async request timed out");
	}
	return new ModelAndView();
}
 
Example 20
Source Project: BlogManagePlatform   Source File: GlobalController.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * 默认异常处理器
 * @param HttpServletResponse 响应
 * @param ServletRequestBindingException 异常
 * @author Frodez
 * @throws IOException
 * @throws JsonProcessingException
 * @date 2018-12-21
 */
@ExceptionHandler(value = AsyncRequestTimeoutException.class)
public void asyncRequestTimeoutExceptionHandler(HttpServletResponse response, AsyncRequestTimeoutException e)
	throws JsonProcessingException, IOException {
	log.error("[frodez.config.mvc.error.GlobalController.asyncRequestTimeoutExceptionHandler]{}");
	ServletUtil.writeJson(response, Result.busy());
}
 
Example 21
/**
 * Handle the case where an async request timed out.
 * <p>The default implementation sends an HTTP 503 error.
 * @param ex the {@link AsyncRequestTimeoutException }to be handled
 * @param request current HTTP request
 * @param response current HTTP response
 * @param handler the executed handler, or {@code null} if none chosen
 * at the time of the exception (for example, if multipart resolution failed)
 * @return an empty ModelAndView indicating the exception was handled
 * @throws IOException potentially thrown from response.sendError()
 * @since 4.2.8
 */
protected ModelAndView handleAsyncRequestTimeoutException(AsyncRequestTimeoutException ex,
		HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {

	if (!response.isCommitted()) {
		response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
	}
	else if (logger.isErrorEnabled()) {
		logger.error("Async timeout for " + request.getMethod() + " [" + request.getRequestURI() + "]");
	}
	return new ModelAndView();
}