1616package feign .hystrix ;
1717
1818import com .netflix .hystrix .HystrixCommand ;
19- import com .netflix .hystrix .HystrixCommandGroupKey ;
20- import com .netflix .hystrix .HystrixCommandKey ;
19+ import com .netflix .hystrix .HystrixCommand .Setter ;
2120
2221import java .lang .reflect .InvocationHandler ;
2322import java .lang .reflect .InvocationTargetException ;
2423import java .lang .reflect .Method ;
2524import java .lang .reflect .Proxy ;
2625import java .util .LinkedHashMap ;
2726import java .util .Map ;
27+ import java .util .Set ;
2828
2929import feign .InvocationHandlerFactory .MethodHandler ;
3030import feign .Target ;
@@ -40,23 +40,27 @@ final class HystrixInvocationHandler implements InvocationHandler {
4040 private final Map <Method , MethodHandler > dispatch ;
4141 private final Object fallback ; // Nullable
4242 private final Map <Method , Method > fallbackMethodMap ;
43+ private final Map <Method , Setter > setterMethodMap ;
4344
44- HystrixInvocationHandler (Target <?> target , Map <Method , MethodHandler > dispatch , Object fallback ) {
45+ HystrixInvocationHandler (Target <?> target , Map <Method , MethodHandler > dispatch ,
46+ SetterFactory setterFactory , Object fallback ) {
4547 this .target = checkNotNull (target , "target" );
4648 this .dispatch = checkNotNull (dispatch , "dispatch" );
4749 this .fallback = fallback ;
4850 this .fallbackMethodMap = toFallbackMethod (dispatch );
51+ this .setterMethodMap = toSetters (setterFactory , target , dispatch .keySet ());
4952 }
5053
5154 /**
5255 * If the method param of InvocationHandler.invoke is not accessible, i.e in a package-private
53- * interface, the fallback call in hystrix command will fail cause of access restrictions.
54- * But methods in dispatch are copied methods. So setting access to dispatch method doesn't take
55- * effect to the method in InvocationHandler.invoke. Use map to store a copy of method
56- * to invoke the fallback to bypass this and reducing the count of reflection calls.
56+ * interface, the fallback call in hystrix command will fail cause of access restrictions. But
57+ * methods in dispatch are copied methods. So setting access to dispatch method doesn't take
58+ * effect to the method in InvocationHandler.invoke. Use map to store a copy of method to invoke
59+ * the fallback to bypass this and reducing the count of reflection calls.
60+ *
5761 * @return cached methods map for fallback invoking
5862 */
59- private Map <Method , Method > toFallbackMethod (Map <Method , MethodHandler > dispatch ) {
63+ static Map <Method , Method > toFallbackMethod (Map <Method , MethodHandler > dispatch ) {
6064 Map <Method , Method > result = new LinkedHashMap <Method , Method >();
6165 for (Method method : dispatch .keySet ()) {
6266 method .setAccessible (true );
@@ -65,6 +69,19 @@ private Map<Method, Method> toFallbackMethod(Map<Method, MethodHandler> dispatch
6569 return result ;
6670 }
6771
72+ /**
73+ * Process all methods in the target so that appropriate setters are created.
74+ */
75+ static Map <Method , Setter > toSetters (SetterFactory setterFactory , Target <?> target ,
76+ Set <Method > methods ) {
77+ Map <Method , Setter > result = new LinkedHashMap <Method , Setter >();
78+ for (Method method : methods ) {
79+ method .setAccessible (true );
80+ result .put (method , setterFactory .create (target , method ));
81+ }
82+ return result ;
83+ }
84+
6885 @ Override
6986 public Object invoke (final Object proxy , final Method method , final Object [] args )
7087 throws Throwable {
@@ -84,13 +101,7 @@ public Object invoke(final Object proxy, final Method method, final Object[] arg
84101 return toString ();
85102 }
86103
87- String groupKey = this .target .name ();
88- String commandKey = method .getName ();
89- HystrixCommand .Setter setter = HystrixCommand .Setter
90- .withGroupKey (HystrixCommandGroupKey .Factory .asKey (groupKey ))
91- .andCommandKey (HystrixCommandKey .Factory .asKey (commandKey ));
92-
93- HystrixCommand <Object > hystrixCommand = new HystrixCommand <Object >(setter ) {
104+ HystrixCommand <Object > hystrixCommand = new HystrixCommand <Object >(setterMethodMap .get (method )) {
94105 @ Override
95106 protected Object run () throws Exception {
96107 try {
@@ -141,7 +152,7 @@ protected Object getFallback() {
141152 } else if (isReturnsSingle (method )) {
142153 // Create a cold Observable as a Single
143154 return hystrixCommand .toObservable ().toSingle ();
144- } else if (isReturnsCompletable (method )) {
155+ } else if (isReturnsCompletable (method )) {
145156 return hystrixCommand .toObservable ().toCompletable ();
146157 }
147158 return hystrixCommand .execute ();
0 commit comments