From 6aadd2d8ffba78f46d571b8199c6106fdd662228 Mon Sep 17 00:00:00 2001 From: Alex Theedom Date: Mon, 15 Aug 2016 22:48:27 +0100 Subject: [PATCH] Minor changes after review --- hystrix/pom.xml | 40 ++++++++++------- .../com/baeldung/hystrix/HystrixAspect.java | 44 ++++++++++--------- .../src/main/resources/application.properties | 2 +- .../baeldung/hystrix/HystrixTimeoutTest.java | 14 +++--- .../SpringAndHystrixIntegrationTest.java | 32 ++++++++++++++ 5 files changed, 87 insertions(+), 45 deletions(-) create mode 100644 hystrix/src/test/java/com/baeldung/hystrix/SpringAndHystrixIntegrationTest.java diff --git a/hystrix/pom.xml b/hystrix/pom.xml index ef443ebd15..7867bbb955 100644 --- a/hystrix/pom.xml +++ b/hystrix/pom.xml @@ -12,9 +12,9 @@ org.springframework.boot spring-boot-starter-parent 1.4.0.RELEASE - + @@ -33,60 +33,68 @@ 2.6 2.19.1 2.7 - + 1.3.16 + 1.4.3 + 1.4.0.RELEASE - + + org.springframework.boot + spring-boot-starter-tomcat + provided + org.springframework.boot spring-boot-starter-web - org.springframework.boot spring-boot-starter-aop - com.netflix.hystrix hystrix-core ${hystrix-core.version} - com.netflix.hystrix hystrix-metrics-event-stream - 1.3.16 + ${hystrix-metrics-event-stream.version} - - - - + ${hystrix-dashboard.version} + com.netflix.rxjava rxjava-core ${rxjava-core.version} - org.hamcrest hamcrest-all ${hamcrest-all.version} test - junit junit ${junit.version} test - + + org.springframework + spring-test + test + + + org.springframework.boot + spring-boot-starter-test + ${spring-boot-starter-test.version} + test + diff --git a/hystrix/src/main/java/com/baeldung/hystrix/HystrixAspect.java b/hystrix/src/main/java/com/baeldung/hystrix/HystrixAspect.java index c2e4af8edb..3a650c2f4f 100644 --- a/hystrix/src/main/java/com/baeldung/hystrix/HystrixAspect.java +++ b/hystrix/src/main/java/com/baeldung/hystrix/HystrixAspect.java @@ -17,6 +17,28 @@ public class HystrixAspect { private HystrixCommandProperties.Setter commandProperties; private HystrixThreadPoolProperties.Setter threadPoolProperties; + @Value("${remoteservice.command.execution.timeout}") + private int executionTimeout; + + @Value("${remoteservice.command.sleepwindow}") + private int sleepWindow; + + @Value("${remoteservice.command.threadpool.maxsize}") + private int maxThreadCount; + + @Value("${remoteservice.command.threadpool.coresize}") + private int coreThreadCount; + + @Value("${remoteservice.command.task.queue.size}") + private int queueCount; + + @Value("${remoteservice.command.group.key}") + private String groupKey; + + @Value("${remoteservice.command.key}") + private String key; + + @Around("@annotation(com.baeldung.hystrix.HystrixCircuitBreaker)") public Object circuitBreakerAround(final ProceedingJoinPoint aJoinPoint) { return new RemoteServiceCommand(config, aJoinPoint).execute(); @@ -31,7 +53,7 @@ public class HystrixAspect { this.commandProperties.withExecutionTimeoutInMilliseconds(executionTimeout); this.commandProperties.withCircuitBreakerSleepWindowInMilliseconds(sleepWindow); - this.threadPoolProperties= HystrixThreadPoolProperties.Setter(); + this.threadPoolProperties = HystrixThreadPoolProperties.Setter(); this.threadPoolProperties.withMaxQueueSize(maxThreadCount).withCoreSize(coreThreadCount).withMaxQueueSize(queueCount); this.config.andCommandPropertiesDefaults(commandProperties); @@ -58,24 +80,4 @@ public class HystrixAspect { } } - @Value("${remoteservice.command.execution.timeout}") - private int executionTimeout; - - @Value("${remoteservice.command.sleepwindow}") - private int sleepWindow; - - @Value("${remoteservice.command.threadpool.maxsize}") - private int maxThreadCount; - - @Value("${remoteservice.command.threadpool.coresize}") - private int coreThreadCount; - - @Value("${remoteservice.command.task.queue.size}") - private int queueCount; - - @Value("${remoteservice.command.group.key}") - private String groupKey; - - @Value("${remoteservice.command.key}") - private String key; } diff --git a/hystrix/src/main/resources/application.properties b/hystrix/src/main/resources/application.properties index abde975550..50c241d03f 100644 --- a/hystrix/src/main/resources/application.properties +++ b/hystrix/src/main/resources/application.properties @@ -5,4 +5,4 @@ remoteservice.command.threadpool.coresize=5 remoteservice.command.threadpool.maxsize=10 remoteservice.command.task.queue.size=5 remoteservice.command.sleepwindow=5000 -remoteservice.timeout=5000 \ No newline at end of file +remoteservice.timeout=15000 \ No newline at end of file diff --git a/hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeoutTest.java b/hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeoutTest.java index 34eb334b32..d65931d9f7 100644 --- a/hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeoutTest.java +++ b/hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeoutTest.java @@ -16,7 +16,7 @@ import static org.hamcrest.Matchers.equalTo; public class HystrixTimeoutTest { private HystrixCommand.Setter config; - private HystrixCommandProperties.Setter commandProperties ; + private HystrixCommandProperties.Setter commandProperties; @Rule @@ -26,12 +26,12 @@ public class HystrixTimeoutTest { public void setup() { commandProperties = HystrixCommandProperties.Setter(); config = HystrixCommand - .Setter - .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroup1")); + .Setter + .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroup1")); } @Test - public void givenInputBob_andDefaultSettings_thenReturnHelloBob(){ + public void givenInputBob_andDefaultSettings_thenReturnHelloBob() { assertThat(new CommandHelloWorld("Bob").execute(), equalTo("Hello Bob!")); } @@ -107,12 +107,12 @@ public class HystrixTimeoutTest { equalTo("Success")); } - public String invokeRemoteService(long timeout) throws InterruptedException{ + public String invokeRemoteService(long timeout) throws InterruptedException { String response = null; - try{ + try { response = new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(timeout)).execute(); - }catch(HystrixRuntimeException ex){ + } catch (HystrixRuntimeException ex) { System.out.println("ex = " + ex); } return response; diff --git a/hystrix/src/test/java/com/baeldung/hystrix/SpringAndHystrixIntegrationTest.java b/hystrix/src/test/java/com/baeldung/hystrix/SpringAndHystrixIntegrationTest.java new file mode 100644 index 0000000000..8f443fc5a4 --- /dev/null +++ b/hystrix/src/test/java/com/baeldung/hystrix/SpringAndHystrixIntegrationTest.java @@ -0,0 +1,32 @@ +package com.baeldung.hystrix; + +import com.netflix.hystrix.exception.HystrixRuntimeException; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, classes = AppConfig.class) +public class SpringAndHystrixIntegrationTest { + + @Autowired + private HystrixController hystrixController; + + @Rule + public final ExpectedException exception = ExpectedException.none(); + + @Test + public void givenTimeOutOf15000_whenExistingClientCalled_thenExpectHystrixRuntimeException() throws InterruptedException { + exception.expect(HystrixRuntimeException.class); + assertThat(hystrixController.index(), equalTo("Success")); + } + + +}