com.alipay.remoting.rpc.protocol.RpcResponseCommand Java Examples
The following examples show how to use
com.alipay.remoting.rpc.protocol.RpcResponseCommand.
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 |
/** * 客户端记录响应反序列化大小和响应反序列化耗时 * * @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 #2
Source File: ProtobufSerializerTest.java From sofa-jraft with Apache License 2.0 | 6 votes |
@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()); }
Example #3
Source File: SofaRpcSerialization.java From sofa-rpc with Apache License 2.0 | 6 votes |
@Override public <Response extends ResponseCommand> boolean serializeContent(Response response) throws SerializationException { if (response instanceof RpcResponseCommand) { RpcResponseCommand responseCommand = (RpcResponseCommand) response; byte serializerCode = response.getSerializer(); 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) { throw new SerializationException(ex.getMessage(), ex); } finally { recordSerializeResponse(responseCommand); } } return false; }
Example #4
Source File: SofaRpcSerialization.java From sofa-rpc with Apache License 2.0 | 6 votes |
@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); } response.setHeader(mapSerializer.encode(sofaResponse.getResponseProps())); } return true; } return false; }
Example #5
Source File: CustomHeaderSerializer.java From sofa-bolt with Apache License 2.0 | 6 votes |
/** * @see com.alipay.remoting.CustomSerializer#deserializeHeader(com.alipay.remoting.rpc.ResponseCommand, InvokeContext) */ @Override public <T extends ResponseCommand> boolean deserializeHeader(T response, InvokeContext invokeContext) throws DeserializationException { if (response instanceof RpcResponseCommand) { RpcResponseCommand responseCommand = (RpcResponseCommand) response; byte[] header = responseCommand.getHeader(); try { responseCommand.setResponseHeader(new String(header, "UTF-8")); } catch (UnsupportedEncodingException e) { System.err.println("UnsupportedEncodingException"); } return true; } return false; }
Example #6
Source File: NormalStringCustomSerializer_InvokeContext.java From sofa-bolt with Apache License 2.0 | 6 votes |
/** * @see CustomSerializer#deserializeContent(ResponseCommand, InvokeContext) */ @Override public <T extends ResponseCommand> boolean deserializeContent(T response, InvokeContext invokeContext) throws DeserializationException { deserialFlag.set(true); RpcResponseCommand rpcResp = (RpcResponseCommand) response; if (StringUtils.equals(SERIALTYPE1_value, (String) invokeContext.get(SERIALTYPE_KEY))) { try { rpcResp.setResponseObject(new String(rpcResp.getContent(), "UTF-8") + "RANDOM"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } else { rpcResp.setResponseObject(UNIVERSAL_RESP); } return true; }
Example #7
Source File: NormalStringCustomSerializer.java From sofa-bolt with Apache License 2.0 | 5 votes |
/** * @see CustomSerializer#deserializeContent(ResponseCommand, InvokeContext) */ @Override public <T extends ResponseCommand> boolean deserializeContent(T response, InvokeContext invokeContext) throws DeserializationException { deserialFlag.set(true); RpcResponseCommand rpcResp = (RpcResponseCommand) response; try { rpcResp.setResponseObject(new String(rpcResp.getContent(), "UTF-8") + "RANDOM"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } contentDeserialier = response.getSerializer(); return true; }
Example #8
Source File: SofaRpcSerialization.java From sofa-rpc with Apache License 2.0 | 5 votes |
@Override public <Response extends ResponseCommand> boolean deserializeContent(Response response, InvokeContext invokeContext) throws DeserializationException { if (response instanceof RpcResponseCommand) { RpcResponseCommand responseCommand = (RpcResponseCommand) response; byte serializer = response.getSerializer(); byte[] content = responseCommand.getContent(); if (content == null || content.length == 0) { return false; } try { Object sofaResponse = ClassUtils.forName(responseCommand.getResponseClass()).newInstance(); Map<String, String> header = (Map<String, String>) responseCommand.getResponseHeader(); if (header == null) { header = new HashMap<String, String>(); } putKV(header, RemotingConstants.HEAD_TARGET_SERVICE, (String) invokeContext.get(RemotingConstants.HEAD_TARGET_SERVICE)); putKV(header, RemotingConstants.HEAD_METHOD_NAME, (String) invokeContext.get(RemotingConstants.HEAD_METHOD_NAME)); putKV(header, RemotingConstants.HEAD_GENERIC_TYPE, (String) invokeContext.get(RemotingConstants.HEAD_GENERIC_TYPE)); Serializer rpcSerializer = com.alipay.sofa.rpc.codec.SerializerFactory.getSerializer(serializer); rpcSerializer.decode(new ByteArrayWrapperByteBuf(responseCommand.getContent()), sofaResponse, header); responseCommand.setResponseObject(sofaResponse); return true; } catch (Exception ex) { throw new DeserializationException(ex.getMessage(), ex); } finally { recordDeserializeResponse(responseCommand, invokeContext); } } return false; }
Example #9
Source File: SofaRpcSerialization.java From sofa-rpc with Apache License 2.0 | 5 votes |
/** * 服务端记录序列化响应的大小和耗时 * * @param responseCommand 响应体 */ private void recordSerializeResponse(RpcResponseCommand responseCommand) { if (!RpcInternalContext.isAttachmentEnable()) { return; } RpcInternalContext 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_SERIALIZE_TIME, cost); }
Example #10
Source File: SofaRpcSerialization.java From sofa-rpc with Apache License 2.0 | 5 votes |
@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; }
Example #11
Source File: CustomHeaderSerializer.java From sofa-bolt with Apache License 2.0 | 5 votes |
/** * @see com.alipay.remoting.CustomSerializer#serializeHeader(com.alipay.remoting.rpc.ResponseCommand) */ @Override public <T extends ResponseCommand> boolean serializeHeader(T response) throws SerializationException { if (response instanceof RpcResponseCommand) { RpcResponseCommand responseCommand = (RpcResponseCommand) response; try { responseCommand.setHeader(EXECUTOR1.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { System.err.println("UnsupportedEncodingException"); } return true; } return false; }
Example #12
Source File: NormalStringCustomSerializer_InvokeContext.java From sofa-bolt with Apache License 2.0 | 5 votes |
/** * @see CustomSerializer#serializeContent(ResponseCommand) */ @Override public <T extends ResponseCommand> boolean serializeContent(T response) throws SerializationException { serialFlag.set(true); RpcResponseCommand rpcResp = (RpcResponseCommand) response; String str = (String) rpcResp.getResponseObject(); try { rpcResp.setContent(str.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return true; }
Example #13
Source File: ProtobufSerializer.java From sofa-jraft with Apache License 2.0 | 5 votes |
@Override public <T extends ResponseCommand> boolean serializeContent(T response) throws SerializationException { final RpcResponseCommand cmd = (RpcResponseCommand) response; final Message msg = (Message) cmd.getResponseObject(); cmd.setContent(msg.toByteArray()); return true; }
Example #14
Source File: NormalStringCustomSerializer.java From sofa-bolt with Apache License 2.0 | 5 votes |
/** * @see CustomSerializer#serializeContent(ResponseCommand) */ @Override public <T extends ResponseCommand> boolean serializeContent(T response) throws SerializationException { serialFlag.set(true); RpcResponseCommand rpcResp = (RpcResponseCommand) response; String str = (String) rpcResp.getResponseObject(); try { rpcResp.setContent(str.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } contentSerializer = response.getSerializer(); return true; }
Example #15
Source File: RpcCommandFactory.java From sofa-bolt with Apache License 2.0 | 5 votes |
@Override public RpcResponseCommand createExceptionResponse(int id, ResponseStatus status, Throwable t) { RpcResponseCommand responseCommand = this.createExceptionResponse(id, status); responseCommand.setResponseObject(createServerException(t, null)); responseCommand.setResponseClass(RpcServerException.class.getName()); return responseCommand; }
Example #16
Source File: RpcCommandFactory.java From sofa-bolt with Apache License 2.0 | 5 votes |
@Override public RpcResponseCommand createExceptionResponse(int id, ResponseStatus status) { RpcResponseCommand responseCommand = new RpcResponseCommand(); responseCommand.setId(id); responseCommand.setResponseStatus(status); return responseCommand; }
Example #17
Source File: RpcCommandFactory.java From sofa-bolt with Apache License 2.0 | 5 votes |
@Override public RpcResponseCommand createResponse(final Object responseObject, final RemotingCommand requestCmd) { RpcResponseCommand response = new RpcResponseCommand(requestCmd.getId(), responseObject); if (null != responseObject) { response.setResponseClass(responseObject.getClass().getName()); } else { response.setResponseClass(null); } response.setSerializer(requestCmd.getSerializer()); response.setProtocolSwitch(requestCmd.getProtocolSwitch()); response.setResponseStatus(ResponseStatus.SUCCESS); return response; }
Example #18
Source File: RpcResponseResolver.java From sofa-bolt with Apache License 2.0 | 5 votes |
/** * Detail your error msg with the error msg returned from response command */ private static String detailErrMsg(String clientErrMsg, ResponseCommand responseCommand) { RpcResponseCommand resp = (RpcResponseCommand) responseCommand; if (StringUtils.isNotBlank(resp.getErrorMsg())) { return String.format("%s, ServerErrorMsg:%s", clientErrMsg, resp.getErrorMsg()); } else { return String.format("%s, ServerErrorMsg:null", clientErrMsg); } }
Example #19
Source File: RpcResponseResolver.java From sofa-bolt with Apache License 2.0 | 5 votes |
/** * Convert remoting response command to throwable if it is a throwable, otherwise return null. */ private static Throwable toThrowable(ResponseCommand responseCommand) throws CodecException { RpcResponseCommand resp = (RpcResponseCommand) responseCommand; resp.deserialize(); Object ex = resp.getResponseObject(); if (ex instanceof Throwable) { return (Throwable) ex; } return null; }
Example #20
Source File: ProtobufSerializer.java From sofa-jraft with Apache License 2.0 | 5 votes |
@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; }
Example #21
Source File: RpcCommandFactory.java From sofa-bolt with Apache License 2.0 | 4 votes |
@Override public RpcResponseCommand createExceptionResponse(int id, String errMsg) { return createExceptionResponse(id, null, errMsg); }
Example #22
Source File: RpcResponseResolver.java From sofa-bolt with Apache License 2.0 | 4 votes |
/** * Convert remoting response command to application response object. */ private static Object toResponseObject(ResponseCommand responseCommand) throws CodecException { RpcResponseCommand response = (RpcResponseCommand) responseCommand; response.deserialize(); return response.getResponseObject(); }