Skip to content

dimosr/service-call-4j

Repository files navigation

Protect your RPCs with ServiceCall4j

Build Status Coverage Status SonarQube Analysis Maven Central

Service-Call-4j

A library for adding resiliency capabilities to your RPCs (Remote Procedure Calls) in a declarative way. The capabilities provided by this library are the following:

  • Caching
  • Monitoring
  • Retrying
  • Timeout
  • Throttling
  • Circuit Breaker

Install

  • 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'

Getting Started

  1. Make sure the call you want to enhance implements the ServiceCall interface provided by Service-Call-4j:
public interface ServiceCall<REQUEST, RESPONSE> {
    RESPONSE call(REQUEST request);
}

...

public class MyAdjustedHelloWorldCall implements ServiceCall<String, String> {
	String call(String input) {
		return "Hello " + input;
	}
}
  1. Use the provided Builder to build your enhanced ServiceCall:
ServiceCall<String, String> enhancedHelloWorldCall = new ServiceCallBuilder<>(new MyAdjustedHelloWorldCall())
                .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();
  1. Perform your calls
String response = enhancedHelloWorldCall.call("World");

More Info

Check the project's Wiki for more documentation about how each capability can be used.
This Wiki also contains a FAQ section, describing what's the difference between this library and alternatives, such as Hystrix.

If you want a quick demo of how you can use the library, check this.