Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circuit breaker design pattern implementation

I have tried unsuccessfully to implement circuit breaker pattern, here, in Java using Spring framework.

How can you implement circuit breaker pattern by Java and Spring?

like image 215
user2585494 Avatar asked May 17 '15 09:05

user2585494


4 Answers

For a simple, straightforward circuit breaker implementation, check out Failsafe. Ex:

CircuitBreaker breaker = new CircuitBreaker()
  .withFailureThreshold(5)
  .withSuccessThreshold(3)
  .withDelay(1, TimeUnit.MINUTES);

Failsafe.with(breaker).run(() -> connect());

Doesn't get much simpler.

like image 123
Jonathan Avatar answered Oct 23 '22 12:10

Jonathan


Apache commons has some implementations for several types of lightweight circuit breakers, here's a link to the docs

The project provides the EventCountCircuitBreaker and ThresholdCircuitBreaker classes, and an abstract AbstractCircuitBreaker so you could implement your own.

The code is open sources and is hosted at github, so anyone attempting to implement the pattern should at least take a peek.

like image 43
svarog Avatar answered Nov 16 '22 08:11

svarog


Spring cloud provides some interesting integration with Hystrix. You should probably have a look into it...

like image 5
Guillaume Avatar answered Nov 16 '22 07:11

Guillaume


Regarding the pattern itself

You can obtain a lot of useful information about this pattern at Martin Fowler's blog. It contains ruby implementation as well as references for implementation in other languages.

Regarding the java spring implementation

Please check the JRugged library. It contains the Circuit Breaker implementation in spring as well as other design patterns.

like image 3
Dawid Bugajewski Avatar answered Nov 16 '22 08:11

Dawid Bugajewski