Getting started - Pscheidl/FortEE GitHub Wiki
FortEE makes sure no unexpected exceptions or errors get past methods guarded by FortEE interceptors.
To start using FortEE, two steps are required.
- Add FortEE dependency to your project
- Use FortEE annotations
FortEE is released and published into Maven central repository.
<dependency>
<groupId>com.github.pscheidl</groupId>
<artifactId>fortee</artifactId>
<version>1.1.0</version>
</dependency>
compile 'com.github.pscheidl:fortee:1.1.0'
Basic fault tolerance mechanism leveraging java.util.Optional<T>
. The underlying method either did return a value or did not.
- Methods annotated with @Failsafe must return Optional. This is checked at startup-time. If this condition is not met, exception is thrown during startup phase with details about which methods failed the test.
- Beans annotated with @Failsafe must enforce this Optional return type on all declared methods.
@Named
public class ServiceImplementation implements SomeService {
// Will return Optional.empty()
@Failsafe
public Optional<String> maybeFail(){
throw new RuntimeException("Failed on purpose");
}
}
@Named
public class ExecutionErrorObserver {
public void observe(@Observes ExecutionErrorEvent executionError){
// Do whatever is needed e.g. log the Throwable cause
}
}
The @Semisafe
annotation allows listing Throwables allowed to be thrown.
- Methods annotated with @Semisafe({}) must return Optional. This is checked at startup-time. If this condition is not met, exception is thrown during startup phase with details about which methods failed the test.
- Methods annotated with @Semisafe must enforce this Optional return type on all declared methods.
@Named
public class ServiceImplementation implements SomeService {
// Will end with RuntimeException
@Semisafe({RuntimeException.class})
public Optional<String> maybeFail(){
throw new RuntimeException("Failed on purpose");
}
}
@Named
public class ServiceImplementation implements SomeService {
// Will return Optional.empty()
@Semisafe({RuntimeException.class})
public Optional<String> maybeFail(){
throw new AnyUnlistedException("This exception will be converted");
}
}