diff --git a/hystrix-core/src/main/java/com/netflix/hystrix/Hystrix.java b/hystrix-core/src/main/java/com/netflix/hystrix/Hystrix.java index db475559b..dd2ae13c6 100644 --- a/hystrix-core/src/main/java/com/netflix/hystrix/Hystrix.java +++ b/hystrix-core/src/main/java/com/netflix/hystrix/Hystrix.java @@ -5,6 +5,7 @@ import java.util.concurrent.TimeUnit; import com.netflix.hystrix.strategy.HystrixPlugins; +import com.netflix.hystrix.strategy.properties.HystrixPropertiesFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -58,6 +59,7 @@ private static void _reset() { // clear circuit breakers HystrixCircuitBreaker.Factory.reset(); HystrixPlugins.reset(); + HystrixPropertiesFactory.reset(); } private static ThreadLocal> currentCommand = new ThreadLocal>() { diff --git a/hystrix-core/src/main/java/com/netflix/hystrix/strategy/properties/HystrixPropertiesFactory.java b/hystrix-core/src/main/java/com/netflix/hystrix/strategy/properties/HystrixPropertiesFactory.java index 83beb8ad9..94edfec75 100644 --- a/hystrix-core/src/main/java/com/netflix/hystrix/strategy/properties/HystrixPropertiesFactory.java +++ b/hystrix-core/src/main/java/com/netflix/hystrix/strategy/properties/HystrixPropertiesFactory.java @@ -36,6 +36,15 @@ */ public class HystrixPropertiesFactory { + /** + * Clears all the defaults in the static property cache. This makes it possible for property defaults to not persist for + * an entire JVM lifetime. May be invoked directly, and also gets invoked by Hystrix.reset() + */ + public static void reset() { + commandProperties.clear(); + threadPoolProperties.clear(); + } + // String is CommandKey.name() (we can't use CommandKey directly as we can't guarantee it implements hashcode/equals correctly) private static final ConcurrentHashMap commandProperties = new ConcurrentHashMap(); diff --git a/hystrix-core/src/test/java/com/netflix/hystrix/HystrixTest.java b/hystrix-core/src/test/java/com/netflix/hystrix/HystrixTest.java index 030bfa16f..f9d834c65 100644 --- a/hystrix-core/src/test/java/com/netflix/hystrix/HystrixTest.java +++ b/hystrix-core/src/test/java/com/netflix/hystrix/HystrixTest.java @@ -168,4 +168,31 @@ protected Boolean run() { assertNull(Hystrix.getCurrentThreadExecutingCommand()); } + //see https://github.com/Netflix/Hystrix/issues/280 + @Test + public void testResetCommandProperties() { + HystrixCommand cmd1 = new ResettableCommand(100, 10); + assertEquals(100L, (long) cmd1.getProperties().executionIsolationThreadTimeoutInMilliseconds().get()); + assertEquals(10L, (long) cmd1.threadPool.getExecutor().getCorePoolSize()); + + Hystrix.reset(); + + HystrixCommand cmd2 = new ResettableCommand(700, 40); + assertEquals(700L, (long) cmd2.getProperties().executionIsolationThreadTimeoutInMilliseconds().get()); + assertEquals(40L, (long) cmd2.threadPool.getExecutor().getCorePoolSize()); + + } + + private static class ResettableCommand extends HystrixCommand { + ResettableCommand(int timeout, int poolCoreSize) { + super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("GROUP")) + .andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionIsolationThreadTimeoutInMilliseconds(timeout)) + .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(poolCoreSize))); + } + + @Override + protected Boolean run() throws Exception { + return true; + } + } }