How to make the Hystrix switch open?

I would like to programmatically make the circuit breaker open for a specific group. I thought I could do this by setting the configuration in the team in the group to force open it and run this command. However, this does not work. Is it possible? Should I take a different approach? Here the test I tried fails in the second call to assertEquals.

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;} } } 
+6
source share
3 answers

I set custom properties like "hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen" using

 import com.netflix.config.ConfigurationManager; ConfigurationManager.getConfigInstance() .setProperty("hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen", true); 

ConfigurationManager is an instance of Archaius that is used internally.

+13
source

You do not have to use the ConfigurationManager . This test should say:

 @Test public void testForceOpen() { assertEquals(Boolean.TRUE, new FakeCommand().execute()); assertEquals(Boolean.FALSE, new OpenCircuitBreakerCommand().execute()); } 
0
source

This is a test edit using Senthilkumar Gopal answer

 @Test public void testForceOpen() { assertEquals(Boolean.TRUE, new OpenCircuitBreakerCommand().execute()); ConfigurationManager.getConfigInstance() .setProperty("hystrix.command.OpenCircuitBreakerCommand.circuitBreaker.forceOpen", true); assertEquals(Boolean.FALSE, new OpenCircuitBreakerCommand().execute()); } 
0
source

Source: https://habr.com/ru/post/984044/


All Articles