org.apache.thrift.protocol.TMessage Java Examples
The following examples show how to use
org.apache.thrift.protocol.TMessage.
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: DefaultWriterListener.java From nettythrift with Apache License 2.0 | 6 votes |
@SuppressWarnings({ "rawtypes" }) @Override public void beforeWrite(TMessage msg, TBase args, TBase result) { // reuse message's buffer when write? yes, we use the pool. ByteBuf readedBuf = message.getContent(); int refCount = readedBuf.refCnt(); if (refCount > 0) { readedBuf.release(refCount); } // voidMethod's return message is very short int initialCapacity = serverDef.trafficForecast.getInitBytesForWrite(msg.name); // logger.debug("initialCapacity = {} , msg = {}",initialCapacity, msg); ByteBuf buf = ctx.alloc().buffer(initialCapacity, serverDef.maxFrameSize); message.setContent(buf).beforeWrite(ctx); transport.setOutputBuffer(buf); }
Example #2
Source File: ThriftIDLSerializer.java From octo-rpc with Apache License 2.0 | 6 votes |
protected RpcResult doDeserializeResponse(DefaultResponse response, TMessage message, TProtocol protocol) throws Exception { RpcResult rpcResult = new RpcResult(); if (message.type == TMessageType.REPLY) { Object realResult = deserializeResult(protocol, response.getServiceInterface().getName(), message.name); if (realResult instanceof Exception) { // 服务端自定义异常 response.setException((Exception) realResult); } rpcResult.setReturnVal(realResult); } else if (message.type == TMessageType.EXCEPTION) { TApplicationException exception = TApplicationException.read(protocol); MetaUtil.wrapException(exception, response); } if (!response.isOctoProtocol() && hasOldRequestHeader(protocol)) { // 解析老协议的Header RequestHeader requestHeader = new RequestHeader(); protocol.readFieldBegin(); requestHeader.read(protocol); protocol.readFieldEnd(); } return rpcResult; }
Example #3
Source File: MappedFileTBinaryProtocol.java From singer with Apache License 2.0 | 6 votes |
/** * Reading methods. */ public TMessage readMessageBegin() throws TException { int size = readI32(); if (size < 0) { int version = size & VERSION_MASK; if (version != VERSION_1) { throw new TProtocolException(TProtocolException.BAD_VERSION, "Bad version in readMessageBegin"); } return new TMessage(readString(), (byte) (size & 0x000000ff), readI32()); } else { if (strictRead) { throw new TProtocolException(TProtocolException.BAD_VERSION, "Missing version in readMessageBegin"); } return new TMessage(readStringBody(size), readByte(), readI32()); } }
Example #4
Source File: ThriftJacksonTest.java From armeria with Apache License 2.0 | 6 votes |
@Test void serializeThriftCall() throws IOException { final ThriftCall call = new ThriftCall( new TMessage(THRIFT_METHOD_NAME, TMessageType.CALL, 0), new hello_args().setName("kawamuray")); final String actualJson = defaultMapper.writeValueAsString(call); final String actualJson2 = customMapper.writeValueAsString(call); assertThatJson(actualJson).isEqualTo( '{' + " \"header\": {" + " \"name\": \"hello\"," + " \"type\": 1," + " \"seqid\": 0" + " }," + " \"args\": {" + " \"name\": \"kawamuray\"" + " }" + '}'); assertThatJson(actualJson2).isEqualTo(actualJson); }
Example #5
Source File: ThriftNativeCodec.java From dubbo-2.6.5 with Apache License 2.0 | 6 votes |
protected void encodeRequest(Channel channel, ChannelBuffer buffer, Request request) throws IOException { Invocation invocation = (Invocation) request.getData(); TProtocol protocol = newProtocol(channel.getUrl(), buffer); try { protocol.writeMessageBegin(new TMessage( invocation.getMethodName(), TMessageType.CALL, thriftSeq.getAndIncrement())); protocol.writeStructBegin(new TStruct(invocation.getMethodName() + "_args")); for (int i = 0; i < invocation.getParameterTypes().length; i++) { Class<?> type = invocation.getParameterTypes()[i]; } } catch (TException e) { throw new IOException(e.getMessage(), e); } }
Example #6
Source File: ThriftIDLSerializer.java From octo-rpc with Apache License 2.0 | 6 votes |
@Override protected Object deserialize4OctoThrift(byte[] buff, Object obj) throws Exception { TMemoryInputTransport transport = new TMemoryInputTransport(buff); TBinaryProtocol protocol = new TBinaryProtocol(transport); TMessage message = protocol.readMessageBegin(); Object result; if (obj instanceof DefaultRequest) { result = doDeserializeRequest((DefaultRequest) obj, message, protocol); } else if (obj instanceof DefaultResponse) { result = doDeserializeResponse((DefaultResponse) obj, message, protocol); } else { throw new ProtocolException("Thrift octoProtocol object type is invalid, it should not happen."); } protocol.readMessageEnd(); return result; }
Example #7
Source File: ThriftJacksonTest.java From armeria with Apache License 2.0 | 6 votes |
@Test void serializeThriftReply() throws IOException { final ThriftReply reply = new ThriftReply( new TMessage(THRIFT_METHOD_NAME, TMessageType.REPLY, 0), new hello_result().setSuccess("Hello kawamuray")); final String actualJson = defaultMapper.writeValueAsString(reply); final String actualJson2 = customMapper.writeValueAsString(reply); assertThatJson(actualJson).isEqualTo( '{' + " \"header\": {" + " \"name\": \"hello\"," + " \"type\": 2," + " \"seqid\": 0" + " }," + " \"result\": {" + " \"success\": \"Hello kawamuray\"" + " }," + " \"exception\": null" + '}'); assertThatJson(actualJson2).isEqualTo(actualJson); }
Example #8
Source File: TTextProtocol.java From armeria with Apache License 2.0 | 6 votes |
/** * I believe these two messages are called for a thrift service * interface. We don't plan on storing any text objects of that * type on disk. */ @Override public void writeMessageBegin(TMessage message) throws TException { try { getCurrentWriter().writeStartObject(); getCurrentWriter().writeFieldName("method"); getCurrentWriter().writeString(message.name); getCurrentWriter().writeFieldName("type"); TypedParser.TMESSAGE_TYPE.writeValue(getCurrentWriter(), message.type); getCurrentWriter().writeFieldName("seqid"); getCurrentWriter().writeNumber(message.seqid); getCurrentWriter().writeFieldName("args"); } catch (IOException e) { throw new TTransportException(e); } }
Example #9
Source File: ThriftJacksonTest.java From armeria with Apache License 2.0 | 6 votes |
@Test void serializeThriftReplyWithException() throws IOException { final ThriftReply reply = new ThriftReply( new TMessage(THRIFT_METHOD_NAME, TMessageType.EXCEPTION, 0), new TApplicationException(1, "don't wanna say hello")); final String actualJson = defaultMapper.writeValueAsString(reply); final String actualJson2 = customMapper.writeValueAsString(reply); assertThatJson(actualJson).isEqualTo( '{' + " \"header\": {" + " \"name\": \"hello\"," + " \"type\": 3," + " \"seqid\": 0" + " }," + " \"result\": null," + " \"exception\": {" + " \"type\": 1," + " \"message\": \"don't wanna say hello\"" + " }" + '}'); assertThatJson(actualJson2).isEqualTo(actualJson); }
Example #10
Source File: ThriftJacksonSerializers.java From armeria with Apache License 2.0 | 6 votes |
@Override public JsonSerializer<?> findSerializer(SerializationConfig config, JavaType type, BeanDescription beanDesc) { final Class<?> rawType = type.getRawClass(); if (TMessage.class.isAssignableFrom(rawType)) { return new TMessageJsonSerializer(); } if (TBase.class.isAssignableFrom(rawType)) { return new TBaseJsonSerializer(useNamedEnums); } if (TApplicationException.class.isAssignableFrom(rawType)) { return new TApplicationExceptionJsonSerializer(useNamedEnums); } if (ThriftCall.class.isAssignableFrom(rawType)) { return new ThriftCallJsonSerializer(useNamedEnums); } if (ThriftReply.class.isAssignableFrom(rawType)) { return new ThriftReplyJsonSerializer(useNamedEnums); } return super.findSerializer(config, type, beanDesc); }
Example #11
Source File: TProtobufProcessor.java From jigsaw-payment with Apache License 2.0 | 6 votes |
@Override public boolean process(TProtocol in, TProtocol out) throws TException { TMessage msg = in.readMessageBegin(); Controller<?, ?> fn = (Controller<?, ?>) this.beanFactory .getBean(msg.name); if (fn == null) { if (LOGGER.isWarnEnabled()) { LOGGER.warn("Invalid request: failed to find interface=" + msg.name + ", from: " + getInetAddress(in)); } TProtocolUtil.skip(in, TType.STRUCT); in.readMessageEnd(); TApplicationException x = new TApplicationException( TApplicationException.UNKNOWN_METHOD, "Invalid method name: '" + msg.name + "'"); out.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid)); x.write(out); out.writeMessageEnd(); out.getTransport().flush(); return true; } process(msg.seqid, msg.name, in, out, fn); return true; }
Example #12
Source File: ThriftNativeCodec.java From dubbox with Apache License 2.0 | 6 votes |
protected void encodeRequest(Channel channel, ChannelBuffer buffer, Request request) throws IOException { Invocation invocation = (Invocation) request.getData(); TProtocol protocol = newProtocol(channel.getUrl(), buffer); try { protocol.writeMessageBegin(new TMessage( invocation.getMethodName(), TMessageType.CALL, thriftSeq.getAndIncrement())); protocol.writeStructBegin(new TStruct(invocation.getMethodName() + "_args")); for(int i = 0; i < invocation.getParameterTypes().length; i++) { Class<?> type = invocation.getParameterTypes()[i]; } } catch (TException e) { throw new IOException(e.getMessage(), e); } }
Example #13
Source File: TestDriftNettyServerTransport.java From drift with Apache License 2.0 | 6 votes |
private static ResultCode readLogResponse(int expectedSequenceId, TProtocol protocol) throws TException { TMessage message = protocol.readMessageBegin(); if (message.type == TMessageType.EXCEPTION) { throw TApplicationException.readFrom(protocol); } if (message.type != TMessageType.REPLY) { throw new TApplicationException(MISSING_RESULT, "request failed"); } if (message.seqid != expectedSequenceId) { throw new TApplicationException(BAD_SEQUENCE_ID, format("expected sequenceId %s, but received %s", expectedSequenceId, message.seqid)); } Log_result result = new Log_result(); result.read(protocol); protocol.readMessageEnd(); return result.success; }
Example #14
Source File: TServiceClientNoPrint.java From rpc-benchmark with Apache License 2.0 | 6 votes |
@Override protected void receiveBase(TBase<?, ?> result, String methodName) throws TException { TMessage msg = iprot_.readMessageBegin(); if (msg.type == TMessageType.EXCEPTION) { TApplicationException x = new TApplicationException(); x.read(iprot_); iprot_.readMessageEnd(); throw x; } // System.out.format("Received %d%n", msg.seqid); if (msg.seqid != seqid_) { throw new TApplicationException(TApplicationException.BAD_SEQUENCE_ID, String.format( "%s failed: out of sequence response: expected %d but got %d", methodName, seqid_, msg.seqid)); } result.read(iprot_); iprot_.readMessageEnd(); }
Example #15
Source File: LocatorServiceImpl.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
@Override public final boolean process(final TProtocol in, final TProtocol out) throws TException { final TMessage msg = in.readMessageBegin(); final ProcessFunction<LocatorServiceImpl, ?> fn = this.fnMap .get(msg.name); if (fn != null) { fn.process(msg.seqid, in, out, this.inst); // terminate connection on receiving closeConnection // direct class comparison should be the fastest way return fn.getClass() != LocatorService.Processor.closeConnection.class; } else { TProtocolUtil.skip(in, TType.STRUCT); in.readMessageEnd(); TApplicationException x = new TApplicationException( TApplicationException.UNKNOWN_METHOD, "Invalid method name: '" + msg.name + "'"); out.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid)); x.write(out); out.writeMessageEnd(); out.getTransport().flush(); return true; } }
Example #16
Source File: NettyDispatcher.java From ikasoa with MIT License | 6 votes |
private void sendTApplicationException(TApplicationException e, ChannelHandlerContext ctx, TNettyMessage request, int responseSequenceId, TNettyTransport requestTransport, TProtocol inProtocol, TProtocol outProtocol) { if (ctx.getChannel().isConnected()) { try { TMessage message = inProtocol.readMessageBegin(); outProtocol.writeMessageBegin(new TMessage(message.name, TMessageType.EXCEPTION, message.seqid)); e.write(outProtocol); outProtocol.writeMessageEnd(); requestTransport.setTApplicationException(e); outProtocol.getTransport().flush(); writeResponse(ctx, request.getMessageFactory().create(requestTransport.getOutputBuffer()), responseSequenceId, DispatcherContext.isResponseOrderingRequired(ctx)); } catch (TException ex) { onDispatchException(ctx, ex); } } }
Example #17
Source File: ThriftNativeCodec.java From dubbox with Apache License 2.0 | 6 votes |
protected void encodeRequest(Channel channel, ChannelBuffer buffer, Request request) throws IOException { Invocation invocation = (Invocation) request.getData(); TProtocol protocol = newProtocol(channel.getUrl(), buffer); try { protocol.writeMessageBegin(new TMessage( invocation.getMethodName(), TMessageType.CALL, thriftSeq.getAndIncrement())); protocol.writeStructBegin(new TStruct(invocation.getMethodName() + "_args")); for(int i = 0; i < invocation.getParameterTypes().length; i++) { Class<?> type = invocation.getParameterTypes()[i]; } } catch (TException e) { throw new IOException(e.getMessage(), e); } }
Example #18
Source File: LocatorServiceImpl.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
@Override public final boolean process(final TProtocol in, final TProtocol out) throws TException { final TMessage msg = in.readMessageBegin(); final ProcessFunction<LocatorServiceImpl, ?> fn = this.fnMap .get(msg.name); if (fn != null) { fn.process(msg.seqid, in, out, this.inst); // terminate connection on receiving closeConnection // direct class comparison should be the fastest way return fn.getClass() != LocatorService.Processor.closeConnection.class; } else { TProtocolUtil.skip(in, TType.STRUCT); in.readMessageEnd(); TApplicationException x = new TApplicationException( TApplicationException.UNKNOWN_METHOD, "Invalid method name: '" + msg.name + "'"); out.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid)); x.write(out); out.writeMessageEnd(); out.getTransport().flush(); return true; } }
Example #19
Source File: THttpClientDelegate.java From armeria with Apache License 2.0 | 6 votes |
@Nullable private static TApplicationException readApplicationException(int seqId, ThriftFunction func, TProtocol inputProtocol, TMessage msg) throws TException { if (msg.seqid != seqId) { throw new TApplicationException(TApplicationException.BAD_SEQUENCE_ID); } if (!func.name().equals(msg.name)) { return new TApplicationException(TApplicationException.WRONG_METHOD_NAME, msg.name); } if (msg.type == TMessageType.EXCEPTION) { final TApplicationException appEx = TApplicationExceptions.read(inputProtocol); inputProtocol.readMessageEnd(); return appEx; } return null; }
Example #20
Source File: ThriftNativeCodec.java From dubbox with Apache License 2.0 | 6 votes |
protected void encodeRequest(Channel channel, ChannelBuffer buffer, Request request) throws IOException { Invocation invocation = (Invocation) request.getData(); TProtocol protocol = newProtocol(channel.getUrl(), buffer); try { protocol.writeMessageBegin(new TMessage( invocation.getMethodName(), TMessageType.CALL, thriftSeq.getAndIncrement())); protocol.writeStructBegin(new TStruct(invocation.getMethodName() + "_args")); for(int i = 0; i < invocation.getParameterTypes().length; i++) { Class<?> type = invocation.getParameterTypes()[i]; } } catch (TException e) { throw new IOException(e.getMessage(), e); } }
Example #21
Source File: ApacheThriftMethodInvoker.java From drift with Apache License 2.0 | 6 votes |
private static void writeRequest(MethodMetadata method, List<Object> parameters, TProtocol protocol) throws Exception { TMessage requestMessage = new TMessage(method.getName(), CALL, SEQUENCE_ID); protocol.writeMessageBegin(requestMessage); // write the parameters ProtocolWriter writer = new ProtocolWriter(new ThriftToDriftProtocolWriter(protocol)); writer.writeStructBegin(method.getName() + "_args"); for (int i = 0; i < parameters.size(); i++) { Object value = parameters.get(i); ParameterMetadata parameter = method.getParameters().get(i); writer.writeField(parameter.getName(), parameter.getFieldId(), parameter.getCodec(), value); } writer.writeStructEnd(); protocol.writeMessageEnd(); protocol.getTransport().flush(); }
Example #22
Source File: ThriftJacksonTest.java From armeria with Apache License 2.0 | 5 votes |
@Test void serializeTMessage() throws IOException { assertThatJson(customMapper.writeValueAsString(new TMessage(THRIFT_METHOD_NAME, TMessageType.EXCEPTION, 0))) .isEqualTo('{' + " \"name\": \"hello\"," + " \"type\": 3," + " \"seqid\": 0" + '}'); }
Example #23
Source File: DefaultNettyProcessor.java From nettythrift with Apache License 2.0 | 5 votes |
private void sumbitTask(TProtocol out, final TMessage msg, final WriterHandler onComplete, Runnable task){ try { serverDef.executor.submit(task); } catch (RejectedExecutionException e) { TApplicationException x = new TApplicationException(TApplicationException.INTERNAL_ERROR, "TooBusy"); writeException(out, msg, onComplete, x, null); logger.error("RejectedExecutionException: "+e.getLocalizedMessage()); } }
Example #24
Source File: DefaultNettyProcessor.java From nettythrift with Apache License 2.0 | 5 votes |
@SuppressWarnings({ "rawtypes" }) private void writeException(final TProtocol out, final TMessage msg, final WriterHandler onComplete, final TApplicationException x, TBase args) { Throwable cause = null; try { onComplete.beforeWrite(msg, args, null); out.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid)); x.write(out); out.writeMessageEnd(); out.getTransport().flush(); } catch (Throwable e) { cause = e; } onComplete.afterWrite(msg, cause, TMessageType.EXCEPTION, args, null); }
Example #25
Source File: InternalScribeCodec.java From zipkin-reporter-java with Apache License 2.0 | 5 votes |
/** Returns false if the scribe response was try later. */ public static boolean readLogResponse(int seqid, TBinaryProtocol iprot) throws TException { TMessage msg = iprot.readMessageBegin(); if (msg.type == TMessageType.EXCEPTION) { throw TApplicationException.readFrom(iprot); } else if (msg.seqid != seqid) { throw new TApplicationException(BAD_SEQUENCE_ID, "Log failed: out of sequence response"); } return parseResponse(iprot); }
Example #26
Source File: GFXDServiceImpl.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
@Override public final boolean process(final TProtocol in, final TProtocol out) throws TException { final TMessage msg = in.readMessageBegin(); final ProcessFunction<GFXDServiceImpl, ?> fn = this.fnMap.get(msg.name); if (fn != null) { fn.process(msg.seqid, in, out, this.inst); // terminate connection on receiving closeConnection // direct class comparison should be the fastest way // TODO: SW: also need to clean up connection artifacts in the case of // client connection failure (ConnectionListener does get a notification // but how to tie the socket/connectionNumber to the connectionID?) return fn.getClass() != GFXDService.Processor.closeConnection.class; } else { TProtocolUtil.skip(in, TType.STRUCT); in.readMessageEnd(); TApplicationException x = new TApplicationException( TApplicationException.UNKNOWN_METHOD, "Invalid method name: '" + msg.name + "'"); out.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid)); x.write(out); out.writeMessageEnd(); out.getTransport().flush(); return true; } }
Example #27
Source File: TTextProtocolTest.java From armeria with Apache License 2.0 | 5 votes |
@Test public void rpcException() throws Exception { final String request = "{\n" + " \"method\" : \"doDebug\",\n" + " \"type\" : \"EXCEPTION\",\n" + " \"seqid\" : 101,\n" + " \"args\" : {\n" + " \"e\" : {\n" + " \"reason\" : \"Bad rpc\"\n" + " }\n" + " }\n" + '}'; TTextProtocol prot = new TTextProtocol( new TIOStreamTransport(new ByteArrayInputStream(request.getBytes()))); final TMessage header = prot.readMessageBegin(); final doDebug_result result = new doDebug_result(); result.read(prot); prot.readMessageEnd(); assertThat(header.name).isEqualTo("doDebug"); assertThat(header.type).isEqualTo(TMessageType.EXCEPTION); assertThat(header.seqid).isEqualTo(101); assertThat(result.getE().getReason()).isEqualTo("Bad rpc"); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); prot = new TTextProtocol(new TIOStreamTransport(outputStream)); prot.writeMessageBegin(header); result.write(prot); prot.writeMessageEnd(); assertThatJson(new String(outputStream.toByteArray(), StandardCharsets.UTF_8)).isEqualTo(request); }
Example #28
Source File: CallBack.java From ikasoa with MIT License | 5 votes |
@Override public void write_args(TProtocol prot) throws TException { prot.writeMessageBegin(new TMessage(Processor.FUNCTION_NAME, TMessageType.CALL, 0)); ArgsThriftBase args = new ArgsThriftBase(); args.setFieldValue(AbstractThriftBase.FieldsEnum.VALUE, arg); args.write(prot); prot.writeMessageEnd(); }
Example #29
Source File: TTextProtocolTest.java From armeria with Apache License 2.0 | 5 votes |
@Test public void rpcTApplicationException() throws Exception { final String request = "{\n" + " \"method\" : \"doDebug\",\n" + " \"type\" : \"EXCEPTION\",\n" + " \"seqid\" : 101,\n" + " \"args\" : {\n" + " \"type\" : 4,\n" + " \"message\" : \"bad_seq_id\"\n" + " }\n" + " }\n" + '}'; TTextProtocol prot = new TTextProtocol( new TIOStreamTransport(new ByteArrayInputStream(request.getBytes()))); final TMessage header = prot.readMessageBegin(); final TApplicationException result = TApplicationExceptions.read(prot); prot.readMessageEnd(); assertThat(header.name).isEqualTo("doDebug"); assertThat(header.type).isEqualTo(TMessageType.EXCEPTION); assertThat(header.seqid).isEqualTo(101); assertThat(result.getType()).isEqualTo(TApplicationException.BAD_SEQUENCE_ID); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); prot = new TTextProtocol(new TIOStreamTransport(outputStream)); prot.writeMessageBegin(header); new TApplicationException(TApplicationException.BAD_SEQUENCE_ID, "bad_seq_id").write(prot); prot.writeMessageEnd(); assertThatJson(new String(outputStream.toByteArray(), StandardCharsets.UTF_8)).isEqualTo(request); }
Example #30
Source File: ThriftCall.java From armeria with Apache License 2.0 | 5 votes |
/** * Creates a new instance that contains a Thrift {@link TMessageType#CALL} or {@link TMessageType#ONEWAY} * message. */ public ThriftCall(TMessage header, TBase<?, ?> args) { super(header); if (header.type != TMessageType.CALL && header.type != TMessageType.ONEWAY) { throw new IllegalArgumentException( "header.type: " + typeStr(header.type) + " (expected: CALL or ONEWAY)"); } this.args = requireNonNull(args, "args"); }