DEV Community

Cover image for HystrixCommand Example
javaknowledgehub
javaknowledgehub

Posted on

HystrixCommand Example

HystrixCommand is the central part of the Hystrix library. If your application has a risky code that may fail or may have high latency. Then HystrixCommand should wrap such code. In this post, we will create a HystrixCommand Example and understand it’s synchronous and asynchronous execution.
Checkout full post here
Alt text of image

In this post, we will create a HystrixCommand example that will.
1) Run a risky code that may fail, or exhibit high latency.
2) Will have a timeout defined. If our function does not return a response within this period then it is assumed to have failed.
3) Fallback function, which will execute in case of timeout or failure.
4) Run the risky code synchronously and asynchronously.

Let us take a look at code below:

package com.tkh.hystrix;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;

public class SampleCommand extends HystrixCommand<String>
{
    private String name;
    SampleCommand(String name) 
    {
        super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"),5000);
        this.name = name;
    }

    @Override
    public String run() throws InterruptedException
    {
        Thread.sleep(1000);
        System.out.println("after 1 second");
        Thread.sleep(1000);
        System.out.println("after 2 seconds");
        Thread.sleep(1000);
        System.out.println("after 3 seconds");
        Thread.sleep(1000);
        System.out.println("after 4 seconds");
        return name+" executed successfully";
    }

    @Override
    public String getFallback()
    {
        return name+" execution failed";
    }
}

SampleCommand.java

package com.tkh.hystrix;


public class HystrixApplication {

    public static void main(String[] args) throws InterruptedException {
        SampleCommand sync=new SampleCommand("Sync");
        System.out.println(sync.execute());
        Thread.sleep(1000);
        System.out.println("1 second");
        Thread.sleep(1000);
        System.out.println("2 seconds");
        Thread.sleep(1000);
        System.out.println("3 seconds");
        Thread.sleep(1000);
        System.out.println("4 seconds");
        SampleCommand async=new SampleCommand("Async");
        System.out.println(async.queue());
        Thread.sleep(1000);
        System.out.println("1 second");
        Thread.sleep(1000);
        System.out.println("2 seconds");
        Thread.sleep(1000);
        System.out.println("3 seconds");
        Thread.sleep(1000);
        System.out.println("4 seconds");
    }

}

HystrixApplication.java

SampleCommand is a HystrixCommand that protects the code that may have latency or may fail. The code that may fail is written in run() function.

We need to do the following to create a HystrixCommand:

  1. Extend HystrixCommand to the class.
  2. Make a super call in the constructor of HystrixCommand, to specify the HystrixCommandGroupKey and timeout duration.
  3. Override the run function, it will contain the code that we need to protect from failure.
  4. Override getFallback function. This function executes when a protected code fails.

Alt text of image
If we check SampleCommand from above. Following points can be noted down.

  1. SampleCommand class has extended HystrixCommand
  2. HystrixCommand constructor is called inside SampleCommad constructor. It takes in the input of Hystrix Command Group Key and timeout in milliseconds.
  3. Timeout set in 5 seconds.
  4. run() function is created. It will take ~4 seconds to execute.
  5. getFallBack() method is also defined. It will return a failure message.

If check HystrixApplication class from above, we can note down the following points:

  1. Two SampleCommand objects have been created
  2. The first one is ‘sync’ it is the blocking process. It has been invoked by. sync.execute().
  3. The second object is ‘async’ it is the nonblocking process. It will be invoked by. async.queue()

Blocking Process: This process will block the execution of the method from which it is invoked. The invoking process will only resume it’s execution once the invoked method has finished its execution.

Non-Blocking Process: This process will not block the execution of the method. The invoking and the invoked process will execute parallelly.

Checkout HystrixCommand documentation for a detailed explanation of function here
Original Post here

Top comments (0)