From b1dd4669ed8760c28c05bb051ba3933b8127fb78 Mon Sep 17 00:00:00 2001 From: "Konstantin A. Zolotukhin" Date: Wed, 27 May 2015 21:48:40 +0300 Subject: [PATCH] command execution timeout value bug fixed --- .../HystrixSimpleFailoverLine.java | 22 +++++++------- ...ystrixSimpleFailoverLineConfiguration.java | 29 +++++++++++++++++-- src/main/resources/application.properties | 2 +- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/ametiste/redgreen/application/HystrixSimpleFailoverLine.java b/src/main/java/org/ametiste/redgreen/application/HystrixSimpleFailoverLine.java index 8159545..5170bfa 100644 --- a/src/main/java/org/ametiste/redgreen/application/HystrixSimpleFailoverLine.java +++ b/src/main/java/org/ametiste/redgreen/application/HystrixSimpleFailoverLine.java @@ -43,15 +43,18 @@ * of this implementation. *

* - *

- * Command execution timeout can be modified using application - * property redgreen.hystrix.simpleFailoverLine.commandTimeout ( note, a value should be defined in millis ). - *

- * * @since 0.1.0 */ public class HystrixSimpleFailoverLine implements FailoverLine { + /** + * Defines {@code Hystrix} command key, this constant can be used as the reference + * to this line execution command. + * + * @since 0.1.0 + */ + public static final String HYSTRIX_COMMAND_KEY = "SimpleFailoverLineExecution"; + // NOTE: ResourceHttpMessageConverter used to handle all possible content-types private RestTemplate restTemplate = new RestTemplate( Arrays.asList(new ResourceHttpMessageConverter()) @@ -65,12 +68,9 @@ public HystrixSimpleFailoverLine(RedgreenBundleRepostitory redgreenBundleReposti this.bundleRepostitory = redgreenBundleRepostitory; } - @HystrixCommand(fallbackMethod = "performFallback", + @HystrixCommand(commandKey=HYSTRIX_COMMAND_KEY, fallbackMethod = "performFallback", commandProperties = { - @HystrixProperty(name="circuitBreaker.enabled", - value="false"), - @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", - value="${redgreen.hystrix.simpleFailoverLine.commandTimeout}") + @HystrixProperty(name="circuitBreaker.enabled", value="false"), } ) @Override @@ -96,7 +96,7 @@ public Object performFallback(Supplier requestSupplier) { return redgreenBundle.mapRedResources((r) -> doSafeResourceRequest(r, requestSupplier.get())) .filter((r) -> r != null) .findFirst() - // TODO: define specific exception + // TODO: define specific exception .orElseThrow(RuntimeException::new); } diff --git a/src/main/java/org/ametiste/redgreen/configuration/HystrixSimpleFailoverLineConfiguration.java b/src/main/java/org/ametiste/redgreen/configuration/HystrixSimpleFailoverLineConfiguration.java index 5c8df7b..81d8e1e 100644 --- a/src/main/java/org/ametiste/redgreen/configuration/HystrixSimpleFailoverLineConfiguration.java +++ b/src/main/java/org/ametiste/redgreen/configuration/HystrixSimpleFailoverLineConfiguration.java @@ -1,9 +1,11 @@ package org.ametiste.redgreen.configuration; +import com.netflix.config.ConfigurationManager; import org.ametiste.redgreen.application.FailoverLine; import org.ametiste.redgreen.application.HystrixSimpleFailoverLine; import org.ametiste.redgreen.data.RedgreenBundleRepostitory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -15,12 +17,17 @@ *

* *

- * Note, in the version 0.1.0 it's only one possible {@link FailoverLine} - * configuration. + * Command execution timeout can be modified using application + * property redgreen.hystrix.simpleFailoverLine.commandTimeout ( note, a value should be defined in millis ). *

* *

- * See {@link HystrixSimpleFailoverLine} documentaion for configuration properties details. + * See {@link HystrixSimpleFailoverLine} documentaion for implementation details. + *

+ * + *

+ * Note, in the version 0.1.0 it's only one possible {@link FailoverLine} + * configuration. *

* * @see HystrixSimpleFailoverLine @@ -29,11 +36,27 @@ @Configuration public class HystrixSimpleFailoverLineConfiguration { + private static final String COMMAND_EXEC_TIMEOUT_PROPERTY = + "hystrix.command." + + HystrixSimpleFailoverLine.HYSTRIX_COMMAND_KEY + + ".execution.isolation.thread.timeoutInMilliseconds"; + @Autowired private RedgreenBundleRepostitory bundleRepostitory; + // TODO: extract to boot properties class + @Value("${redgreen.hystrix.simpleFailoverLine.commandTimeout:300}") + private String commandExecutionTimeout; + @Bean public HystrixSimpleFailoverLine simpleFailoverLine() { + + // NOTE: there are no another way to set values obtained from properties, + // hystrix-javanica does not support spring's properties placeholders atm + ConfigurationManager + .getConfigInstance() + .setProperty(COMMAND_EXEC_TIMEOUT_PROPERTY, commandExecutionTimeout); + return new HystrixSimpleFailoverLine(bundleRepostitory); } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 6f3eaa1..ff20b71 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,6 +1,6 @@ logging.level.ROOT=INFO -redgreen.hystrix.simpleFailoverLine.commandTimeout=900000 +redgreen.hystrix.simpleFailoverLine.commandTimeout=100 redgreen.direct.bundles.test-bundle.green=http://example.xcom/ redgreen.direct.bundles.test-bundle.red=http://example.com/ \ No newline at end of file