Search in sources :

Example 1 with ClientStartInvokeEvent

use of com.alipay.sofa.rpc.event.ClientStartInvokeEvent 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)

Aggregations

RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)1 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)1 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)1 ClientEndInvokeEvent (com.alipay.sofa.rpc.event.ClientEndInvokeEvent)1 ClientStartInvokeEvent (com.alipay.sofa.rpc.event.ClientStartInvokeEvent)1