Java Code Examples for com.alipay.remoting.rpc.protocol.RpcRequestCommand#getRequestHeader()

The following examples show how to use com.alipay.remoting.rpc.protocol.RpcRequestCommand#getRequestHeader() . 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: SofaRpcSerialization.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public <Request extends RequestCommand> boolean deserializeHeader(Request request)
    throws DeserializationException {
    if (request instanceof RpcRequestCommand) {
        RpcInternalContext.getContext().getStopWatch().tick();

        RpcRequestCommand requestCommand = (RpcRequestCommand) request;
        if (requestCommand.getRequestHeader() != null) {
            // 代表已经提前解析过了,例如使用自定义业务线程池的时候,bolt会提前解析变长Header的数据
            return true;
        }
        byte[] header = requestCommand.getHeader();
        // 解析头部
        Map<String, String> headerMap = mapSerializer.decode(header);
        requestCommand.setRequestHeader(headerMap);
        RpcInvokeContext.getContext().put(RpcConstants.SOFA_REQUEST_HEADER_KEY,
            Collections.unmodifiableMap(headerMap));

        return true;
    }
    return false;
}
 
Example 2
Source File: ProtobufSerializerTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncodeDecodeAppendEntiresRequestHeader() throws Exception {
    final AppendEntriesRequest reqObject = AppendEntriesRequest.newBuilder() //
        .setGroupId("testGroup") //
        .setPeerId("testPeer")//
        .setServerId("testServer") //
        .setTerm(1)//
        .setPrevLogIndex(1)//
        .setPrevLogTerm(0) //
        .setCommittedIndex(1).build();
    final RpcCommandFactory cmdFactory = new RpcCommandFactory();
    final RpcRequestCommand request = cmdFactory.createRequestCommand(reqObject);
    request.setRequestClass(AppendEntriesRequest.class.getName());
    assertNull(request.getHeader());
    assertTrue(serializer.serializeContent(request, null));
    assertTrue(serializer.serializeHeader(request, null));
    assertNull(request.getRequestHeader());

    request.setRequestObject(null);
    assertTrue(serializer.deserializeContent(request));
    assertTrue(serializer.deserializeHeader(request));
    assertNotNull(request.getRequestObject());
    assertNotNull(request.getRequestHeader());

    assertEquals(reqObject, request.getRequestObject());
    assertNotSame(reqObject, request.getRequestObject());

    final AppendEntriesRequestHeader header = (AppendEntriesRequestHeader) request.getRequestHeader();
    assertEquals("testGroup", header.getGroupId());
    assertEquals("testPeer", header.getPeerId());
    assertEquals("testServer", header.getServerId());

}
 
Example 3
Source File: SofaRpcSerialization.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public <Request extends RequestCommand> boolean serializeContent(Request request, InvokeContext invokeContext)
    throws SerializationException {
    if (request instanceof RpcRequestCommand) {
        RpcRequestCommand requestCommand = (RpcRequestCommand) request;
        Object requestObject = requestCommand.getRequestObject();
        byte serializerCode = requestCommand.getSerializer();
        try {
            Map<String, String> header = (Map<String, String>) requestCommand.getRequestHeader();
            if (header == null) {
                header = new HashMap<String, String>();
            }
            putKV(header, RemotingConstants.HEAD_GENERIC_TYPE,
                (String) invokeContext.get(RemotingConstants.HEAD_GENERIC_TYPE));

            Serializer rpcSerializer = com.alipay.sofa.rpc.codec.SerializerFactory
                .getSerializer(serializerCode);
            AbstractByteBuf byteBuf = rpcSerializer.encode(requestObject, header);
            request.setContent(byteBuf.array());
            return true;
        } catch (Exception ex) {
            throw new SerializationException(ex.getMessage(), ex);
        } finally {
            recordSerializeRequest(requestCommand, invokeContext);
        }
    }
    return false;
}
 
Example 4
Source File: SofaRpcSerialization.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Override
public <Request extends RequestCommand> boolean deserializeContent(Request request)
    throws DeserializationException {
    if (request instanceof RpcRequestCommand) {
        RpcRequestCommand requestCommand = (RpcRequestCommand) request;
        Object header = requestCommand.getRequestHeader();
        if (!(header instanceof Map)) {
            throw new DeserializationException("Head of request is null or is not map");
        }
        Map<String, String> headerMap = (Map<String, String>) header;
        byte[] content = requestCommand.getContent();
        if (content == null || content.length == 0) {
            throw new DeserializationException("Content of request is null");
        }
        try {
            String service = headerMap.get(RemotingConstants.HEAD_SERVICE);
            ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();

            ClassLoader serviceClassLoader = ReflectCache.getServiceClassLoader(service);
            try {
                Thread.currentThread().setContextClassLoader(serviceClassLoader);

                Serializer rpcSerializer = com.alipay.sofa.rpc.codec.SerializerFactory
                    .getSerializer(requestCommand.getSerializer());
                Object sofaRequest = ClassUtils.forName(requestCommand.getRequestClass()).newInstance();
                rpcSerializer.decode(new ByteArrayWrapperByteBuf(requestCommand.getContent()),
                    sofaRequest, headerMap);

                //for service mesh or other scene, we need to add more info from header
                if (sofaRequest instanceof SofaRequest) {
                    for (Map.Entry<String, String> entry : headerMap.entrySet()) {
                        ((SofaRequest) sofaRequest).addRequestProp(entry.getKey(), entry.getValue());
                    }
                }

                requestCommand.setRequestObject(sofaRequest);
            } finally {
                Thread.currentThread().setContextClassLoader(oldClassLoader);
            }

            return true;
        } catch (Exception ex) {
            throw new DeserializationException(ex.getMessage(), ex);
        } finally {
            recordDeserializeRequest(requestCommand);
        }
    }
    return false;
}