1 /*******************************************************************************
2 * Copyright 2017 ZTE, Inc. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
13 ******************************************************************************/
17 package org.onap.msb.sdk.httpclient.handler;
19 import java.lang.reflect.InvocationHandler;
20 import java.lang.reflect.Method;
22 import java.util.concurrent.atomic.AtomicReference;
24 import org.onap.msb.sdk.httpclient.ProxyRetrofitCall;
25 import org.onap.msb.sdk.httpclient.ServiceHttpEndPointObject;
26 import org.onap.msb.sdk.httpclient.exception.RetrofitServiceRuntimeException;
27 import org.onap.msb.sdk.httpclient.lb.LoadBalanceContext;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import com.google.common.collect.Lists;
32 import retrofit2.Call;
38 public class RetrofitServiceHandler implements InvocationHandler {
40 private final static Logger logger = LoggerFactory.getLogger(RetrofitServiceHandler.class);
41 private static long periodTime = 60;
45 String periodStr = System.getenv("retrofit_route_cache_refresh_period");
46 periodTime = periodStr != null ? Long.valueOf(periodStr) : 60;
47 logger.info("retrofit_route_cache_refresh_period:" + periodTime);
48 } catch (Exception e) {
56 private RetrofitServiceHandlerContext flowContext;
58 private AtomicReference<Map<ServiceHttpEndPointObject, Object>> endPointToRetrofitRef = new AtomicReference();
60 public RetrofitServiceHandler(RetrofitServiceHandlerContext flowContext) {
62 this.flowContext = flowContext;
63 logger.info("retrofit_route_cache_refresh_period:" + periodTime);
70 * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method,
74 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
76 Object retrofitObject = null;
77 ServiceHttpEndPointObjectWapper wapper = null;
80 wapper = selectRetrofitObjectByLBStrategy(
81 flowContext.getRetrofitObjectBuilder().buildRetrofitObject(endPointToRetrofitRef, null), method,
83 retrofitObject = wapper.retrofitObject;
85 Object resultObjecct = method.invoke(retrofitObject, args);
87 if (resultObjecct instanceof Call) {
88 Call targetCall = (Call) resultObjecct;
89 return new ProxyRetrofitCall(targetCall, this, wapper.endPoint, proxy, method, args);
95 public Object reInvoke(Object proxy, Method method, Object[] args, ServiceHttpEndPointObject endPoint)
99 Object retrofitObject = null;
100 ServiceHttpEndPointObjectWapper wapper = null;
104 Map<ServiceHttpEndPointObject, Object> serviceHttpEndPointObjectMap =
105 flowContext.getRetrofitObjectBuilder().buildRetrofitObject(endPointToRetrofitRef, endPoint);
107 wapper = selectRetrofitObjectByLBStrategy(serviceHttpEndPointObjectMap, method, args);
111 retrofitObject = wapper.retrofitObject;
113 Object resultObjecct = method.invoke(retrofitObject, args);
115 return resultObjecct;
119 private void updateMsbInfo() {
123 if (System.currentTimeMillis() - flowContext.getLastUpdateMsbTime() > periodTime * 1000) {
128 public void clean() {
129 endPointToRetrofitRef.set(null);
133 private ServiceHttpEndPointObjectWapper selectRetrofitObjectByLBStrategy(
134 Map<ServiceHttpEndPointObject, Object> srvEndPointToRetrofit, Method method, Object[] args)
135 throws RetrofitServiceRuntimeException {
137 LoadBalanceContext ctx = new LoadBalanceContext();
138 ctx.setEndPoints(Lists.newArrayList(srvEndPointToRetrofit.keySet()));
140 ctx.setMethod(method);
141 ServiceHttpEndPointObject endPoint = flowContext.getLbStrategy().chooseEndPointObject(ctx);
142 return new ServiceHttpEndPointObjectWapper(endPoint, srvEndPointToRetrofit.get(endPoint));
148 class ServiceHttpEndPointObjectWapper {
150 protected ServiceHttpEndPointObject endPoint;
151 protected Object retrofitObject;
153 public ServiceHttpEndPointObjectWapper(ServiceHttpEndPointObject endPoint, Object retrofitObject) {
155 this.endPoint = endPoint;
156 this.retrofitObject = retrofitObject;