From 89cb8d1d9ce6bcc1a71d4b80bd2a9c74442eb12b Mon Sep 17 00:00:00 2001 From: Toshiaki Maki Date: Thu, 18 Jan 2018 05:09:24 +0900 Subject: [PATCH] Add commandProperties in HystrixCommands (#2656) --- .../netflix/hystrix/HystrixCommands.java | 24 ++++++++++++++++++- .../netflix/hystrix/HystrixCommandsTests.java | 13 ++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixCommands.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixCommands.java index 41735616..763442dd 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixCommands.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixCommands.java @@ -23,6 +23,7 @@ import org.springframework.util.StringUtils; import com.netflix.hystrix.HystrixCommandGroupKey; import com.netflix.hystrix.HystrixCommandKey; +import com.netflix.hystrix.HystrixCommandProperties; import com.netflix.hystrix.HystrixObservableCommand; import com.netflix.hystrix.HystrixObservableCommand.Setter; @@ -49,6 +50,7 @@ public class HystrixCommands { private String groupName; private Publisher fallback; private Setter setter; + private HystrixCommandProperties.Setter commandProperties; private boolean eager = false; private Function, Observable> toObservable; @@ -76,6 +78,22 @@ public class HystrixCommands { return this; } + public PublisherBuilder commandProperties( + HystrixCommandProperties.Setter commandProperties) { + this.commandProperties = commandProperties; + return this; + } + + public PublisherBuilder commandProperties( + Function commandProperties) { + if (commandProperties == null) { + throw new IllegalArgumentException( + "commandProperties must not both be null"); + } + return this.commandProperties( + commandProperties.apply(HystrixCommandProperties.Setter())); + } + public PublisherBuilder eager() { this.eager = true; return this; @@ -126,7 +144,11 @@ public class HystrixCommands { HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey(groupNameToUse); HystrixCommandKey commandKey = HystrixCommandKey.Factory.asKey(this.commandName); - setterToUse = Setter.withGroupKey(groupKey).andCommandKey(commandKey); + HystrixCommandProperties.Setter commandProperties = this.commandProperties != null + ? this.commandProperties + : HystrixCommandProperties.Setter(); + setterToUse = Setter.withGroupKey(groupKey).andCommandKey(commandKey) + .andCommandPropertiesDefaults(commandProperties); } return setterToUse; } diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixCommandsTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixCommandsTests.java index 9e7a409c..698ee8a2 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixCommandsTests.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixCommandsTests.java @@ -134,4 +134,17 @@ public class HystrixCommandsTests { .verifyComplete(); } + @Test + public void extendTimeout() { + StepVerifier.create(HystrixCommands.from(Mono.fromCallable(() -> { + Thread.sleep(1500); + return "works"; + })).commandName("extendTimeout") + .commandProperties( + setter -> setter.withExecutionTimeoutInMilliseconds(2000)) + .toMono()) + .expectNext("works") + .verifyComplete(); + } + }