Java Code Examples for net.jodah.failsafe.CircuitBreaker#isOpen()

The following examples show how to use net.jodah.failsafe.CircuitBreaker#isOpen() . 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: CircuitBreakerManager.java    From heimdall with Apache License 2.0 6 votes vote down vote up
public <T> T failsafe(Callable<T> callable, Long operationId, String operationPath) {
	CircuitBreakerHolder circuitBreakerHolder = getCircuitHolder(operationId, circuits);
	CircuitBreaker circuitBreaker = circuitBreakerHolder.getCircuitBreaker();
	
	if (circuitBreaker.isOpen()) {
		return Failsafe.with(circuitBreaker)
				.withFallback(() ->  {
					String body = logAndCreateBody("CircuitBreaker ENABLED | Operation: {0}, Exception: {1}",
							operationPath,
                               circuitBreakerHolder.getMessage());

					RequestContext context = RequestContext.getCurrentContext();
					context.setSendZuulResponse(false);
					context.setResponseStatusCode(HttpStatus.SERVICE_UNAVAILABLE.value());
					context.setResponseBody(body);
					context.addZuulResponseHeader(ConstantsContext.CIRCUIT_BREAKER_ENABLED, "enabled");
					context.getResponse().setContentType(MediaType.APPLICATION_JSON_VALUE);
				})
				.get(callable);
	}

	return Failsafe.with(circuitBreaker)
			.onFailure((ignored, throwable) -> circuitBreakerHolder.setThrowable(throwable))
			.get(callable);
}
 
Example 2
Source File: CircuitBreakerManager.java    From heimdall with Apache License 2.0 6 votes vote down vote up
public <T> T failsafe(Callable<T> callable, String url) {
	CircuitBreakerHolder circuitBreakerHolder = getCircuitHolder(url, middlewareCircuits);
	CircuitBreaker circuitBreaker = circuitBreakerHolder.getCircuitBreaker();

	if (circuitBreaker.isOpen()) {
		return Failsafe.with(circuitBreaker)
				.withFallback(() -> {

					String body = logAndCreateBody("CircuitBreaker ENABLED | URL: {0}, Exception: {1}",
							url,
                               circuitBreakerHolder.getMessage());

					return ResponseEntity
							.status(HttpStatus.SERVICE_UNAVAILABLE.value())
							.header(ConstantsContext.CIRCUIT_BREAKER_ENABLED, "enabled")
							.body(body);

				}).get(callable);
	}

	return Failsafe.with(circuitBreaker)
			.onFailure((ignored, throwable) -> circuitBreakerHolder.setThrowable(throwable))
			.get(callable);
}