Basic Usage - ThatJoeMoore/HysNotes GitHub Wiki

Note that all of these examples can be seen in action in the Unit Tests.

The most basic usage of HysNotes is easy - just annotate an interface with @HysCommands:

package your.company;

import com.thatjoemoore.hystrix.annotations.HysCommands;

@HysCommands
public interface RemoteService {

    String doSomething(String input);
    int somethingElse(int one, int two);

}

source

This will generate three classes in your.company: RemoteServiceDoSomethingCommand, RemoteServiceSomethingElseCommand, and RemoteServiceHystrixFacade.

RemoteServiceDoSomethingCommand and RemoteServiceSomethingElseCommand are both implementations of HystrixCommand which will delegate to RemoteService.doSomething and RemoteService.somethingElse, respectively. Both have constructors which accept an instance of RemoteService, followed by their respective arguments.

You can invoke them like so:

    RemoteService svc = new MyRealImplementation();

    String something = new RemoteServiceDoSomethingCommand(svc, "hello, world!").execute();

    int somethingElse = new RemoteServiceSomethingElseCommand(svc, 42, 43).execute();

Easy! But wait, there's more!

RemoteServiceHystrixFacade is an implementation of RemoteService which, when invoked, will create instances of RemoteServiceDoSomethingCommand or RemoteServiceSomethingElseCommand and invoke them using execute() to get an immediate response.

So now, you can invoke your remote service like so:

    RemoteService svc = new MyRealImplementation();
    RemoteService enhanced = new RemoteServiceHystrixFacade(svc);

    String something = enhanced.doSomething("hello, world!");
    int somethingElse = enhanced.somethingElse(42, 43);

This is especially powerful when used in conjunction with some form of Inversion of Control container, where you can easily swap out the real implementation in favor of the Hystrix-enhanced one. For advanced IoC usage, see the section that doesn't exist yet.

#Look, ma, no interfaces! In addition to interfaces, HysNotes is capable of generating one-off commands and wrappers around concrete methods:

@HysCommands
public class ClassLevel {

    public String sayHi() {
        return "Hello!";
    }

}

source

This will generate ClassLevelSayHiCommand and ClassLevelHystrixFacade. These are similar to the output from an annotated interface, except that the HystrixFacade does not extends the original class.

In addition, you can skip the class-level annotation and use the method-level @HysCommand to just generate commands for one or more methods. This will not result in the generation of a facade class, just the command classes.

public class MethodLevel {

    @HysCommand
    public String riskyCall(int arg) {
        return Integer.toBinaryString(arg);
    }

    @HysCommand
    public String aGamble() {
        return "Cha-Ching!";
    }

    public int safeBet(String arg) {
        return arg.length();
    }

}

source

This will generate MethodLevelRiskyCallCommand and MethodLevelAGambleCommand. Notice that a facade class was not generated, just the two command classes.

Next: Configuration

⚠️ **GitHub.com Fallback** ⚠️