2929import feign .Target ;
3030import rx .Observable ;
3131import rx .Single ;
32+ import rx .functions .Action1 ;
3233
3334import 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