Skip to content

Commit ebe8703

Browse files
committed
Add hello world examples
1 parent 0d1476c commit ebe8703

4 files changed

Lines changed: 174 additions & 0 deletions

File tree

‎hystrix/pom.xml‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>hystrix</artifactId>
8+
<version>1.0</version>
9+
10+
<name>hystrix</name>
11+
12+
<properties>
13+
14+
<!-- General -->
15+
<java.version>1.8</java.version>
16+
17+
<!-- Hystrix -->
18+
<hystrix-core.version>1.4.10</hystrix-core.version>
19+
<rxjava-core.version>0.20.7</rxjava-core.version>
20+
21+
<!-- Testing -->
22+
<hamcrest-all.version>1.3</hamcrest-all.version>
23+
<junit.version>4.12</junit.version>
24+
25+
<!-- maven plugins -->
26+
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
27+
<maven-war-plugin.version>2.6</maven-war-plugin.version>
28+
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
29+
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
30+
31+
</properties>
32+
33+
<dependencies>
34+
35+
<dependency>
36+
<groupId>com.netflix.hystrix</groupId>
37+
<artifactId>hystrix-core</artifactId>
38+
<version>${hystrix-core.version}</version>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>com.netflix.rxjava</groupId>
43+
<artifactId>rxjava-core</artifactId>
44+
<version>${rxjava-core.version}</version>
45+
</dependency>
46+
47+
<dependency>
48+
<groupId>org.hamcrest</groupId>
49+
<artifactId>hamcrest-all</artifactId>
50+
<version>${hamcrest-all.version}</version>
51+
<scope>test</scope>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>junit</groupId>
56+
<artifactId>junit</artifactId>
57+
<version>${junit.version}</version>
58+
<scope>test</scope>
59+
</dependency>
60+
61+
</dependencies>
62+
63+
<build>
64+
<plugins>
65+
<plugin>
66+
<groupId>org.apache.maven.plugins</groupId>
67+
<artifactId>maven-compiler-plugin</artifactId>
68+
<version>${maven-compiler-plugin.version}</version>
69+
<configuration>
70+
<source>1.8</source>
71+
<target>1.8</target>
72+
</configuration>
73+
</plugin>
74+
</plugins>
75+
</build>
76+
77+
</project>
78+
79+
80+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.hystrix;
2+
3+
import com.netflix.hystrix.*;
4+
import com.netflix.hystrix.exception.HystrixRuntimeException;
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.Rule;
8+
import org.junit.Test;
9+
import org.junit.rules.ExpectedException;
10+
11+
import static org.hamcrest.MatcherAssert.assertThat;
12+
import static org.hamcrest.Matchers.equalTo;
13+
14+
public class HystrixTimeoutTest {
15+
16+
private static HystrixCommand.Setter config;
17+
private static HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter();
18+
19+
20+
@Rule
21+
public final ExpectedException exception = ExpectedException.none();
22+
23+
@Before
24+
public void setup() {
25+
config = HystrixCommand
26+
.Setter
27+
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroup1"));
28+
}
29+
30+
@Test
31+
public void givenTimeoutEqualTo100_andDefaultSettings_thenReturnSuccess() throws InterruptedException {
32+
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(100)).execute(), equalTo("Success"));
33+
}
34+
35+
@Test
36+
public void givenTimeoutEqualTo10000_andDefaultSettings_thenExpectHystrixRuntimeException() throws InterruptedException {
37+
exception.expect(HystrixRuntimeException.class);
38+
new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(10_000)).execute();
39+
40+
}
41+
42+
@Test
43+
public void givenTimeoutEqualTo5000_andExecutionTimeoutEqualTo10000_thenReturnSuccess() throws InterruptedException {
44+
commandProperties.withExecutionTimeoutInMilliseconds(10_000);
45+
config.andCommandPropertiesDefaults(commandProperties);
46+
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(5_000)).execute(), equalTo("Success"));
47+
}
48+
49+
@Test
50+
public void givenTimeoutEqualTo15000_andExecutionTimeoutEqualTo10000_thenExpectHystrixRuntimeException() throws InterruptedException {
51+
exception.expect(HystrixRuntimeException.class);
52+
commandProperties.withExecutionTimeoutInMilliseconds(10_000);
53+
config.andCommandPropertiesDefaults(commandProperties);
54+
new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(15_000)).execute();
55+
}
56+
57+
58+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.hystrix;
2+
3+
import com.netflix.hystrix.HystrixCommand;
4+
import com.netflix.hystrix.HystrixCommandGroupKey;
5+
6+
7+
class RemoteServiceTestCommand extends HystrixCommand<String> {
8+
9+
private final RemoteServiceTestSimulator remoteService;
10+
11+
RemoteServiceTestCommand(Setter config, RemoteServiceTestSimulator remoteService) {
12+
super(config);
13+
this.remoteService = remoteService;
14+
}
15+
16+
@Override
17+
protected String run() throws Exception {
18+
return remoteService.execute();
19+
}
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.hystrix;
2+
3+
4+
class RemoteServiceTestSimulator {
5+
6+
private long wait;
7+
8+
RemoteServiceTestSimulator(long wait) throws InterruptedException {
9+
this.wait = wait;
10+
}
11+
12+
String execute() throws InterruptedException {
13+
Thread.sleep(wait);
14+
return "Success";
15+
}
16+
}

0 commit comments

Comments
 (0)