Skip to content

Commit 6aadd2d

Browse files
committed
Minor changes after review
1 parent 2f331ce commit 6aadd2d

5 files changed

Lines changed: 87 additions & 45 deletions

File tree

hystrix/pom.xml

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
<groupId>org.springframework.boot</groupId>
1313
<artifactId>spring-boot-starter-parent</artifactId>
1414
<version>1.4.0.RELEASE</version>
15-
<relativePath></relativePath>
1615
</parent>
1716

17+
1818
<properties>
1919

2020
<!-- General -->
@@ -33,60 +33,68 @@
3333
<maven-war-plugin.version>2.6</maven-war-plugin.version>
3434
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
3535
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
36-
36+
<hystrix-metrics-event-stream.version>1.3.16</hystrix-metrics-event-stream.version>
37+
<hystrix-dashboard.version>1.4.3</hystrix-dashboard.version>
38+
<spring-boot-starter-test.version>1.4.0.RELEASE</spring-boot-starter-test.version>
3739
</properties>
3840

3941
<dependencies>
40-
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-tomcat</artifactId>
45+
<scope>provided</scope>
46+
</dependency>
4147
<dependency>
4248
<groupId>org.springframework.boot</groupId>
4349
<artifactId>spring-boot-starter-web</artifactId>
4450
</dependency>
45-
4651
<dependency>
4752
<groupId>org.springframework.boot</groupId>
4853
<artifactId>spring-boot-starter-aop</artifactId>
4954
</dependency>
50-
5155
<dependency>
5256
<groupId>com.netflix.hystrix</groupId>
5357
<artifactId>hystrix-core</artifactId>
5458
<version>${hystrix-core.version}</version>
5559
</dependency>
56-
5760
<dependency>
5861
<groupId>com.netflix.hystrix</groupId>
5962
<artifactId>hystrix-metrics-event-stream</artifactId>
60-
<version>1.3.16</version>
63+
<version>${hystrix-metrics-event-stream.version}</version>
6164
</dependency>
62-
63-
<!-- https://mvnrepository.com/artifact/com.netflix.hystrix/hystrix-dashboard -->
64-
<!--<dependency>
65+
<dependency>
6566
<groupId>com.netflix.hystrix</groupId>
6667
<artifactId>hystrix-dashboard</artifactId>
67-
<version>1.4.3</version>
68-
</dependency>-->
69-
68+
<version>${hystrix-dashboard.version}</version>
69+
</dependency>
7070
<dependency>
7171
<groupId>com.netflix.rxjava</groupId>
7272
<artifactId>rxjava-core</artifactId>
7373
<version>${rxjava-core.version}</version>
7474
</dependency>
75-
7675
<dependency>
7776
<groupId>org.hamcrest</groupId>
7877
<artifactId>hamcrest-all</artifactId>
7978
<version>${hamcrest-all.version}</version>
8079
<scope>test</scope>
8180
</dependency>
82-
8381
<dependency>
8482
<groupId>junit</groupId>
8583
<artifactId>junit</artifactId>
8684
<version>${junit.version}</version>
8785
<scope>test</scope>
8886
</dependency>
89-
87+
<dependency>
88+
<groupId>org.springframework</groupId>
89+
<artifactId>spring-test</artifactId>
90+
<scope>test</scope>
91+
</dependency>
92+
<dependency>
93+
<groupId>org.springframework.boot</groupId>
94+
<artifactId>spring-boot-starter-test</artifactId>
95+
<version>${spring-boot-starter-test.version}</version>
96+
<scope>test</scope>
97+
</dependency>
9098
</dependencies>
9199

92100
<build>

hystrix/src/main/java/com/baeldung/hystrix/HystrixAspect.java

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,28 @@ public class HystrixAspect {
1717
private HystrixCommandProperties.Setter commandProperties;
1818
private HystrixThreadPoolProperties.Setter threadPoolProperties;
1919

20+
@Value("${remoteservice.command.execution.timeout}")
21+
private int executionTimeout;
22+
23+
@Value("${remoteservice.command.sleepwindow}")
24+
private int sleepWindow;
25+
26+
@Value("${remoteservice.command.threadpool.maxsize}")
27+
private int maxThreadCount;
28+
29+
@Value("${remoteservice.command.threadpool.coresize}")
30+
private int coreThreadCount;
31+
32+
@Value("${remoteservice.command.task.queue.size}")
33+
private int queueCount;
34+
35+
@Value("${remoteservice.command.group.key}")
36+
private String groupKey;
37+
38+
@Value("${remoteservice.command.key}")
39+
private String key;
40+
41+
2042
@Around("@annotation(com.baeldung.hystrix.HystrixCircuitBreaker)")
2143
public Object circuitBreakerAround(final ProceedingJoinPoint aJoinPoint) {
2244
return new RemoteServiceCommand(config, aJoinPoint).execute();
@@ -31,7 +53,7 @@ private void setup() {
3153
this.commandProperties.withExecutionTimeoutInMilliseconds(executionTimeout);
3254
this.commandProperties.withCircuitBreakerSleepWindowInMilliseconds(sleepWindow);
3355

34-
this.threadPoolProperties= HystrixThreadPoolProperties.Setter();
56+
this.threadPoolProperties = HystrixThreadPoolProperties.Setter();
3557
this.threadPoolProperties.withMaxQueueSize(maxThreadCount).withCoreSize(coreThreadCount).withMaxQueueSize(queueCount);
3658

3759
this.config.andCommandPropertiesDefaults(commandProperties);
@@ -58,24 +80,4 @@ protected String run() throws Exception {
5880
}
5981
}
6082

61-
@Value("${remoteservice.command.execution.timeout}")
62-
private int executionTimeout;
63-
64-
@Value("${remoteservice.command.sleepwindow}")
65-
private int sleepWindow;
66-
67-
@Value("${remoteservice.command.threadpool.maxsize}")
68-
private int maxThreadCount;
69-
70-
@Value("${remoteservice.command.threadpool.coresize}")
71-
private int coreThreadCount;
72-
73-
@Value("${remoteservice.command.task.queue.size}")
74-
private int queueCount;
75-
76-
@Value("${remoteservice.command.group.key}")
77-
private String groupKey;
78-
79-
@Value("${remoteservice.command.key}")
80-
private String key;
8183
}

hystrix/src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ remoteservice.command.threadpool.coresize=5
55
remoteservice.command.threadpool.maxsize=10
66
remoteservice.command.task.queue.size=5
77
remoteservice.command.sleepwindow=5000
8-
remoteservice.timeout=5000
8+
remoteservice.timeout=15000

hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeoutTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
public class HystrixTimeoutTest {
1717

1818
private HystrixCommand.Setter config;
19-
private HystrixCommandProperties.Setter commandProperties ;
19+
private HystrixCommandProperties.Setter commandProperties;
2020

2121

2222
@Rule
@@ -26,12 +26,12 @@ public class HystrixTimeoutTest {
2626
public void setup() {
2727
commandProperties = HystrixCommandProperties.Setter();
2828
config = HystrixCommand
29-
.Setter
30-
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroup1"));
29+
.Setter
30+
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroup1"));
3131
}
3232

3333
@Test
34-
public void givenInputBob_andDefaultSettings_thenReturnHelloBob(){
34+
public void givenInputBob_andDefaultSettings_thenReturnHelloBob() {
3535
assertThat(new CommandHelloWorld("Bob").execute(), equalTo("Hello Bob!"));
3636
}
3737

@@ -107,12 +107,12 @@ public void givenCircuitBreakerSetup_thenReturnSuccess() throws InterruptedExcep
107107
equalTo("Success"));
108108
}
109109

110-
public String invokeRemoteService(long timeout) throws InterruptedException{
110+
public String invokeRemoteService(long timeout) throws InterruptedException {
111111
String response = null;
112-
try{
112+
try {
113113
response = new RemoteServiceTestCommand(config,
114114
new RemoteServiceTestSimulator(timeout)).execute();
115-
}catch(HystrixRuntimeException ex){
115+
} catch (HystrixRuntimeException ex) {
116116
System.out.println("ex = " + ex);
117117
}
118118
return response;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.hystrix;
2+
3+
import com.netflix.hystrix.exception.HystrixRuntimeException;
4+
import org.junit.Rule;
5+
import org.junit.Test;
6+
import org.junit.rules.ExpectedException;
7+
import org.junit.runner.RunWith;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.test.context.junit4.SpringRunner;
11+
12+
import static org.hamcrest.MatcherAssert.assertThat;
13+
import static org.hamcrest.Matchers.equalTo;
14+
15+
@RunWith(SpringRunner.class)
16+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, classes = AppConfig.class)
17+
public class SpringAndHystrixIntegrationTest {
18+
19+
@Autowired
20+
private HystrixController hystrixController;
21+
22+
@Rule
23+
public final ExpectedException exception = ExpectedException.none();
24+
25+
@Test
26+
public void givenTimeOutOf15000_whenExistingClientCalled_thenExpectHystrixRuntimeException() throws InterruptedException {
27+
exception.expect(HystrixRuntimeException.class);
28+
assertThat(hystrixController.index(), equalTo("Success"));
29+
}
30+
31+
32+
}

0 commit comments

Comments
 (0)