Search in sources :

Example 1 with Response

use of com.baidu.brpc.protocol.Response in project skywalking-java by apache.

the class ServerInterceptor method afterMethod.

@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
    Response response = (Response) allArguments[1];
    if (response != null && response.getException() != null) {
        AbstractSpan span = ContextManager.activeSpan();
        span.log(response.getException());
    }
    ContextManager.stopSpan();
    return ret;
}
Also used : Response(com.baidu.brpc.protocol.Response) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Example 2 with Response

use of com.baidu.brpc.protocol.Response in project brpc-java by baidu.

the class DefaultServerPushProtocol method getResponse.

@Override
public Response getResponse() {
    // tcp protocol implementation, http protocols should override this method
    Response response = RpcResponse.getRpcResponse();
    response.reset();
    return response;
}
Also used : RpcResponse(com.baidu.brpc.protocol.RpcResponse) Response(com.baidu.brpc.protocol.Response)

Example 3 with Response

use of com.baidu.brpc.protocol.Response in project brpc-java by baidu.

the class HttpRpcProtocol method getResponse.

@Override
public Response getResponse() {
    Response response = HttpResponse.getHttpResponse();
    response.reset();
    return response;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(com.baidu.brpc.protocol.HttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Response(com.baidu.brpc.protocol.Response)

Example 4 with Response

use of com.baidu.brpc.protocol.Response in project brpc-java by baidu.

the class ClientWorkTask method handlePushRequest.

/**
 * 收到push请求的处理方法
 */
private void handlePushRequest() {
    Request request = null;
    Response response = protocol.createResponse();
    try {
        request = protocol.decodeRequest(packet);
    } catch (Exception ex) {
        // throw request
        log.warn("decode request failed:", ex);
        response.setException(ex);
    } finally {
        if (request != null && request.getException() != null) {
            response.setException(request.getException());
        }
    }
    RpcContext rpcContext = null;
    request.setChannel(ctx.channel());
    if (request.getBinaryAttachment() != null || request.getKvAttachment() != null) {
        rpcContext = RpcContext.getContext();
        if (request.getBinaryAttachment() != null) {
            rpcContext.setRequestBinaryAttachment(request.getBinaryAttachment());
        }
        if (request.getKvAttachment() != null) {
            rpcContext.setRequestKvAttachment(request.getKvAttachment());
        }
        rpcContext.setRemoteAddress(ctx.channel().remoteAddress());
    }
    response.setLogId(request.getLogId());
    response.setCorrelationId(request.getCorrelationId());
    response.setCompressType(request.getCompressType());
    response.setException(request.getException());
    response.setRpcMethodInfo(request.getRpcMethodInfo());
    String serviceName = request.getServiceName();
    String methodName = request.getMethodName();
    RpcMethodInfo service = ServiceManager.getInstance().getService(serviceName, methodName);
    Method targetMethod = request.getTargetMethod();
    Object t = service.getTarget();
    Object result = null;
    try {
        result = targetMethod.invoke(t, request.getArgs());
    } catch (Exception e) {
        log.error("exception :", e);
    }
    response.setResult(result);
    try {
        ByteBuf byteBuf = protocol.encodeResponse(request, response);
        ChannelFuture channelFuture = ctx.channel().writeAndFlush(byteBuf);
        protocol.afterResponseSent(request, response, channelFuture);
    } catch (Exception ex) {
        log.warn("send response failed:", ex);
    }
    if (rpcContext != null) {
        rpcContext.reset();
    }
}
Also used : Response(com.baidu.brpc.protocol.Response) RpcContext(com.baidu.brpc.RpcContext) ChannelFuture(io.netty.channel.ChannelFuture) RpcMethodInfo(com.baidu.brpc.RpcMethodInfo) Request(com.baidu.brpc.protocol.Request) Method(java.lang.reflect.Method) ByteBuf(io.netty.buffer.ByteBuf)

Example 5 with Response

use of com.baidu.brpc.protocol.Response in project brpc-java by baidu.

the class BrpcProxy method executeWithRetry.

public Response executeWithRetry(Request request) {
    Response response = null;
    RpcException exception = null;
    int currentTryTimes = 0;
    int maxTryTimes = rpcClient.getRpcClientOptions().getMaxTryTimes();
    while (currentTryTimes < maxTryTimes) {
        try {
            // therefore, it need LoadBalanceStrategy to judge if selectInstances is null.
            if (currentTryTimes > 0) {
                if (request.getChannel() != null) {
                    if (request.getSelectedInstances() == null) {
                        request.setSelectedInstances(new HashSet<CommunicationClient>(maxTryTimes - 1));
                    }
                    request.getSelectedInstances().add(request.getCommunicationClient());
                }
            }
            response = rpcClient.execute(request, rpcClient.getCommunicationOptions());
            break;
        } catch (RpcException ex) {
            exception = ex;
            if (exception.getCode() == RpcException.INTERCEPT_EXCEPTION) {
                break;
            }
        } finally {
            currentTryTimes++;
        }
    }
    if (response == null || (response.getResult() == null && response.getRpcFuture() == null)) {
        if (exception == null) {
            exception = new RpcException(RpcException.UNKNOWN_EXCEPTION, "unknown error");
        }
        throw exception;
    }
    return response;
}
Also used : Response(com.baidu.brpc.protocol.Response) RpcException(com.baidu.brpc.exceptions.RpcException)

Aggregations

Response (com.baidu.brpc.protocol.Response)20 RpcException (com.baidu.brpc.exceptions.RpcException)8 Request (com.baidu.brpc.protocol.Request)7 RpcResponse (com.baidu.brpc.protocol.RpcResponse)5 SPHead (com.baidu.brpc.protocol.push.SPHead)4 ServerPushProtocol (com.baidu.brpc.protocol.push.ServerPushProtocol)4 Test (org.junit.Test)4 RpcFuture (com.baidu.brpc.client.RpcFuture)3 BaseMockitoTest (com.baidu.brpc.test.BaseMockitoTest)3 RpcContext (com.baidu.brpc.RpcContext)2 RpcMethodInfo (com.baidu.brpc.RpcMethodInfo)2 HttpResponse (com.baidu.brpc.protocol.HttpResponse)2 NSHead (com.baidu.brpc.protocol.nshead.NSHead)2 NSHeadMeta (com.baidu.brpc.protocol.nshead.NSHeadMeta)2 ByteBuf (io.netty.buffer.ByteBuf)2 ChannelFuture (io.netty.channel.ChannelFuture)2 ArrayList (java.util.ArrayList)2 ChannelInfo (com.baidu.brpc.ChannelInfo)1 JprotobufRpcMethodInfo (com.baidu.brpc.JprotobufRpcMethodInfo)1 ProtobufRpcMethodInfo (com.baidu.brpc.ProtobufRpcMethodInfo)1