com.netflix.hystrix.exception.HystrixTimeoutException Java Examples

The following examples show how to use com.netflix.hystrix.exception.HystrixTimeoutException. 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
Source File: LocFeignRetryAutoConfiguration.java    From loc-framework with MIT License 6 votes vote down vote up
public Problem handleException(Throwable e) {
  log.error("feign error: {}", e.getMessage(), e);
  if (e instanceof HystrixTimeoutException) {
    return Problem.valueOf(Status.BAD_REQUEST, "Hystrix time out");
  } else if (e instanceof feign.RetryableException) {
    if (StringUtils.isNotBlank(e.getLocalizedMessage())) {
      if (e.getLocalizedMessage().contains("Connection refused")) {
        return Problem.valueOf(Status.REQUEST_TIMEOUT, "Connect timed out");
      } else if (e.getLocalizedMessage().contains("Read timed out")) {
        return Problem.valueOf(Status.REQUEST_TIMEOUT, "Read timed out");
      }
    }
  } else if (e instanceof RuntimeException) {
    if (e.getCause() != null) {
      String message = e.getCause().getLocalizedMessage();
      if (StringUtils.isNotBlank(message) && message.contains(LOAD_BALANCER_NOT_AVAILABLE)) {
        return Problem.valueOf(Status.SERVICE_UNAVAILABLE, "Service not available");
      }
    }
  }
  return Problem.valueOf(Status.BAD_REQUEST, "Http bad request");
}
 
Example #2
Source File: ApiGatewayFallbackProvider.java    From open-capacity-platform with Apache License 2.0 5 votes vote down vote up
/**
 * 网关向api服务请求是失败了,但是消费者客户端向网关发起的请求是OK的,
 * 不应该把api的404,500等问题抛给客户端
 * 网关和api服务集群对于客户端来说是黑盒子
 */

@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause){

	if(cause instanceof HystrixTimeoutException){
		return response(HttpStatus.GATEWAY_TIMEOUT);
	}else{
		return this.fallbackResponse();
	}
}
 
Example #3
Source File: MyFallback.java    From springcloud-study with Apache License 2.0 5 votes vote down vote up
@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {

	//标记不同的异常为不同的http状态值
	if (cause instanceof HystrixTimeoutException) {
		return response(HttpStatus.GATEWAY_TIMEOUT);
	} else {
		//可继续添加自定义异常类
		return response(HttpStatus.INTERNAL_SERVER_ERROR);
	}
}
 
Example #4
Source File: UacFallbackProvider.java    From paascloud-master with Apache License 2.0 5 votes vote down vote up
@Override
public ClientHttpResponse fallbackResponse(final Throwable cause) {
	if (cause instanceof HystrixTimeoutException) {
		return response(HttpStatus.GATEWAY_TIMEOUT);
	} else {
		return fallbackResponse();
	}
}
 
Example #5
Source File: ApiFallbackProvider.java    From springcloud-course with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
    logger.warning(String.format("route:%s,exceptionType:%s,stackTrace:%s", route, cause.getClass().getName(), cause.getStackTrace()));
    String message = "";
    if (cause instanceof HystrixTimeoutException) {
        message = "Timeout";
    } else {
        message = "Service exception";
    }
    return fallbackResponse(message);
}
 
Example #6
Source File: ApiFallbackProvider.java    From springcloud-course with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
    logger.warning(String.format("route:%s,exceptionType:%s,stackTrace:%s", route, cause.getClass().getName(), cause.getStackTrace()));
    String message = "";
    if (cause instanceof HystrixTimeoutException) {
        message = "Timeout";
    } else {
        message = "Service exception";
    }
    return fallbackResponse(message);
}
 
Example #7
Source File: MyFallbackProvider.java    From spring-cloud-docker-microservice-book-code with Apache License 2.0 5 votes vote down vote up
@Override
public ClientHttpResponse fallbackResponse(Throwable cause) {
  if (cause instanceof HystrixTimeoutException) {
    return response(HttpStatus.GATEWAY_TIMEOUT);
  } else {
    return this.fallbackResponse();
  }
}
 
Example #8
Source File: ConnoteCommand.java    From resilient-transport-service with Apache License 2.0 5 votes vote down vote up
@Override
protected ConnoteDTO getFallback() {

    if (secondTry) {
        LOGGER.debug(LOGGER.isDebugEnabled() ? "Second Connote Service Call started" : null);

        ConnoteCommand connoteCommand = new ConnoteCommand(restTemplate, false);
        return connoteCommand.execute();

    } else {

        LOGGER.debug(LOGGER.isDebugEnabled() ? "Fallback Connote Service call" : null);

        ConnoteDTO connoteDTO = new ConnoteDTO();
        connoteDTO.setFallback(true);

        if (getExecutionException() != null) {
            Exception exceptionFromThrowable = getExceptionFromThrowable(getExecutionException());

            if (exceptionFromThrowable == null) {
                connoteDTO.setErrorMsg("Unable to check exception type");

            } else if (exceptionFromThrowable instanceof HystrixRuntimeException) {
                HystrixRuntimeException hystrixRuntimeException = (HystrixRuntimeException) exceptionFromThrowable;
                connoteDTO.setErrorMsg(hystrixRuntimeException.getFailureType().name());

            } else if (exceptionFromThrowable instanceof HystrixTimeoutException) {
                connoteDTO.setErrorMsg(HystrixRuntimeException.FailureType.TIMEOUT.name());
            } else {

                connoteDTO.setErrorMsg(exceptionFromThrowable.getMessage());
            }

        } else
            connoteDTO.setErrorMsg("unable to create connote");
        return connoteDTO;
    }
}
 
Example #9
Source File: BookingCommand.java    From resilient-transport-service with Apache License 2.0 5 votes vote down vote up
@Override
protected BookingServiceResponseDTO getFallback() {

    if (secondTry) {
        LOGGER.debug(LOGGER.isDebugEnabled() ? "Second Booking Service Call started" : null);

        return new BookingCommand(bookingServiceRequestDTO, restTemplate, false).execute();

    } else {

        BookingServiceResponseDTO bookingServiceResponseDTO = new BookingServiceResponseDTO();
        bookingServiceResponseDTO.setFallback(true);

        if (getExecutionException() != null) {
            Exception exceptionFromThrowable = getExceptionFromThrowable(getExecutionException());
            if (exceptionFromThrowable == null) {
                bookingServiceResponseDTO.setErrorMsg("Unable to check exception type");

            } else if (exceptionFromThrowable instanceof HystrixRuntimeException) {
                HystrixRuntimeException hystrixRuntimeException = (HystrixRuntimeException) exceptionFromThrowable;
                bookingServiceResponseDTO.setErrorMsg(hystrixRuntimeException.getFailureType().name());

            } else if (exceptionFromThrowable instanceof HystrixTimeoutException) {
                bookingServiceResponseDTO.setErrorMsg(HystrixRuntimeException.FailureType.TIMEOUT.name());
            } else {

                bookingServiceResponseDTO.setErrorMsg(exceptionFromThrowable.getMessage());
            }
            return bookingServiceResponseDTO;

        } else {

            bookingServiceResponseDTO.setErrorMsg("Error: unable to create booking");
            return bookingServiceResponseDTO;
        }

    }

}
 
Example #10
Source File: AddressCommand.java    From resilient-transport-service with Apache License 2.0 5 votes vote down vote up
@Override
protected AddressResponseDTO getFallback() {

    if (secondTry) {
        LOGGER.debug(LOGGER.isDebugEnabled() ? "Second Address Service Call started" : null);
        // final second call
        AddressCommand addressCommand = new AddressCommand(addressDTO, restTemplate, false);
        return addressCommand.execute();

    } else {
        LOGGER.debug(LOGGER.isDebugEnabled() ? "Fallback Address Service call" : null);

        AddressResponseDTO addressResponseDTO = new AddressResponseDTO();
        addressResponseDTO.setFallback(true);

        if (getExecutionException() != null) {
            Exception exceptionFromThrowable = getExceptionFromThrowable(getExecutionException());
            if (exceptionFromThrowable == null) {
                addressResponseDTO.setErrorMsg("Unable to check exception type");

            } else if (exceptionFromThrowable instanceof HystrixRuntimeException) {
                HystrixRuntimeException hystrixRuntimeException = (HystrixRuntimeException) exceptionFromThrowable;
                addressResponseDTO.setErrorMsg(hystrixRuntimeException.getFailureType().name());

            } else if (exceptionFromThrowable instanceof HystrixTimeoutException) {
                addressResponseDTO.setErrorMsg(HystrixRuntimeException.FailureType.TIMEOUT.name());
            } else {

                addressResponseDTO.setErrorMsg(exceptionFromThrowable.getMessage());
            }
            return addressResponseDTO;
        } else {

            addressResponseDTO.setErrorMsg("Error: unable to validate address");

            return addressResponseDTO;
        }
    }
}
 
Example #11
Source File: CustommerCommand.java    From resilient-transport-service with Apache License 2.0 5 votes vote down vote up
@Override
protected CustomerResponseDTO getFallback() {

    if (secondTry) {
        LOGGER.debug(LOGGER.isDebugEnabled() ? "Second Customer Service Call started" : null);
        // final second call
        return new CustommerCommand(custommerId, restTemplate, false).execute();
    } else {

        CustomerResponseDTO customerReqResponseDTO = new CustomerResponseDTO();
        customerReqResponseDTO.setFallback(true);

        if (getExecutionException() != null) {
            Exception exceptionFromThrowable = getExceptionFromThrowable(getExecutionException());
            if (exceptionFromThrowable == null) {
                customerReqResponseDTO.setErrorMsg("Unable to check exception type");

            } else if (exceptionFromThrowable instanceof HystrixRuntimeException) {
                HystrixRuntimeException hystrixRuntimeException = (HystrixRuntimeException) exceptionFromThrowable;
                customerReqResponseDTO.setErrorMsg(hystrixRuntimeException.getFailureType().name());

            } else if (exceptionFromThrowable instanceof HystrixTimeoutException) {
                customerReqResponseDTO.setErrorMsg(HystrixRuntimeException.FailureType.TIMEOUT.name());
            } else {

                customerReqResponseDTO.setErrorMsg(exceptionFromThrowable.getMessage());
            }
            return customerReqResponseDTO;

        } else {
            customerReqResponseDTO.setErrorMsg("Error: unable to get customer");
            return customerReqResponseDTO;
        }

    }
}
 
Example #12
Source File: GatewayServiceFallback.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
    if (cause instanceof HystrixTimeoutException) {
        return new GatewayClientResponse(HttpStatus.GATEWAY_TIMEOUT, DEFAULT_MESSAGE);
    } else {
        return new GatewayClientResponse(HttpStatus.INTERNAL_SERVER_ERROR, DEFAULT_MESSAGE);
    }
}
 
Example #13
Source File: WeatherServiceFallback.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
    if (cause instanceof HystrixTimeoutException) {
        return new GatewayClientResponse(HttpStatus.GATEWAY_TIMEOUT, DEFAULT_MESSAGE);
    } else {
        return new GatewayClientResponse(HttpStatus.INTERNAL_SERVER_ERROR, DEFAULT_MESSAGE);
    }
}
 
Example #14
Source File: GatewayServiceFallbackUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void testFallbackResponse_whenHystrixException_thenGatewayTimeout() throws Exception {
    HystrixTimeoutException exception = new HystrixTimeoutException();
    ClientHttpResponse response = fallback.fallbackResponse(ROUTE, exception);

    assertEquals(HttpStatus.GATEWAY_TIMEOUT, response.getStatusCode());
}
 
Example #15
Source File: WeatherServiceFallbackUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void testFallbackResponse_whenHystrixException_thenGatewayTimeout() throws Exception {
    HystrixTimeoutException exception = new HystrixTimeoutException();
    ClientHttpResponse response = fallback.fallbackResponse(ROUTE, exception);

    assertEquals(HttpStatus.GATEWAY_TIMEOUT, response.getStatusCode());
}