反射调用外部服务

public class OutServiceUtil {
private static final Logger logger = LoggerFactory.getLogger(OutServiceUtil.class);
private static final Gson gson=new Gson();
private static final String CONSUMER="CONSUMER";
public static final String UNKNOW_EXCEPTION="未知异常";


private static class CommonResult{
private boolean success = false;
private String errorCode;
private String errorMessage;
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}

}

/**
* 调用标准微服务获取result
* @param producerName 生产者的系统名
* @param beanName 调用的bean名称
* @param methodName 方法名
* @param checkException 是否由工具类处理异常
* @param paramValues 参数列表
* @return Result
* @author zhaochong 01375950 on 2018年8月3日
*/
public static Result callOutServiceGetResult(String producerName,String beanName,String methodName,boolean checkException,Object ...paramValues ){
return (Result)callCommonOutServiceGetResult(producerName,beanName,methodName,checkException,paramValues);
}

/**
* 根据bean名称调用非标准微服务
* @param producerName
* @param beanName
* @param methodName
* @param checkException
* @param paramValues
* @return
* @author zhaochong 01375950 on 2018年8月3日
*/
public static Object callCommonOutServiceGetResult(String producerName,String beanName,String methodName,boolean checkException,Object ...paramValues ){
Object service = BeanHelper.getBean(beanName);
if(service==null){
throw new NovatarRuntimeException("service "+beanName+"不存在");
}
return callCommonOutServiceGetResultWithBean(service,producerName,beanName,methodName,checkException,paramValues);
}

/**
* 根据ben对象调用非标准的微服务
* @param service 目标service对象
* @param producerName 生产者的系统名
* @param beanName 调用的bean名称
* @param methodName 方法名
* @param checkException 是否由工具类处理异常
* @param paramValues 参数列表
* @return
* @author zhaochong 01375950 on 2018年8月3日
*/
public static Object callCommonOutServiceGetResultWithBean(Object service,String producerName,String beanName,String methodName,boolean checkException,Object ...paramValues){
if(service==null){
throw new NovatarRuntimeException("service "+beanName+"为空");
}
Class[] paramClasses=null;
if(paramValues!=null){
paramClasses=new Class[paramValues.length];
for(int i=0;i<paramValues.length;i++){
paramClasses[i]=paramValues[i].getClass();
}
}

Method method = ReflectionUtils.findMethod(BeanHelper.getBean(beanName).getClass(), methodName, paramClasses);
if(method==null){
throw new NovatarRuntimeException("方法"+beanName+"."+methodName+"不存在");
}
return callMethod(producerName,beanName,method,checkException,service,paramValues);
}


/**
* 调用目标方法,记录日志,处理异常
* @author zhaochong 01375950 on 2018年8月3日
*/
private static Object callMethod(String producerName, String beanName,Method method,boolean checkException,Object service,Object ...paramValues){
String methodName=method.getName();
long startTm = System.currentTimeMillis();
String producerAndBean=producerName+" "+beanName;
Object result;
try{
result = ReflectionUtils.invokeMethod(method, service, paramValues);//执行该方法
}catch(Exception e){
logger.error("调用"+producerAndBean+"接口失败.",e);
throw new NovatarRuntimeException("调用"+producerAndBean+"接口失败");
}
StringBuilder sb=new StringBuilder();
if(paramValues!=null){
for(Object o:paramValues){
sb.append(gson.toJson(o));
}
}
logger.info("{I, 1, {}, {}, {}, {}.{}, {}, {}}",CONSUMER, AppContext.getContext().getBindHost(),producerName,
beanName,methodName, sb, System.currentTimeMillis() - startTm);
logger.info("{}.{} response: {}", beanName,methodName,gson.toJson(result));
if(!checkException){//由业务代码负责处理异常,工具类不做后续处理
return result;
}

if(result==null){
throw new NovatarRuntimeException("调用"+producerAndBean+"接口无返回结果");
}

CommonResult commonResult=getCommonResult(result);
if(commonResult.isSuccess()) {//调用成功
return result;
}

throw new RuntimeException(errorMessageForShow(producerAndBean,commonResult));


}

/**
* 将错误消息抛到前端,优先抛错误消息,为空则抛错误代码
* @param commonResult
* @return
* @author zhaochong 01375950 on 2018年8月6日
*/
private static String errorMessageForShow(String producerAndBean,CommonResult commonResult){
String prefix="["+producerAndBean+"]";
if(StringUtils.isNotEmpty(commonResult.getErrorMessage())){
return prefix+commonResult.getErrorMessage();
}
if(StringUtils.isNotEmpty(commonResult.getErrorCode())){
return prefix+commonResult.getErrorCode();
}
return prefix+UNKNOW_EXCEPTION;

}

private static CommonResult getCommonResult(Object obj){
CommonResult result=new CommonResult();
if(obj instanceof Result){//标准微服务
Result r=(Result)obj;
result.setSuccess(r.isSuccess());
result.setErrorCode(r.getErrorCode());
result.setErrorMessage(r.getErrorMessage());
return result;
}


result.setSuccess(true);
return result;
}






/**
* 调用标准微服务获取result中的Object
* @param producerName 生产者的系统名
* @param beanName 调用的bean名称
* @param methodName 方法名
* @param paramValues 参数列表
* @return Object 从result中剥出的object对象
* @author zhaochong 01375950 on 2018年8月3日
*/
public static Object callOutServiceGetObject(String producerName,String beanName,String methodName,Object ...objects ){
Result result= OutServiceUtil.callOutServiceGetResult(producerName,beanName, methodName, true, objects);
return result.getObj();
}



private OutServiceUtil() {
throw new IllegalAccessError("Utility class");
}

posted @ 2019-04-12 16:47  zhao0607  阅读(325)  评论(0编辑  收藏  举报