Hystrix Circuit Breaker With Java8 - No Fluff Just Stuff

Hystrix Circuit Breaker With Java8

Posted by: Demian Neidetcher on June 3, 2015

It'll continue to be interesting to see what happens with Java8. Sure the version is out there, you can write all the lambdas you want. But it's going to get more fun to see how every day coders use it and how library authors use it.

During the day I use Groovy, enjoy it's closures. I still do a bit of Scala but not as much as I'd like. But at night I've been working with Java8.

Generic Hystrix Circuit Breaker

From what I understand you should encapsulate the code that talks to remote systems inside of a class that implements a HystrixCommand. I had a situation where there were a lot of little calls to a back-end system. Creating new classes for each call was getting tedious. So I endeavored to create a generic circuit breaker (implementation of HystrixCommand) for each dependent system.

You just give it 2 suppliers, one for the actual thing you're trying to do and another as a fallback if what you're trying to do fails. The suppliers have to adhere to the normal contract with circuit breakers. If an exception bubbles up from one of your lambdas then the circuit breaker trips.

public class DatabaseCommand<Out> extends HystrixCommand<Out> {

    private Supplier<Out> runSupplier;
    private Supplier<Out> fallbackSupplier;

    public DatabaseCommand(
            Supplier<Out> runSupplierIn,
            Supplier<Out> fallbackSupplierIn) {
        super(CommandProvider.getSetter(CommandProvider.DB_COMMAND_KEY));

        runSupplier = runSupplierIn;
        fallbackSupplier = fallbackSupplierIn;
    }

    @Override protected Out run() throws Exception {
        return runSupplier.get();
    }

    @Override protected Out getFallback() {
        return fallbackSupplier.get();
    }
}

There's some hand waving with the CommandProvider.getSetter(), that's the monster set of calls where you configure your circuit breaker.

Using it

Then when you want to use the circuit breaker it looks something like this.

DatabaseCommand<Customer> databaseCommand = new DatabaseCommand<>(
         () -> databaseClient.findCustomer(customerId),
         () -> databaseClient.findCustomerFromCache(findCustomerId);

return databaseCommand.execute();

This isn't so bad. Sure it's convenient that I have those suppliers wrapped inside of functions already so I don't have nasty multi-line lambdas.

Demian Neidetcher

About Demian Neidetcher

Demian Neidetcher is a Senior Engineer at Time Warner Cable working on customer portals and getting television content to IP devices. He first got the programming bug staying up late nights with his Commodore64.

He has been professionally writing software for over 15 years. Most of his experience is with JVM languages (Java, Scala, Groovy) in the telecommunications domain doing things like inventory systems for a long-haul carrier, integrating conferencing software and routing VoIP traffic including geo-spatial 911 call routing. He has worked for companies ranging from Fortune 500 to small start-ups. In every environment Demian has looked for pragmatic approaches, solutions and process to get teams delivering software that benefits users.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »