Search in sources :

Example 26 with RpcInternalContext

use of com.alipay.sofa.rpc.context.RpcInternalContext 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 27 with RpcInternalContext

use of com.alipay.sofa.rpc.context.RpcInternalContext 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 28 with RpcInternalContext

use of com.alipay.sofa.rpc.context.RpcInternalContext in project sofa-rpc by sofastack.

the class EventBus method post.

/**
 * 给事件总线中丢一个事件
 *
 * @param event 事件
 */
public static void post(final Event event) {
    if (!isEnable()) {
        return;
    }
    CopyOnWriteArraySet<Subscriber> subscribers = SUBSCRIBER_MAP.get(event.getClass());
    if (CommonUtils.isNotEmpty(subscribers)) {
        for (final Subscriber subscriber : subscribers) {
            if (subscriber.isSync()) {
                handleEvent(subscriber, event);
            } else {
                // 异步
                final RpcInternalContext context = RpcInternalContext.peekContext();
                final ThreadPoolExecutor asyncThreadPool = AsyncRuntime.getAsyncThreadPool();
                try {
                    asyncThreadPool.execute(new Runnable() {

                        @Override
                        public void run() {
                            try {
                                RpcInternalContext.setContext(context);
                                handleEvent(subscriber, event);
                            } finally {
                                RpcInternalContext.removeContext();
                            }
                        }
                    });
                } catch (RejectedExecutionException e) {
                    LOGGER.warn("This queue is full when post event to async execute, queue size is " + asyncThreadPool.getQueue().size() + ", please optimize this async thread pool of eventbus.");
                }
            }
        }
    }
}
Also used : RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 29 with RpcInternalContext

use of com.alipay.sofa.rpc.context.RpcInternalContext in project sofa-rpc by sofastack.

the class Router method recordRouterWay.

/**
 * 记录路由路径记录
 *
 * @param routerName 路由名字
 * @since 5.2.0
 */
protected void recordRouterWay(String routerName) {
    if (RpcInternalContext.isAttachmentEnable()) {
        RpcInternalContext context = RpcInternalContext.getContext();
        String record = (String) context.getAttachment(RpcConstants.INTERNAL_KEY_ROUTER_RECORD);
        record = record == null ? routerName : record + ">" + routerName;
        context.setAttachment(RpcConstants.INTERNAL_KEY_ROUTER_RECORD, record);
    }
}
Also used : RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext)

Example 30 with RpcInternalContext

use of com.alipay.sofa.rpc.context.RpcInternalContext in project sofa-rpc by sofastack.

the class AbstractHttp2ClientTransport method asyncSend.

@Override
public ResponseFuture asyncSend(SofaRequest request, int timeout) throws SofaRpcException {
    checkConnection();
    RpcInternalContext context = RpcInternalContext.getContext();
    try {
        beforeSend(context, request);
        return doInvokeAsync(request, context, timeout);
    } catch (SofaRpcException e) {
        throw e;
    } catch (Exception e) {
        throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, e.getMessage(), e);
    } finally {
        afterSend(context, request);
    }
}
Also used : RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) TimeoutException(java.util.concurrent.TimeoutException) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) ExecutionException(java.util.concurrent.ExecutionException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Aggregations

RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)52 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)15 SofaTraceContext (com.alipay.common.tracer.core.context.trace.SofaTraceContext)13 SofaTracerSpan (com.alipay.common.tracer.core.span.SofaTracerSpan)12 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)12 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)10 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)6 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)5 HashMap (java.util.HashMap)5 InvokeContext (com.alipay.remoting.InvokeContext)4 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)4 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)4 ClientSyncReceiveEvent (com.alipay.sofa.rpc.event.ClientSyncReceiveEvent)4 Test (org.junit.Test)4 SofaTracerSpanContext (com.alipay.common.tracer.core.context.span.SofaTracerSpanContext)3 ConnectionClosedException (com.alipay.remoting.exception.ConnectionClosedException)3 DeserializationException (com.alipay.remoting.exception.DeserializationException)3 RemotingException (com.alipay.remoting.exception.RemotingException)3 SerializationException (com.alipay.remoting.exception.SerializationException)3 InvokeSendFailedException (com.alipay.remoting.rpc.exception.InvokeSendFailedException)3