Skip to content

Commit bc38da4

Browse files
author
sbalachandran
committed
added missing new files
1 parent 3c31664 commit bc38da4

7 files changed

Lines changed: 217 additions & 20 deletions

File tree

‎hystrix/pom.xml‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
<groupId>com.baeldung</groupId>
77
<artifactId>hystrix</artifactId>
88
<version>1.0</version>
9-
109
<name>hystrix</name>
1110

11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>1.4.0.RELEASE</version>
15+
<relativePath></relativePath>
16+
</parent>
17+
1218
<properties>
1319

1420
<!-- General -->
@@ -32,12 +38,35 @@
3238

3339
<dependencies>
3440

41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-web</artifactId>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-aop</artifactId>
49+
</dependency>
50+
3551
<dependency>
3652
<groupId>com.netflix.hystrix</groupId>
3753
<artifactId>hystrix-core</artifactId>
3854
<version>${hystrix-core.version}</version>
3955
</dependency>
4056

57+
<dependency>
58+
<groupId>com.netflix.hystrix</groupId>
59+
<artifactId>hystrix-metrics-event-stream</artifactId>
60+
<version>1.3.16</version>
61+
</dependency>
62+
63+
<!-- https://mvnrepository.com/artifact/com.netflix.hystrix/hystrix-dashboard -->
64+
<!--<dependency>
65+
<groupId>com.netflix.hystrix</groupId>
66+
<artifactId>hystrix-dashboard</artifactId>
67+
<version>1.4.3</version>
68+
</dependency>-->
69+
4170
<dependency>
4271
<groupId>com.netflix.rxjava</groupId>
4372
<artifactId>rxjava-core</artifactId>
@@ -62,6 +91,10 @@
6291

6392
<build>
6493
<plugins>
94+
<plugin>
95+
<groupId>org.springframework.boot</groupId>
96+
<artifactId>spring-boot-maven-plugin</artifactId>
97+
</plugin>
6598
<plugin>
6699
<groupId>org.apache.maven.plugins</groupId>
67100
<artifactId>maven-compiler-plugin</artifactId>
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
package com.baeldung.hystrix;
22

3+
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
34
import org.springframework.boot.SpringApplication;
45
import org.springframework.boot.autoconfigure.SpringBootApplication;
5-
import org.springframework.context.ApplicationContext;
6+
import org.springframework.boot.web.servlet.ServletRegistrationBean;
7+
import org.springframework.context.annotation.Bean;
68

79
@SpringBootApplication
810
public class AppConfig {
911

1012
public static void main(String[] args) {
1113
SpringApplication.run(AppConfig.class, args);
1214
}
15+
16+
@Bean
17+
public ServletRegistrationBean adminServletRegistrationBean() {
18+
return new ServletRegistrationBean(new HystrixMetricsStreamServlet(), "/hystrix.stream");
19+
}
1320
}
Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,81 @@
11
package com.baeldung.hystrix;
22

3-
/**
4-
* Created by sbalachandran on 8/5/2016.
5-
*/
3+
import com.netflix.hystrix.*;
4+
import org.aspectj.lang.ProceedingJoinPoint;
5+
import org.aspectj.lang.annotation.Around;
6+
import org.aspectj.lang.annotation.Aspect;
7+
import org.springframework.beans.factory.annotation.Value;
8+
import org.springframework.stereotype.Component;
9+
10+
import javax.annotation.PostConstruct;
11+
12+
@Component
13+
@Aspect
614
public class HystrixAspect {
15+
16+
private HystrixCommand.Setter config;
17+
private HystrixCommandProperties.Setter commandProperties;
18+
private HystrixThreadPoolProperties.Setter threadPoolProperties;
19+
20+
@Around("@annotation(com.baeldung.hystrix.HystrixCircuitBreaker)")
21+
public Object circuitBreakerAround(final ProceedingJoinPoint aJoinPoint) {
22+
return new RemoteServiceCommand(config, aJoinPoint).execute();
23+
}
24+
25+
@PostConstruct
26+
private void setup() {
27+
this.config = HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey));
28+
this.config = config.andCommandKey(HystrixCommandKey.Factory.asKey(key));
29+
30+
this.commandProperties = HystrixCommandProperties.Setter();
31+
this.commandProperties.withExecutionTimeoutInMilliseconds(executionTimeout);
32+
this.commandProperties.withCircuitBreakerSleepWindowInMilliseconds(sleepWindow);
33+
34+
this.threadPoolProperties= HystrixThreadPoolProperties.Setter();
35+
this.threadPoolProperties.withMaxQueueSize(maxThreadCount).withCoreSize(coreThreadCount).withMaxQueueSize(queueCount);
36+
37+
this.config.andCommandPropertiesDefaults(commandProperties);
38+
this.config.andThreadPoolPropertiesDefaults(threadPoolProperties);
39+
}
40+
41+
private static class RemoteServiceCommand extends HystrixCommand<String> {
42+
43+
private final ProceedingJoinPoint joinPoint;
44+
45+
RemoteServiceCommand(final Setter config, final ProceedingJoinPoint joinPoint) {
46+
super(config);
47+
this.joinPoint = joinPoint;
48+
}
49+
50+
@Override
51+
protected String run() throws Exception {
52+
try {
53+
return (String) joinPoint.proceed();
54+
} catch (final Throwable th) {
55+
throw new Exception(th);
56+
}
57+
58+
}
59+
}
60+
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;
781
}
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.baeldung.hystrix;
22

3-
/**
4-
* Created by sbalachandran on 8/5/2016.
5-
*/
6-
public class HystrixCircuitBreaker {
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.METHOD)
10+
public @interface HystrixCircuitBreaker {
711
}
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
package com.baeldung.hystrix;
22

3-
/**
4-
* Created by sbalachandran on 8/5/2016.
5-
*/
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component("springClient")
67
public class SpringExistingClient {
8+
9+
@Value("${remoteservice.timeout}")
10+
private int remoteServiceDelay;
11+
12+
@HystrixCircuitBreaker
13+
public String invokeRemoteService() throws InterruptedException{
14+
return new RemoteServiceTestSimulator(remoteServiceDelay).execute();
15+
}
16+
717
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
remoteservice.command.group.key=RemoteServiceGroup
2+
remoteservice.command.key=RemoteServiceKey
3+
remoteservice.command.execution.timeout=10000
4+
remoteservice.command.threadpool.coresize=5
5+
remoteservice.command.threadpool.maxsize=10
6+
remoteservice.command.task.queue.size=5
7+
remoteservice.command.sleepwindow=5000
8+
remoteservice.timeout=5000

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

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,23 @@
99
import org.junit.Test;
1010
import org.junit.rules.ExpectedException;
1111

12+
import java.util.concurrent.ExecutionException;
13+
1214
import static org.hamcrest.MatcherAssert.assertThat;
1315
import static org.hamcrest.Matchers.equalTo;
1416

1517
public class HystrixTimeoutTest {
1618

17-
private static HystrixCommand.Setter config;
18-
private static HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter();
19+
private HystrixCommand.Setter config;
20+
private HystrixCommandProperties.Setter commandProperties ;
1921

2022

2123
@Rule
2224
public final ExpectedException exception = ExpectedException.none();
2325

2426
@Before
2527
public void setup() {
28+
commandProperties = HystrixCommandProperties.Setter();
2629
config = HystrixCommand
2730
.Setter
2831
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroup1"));
@@ -34,29 +37,87 @@ public void givenInputBob_andDefaultSettings_thenReturnHelloBob(){
3437
}
3538

3639
@Test
37-
public void givenTimeoutEqualTo100_andDefaultSettings_thenReturnSuccess() throws InterruptedException {
40+
public void givenServiceTimeoutEqualTo100_andDefaultSettings_thenReturnSuccess() throws InterruptedException {
3841
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(100)).execute(), equalTo("Success"));
3942
}
4043

4144
@Test
42-
public void givenTimeoutEqualTo10000_andDefaultSettings_thenExpectHystrixRuntimeException() throws InterruptedException {
45+
public void givenServiceTimeoutEqualTo10000_andDefaultSettings_thenExpectHRE() throws InterruptedException {
4346
exception.expect(HystrixRuntimeException.class);
4447
new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(10_000)).execute();
4548
}
4649

4750
@Test
48-
public void givenTimeoutEqualTo5000_andExecutionTimeoutEqualTo10000_thenReturnSuccess() throws InterruptedException {
51+
public void givenServiceTimeoutEqualTo5000_andExecutionTimeoutEqualTo10000_thenReturnSuccess()
52+
throws InterruptedException {
4953
commandProperties.withExecutionTimeoutInMilliseconds(10_000);
5054
config.andCommandPropertiesDefaults(commandProperties);
51-
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(5_000)).execute(), equalTo("Success"));
55+
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(5_000)).execute(),
56+
equalTo("Success"));
5257
}
5358

5459
@Test
55-
public void givenTimeoutEqualTo15000_andExecutionTimeoutEqualTo10000_thenExpectHystrixRuntimeException() throws InterruptedException {
60+
public void givenServiceTimeoutEqualTo15000_andExecutionTimeoutEqualTo5000_thenExpectHRE()
61+
throws InterruptedException {
5662
exception.expect(HystrixRuntimeException.class);
57-
commandProperties.withExecutionTimeoutInMilliseconds(10_000);
63+
commandProperties.withExecutionTimeoutInMilliseconds(5_000);
5864
config.andCommandPropertiesDefaults(commandProperties);
5965
new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(15_000)).execute();
6066
}
6167

68+
@Test
69+
public void givenServiceTimeoutEqual_andExecutionTimeout_andThreadPool_thenReturnSuccess()
70+
throws InterruptedException {
71+
commandProperties.withExecutionTimeoutInMilliseconds(10_000);
72+
config.andCommandPropertiesDefaults(commandProperties);
73+
config.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
74+
.withMaxQueueSize(10)
75+
.withCoreSize(3)
76+
.withQueueSizeRejectionThreshold(10));
77+
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(9_000)).execute(),
78+
equalTo("Success"));
79+
}
80+
81+
@Test
82+
public void givenCircuitBreakerSetup_thenReturnSuccess() throws InterruptedException {
83+
84+
commandProperties.withExecutionTimeoutInMilliseconds(1000);
85+
86+
commandProperties.withCircuitBreakerSleepWindowInMilliseconds(4000);
87+
commandProperties.withExecutionIsolationStrategy(
88+
HystrixCommandProperties.ExecutionIsolationStrategy.THREAD);
89+
commandProperties.withCircuitBreakerEnabled(true);
90+
commandProperties.withCircuitBreakerRequestVolumeThreshold(1);
91+
92+
config.andCommandPropertiesDefaults(commandProperties);
93+
94+
config.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
95+
.withMaxQueueSize(1)
96+
.withCoreSize(1)
97+
.withQueueSizeRejectionThreshold(1));
98+
99+
assertThat(this.invokeRemoteService(10000), equalTo(null));
100+
assertThat(this.invokeRemoteService(10000), equalTo(null));
101+
assertThat(this.invokeRemoteService(500), equalTo(null));
102+
Thread.sleep(5000);
103+
104+
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
105+
equalTo("Success"));
106+
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
107+
equalTo("Success"));
108+
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
109+
equalTo("Success"));
110+
}
111+
112+
public String invokeRemoteService(long timeout) throws InterruptedException{
113+
String response = null;
114+
try{
115+
response = new RemoteServiceTestCommand(config,
116+
new RemoteServiceTestSimulator(timeout)).execute();
117+
}catch(HystrixRuntimeException ex){
118+
System.out.println("ex = " + ex);
119+
}
120+
return response;
121+
}
122+
62123
}

0 commit comments

Comments
 (0)