com.alipay.remoting.rpc.protocol.RpcRequestCommand Java Examples
The following examples show how to use
com.alipay.remoting.rpc.protocol.RpcRequestCommand.
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: ProtobufSerializer.java From sofa-jraft with Apache License 2.0 | 6 votes |
@Override public <T extends RequestCommand> boolean serializeHeader(T request, InvokeContext invokeContext) throws SerializationException { final RpcRequestCommand cmd = (RpcRequestCommand) request; final Message msg = (Message) cmd.getRequestObject(); if (msg instanceof RpcRequests.AppendEntriesRequest) { final RpcRequests.AppendEntriesRequest req = (RpcRequests.AppendEntriesRequest) msg; final RpcRequests.AppendEntriesRequestHeader.Builder hb = RpcRequests.AppendEntriesRequestHeader .newBuilder() // .setGroupId(req.getGroupId()) // .setPeerId(req.getPeerId()) // .setServerId(req.getServerId()); cmd.setHeader(hb.build().toByteArray()); return true; } return false; }
Example #2
Source File: SofaRpcSerialization.java From sofa-rpc with Apache License 2.0 | 6 votes |
@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 #3
Source File: SofaRpcSerialization.java From sofa-rpc with Apache License 2.0 | 6 votes |
@Override public <Request extends RequestCommand> boolean serializeHeader(Request request, InvokeContext invokeContext) throws SerializationException { if (request instanceof RpcRequestCommand) { RpcInternalContext.getContext().getStopWatch().tick(); RpcRequestCommand requestCommand = (RpcRequestCommand) request; Object requestObject = requestCommand.getRequestObject(); String service = getTargetServiceName(requestObject); if (StringUtils.isNotEmpty(service)) { Map<String, String> header = new HashMap<String, String>(16); header.put(RemotingConstants.HEAD_SERVICE, service); putRequestMetadataToHeader(requestObject, header); requestCommand.setHeader(mapSerializer.encode(header)); } return true; } return false; }
Example #4
Source File: CustomHeaderSerializer.java From sofa-bolt with Apache License 2.0 | 6 votes |
/** * @see com.alipay.remoting.CustomSerializer#deserializeHeader(com.alipay.remoting.rpc.RequestCommand) */ @Override public <T extends RequestCommand> boolean deserializeHeader(T request) throws DeserializationException { if (request instanceof RpcRequestCommand) { RpcRequestCommand requestCommand = (RpcRequestCommand) request; byte[] header = requestCommand.getHeader(); try { requestCommand.setRequestHeader(new String(header, "UTF-8")); } catch (UnsupportedEncodingException e) { System.err.println("UnsupportedEncodingException"); } return true; } return false; }
Example #5
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 #6
Source File: NormalRequestBodyCustomSerializer_InvokeContext.java From sofa-bolt with Apache License 2.0 | 6 votes |
/** * @see CustomSerializer#deserializeContent(RequestCommand) */ @Override public <T extends RequestCommand> boolean deserializeContent(T req) throws DeserializationException { deserialFlag.set(true); RpcRequestCommand rpcReq = (RpcRequestCommand) req; byte[] content = rpcReq.getContent(); ByteBuffer bb = ByteBuffer.wrap(content); int a = bb.getInt(); byte[] dst = new byte[content.length - 4]; bb.get(dst, 0, dst.length); try { String b = new String(dst, "UTF-8"); RequestBody bd = new RequestBody(a, b); rpcReq.setRequestObject(bd); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return true; }
Example #7
Source File: NormalRequestBodyCustomSerializer.java From sofa-bolt with Apache License 2.0 | 6 votes |
/** * @see CustomSerializer#serializeContent(RequestCommand, InvokeContext) */ @Override public <T extends RequestCommand> boolean serializeContent(T req, InvokeContext invokeContext) throws SerializationException { serialFlag.set(true); RpcRequestCommand rpcReq = (RpcRequestCommand) req; RequestBody bd = (RequestBody) rpcReq.getRequestObject(); int id = bd.getId(); byte[] msg; try { msg = bd.getMsg().getBytes("UTF-8"); ByteBuffer bb = ByteBuffer.allocate(4 + msg.length); bb.putInt(id); bb.put(msg); rpcReq.setContent(bb.array()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } contentSerializer = rpcReq.getSerializer(); return true; }
Example #8
Source File: NormalRequestBodyCustomSerializer.java From sofa-bolt with Apache License 2.0 | 6 votes |
/** * @see CustomSerializer#deserializeContent(RequestCommand) */ @Override public <T extends RequestCommand> boolean deserializeContent(T req) throws DeserializationException { deserialFlag.set(true); RpcRequestCommand rpcReq = (RpcRequestCommand) req; byte[] content = rpcReq.getContent(); ByteBuffer bb = ByteBuffer.wrap(content); int a = bb.getInt(); byte[] dst = new byte[content.length - 4]; bb.get(dst, 0, dst.length); try { String b = new String(dst, "UTF-8"); RequestBody bd = new RequestBody(a, b); rpcReq.setRequestObject(bd); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } contentDeserializer = rpcReq.getSerializer(); return true; }
Example #9
Source File: ProtobufSerializer.java From sofa-jraft with Apache License 2.0 | 5 votes |
@Override public <T extends RequestCommand> boolean deserializeHeader(T request) throws DeserializationException { final RpcRequestCommand cmd = (RpcRequestCommand) request; final String className = cmd.getRequestClass(); if (className.equals(RpcRequests.AppendEntriesRequest.class.getName())) { final byte[] header = cmd.getHeader(); cmd.setRequestHeader(ProtobufMsgFactory.newMessageByJavaClassName( RpcRequests.AppendEntriesRequestHeader.class.getName(), header)); return true; } return false; }
Example #10
Source File: SofaRpcSerialization.java From sofa-rpc with Apache License 2.0 | 5 votes |
@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 #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.RequestCommand, InvokeContext) */ @Override public <T extends RequestCommand> boolean serializeHeader(T request, InvokeContext invokeContext) throws SerializationException { if (request instanceof RpcRequestCommand) { RpcRequestCommand requestCommand = (RpcRequestCommand) request; try { requestCommand.setHeader(EXECUTOR1.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { System.err.println("UnsupportedEncodingException"); } return true; } return false; }
Example #12
Source File: RpcRemoting.java From sofa-bolt with Apache License 2.0 | 5 votes |
/** * 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 #13
Source File: ProtobufSerializerTest.java From sofa-jraft with Apache License 2.0 | 5 votes |
@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 #14
Source File: ProtobufSerializerTest.java From sofa-jraft with Apache License 2.0 | 5 votes |
@Test public void testEncodeDecodeRequestContent() throws Exception { final PingRequest reqObject = TestUtils.createPingRequest(); final RpcRequestCommand request = cmdFactory.createRequestCommand(reqObject); request.setRequestClass(PingRequest.class.getName()); assertTrue(serializer.serializeContent(request, null)); request.setRequestObject(null); assertTrue(serializer.deserializeContent(request)); assertNotNull(request.getRequestObject()); assertEquals(reqObject, request.getRequestObject()); assertNotSame(reqObject, request.getRequestObject()); }
Example #15
Source File: ProtobufSerializer.java From sofa-jraft with Apache License 2.0 | 5 votes |
@Override public <T extends RequestCommand> boolean deserializeContent(T request) throws DeserializationException { final RpcRequestCommand cmd = (RpcRequestCommand) request; final String className = cmd.getRequestClass(); cmd.setRequestObject(ProtobufMsgFactory.newMessageByJavaClassName(className, request.getContent())); return true; }
Example #16
Source File: ProtobufSerializer.java From sofa-jraft with Apache License 2.0 | 5 votes |
@Override public <T extends RequestCommand> boolean serializeContent(T request, InvokeContext invokeContext) throws SerializationException { final RpcRequestCommand cmd = (RpcRequestCommand) request; final Message msg = (Message) cmd.getRequestObject(); cmd.setContent(msg.toByteArray()); return true; }
Example #17
Source File: RpcCommandFactory.java From sofa-bolt with Apache License 2.0 | 4 votes |
@Override public RpcRequestCommand createRequestCommand(Object requestObject) { return new RpcRequestCommand(requestObject); }
Example #18
Source File: SofaRpcSerialization.java From sofa-rpc with Apache License 2.0 | 4 votes |
@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; }