How to force a circuit breaker to open?

1,295 views
Skip to first unread message

Rebecca Relyea

unread,
Mar 20, 2015, 7:32:04 AM3/20/15
to hystr...@googlegroups.com
Hello,

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?  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;}
}
}
Message has been deleted

Senthilkumar Gopal

unread,
Mar 20, 2015, 12:30:05 PM3/20/15
to hystr...@googlegroups.com
Hi Rebecca,
 
 I have set custom properties such as "hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen" using ConfigurationManager.getConfigInstance().setProperty("hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen", true); Configuration Manager is the Archaius instance that is used internally "import com.netflix.config.ConfigurationManager;"

Rebecca Relyea

unread,
Mar 20, 2015, 4:11:41 PM3/20/15
to hystr...@googlegroups.com
That worked - thank you!  If you would like to post your answer on Stackoverflow, where I asked the same question, I will accept your answer.  Here's my StackOverflow question: http://stackoverflow.com/questions/29165654/how-to-force-a-hystrix-circuit-breaker-open

And here is my new passing test:

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

import com.netflix.config.ConfigurationManager;


public class ForceCircuitBreakerCommandTest {

@Test
public void testForceOpen(){

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

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


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;}
}
}

Senthilkumar Gopal

unread,
Mar 20, 2015, 11:19:38 PM3/20/15
to Rebecca Relyea, hystr...@googlegroups.com
Hi Rebecca,
  Glad to hear that worked. I have replied on SO as well. good luck!

Thanks
senthilkumar g

--
You received this message because you are subscribed to a topic in the Google Groups "HystrixOSS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/hystrixoss/D9sWSE8RvZc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to hystrixoss+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages