I am using Play! 2.5 (Java) and Akka. Here's my problem: I am trying to call an external service using WSClient and would like it to be behind a Akka circuit breaker. The code I have shown below doesn't seem to work as expected.
I would expect the circuit breaker to open when the external service is not reachable, but it doesn't. I am guessing the problem is in wrapping a java 8 lambda expression returning a completableFuture inside a akaa.dispatch.Future.future .
Does the code I have look correct? If there is something wrong how can i fix it?
@Inject
public ServiceActor(WSClient ws) {
this.ws = ws;
circuitBreaker = new CircuitBreaker(getContext().dispatcher(),
getContext().system().scheduler(),
MAX_FAILURES,
CALL_TIMEOUT,
RESET_TIMEOUT
)
.onOpen(this::onOpen)
.onClose(this::onClose)
.onHalfOpen(this::onHalfOpen);
receive(ReceiveBuilder
.match(String.class, x -> {
circuitBreaker.callWithCircuitBreaker(() -> future(() -> callService(),getContext().dispatcher()));
})
.matchAny(o -> log.info("Unknown message"))
.build()
);
}
private CompletableFuture<JsonNode> callService() {
return ws.url(SOME_URL).get()
.thenApply(WSResponse::asJson)
.toCompletableFuture();
}
If i simply throw a runtime expression in callService without doing anything like:
private CompletableFuture<JsonNode> callService() {
throw new RuntimeException()
}
Now, the circuit-breaker opens.