MicroProfile
This example walks you through how to use MicroProfile Fault Tolerance in the Helidon Project.
Fault Tolerance MicroProfile feature
MicroProfile brings to Java/Jakarta EE ecosystem resilient features as Circuit Breakers, Timeout, Fallback and Retry.
Example
Running the application
mvn clean package && java -jar target/helidon-microprofile.jar
How does it work?
When statusOfDayByAccuWeather
is invoked, it calls longRunningTask
that hangs for 80ms which causes the interruption of statusOfDayByAccuWeather
due to the threshold of @Timeout
annotation that is set up for 50ms only. When the interruption happens, an exception is thrown and it actives the Retry Policy
which detects that an error happened
and repeat the same operation accordingly to the value defined by maxRetries
.
As longRunningTask
is meant to fail by our timeout criteria, @Fallback
annotation take place with a fallback method
to ensure that the operation has another chance to concluded successfully.
@ApplicationScoped
public class WeatherGateway {
..
@Timeout(50)
@Retry(maxRetries = 3)
@Fallback(fallbackMethod = "statusOfWeekByMetEireann")
@CircuitBreaker(requestVolumeThreshold=2, failureRatio=0.5, successThreshold=2)
public String statusOfDayByAccuWeather(){
return longRunningTask();
}
..
public String statusOfWeekByMetEireann(){
LOGGER.log(Level.WARNING, "MetEireann backup service has been requested due to AccuWeather timeout");
return "A beautiful day";
}
..
private String longRunningTask(){
try {
Thread.sleep(80);
} catch (InterruptedException e) {
LOGGER.log(Level.WARNING,"AccuWeather task has been interrupted.");
}
return null;
}
}
Day status call
GET http://localhost:8080/weather/statusOfDay
Server log
INFO ...CommandRetrier : About to execute command with key statusOfDayByAccuWeather361059497
INFO ...FaultToleranceCommand : Enter: breaker for statusOfDayByAccuWeather361059497 in state CLOSED_MP
> WARNING io.jventura.weather.WeatherGateway : AccuWeather task has been interrupted.
INFO ...CommandRetrier : About to execute command with key statusOfDayByAccuWeather361059497
INFO ...FaultToleranceCommand : Enter: breaker for statusOfDayByAccuWeather361059497 in state CLOSED_MP
> WARNING io.jventura.weather.WeatherGateway : AccuWeather task has been interrupted.
INFO ...CommandRetrier : About to execute command with key statusOfDayByAccuWeather361059497
INFO ...FaultToleranceCommand : Enter: breaker for statusOfDayByAccuWeather361059497 in state CLOSED_MP
> WARNING io.jventura.weather.WeatherGateway : AccuWeather task has been interrupted.
INFO ...CircuitBreakerHelper : Circuit breaker for statusOfDayByAccuWeather361059497 now in state OPEN_MP
INFO ...FaultToleranceCommand : Attempting to trip circuit breaker for command statusOfDayByAccuWeather361059497
INFO ...FaultToleranceCommand : Attempt to manually open breaker failed for command statusOfDayByAccuWeather36105949
INFO ...CommandRetrier : About to execute command with key statusOfDayByAccuWeather361059497
INFO ...FaultToleranceCommand : Enter: breaker for statusOfDayByAccuWeather361059497 in state OPEN_MP
> WARNING io.jventura.weather.WeatherGateway : MetEireann backup service has been requested due to AccuWeather timeout
Response
A beautiful day!
Running the test
You can also try it out using the WeatherTest.java available in the project.
mvn clean test
[INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0