Skip to content

Commit 07dcd87

Browse files
Jimmy LuAdrian Cole
authored andcommitted
Adds fallback support for HystrixCommand, Observable, and Single results
1 parent 14402d8 commit 07dcd87

3 files changed

Lines changed: 265 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### Version 8.16
22
* Adds `@QueryMap` annotation to support dynamic query parameters
3+
* Adds fallback support for HystrixCommand, Observable, and Single results
34

45
### Version 8.15
56
* Supports PUT without a body parameter

hystrix/src/main/java/feign/hystrix/HystrixInvocationHandler.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import feign.Target;
3030
import rx.Observable;
3131
import rx.Single;
32+
import rx.functions.Action1;
3233

3334
import static feign.Util.checkNotNull;
3435

@@ -68,7 +69,18 @@ protected Object run() throws Exception {
6869
protected Object getFallback() {
6970
if (fallback == null) return super.getFallback();
7071
try {
71-
return method.invoke(fallback, args);
72+
Object result = method.invoke(fallback, args);
73+
if (isReturnsHystrixCommand(method)) {
74+
return ((HystrixCommand) result).execute();
75+
} else if (isReturnsObservable(method)) {
76+
// Create a cold Observable
77+
return ((Observable) result).toBlocking().first();
78+
} else if (isReturnsSingle(method)) {
79+
// Create a cold Observable as a Single
80+
return ((Single) result).toObservable().toBlocking().first();
81+
} else {
82+
return result;
83+
}
7284
} catch (IllegalAccessException e) {
7385
// shouldn't happen as method is public due to being an interface
7486
throw new AssertionError(e);
@@ -79,18 +91,30 @@ protected Object getFallback() {
7991
}
8092
};
8193

82-
if (HystrixCommand.class.isAssignableFrom(method.getReturnType())) {
94+
if (isReturnsHystrixCommand(method)) {
8395
return hystrixCommand;
84-
} else if (Observable.class.isAssignableFrom(method.getReturnType())) {
96+
} else if (isReturnsObservable(method)) {
8597
// Create a cold Observable
8698
return hystrixCommand.toObservable();
87-
} else if (Single.class.isAssignableFrom(method.getReturnType())) {
99+
} else if (isReturnsSingle(method)) {
88100
// Create a cold Observable as a Single
89101
return hystrixCommand.toObservable().toSingle();
90102
}
91103
return hystrixCommand.execute();
92104
}
93105

106+
private boolean isReturnsHystrixCommand(Method method) {
107+
return HystrixCommand.class.isAssignableFrom(method.getReturnType());
108+
}
109+
110+
private boolean isReturnsObservable(Method method) {
111+
return Observable.class.isAssignableFrom(method.getReturnType());
112+
}
113+
114+
private boolean isReturnsSingle(Method method) {
115+
return Single.class.isAssignableFrom(method.getReturnType());
116+
}
117+
94118
static final class Factory implements InvocationHandlerFactory {
95119

96120
@Override

0 commit comments

Comments
 (0)