-
Notifications
You must be signed in to change notification settings - Fork 1
Home
Dimos Raptis edited this page Aug 12, 2017
·
10 revisions
ServiceCall4j is a library, providing resiliency capabilities over RPCs (Remote Procedure Calls) in a declarative way. The resiliency capabilities provided by the library are the following:
- Caching
- Monitoring
- Retrying
- Timeout
- Throttling
- Circuit Breaker
ServiceCall4j is published through Maven Central and is available as a Maven/Gradle dependency. You can install it using the following code:
- Maven
<dependency>
<groupId>com.github.dimosr</groupId>
<artifactId>ServiceCall4j</artifactId>
<version>1.1.0</version>
</dependency>
- Gradle
compile 'com.github.dimosr:ServiceCall4j:1.1.0'
- To decorate one of your RPCs with ServiceCall4j capabilities, you will first have to make sure that the RPC is compliant with the
ServiceCall
interface provided by the library. If it does not, you can simply do so by creating a simple wrapper over it. TheServiceCall
interface is the following:
public interface ServiceCall<REQUEST, RESPONSE> {
RESPONSE call(REQUEST request);
}
- After you have made your RPC compliant with this interface, you can use the
Builder
provided by the library to add the required capabilities on top of your RPC. You also have to provide a callID, which will be appended in the emitted logs/metrics, so that you can differentiate between your various calls. For example:
public class MyAdjustedHelloWorldCall implements ServiceCall<String, String> {
String call(String input) {
return "Hello " + input;
}
}
...
ServiceCall<String, String> enhancedHelloWorldCall = new ServiceCallBuilder<>(new MyAdjustedHelloWorldCall(), "hello-world-call")
.withCircuitBreaker(15, 5, 3, 300)
.withCache(cache)
.withMonitoring((i, d) -> System.out.println("Duration: " + d.toMillis()))
.withTimeouts(Duration.ofMillis(1), TimeUnit.MILLISECONDS, Executors.newFixedThreadPool(10))
.withThrottling(100)
.withRetrying(false, 2)
.build();
You can find detailed information for each capability in the documentation page for each feature.
- Now you are ready to perform your calls and every request will be processed by each intermediate capability automatically and transparently:
String response = enhancedHelloWorldCall.call("World");
- Contributing