org.springframework.http.HttpStatus#TOO_MANY_REQUESTS源码实例Demo

下面列出了org.springframework.http.HttpStatus#TOO_MANY_REQUESTS 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@Override
public void run() {
	while (running.get()) {
		try {
			readStream(twitter.getRestTemplate());
		}
		catch (HttpStatusCodeException sce) {
			if (sce.getStatusCode() == HttpStatus.UNAUTHORIZED) {
				logger.error("Twitter authentication failed: " + sce.getMessage());
				running.set(false);
			}
			else if (sce.getStatusCode() == HttpStatus.TOO_MANY_REQUESTS) {
				waitRateLimitBackoff();
			}
			else {
				waitHttpErrorBackoff();
			}
		}
		catch (Exception e) {
			logger.warn("Exception while reading stream.", e);
			waitLinearBackoff();
		}
	}
}
 
源代码2 项目: sdk-rest   文件: StandardBullhornData.java
/**
 * @param uriVariables
 * @param tryNumber
 * @param error
 * @throws RestApiException if tryNumber >= API_RETRY.
 */
protected boolean handleHttpStatusCodeError(Map<String, String> uriVariables, int tryNumber, HttpStatusCodeException error) {
    boolean isTooManyRequestsError = false;
    if (error.getStatusCode() == HttpStatus.UNAUTHORIZED) {
        resetBhRestToken(uriVariables);
    } else if (error.getStatusCode() == HttpStatus.TOO_MANY_REQUESTS) {
        isTooManyRequestsError = true;
    }
    log.error(
            "HttpStatusCodeError making api call. Try number:" + tryNumber + " out of " + API_RETRY + ". Http status code: "
                    + error.getStatusCode() + ". Response body: " + error.getResponseBodyAsString(), error);
    if (tryNumber >= API_RETRY && !isTooManyRequestsError) {
        throw new RestApiException("HttpStatusCodeError making api call with url variables " + uriVariables.toString()
                + ". Http status code: " + error.getStatusCode().toString() + ". Response body: " + error == null ? ""
                : error.getResponseBodyAsString());
    }
    return isTooManyRequestsError;
}
 
TooManyRequests(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
	super(HttpStatus.TOO_MANY_REQUESTS, statusText, headers, body, charset);
}
 
源代码4 项目: kylin-on-parquet-v2   文件: BasicController.java
@ResponseStatus(HttpStatus.TOO_MANY_REQUESTS)
@ExceptionHandler(TooManyRequestException.class)
@ResponseBody
ErrorResponse handleTooManyRequest(HttpServletRequest req, Exception ex) {
    return new ErrorResponse(req.getRequestURL().toString(), ex);
}
 
TooManyRequests(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
	super(HttpStatus.TOO_MANY_REQUESTS, statusText, headers, body, charset);
}
 
源代码6 项目: lion   文件: CustomExceptionHandler.java
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {

    log.error(ex.getMessage());

    /**
     * 按照异常类型进行处理
     */
    HttpStatus httpStatus;
    String body;
    if (ex instanceof BlockException) {
        httpStatus = HttpStatus.TOO_MANY_REQUESTS;
        // Too Many Request Server
        body = JsonUtil.jsonObj2Str(Result.failure(httpStatus.value(), "前方拥挤,请稍后再试"));
    } else {
        //ResponseStatusException responseStatusException = (ResponseStatusException) ex;
        //httpStatus = responseStatusException.getStatus();
        //body = responseStatusException.getMessage();

        httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
        // Internal Server Error
        body = JsonUtil.jsonObj2Str(Result.failure(httpStatus.value(), "调用失败,服务不可用"));
    }
    /**
     * 封装响应体,此body可修改为自己的jsonBody
     */
    Map<String, Object> result = new HashMap<>(2, 1);
    result.put("httpStatus", httpStatus);
    result.put("body", body);
    /**
     * 错误记录
     */
    ServerHttpRequest request = exchange.getRequest();
    log.error("[全局异常处理]异常请求路径:{},记录异常信息:{}", request.getPath(), body);
    /**
     * 参考AbstractErrorWebExceptionHandler
     */
    if (exchange.getResponse().isCommitted()) {
        return Mono.error(ex);
    }
    exceptionHandlerResult.set(result);
    ServerRequest newRequest = ServerRequest.create(exchange, this.messageReaders);
    Mono<Void> mono = RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse).route(newRequest)
            .switchIfEmpty(Mono.error(ex))
            .flatMap((handler) -> handler.handle(newRequest))
            .flatMap((response) -> write(exchange, response));
    exceptionHandlerResult.remove();
    return mono;
}
 
源代码7 项目: seckill   文件: TooManyRequestsExceptionAdvice.java
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.TOO_MANY_REQUESTS)
public Object exceptionHandler(Exception e) {
  return new HttpEntity<>(e.getMessage());
}
 
public WebfluxRateLimitException(String reason) {
	super(HttpStatus.TOO_MANY_REQUESTS, reason);
}
 
源代码9 项目: backstopper   文件: ClientfacingErrorITest.java
@RequestMapping("/throwTooManyRequestsException")
public void throwTooManyRequestsException() {
    HttpServerErrorException serverResponseEx = new HttpServerErrorException(HttpStatus.TOO_MANY_REQUESTS);
    throw new ServerHttpStatusCodeException(new Exception("Intentional test exception"), "FOO", serverResponseEx, serverResponseEx.getStatusCode().value(), serverResponseEx.getResponseHeaders(), serverResponseEx.getResponseBodyAsString());
}
 
源代码10 项目: kylin   文件: BasicController.java
@ResponseStatus(HttpStatus.TOO_MANY_REQUESTS)
@ExceptionHandler(TooManyRequestException.class)
@ResponseBody
ErrorResponse handleTooManyRequest(HttpServletRequest req, Exception ex) {
    return new ErrorResponse(req.getRequestURL().toString(), ex);
}