use of com.alipay.remoting.rpc.protocol.RpcResponseCommand in project sofa-jraft by sofastack.
the class ProtobufSerializerTest method testEncodeDecodeResponseContent.
@Test
public void testEncodeDecodeResponseContent() throws Exception {
final PingRequest reqObject = TestUtils.createPingRequest();
final RpcRequestCommand request = cmdFactory.createRequestCommand(reqObject);
final ErrorResponse respObject = (ErrorResponse) RpcFactoryHelper.responseFactory().newResponse(null, new Status(-1, "test"));
final RpcResponseCommand response = cmdFactory.createResponse(respObject, request);
response.setResponseClass(ErrorResponse.class.getName());
assertTrue(serializer.serializeContent(response));
response.setResponseObject(null);
assertTrue(serializer.deserializeContent(response, null));
assertNotNull(response.getResponseObject());
assertEquals(respObject, response.getResponseObject());
assertNotSame(respObject, response.getResponseObject());
}
use of com.alipay.remoting.rpc.protocol.RpcResponseCommand in project sofa-rpc by sofastack.
the class SofaRpcSerialization method deserializeHeader.
@Override
public <Response extends ResponseCommand> boolean deserializeHeader(Response response, InvokeContext invokeContext) throws DeserializationException {
if (response instanceof RpcResponseCommand) {
RpcInternalContext.getContext().getStopWatch().tick();
RpcResponseCommand responseCommand = (RpcResponseCommand) response;
byte[] header = responseCommand.getHeader();
responseCommand.setResponseHeader(mapSerializer.decode(header));
return true;
}
return false;
}
use of com.alipay.remoting.rpc.protocol.RpcResponseCommand in project sofa-rpc by sofastack.
the class SofaRpcSerialization method serializeHeader.
@Override
public <Response extends ResponseCommand> boolean serializeHeader(Response response) throws SerializationException {
if (response instanceof RpcResponseCommand) {
RpcInternalContext.getContext().getStopWatch().tick();
Object responseObject = ((RpcResponseCommand) response).getResponseObject();
if (responseObject instanceof SofaResponse) {
SofaResponse sofaResponse = (SofaResponse) responseObject;
if (sofaResponse.isError() || sofaResponse.getAppResponse() instanceof Throwable) {
sofaResponse.addResponseProp(RemotingConstants.HEAD_RESPONSE_ERROR, StringUtils.TRUE);
}
byte[] header = null;
try {
header = mapSerializer.encode(sofaResponse.getResponseProps());
} catch (Exception e) {
String traceId = (String) RpcInternalContext.getContext().getAttachment("_trace_id");
String rpcId = (String) RpcInternalContext.getContext().getAttachment("_span_id");
LOGGER.error("traceId={}, rpcId={}, Response serializeHeader exception, msg={}", traceId, rpcId, e.getMessage(), e);
throw new SerializationException(e.getMessage() + ", traceId=" + traceId + ", rpcId=" + rpcId, e);
}
response.setHeader(header);
}
return true;
}
return false;
}
use of com.alipay.remoting.rpc.protocol.RpcResponseCommand in project sofa-rpc by sofastack.
the class SofaRpcSerialization method serializeContent.
@Override
public <Response extends ResponseCommand> boolean serializeContent(Response response) throws SerializationException {
if (response instanceof RpcResponseCommand) {
RpcResponseCommand responseCommand = (RpcResponseCommand) response;
byte serializerCode = response.getSerializer();
long serializeStartTime = System.nanoTime();
try {
Serializer rpcSerializer = com.alipay.sofa.rpc.codec.SerializerFactory.getSerializer(serializerCode);
AbstractByteBuf byteBuf = rpcSerializer.encode(responseCommand.getResponseObject(), null);
responseCommand.setContent(byteBuf.array());
return true;
} catch (Exception ex) {
String traceId = (String) RpcInternalContext.getContext().getAttachment("_trace_id");
String rpcId = (String) RpcInternalContext.getContext().getAttachment("_span_id");
LOGGER.error("traceId={}, rpcId={}, Response serializeContent exception, msg = {}", traceId, rpcId, ex.getMessage(), ex);
throw new SerializationException(ex.getMessage() + ", traceId=" + traceId + ", rpcId=" + rpcId, ex);
} finally {
// R6:Record response serialization time
recordSerializeResponse(responseCommand, serializeStartTime);
}
}
return false;
}
use of com.alipay.remoting.rpc.protocol.RpcResponseCommand in project dingo by dingodb.
the class ProtobufSerializer method deserializeContent.
@Override
public <T extends ResponseCommand> boolean deserializeContent(T response, InvokeContext invokeContext) throws DeserializationException {
final RpcResponseCommand cmd = (RpcResponseCommand) response;
final String className = cmd.getResponseClass();
cmd.setResponseObject(ProtobufMsgFactory.newMessageByJavaClassName(className, response.getContent()));
return true;
}
Aggregations