rzrelyea rzrelyea - 5 months ago 45
Java Question

How to force a Hystrix circuit breaker open?

I would like to programmatically force a circuit breaker to open for a particular group. I thought I might be able to do that by setting the config on a command in a group to force open, and running that command. However, that doesn't seem to work. Is this possible? Should I take a different approach? Here's the test I tried that fails on the 2nd assertEquals call.

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class ForceCircuitBreakerCommandTest {

@Test
public void testForceOpen(){

assertEquals(Boolean.TRUE, new FakeCommand().execute());

new OpenCircuitBreakerCommand().execute();

assertEquals(Boolean.FALSE, new FakeCommand().execute());

}

private class FakeCommand extends HystrixCommand<Boolean> {

public FakeCommand(){
super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestGroup")));
}

@Override
public Boolean run(){return Boolean.TRUE;}

@Override
public Boolean getFallback() {return Boolean.FALSE;}
}

private class OpenCircuitBreakerCommand extends HystrixCommand<Boolean> {

public OpenCircuitBreakerCommand(){
super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestGroup"))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
.withCircuitBreakerForceOpen(true)));
}

@Override
public Boolean run(){return Boolean.TRUE;}

@Override
public Boolean getFallback() {return Boolean.FALSE;}
}
}

Answer

I have set custom properties such as "hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen" using

import com.netflix.config.ConfigurationManager;

ConfigurationManager.getConfigInstance()
    .setProperty("hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen",
    true);

ConfigurationManager is the Archaius instance that is used internally.

Comments