0

Working method with hardcoded values for @HystricProperty:

@HystrixCommand(ignoreExceptions={HttpClientErrorException.class},
        //groupKey="ProductServiceGroup",commandKey = "test", threadPoolKey = "ProductInfoDetailsThreadPool",
        commandProperties = {
                @HystrixProperty(name=HystrixPropertiesManager.CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS, value="500"),
                @HystrixProperty(name=HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS, value="1500"),
                @HystrixProperty(name=HystrixPropertiesManager.CIRCUIT_BREAKER_ENABLED, value="true"),
                @HystrixProperty(name=HystrixPropertiesManager.CIRCUIT_BREAKER_REQUEST_VOLUME_THRESHOLD, value="20"),
                @HystrixProperty(name=HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_INTERRUPT_ON_TIMEOUT, value="true"),
                @HystrixProperty(name=HystrixPropertiesManager.EXECUTION_ISOLATION_SEMAPHORE_MAX_CONCURRENT_REQUESTS, value="20")
        },
        fallbackMethod = "reliable")
public Map readingList() {
    try {
        Thread.sleep(950);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    URI uri = URI.create("http://localhost:8090/recommended");
    return this.restTemplate.getForObject(uri, Map.class);
}

I don't want to hardcode these values in @HystrixProperty annotation, instead want to read these properties from application.properties.

some thing like this: @HystrixProperty(name=HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS, value="${timeout.in.millis}")

  • create a property in application.properties like this `hystrix.command.test.execution.isolation.thread.timeoutInMilliseconds=3000` where test is the commandkey – pvpkiran Aug 29 '18 at 09:12
  • you can follow this... https://stackoverflow.com/questions/31211685/configuring-hystrix-command-properties-using-application-yaml-in-spring-boot-app – akash777.sharma Sep 20 '19 at 10:03

1 Answers1

0

you could use a properties placeholder. Define it in your spring configuration

<context:property-placeholder location="classpath:myapp.properties" /> 

then create the properties file myapp.propertiesand put in in your classpath, as referenced in the configuration. the content could be

CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS_VALUE=500
EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS_VALUE=1500

and so on...

then you can use that parameters in your @HistrixCommand just as you wrote

HystrixCommand(ignoreExceptions={HttpClientErrorException.class},
        //groupKey="ProductServiceGroup",commandKey = "test", threadPoolKey = "ProductInfoDetailsThreadPool",
        commandProperties = {
                @HystrixProperty(name=HystrixPropertiesManager.CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS, value=${CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS_VALUE}),
                @HystrixProperty(name=HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS, value=${EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS_VALUE}),
 ...
e.g78
  • 667
  • 4
  • 8
  • I have added these properties to application.properties and it is set in classpath. I can use these properties using "${timeout.in.millis}" in @Value() annotation and other places but not in @HystrixProperty annotation. Any thing that you can help here? – Mallu Golageri Aug 29 '18 at 09:44
  • if you try @HystrixProperty(name=HystrixPropertiesManager.CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS, value=${CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS_VALUE}), you have an error ? – e.g78 Aug 29 '18 at 09:51
  • Yes, I am getting error that invalid value is being passed. expected value is Integer. wait i will give you the exact error – Mallu Golageri Aug 29 '18 at 10:13
  • I would try with value=#{new Integer('${CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS_VALUE}')} – e.g78 Aug 29 '18 at 10:46
  • When I try with what you suggested, I am getting this error. java.lang.IllegalArgumentException: bad property value. property name 'execution.isolation.thread.timeoutInMilliseconds'. Expected int value, actual = #{new Integer('${hystrix.timeout.millis}')} – Mallu Golageri Aug 29 '18 at 12:25
  • maybe this post is good for you https://stackoverflow.com/questions/44143918/setting-class-level-annotation-values-from-application-yml?rq=1 – e.g78 Aug 29 '18 at 12:49