Java Code Examples for com.alipay.remoting.InvokeContext#get()

The following examples show how to use com.alipay.remoting.InvokeContext#get() . 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: BoltChannelUtil.java    From sofa-registry with Apache License 2.0 6 votes vote down vote up
public static Byte getBoltCustomSerializer(Channel channel) {
    if (channel instanceof BoltChannel) {
        BoltChannel boltChannel = (BoltChannel) channel;
        InvokeContext invokeContext = boltChannel.getBizContext().getInvokeContext();

        if (null != invokeContext) {
            // set client custom codec for request command if not null
            Object clientCustomCodec = invokeContext.get(InvokeContext.BOLT_CUSTOM_SERIALIZER);
            if (null != clientCustomCodec) {
                try {
                    return (Byte) clientCustomCodec;
                } catch (ClassCastException e) {
                    throw new IllegalArgumentException(
                        "Illegal custom codec [" + clientCustomCodec
                                + "], the type of value should be [byte], but now is ["
                                + clientCustomCodec.getClass().getName() + "].");
                }
            }
        }
    }
    return null;
}
 
Example 2
Source File: TraceLogUtil.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
/**
 * print trace log
 * @param traceId
 * @param invokeContext
 */
public static void printConnectionTraceLog(Logger logger, String traceId,
                                           InvokeContext invokeContext) {
    String sourceIp = invokeContext.get(InvokeContext.CLIENT_LOCAL_IP);
    Integer sourcePort = invokeContext.get(InvokeContext.CLIENT_LOCAL_PORT);
    String targetIp = invokeContext.get(InvokeContext.CLIENT_REMOTE_IP);
    Integer targetPort = invokeContext.get(InvokeContext.CLIENT_REMOTE_PORT);
    StringBuilder logMsg = new StringBuilder();
    logMsg.append(traceId).append(",");
    logMsg.append(sourceIp).append(",");
    logMsg.append(sourcePort).append(",");
    logMsg.append(targetIp).append(",");
    logMsg.append(targetPort);
    if (logger.isInfoEnabled()) {
        logger.info(logMsg.toString());
    }
}
 
Example 3
Source File: SofaRpcSerialization.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
/**
 * 客户端记录序列化请求的耗时和
 *
 * @param requestCommand 请求对象
 */
protected void recordSerializeRequest(RequestCommand requestCommand, InvokeContext invokeContext) {
    if (!RpcInternalContext.isAttachmentEnable()) {
        return;
    }
    RpcInternalContext context = null;
    if (invokeContext != null) {
        // 客户端异步调用的情况下,上下文会放在InvokeContext中传递
        context = invokeContext.get(RemotingConstants.INVOKE_CTX_RPC_CTX);
    }
    if (context == null) {
        context = RpcInternalContext.getContext();
    }
    int cost = context.getStopWatch().tick().read();
    int requestSize = RpcProtocol.getRequestHeaderLength()
        + requestCommand.getClazzLength()
        + requestCommand.getContentLength()
        + requestCommand.getHeaderLength();
    // 记录请求序列化大小和请求序列化耗时
    context.setAttachment(RpcConstants.INTERNAL_KEY_REQ_SIZE, requestSize);
    context.setAttachment(RpcConstants.INTERNAL_KEY_REQ_SERIALIZE_TIME, cost);
}
 
Example 4
Source File: SofaRpcSerialization.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
/**
 * 客户端记录响应反序列化大小和响应反序列化耗时
 *
 * @param responseCommand 响应体
 */
private void recordDeserializeResponse(RpcResponseCommand responseCommand, InvokeContext invokeContext) {
    if (!RpcInternalContext.isAttachmentEnable()) {
        return;
    }
    RpcInternalContext context = null;
    if (invokeContext != null) {
        // 客户端异步调用的情况下,上下文会放在InvokeContext中传递
        context = invokeContext.get(RemotingConstants.INVOKE_CTX_RPC_CTX);
    }
    if (context == null) {
        context = RpcInternalContext.getContext();
    }
    int cost = context.getStopWatch().tick().read();
    int respSize = RpcProtocol.getResponseHeaderLength()
        + responseCommand.getClazzLength()
        + responseCommand.getContentLength()
        + responseCommand.getHeaderLength();
    // 记录响应反序列化大小和响应反序列化耗时
    context.setAttachment(RpcConstants.INTERNAL_KEY_RESP_SIZE, respSize);
    context.setAttachment(RpcConstants.INTERNAL_KEY_RESP_DESERIALIZE_TIME, cost);
}
 
Example 5
Source File: RpcRemoting.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
/**
 * Convert application request object to remoting request command.
 * 
 * @param request
 * @param conn
 * @param timeoutMillis
 * @return
 * @throws CodecException
 */
protected RemotingCommand toRemotingCommand(Object request, Connection conn,
                                            InvokeContext invokeContext, int timeoutMillis)
                                                                                           throws SerializationException {
    RpcRequestCommand command = this.getCommandFactory().createRequestCommand(request);

    if (null != invokeContext) {
        // set client custom serializer for request command if not null
        Object clientCustomSerializer = invokeContext.get(InvokeContext.BOLT_CUSTOM_SERIALIZER);
        if (null != clientCustomSerializer) {
            try {
                command.setSerializer((Byte) clientCustomSerializer);
            } catch (ClassCastException e) {
                throw new IllegalArgumentException(
                    "Illegal custom serializer [" + clientCustomSerializer
                            + "], the type of value should be [byte], but now is ["
                            + clientCustomSerializer.getClass().getName() + "].");
            }
        }

        // enable crc by default, user can disable by set invoke context `false` for key `InvokeContext.BOLT_CRC_SWITCH`
        Boolean crcSwitch = invokeContext.get(InvokeContext.BOLT_CRC_SWITCH,
            ProtocolSwitch.CRC_SWITCH_DEFAULT_VALUE);
        if (null != crcSwitch && crcSwitch) {
            command.setProtocolSwitch(ProtocolSwitch
                .create(new int[] { ProtocolSwitch.CRC_SWITCH_INDEX }));
        }
    } else {
        // enable crc by default, if there is no invoke context.
        command.setProtocolSwitch(ProtocolSwitch
            .create(new int[] { ProtocolSwitch.CRC_SWITCH_INDEX }));
    }
    command.setTimeout(timeoutMillis);
    command.setRequestClass(request.getClass().getName());
    command.setInvokeContext(invokeContext);
    command.serialize();
    logDebugInfo(command);
    return command;
}
 
Example 6
Source File: CreateConnLockTest.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
private long getAndPrintCreateConnTime(InvokeContext ctx) {
    long time = ctx.get(InvokeContext.CLIENT_CONN_CREATETIME) == null ? -1l : (Long) ctx
        .get(InvokeContext.CLIENT_CONN_CREATETIME);
    if (time > 1500) {
        whetherConnectTimeoutConsumedTooLong.set(true);
    }
    logger.warn("CREATE CONN TIME CONSUMED: " + time);
    return time;
}
 
Example 7
Source File: BoltClientTransport.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
protected void putToContextIfNotNull(InvokeContext invokeContext, String oldKey,
                                     RpcInternalContext context, String key) {
    Object value = invokeContext.get(oldKey);
    if (value != null) {
        context.setAttachment(key, value);
    }
}
 
Example 8
Source File: BoltServerProcessor.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
private void putToContextIfNotNull(InvokeContext invokeContext, String oldKey,
                                   RpcInternalContext context, String key) {
    Object value = invokeContext.get(oldKey);
    if (value != null) {
        context.setAttachment(key, value);
    }
}