CircuitBreakerPanel with lagom


Up to v1.3.x, you could use circuit breaker pattern with lagom only in service descriptor usually. To apply circuit breaker to some arbitrary API you had to generally use the underlying akka circuit breaker . How to use circuit breaker with unmanaged services is explained pretty well at https://blog.knoldus.com/2017/06/15/lagom-circuit-breaker-what-why-and-how/  and if you want to poke around with the underlying akka circuit breaker you can take a look at https://doc.akka.io/docs/akka/2.5/common/circuitbreaker.html.

To use a circuitbreaker with some arbitrary API ( for e.g. a database API like couchbase) with lagom 1.4.x you can use CircuitBreakerPanel. The sole method in this class allows you to write code like –


circuitbreaker.withCircuitBreaker("circuit-breaker-name", CompletableFuture.supplyAsync(() -> yourCode);

With correct configuration defined for “circuit-breaker-name”, you can leverage the circuit breaker pattern easily with arbitrary API calls. If your CompletionStage throws any error then it counts towards your CircuitBreaker going to an open state.

Now, you might not want your CircuitBreaker to go into an open state for certain type of exceptions ( say there is some sort of validation going inside your CompletionStage). For this, you can whitelist certain exceptions using the field “lagom.circuit-breaker.circuit-breaker-name.exception-whitelist” as shown below –

lagom.circuit-breaker {

  breakerA {
    max-failures = 3
    reset-timeout = 20s
    exception-whitelist = ["java.lang.NullPointerException"]
  }

}

 

It accepts an array of strings in which you can define all the exceptions for which you don’t want the circuit breaker to go towards an open state. I have created a minimal example to showcase how to leverage CircuitBreakerPanel along with exception whitelisting at  –

Demo Repo

 

knoldus-advt-sticker

 

This entry was posted in Java and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s