Java Code Examples for com.alipay.sofa.rpc.core.request.SofaRequest#isAsync()

The following examples show how to use com.alipay.sofa.rpc.core.request.SofaRequest#isAsync() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RpcSofaTracer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public void clientAsyncAfterSend(SofaRequest request) {

    //客户端的启动
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    //获取并不弹出
    SofaTracerSpan clientSpan = sofaTraceContext.getCurrentSpan();
    if (clientSpan == null) {
        SelfLog.warn("ClientSpan is null.Before call interface=" + request.getInterfaceName() + ",method=" +
            request.getMethodName());
        return;
    }
    RpcInternalContext rpcInternalContext = RpcInternalContext.getContext();

    // 异步callback同步
    if (request.isAsync()) {
        //异步,这个时候除了缓存spanContext clientBeforeSendRequest() rpc 已经调用
        //还需要这个时候需要还原回父 span
        //弹出;不弹出的话当前线程就会一直是client了
        clientSpan = sofaTraceContext.pop();
        if (clientSpan != null) {
            // Record client send event
            clientSpan.log(LogData.CLIENT_SEND_EVENT_VALUE);
        }
        //将当前 span 缓存在 request 中,注意:这个只是缓存不需要序列化到服务端
        rpcInternalContext.setAttachment(RpcConstants.INTERNAL_KEY_TRACER_SPAN, clientSpan);
        if (clientSpan != null && clientSpan.getParentSofaTracerSpan() != null) {
            //restore parent
            sofaTraceContext.push(clientSpan.getParentSofaTracerSpan());
        }
    } else {
        // Record client send event
        clientSpan.log(LogData.CLIENT_SEND_EVENT_VALUE);
    }
}
 
Example 2
Source File: AbstractHttp2ClientTransport.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
protected void doSend(final SofaRequest request, AbstractHttpClientHandler callback, final int timeoutMills) {
    AbstractByteBuf data = null;
    try {
        // 序列化
        byte serializeType = request.getSerializeType();
        Serializer serializer = SerializerFactory.getSerializer(serializeType);
        data = serializer.encode(request, null);
        request.setData(data);
        // 记录请求序列化大小 不是很准,没有记录HTTP头
        RpcInternalContext.getContext().setAttachment(RpcConstants.INTERNAL_KEY_REQ_SIZE, data.readableBytes());

        // 转换请求
        FullHttpRequest httpRequest = convertToHttpRequest(request);

        // 发送请求
        final int requestId = sendHttpRequest(httpRequest, callback);

        if (request.isAsync()) {
            TIMEOUT_TIMER.newTimeout(new TimerTask() {
                @Override
                public void run(Timeout timeout) throws Exception {
                    Map.Entry<ChannelFuture, AbstractHttpClientHandler> entry = responseChannelHandler
                        .removePromise(requestId);
                    if (entry != null) {
                        ClientHandler handler = entry.getValue();
                        Exception e = timeoutException(request, timeoutMills, null);
                        handler.onException(e);
                    }
                }

            }, timeoutMills, TimeUnit.MILLISECONDS);
        }
    } finally {
        if (data != null) {
            data.release();
        }
    }
}
 
Example 3
Source File: TestAsyncFilter.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public void onAsyncResponse(ConsumerConfig config, SofaRequest request, SofaResponse response, Throwable throwable)
    throws SofaRpcException {
    if (request.isAsync() && response != null) {
        response.setAppResponse(response.getAppResponse() + "append by async filter");
    }
}
 
Example 4
Source File: ClientProxyInvoker.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
    }
}
 
Example 5
Source File: TestChainFilter2.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    request.getMethodArgs()[0] = request.getMethodArgs()[0] + "_q2";
    SofaResponse response = invoker.invoke(request);
    if (!request.isAsync()) {
        response.setAppResponse(response.getAppResponse() + "_s2");
    }
    Map<String, Object> context = invoker.getConfigContext();
    Assert.assertNotNull(context);
    invoker.getMethodParam(request.getMethodName(), "invokeType");
    invoker.getStringMethodParam(request.getMethodName(), "invokeType", "sync");
    invoker.getIntMethodParam(request.getMethodName(), "timeout", 3000);
    invoker.getBooleanMethodParam(request.getMethodName(), "cache", false);
    return response;
}
 
Example 6
Source File: TestChainFilter6.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    request.getMethodArgs()[0] = request.getMethodArgs()[0] + "_q6";
    SofaResponse response = invoker.invoke(request);
    if (!request.isAsync()) {
        response.setAppResponse(response.getAppResponse() + "_s6");
    }
    return response;
}
 
Example 7
Source File: TestChainFilter3.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    request.getMethodArgs()[0] = request.getMethodArgs()[0] + "_q3";
    SofaResponse response = invoker.invoke(request);
    if (!request.isAsync()) {
        response.setAppResponse(response.getAppResponse() + "_s3");
    }
    return response;
}
 
Example 8
Source File: TestChainFilter5.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    request.getMethodArgs()[0] = request.getMethodArgs()[0] + "_q5";
    SofaResponse response = invoker.invoke(request);
    if (!request.isAsync()) {
        response.setAppResponse(response.getAppResponse() + "_s5");
    }
    return response;
}
 
Example 9
Source File: TestChainFilter1.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    request.getMethodArgs()[0] = request.getMethodArgs()[0] + "_q1";
    SofaResponse response = invoker.invoke(request);
    if (!request.isAsync()) {
        response.setAppResponse(response.getAppResponse() + "_s1");
    }
    return response;
}
 
Example 10
Source File: TestChainFilter8.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    request.getMethodArgs()[0] = request.getMethodArgs()[0] + "_q8";
    SofaResponse response = invoker.invoke(request);
    if (!request.isAsync()) {
        response.setAppResponse(response.getAppResponse() + "_s8");
    }
    return response;
}
 
Example 11
Source File: TestChainFilter7.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    request.getMethodArgs()[0] = request.getMethodArgs()[0] + "_q7";
    SofaResponse response = invoker.invoke(request);
    if (!request.isAsync()) {
        response.setAppResponse(response.getAppResponse() + "_s7");
    }
    return response;
}
 
Example 12
Source File: TestChainFilter0.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    request.getMethodArgs()[0] = request.getMethodArgs()[0] + "_q0";
    SofaResponse response = invoker.invoke(request);
    if (!request.isAsync()) {
        response.setAppResponse(response.getAppResponse() + "_s0");
    }
    return response;
}
 
Example 13
Source File: TestChainFilter4.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    request.getMethodArgs()[0] = request.getMethodArgs()[0] + "_q4";
    SofaResponse response = invoker.invoke(request);
    if (!request.isAsync()) {
        response.setAppResponse(response.getAppResponse() + "_s4");
    }
    return response;
}