Search in sources :

Example 56 with SofaRpcException

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));
        }
    }
}
Also used : RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) ClientBeforeSendEvent(com.alipay.sofa.rpc.event.ClientBeforeSendEvent) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) ClientSyncReceiveEvent(com.alipay.sofa.rpc.event.ClientSyncReceiveEvent) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SocketTimeoutException(java.net.SocketTimeoutException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) ConnectException(java.net.ConnectException)

Example 57 with SofaRpcException

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;
}
Also used : Method(java.lang.reflect.Method) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 58 with SofaRpcException

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();
    }
}
Also used : ClientStartInvokeEvent(com.alipay.sofa.rpc.event.ClientStartInvokeEvent) ClientEndInvokeEvent(com.alipay.sofa.rpc.event.ClientEndInvokeEvent) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 59 with SofaRpcException

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;
}
Also used : Method(java.lang.reflect.Method) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 60 with SofaRpcException

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);
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Test(org.junit.Test)

Aggregations

SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)91 Test (org.junit.Test)35 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)28 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)27 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)24 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)23 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)22 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)21 CountDownLatch (java.util.concurrent.CountDownLatch)20 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)19 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)16 HelloService (com.alipay.sofa.rpc.test.HelloService)16 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)15 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)14 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)14 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)11 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)11 HashMap (java.util.HashMap)9 Filter (com.alipay.sofa.rpc.filter.Filter)8 InvokeTimeoutException (com.alipay.remoting.rpc.exception.InvokeTimeoutException)7