use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.
the class AbstractProxyClientTransport method syncSend.
@Override
public SofaResponse syncSend(SofaRequest request, int timeout) throws SofaRpcException {
RpcInternalContext context = RpcInternalContext.getContext();
SofaResponse response = null;
SofaRpcException throwable = null;
try {
beforeSend(context, request);
if (EventBus.isEnable(ClientBeforeSendEvent.class)) {
EventBus.post(new ClientBeforeSendEvent(request));
}
response = doInvokeSync(request, timeout);
return response;
} catch (InvocationTargetException e) {
throwable = convertToRpcException(e);
throw throwable;
} catch (SofaRpcException e) {
throwable = e;
throw e;
} catch (Exception e) {
throwable = new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, "Failed to send message to remote", e);
throw throwable;
} finally {
afterSend(context, request);
if (EventBus.isEnable(ClientSyncReceiveEvent.class)) {
EventBus.post(new ClientSyncReceiveEvent(transportConfig.getConsumerConfig(), transportConfig.getProviderInfo(), request, response, throwable));
}
}
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.
the class AbstractProxyClientTransport method doInvokeSync.
/**
* 同步调用
*
* @param request 请求对象
* @param timeoutMillis 超时时间(毫秒)
* @return 返回对象
* @throws InvocationTargetException 反射调用异常
* @since 5.2.0
*/
protected SofaResponse doInvokeSync(SofaRequest request, int timeoutMillis) throws InvocationTargetException, IllegalAccessException {
SofaResponse response = new SofaResponse();
Method method = getMethod(request);
if (method == null) {
throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, "Not found method :" + request.getInterfaceName() + "." + request.getMethodName());
}
Object o = method.invoke(proxy, request.getMethodArgs());
response.setAppResponse(o);
return response;
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.
the class ClientProxyInvoker method invoke.
/**
* proxy拦截的调用
*
* @param request 请求消息
* @return 调用结果
*/
@Override
public SofaResponse invoke(SofaRequest request) throws SofaRpcException {
SofaResponse response = null;
Throwable throwable = null;
try {
RpcInternalContext.pushContext();
RpcInternalContext context = RpcInternalContext.getContext();
context.setProviderSide(false);
// 包装请求
decorateRequest(request);
try {
// 产生开始调用事件
if (EventBus.isEnable(ClientStartInvokeEvent.class)) {
EventBus.post(new ClientStartInvokeEvent(request));
}
// 得到结果
response = cluster.invoke(request);
} catch (SofaRpcException e) {
throwable = e;
throw e;
} finally {
// 产生调用结束事件
if (!request.isAsync()) {
if (EventBus.isEnable(ClientEndInvokeEvent.class)) {
EventBus.post(new ClientEndInvokeEvent(request, response, throwable));
}
}
}
// 包装响应
decorateResponse(response);
return response;
} finally {
RpcInternalContext.removeContext();
RpcInternalContext.popContext();
}
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.
the class ProviderInvoker method invoke.
@Override
public SofaResponse invoke(SofaRequest request) throws SofaRpcException {
/*// 将接口的<sofa:param />的配置复制到RpcInternalContext
RpcInternalContext context = RpcInternalContext.getContext();
Map params = providerConfig.getParameters();
if (params != null) {
context.setAttachments(params);
}
// 将方法的<sofa:param />的配置复制到invocation
String methodName = request.getMethodName();
params = (Map) providerConfig.getMethodConfigValue(methodName, SofaConstants.CONFIG_KEY_PARAMS);
if (params != null) {
context.setAttachments(params);
}*/
SofaResponse sofaResponse = new SofaResponse();
long startTime = RpcRuntimeContext.now();
long bizStartTime = System.nanoTime();
RpcInvokeContext.getContext().put(RpcConstants.INTERNAL_KEY_PROVIDER_INVOKE_START_TIME_NANO, System.nanoTime());
try {
// 反射 真正调用业务代码
Method method = request.getMethod();
if (method == null) {
throw new SofaRpcException(RpcErrorType.SERVER_FILTER, LogCodes.getLog(LogCodes.ERROR_NEED_DECODE_METHOD));
}
Object result = method.invoke(providerConfig.getRef(), request.getMethodArgs());
sofaResponse.setAppResponse(result);
} catch (IllegalArgumentException e) {
// 非法参数,可能是实现类和接口类不对应)
sofaResponse.setErrorMsg(e.getMessage());
} catch (IllegalAccessException e) {
// 如果此 Method 对象强制执行 Java 语言访问控制,并且底层方法是不可访问的
sofaResponse.setErrorMsg(e.getMessage());
// } catch (NoSuchMethodException e) { // 如果找不到匹配的方法
// sofaResponse.setErrorMsg(e.getMessage());
// } catch (ClassNotFoundException e) { // 如果指定的类加载器无法定位该类
// sofaResponse.setErrorMsg(e.getMessage());
} catch (InvocationTargetException e) {
// 业务代码抛出异常
cutCause(e.getCause());
sofaResponse.setAppResponse(e.getCause());
} finally {
// R8: Record business processing execution time
if (RpcInternalContext.isAttachmentEnable()) {
long endTime = RpcRuntimeContext.now();
RpcInternalContext.getContext().setAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE, endTime - startTime);
}
RpcInvokeContext.getContext().put(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE_NANO, System.nanoTime() - bizStartTime);
RpcInvokeContext.getContext().put(RpcConstants.INTERNAL_KEY_PROVIDER_INVOKE_END_TIME_NANO, System.nanoTime());
}
return sofaResponse;
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.
the class ExceptionUtilsTest method toShortString.
@Test
public void toShortString() throws Exception {
SofaRpcException exception = new SofaRpcException(RpcErrorType.SERVER_BUSY, "111");
String string = ExceptionUtils.toShortString(exception, 1);
Assert.assertNotNull(string);
Pattern pattern = Pattern.compile("at");
Matcher matcher = pattern.matcher(string);
int count = 0;
while (matcher.find()) {
count++;
}
Assert.assertTrue(count == 1);
}
Aggregations