Java Code Examples for org.apache.thrift.protocol.TMessageType#CALL

The following examples show how to use org.apache.thrift.protocol.TMessageType#CALL . 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: TypedParser.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Override
void writeValue(JsonGenerator jw, Byte val) throws IOException {
    final String serialized;
    switch (val.byteValue()) {
    case TMessageType.CALL:
        serialized = "CALL";
        break;
    case TMessageType.REPLY:
        serialized = "REPLY";
        break;
    case TMessageType.EXCEPTION:
        serialized = "EXCEPTION";
        break;
    case TMessageType.ONEWAY:
        serialized = "ONEWAY";
        break;
    default:
        throw new IllegalArgumentException("Unsupported message type: " + val);
    }
    jw.writeString(serialized);
}
 
Example 2
Source File: ThriftJacksonTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@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 3
Source File: ThriftIDLSerializer.java    From octo-rpc with Apache License 2.0 5 votes vote down vote up
@Override
protected Object deserialize4Thrift(byte[] buff, Class<?> iface, Map<String, Object> attachments) throws Exception {
    TraceTimeline timeline = TraceTimeline.newRecord(CommonUtil.objectToBool(attachments.get(Constants.TRACE_IS_RECORD_TIMELINE), false),
            TraceTimeline.DECODE_START_TS);

    Object obj = null;
    TMemoryInputTransport transport = new TMemoryInputTransport(buff);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    TMessage message = protocol.readMessageBegin();
    if (message.type == TMessageType.CALL) {
        DefaultRequest request = new DefaultRequest(Long.valueOf(message.seqid));
        request.setServiceInterface(iface);
        RpcInvocation rpcInvocation = doDeserializeRequest(request, message, protocol);
        request.setData(rpcInvocation);
        obj = request;

        rpcInvocation.putAttachment(Constants.TRACE_TIMELINE, timeline);
    } else if (message.type == TMessageType.REPLY || message.type == TMessageType.EXCEPTION) {
        DefaultResponse response = new DefaultResponse(Long.valueOf(message.seqid));
        response.setServiceInterface(iface);
        try {
            RpcResult rpcResult = doDeserializeResponse(response, message, protocol);
            response.setResult(rpcResult);
        } catch (Exception e) {
            response.setException(e);
        }

        obj = response;
        if (response.getRequest() != null && response.getRequest().getData() != null) {
            TraceTimeline.copyRecord(timeline, response.getRequest().getData());
        }
    } else {
        throw new ProtocolException("Thrift deserialize message type is invalid");
    }
    protocol.readMessageEnd();

    MetaUtil.recordTraceInfoInDecode(buff, obj);
    return obj;
}
 
Example 4
Source File: ThriftIDLSerializer.java    From octo-rpc with Apache License 2.0 5 votes vote down vote up
private byte[] doSerializeRequest(DefaultRequest request) throws Exception {
    RpcInvocation rpcInvocation = request.getData();
    TMessage message = new TMessage(rpcInvocation.getMethod().getName(), TMessageType.CALL, request.getSeqToInt());

    TMemoryBuffer memoryBuffer = new TMemoryBuffer(INITIAL_BYTE_ARRAY_SIZE);
    TBinaryProtocol protocol = new TBinaryProtocol(memoryBuffer);

    protocol.writeMessageBegin(message);
    serializeArguments(rpcInvocation, protocol);
    protocol.writeMessageEnd();

    protocol.getTransport().flush();
    return getActualBytes(memoryBuffer);
}
 
Example 5
Source File: ThriftIDLSerializer.java    From octo-rpc with Apache License 2.0 5 votes vote down vote up
protected RpcInvocation doDeserializeRequest(DefaultRequest request, TMessage message,
                                             TProtocol protocol) throws Exception {
    if (message.type != TMessageType.CALL) {
        throw new ProtocolException("Thrift deserialize request: message type is invalid.");
    }

    ThriftMessageInfo thriftMessageInfo = new ThriftMessageInfo(message.name, message.seqid);
    if (!request.isOctoProtocol()) {
        if (hasOldRequestHeader(protocol)) {
            // 解析老协议的header
            RequestHeader requestHeader = new RequestHeader();
            protocol.readFieldBegin();
            requestHeader.read(protocol);
            protocol.readFieldEnd();
            request = MetaUtil.convertOldProtocolHeaderToRequest(requestHeader, request);
            thriftMessageInfo.setOldProtocol(true);
        }
        request.setServiceName(ThriftUtil.getIDLClassName(request.getServiceInterface()));
    }

    List<Object> arguments = new ArrayList<>();
    List<Class<?>> parameterTypes = new ArrayList<>();
    deserializeArguments(request.getServiceInterface().getName(), message.name, protocol, arguments, parameterTypes);

    Class<?>[] parameterTypeArray = parameterTypes.toArray(new Class[parameterTypes.size()]);
    Method method = obtainMethod(request.getServiceInterface(), message.name, parameterTypeArray);

    RpcInvocation invocation = new RpcInvocation(request.getServiceInterface(), method, arguments.toArray(), parameterTypeArray);
    request.setThriftMsgInfo(thriftMessageInfo);
    return invocation;
}
 
Example 6
Source File: THttpService.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static String typeString(byte typeValue) {
    switch (typeValue) {
        case TMessageType.CALL:
            return "CALL";
        case TMessageType.REPLY:
            return "REPLY";
        case TMessageType.EXCEPTION:
            return "EXCEPTION";
        case TMessageType.ONEWAY:
            return "ONEWAY";
        default:
            return "UNKNOWN(" + (typeValue & 0xFF) + ')';
    }
}
 
Example 7
Source File: ThriftCall.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * 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");
}
 
Example 8
Source File: ThriftMessage.java    From armeria with Apache License 2.0 5 votes vote down vote up
static String typeStr(byte type) {
    switch (type) {
        case TMessageType.CALL:
            return "CALL";
        case TMessageType.ONEWAY:
            return "ONEWAY";
        case TMessageType.REPLY:
            return "REPLY";
        case TMessageType.EXCEPTION:
            return "EXCEPTION";
        default:
            return "UNKNOWN(" + (type & 0xFF) + ')';
    }
}
 
Example 9
Source File: TypedParser.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
Byte readFromString(String s) {
    switch (s) {
    case "CALL":
        return TMessageType.CALL;
    case "REPLY":
        return TMessageType.REPLY;
    case "EXCEPTION":
        return TMessageType.EXCEPTION;
    case "ONEWAY":
        return TMessageType.ONEWAY;
    default:
        throw new IllegalArgumentException("Unsupported message type: " + s);
    }
}
 
Example 10
Source File: ThriftCodecTest.java    From dubbo-2.6.5 with Apache License 2.0 4 votes vote down vote up
@Test
public void testDecodeRequest() throws Exception {
    Request request = createRequest();
    // encode
    RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream(1024);

    TIOStreamTransport transport = new TIOStreamTransport(bos);

    TBinaryProtocol protocol = new TBinaryProtocol(transport);

    int messageLength, headerLength;

    protocol.writeI16(ThriftCodec.MAGIC);
    protocol.writeI32(Integer.MAX_VALUE);
    protocol.writeI16(Short.MAX_VALUE);
    protocol.writeByte(ThriftCodec.VERSION);
    protocol.writeString(
            ((RpcInvocation) request.getData())
                    .getAttachment(Constants.INTERFACE_KEY));
    protocol.writeI64(request.getId());
    protocol.getTransport().flush();
    headerLength = bos.size();

    Demo.echoString_args args = new Demo.echoString_args();
    args.setArg("Hell, World!");

    TMessage message = new TMessage("echoString", TMessageType.CALL, ThriftCodec.getSeqId());

    protocol.writeMessageBegin(message);
    args.write(protocol);
    protocol.writeMessageEnd();
    protocol.getTransport().flush();
    int oldIndex = messageLength = bos.size();

    try {
        bos.setWriteIndex(ThriftCodec.MESSAGE_HEADER_LENGTH_INDEX);
        protocol.writeI16((short) (0xffff & headerLength));
        bos.setWriteIndex(ThriftCodec.MESSAGE_LENGTH_INDEX);
        protocol.writeI32(messageLength);
    } finally {
        bos.setWriteIndex(oldIndex);
    }

    Object obj = codec.decode((Channel) null, ChannelBuffers.wrappedBuffer(
            encodeFrame(bos.toByteArray())));

    Assert.assertTrue(obj instanceof Request);

    obj = ((Request) obj).getData();

    Assert.assertTrue(obj instanceof RpcInvocation);

    RpcInvocation invocation = (RpcInvocation) obj;

    Assert.assertEquals("echoString", invocation.getMethodName());
    Assert.assertArrayEquals(new Class[]{String.class}, invocation.getParameterTypes());
    Assert.assertArrayEquals(new Object[]{args.getArg()}, invocation.getArguments());

}
 
Example 11
Source File: ThriftCodecTest.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@Test
public void testDecodeRequest() throws Exception {
    Request request = createRequest();
    // encode
    RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream( 1024 );

    TIOStreamTransport transport = new TIOStreamTransport( bos );

    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    int messageLength, headerLength;

    protocol.writeI16( ThriftCodec.MAGIC );
    protocol.writeI32( Integer.MAX_VALUE );
    protocol.writeI16( Short.MAX_VALUE );
    protocol.writeByte( ThriftCodec.VERSION );
    protocol.writeString(
            ( ( RpcInvocation ) request.getData() )
                    .getAttachment( Constants.INTERFACE_KEY) );
    protocol.writeI64( request.getId() );
    protocol.getTransport().flush();
    headerLength = bos.size();

    Demo.echoString_args args = new Demo.echoString_args(  );
    args.setArg( "Hell, World!" );

    TMessage message = new TMessage( "echoString", TMessageType.CALL, ThriftCodec.getSeqId() );

    protocol.writeMessageBegin( message );
    args.write( protocol );
    protocol.writeMessageEnd();
    protocol.getTransport().flush();
    int oldIndex = messageLength = bos.size();

    try{
        bos.setWriteIndex( ThriftCodec.MESSAGE_HEADER_LENGTH_INDEX );
        protocol.writeI16( ( short ) ( 0xffff & headerLength ) );
        bos.setWriteIndex( ThriftCodec.MESSAGE_LENGTH_INDEX );
        protocol.writeI32( messageLength );
    } finally {
        bos.setWriteIndex( oldIndex );
    }

    Object obj = codec.decode( ( Channel ) null, ChannelBuffers.wrappedBuffer(
        encodeFrame(bos.toByteArray())) );

    Assert.assertTrue( obj instanceof Request );

    obj = ( ( Request ) obj ).getData();

    Assert.assertTrue( obj instanceof RpcInvocation );

    RpcInvocation invocation = ( RpcInvocation ) obj;

    Assert.assertEquals( "echoString", invocation.getMethodName() );
    Assert.assertArrayEquals( new Class[] {String .class}, invocation.getParameterTypes() );
    Assert.assertArrayEquals( new Object[] { args.getArg() }, invocation.getArguments() );

}
 
Example 12
Source File: ThriftCodecTest.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
@Test
public void testDecodeRequest() throws Exception {
    Request request = createRequest();
    // encode
    RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream( 1024 );

    TIOStreamTransport transport = new TIOStreamTransport( bos );

    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    int messageLength, headerLength;

    protocol.writeI16( ThriftCodec.MAGIC );
    protocol.writeI32( Integer.MAX_VALUE );
    protocol.writeI16( Short.MAX_VALUE );
    protocol.writeByte( ThriftCodec.VERSION );
    protocol.writeString(
            ( ( RpcInvocation ) request.getData() )
                    .getAttachment( Constants.INTERFACE_KEY) );
    protocol.writeI64( request.getId() );
    protocol.getTransport().flush();
    headerLength = bos.size();

    Demo.echoString_args args = new Demo.echoString_args(  );
    args.setArg( "Hell, World!" );

    TMessage message = new TMessage( "echoString", TMessageType.CALL, ThriftCodec.getSeqId() );

    protocol.writeMessageBegin( message );
    args.write( protocol );
    protocol.writeMessageEnd();
    protocol.getTransport().flush();
    int oldIndex = messageLength = bos.size();

    try{
        bos.setWriteIndex( ThriftCodec.MESSAGE_HEADER_LENGTH_INDEX );
        protocol.writeI16( ( short ) ( 0xffff & headerLength ) );
        bos.setWriteIndex( ThriftCodec.MESSAGE_LENGTH_INDEX );
        protocol.writeI32( messageLength );
    } finally {
        bos.setWriteIndex( oldIndex );
    }

    Object obj = codec.decode( ( Channel ) null, ChannelBuffers.wrappedBuffer(
        encodeFrame(bos.toByteArray())) );

    Assert.assertTrue( obj instanceof Request );

    obj = ( ( Request ) obj ).getData();

    Assert.assertTrue( obj instanceof RpcInvocation );

    RpcInvocation invocation = ( RpcInvocation ) obj;

    Assert.assertEquals( "echoString", invocation.getMethodName() );
    Assert.assertArrayEquals( new Class[] {String .class}, invocation.getParameterTypes() );
    Assert.assertArrayEquals( new Object[] { args.getArg() }, invocation.getArguments() );

}
 
Example 13
Source File: ThriftCodecTest.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@Test
public void testDecodeRequest() throws Exception {
    Request request = createRequest();
    // encode
    RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream( 1024 );

    TIOStreamTransport transport = new TIOStreamTransport( bos );

    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    int messageLength, headerLength;

    protocol.writeI16( ThriftCodec.MAGIC );
    protocol.writeI32( Integer.MAX_VALUE );
    protocol.writeI16( Short.MAX_VALUE );
    protocol.writeByte( ThriftCodec.VERSION );
    protocol.writeString(
            ( ( RpcInvocation ) request.getData() )
                    .getAttachment( Constants.INTERFACE_KEY) );
    protocol.writeI64( request.getId() );
    protocol.getTransport().flush();
    headerLength = bos.size();

    Demo.echoString_args args = new Demo.echoString_args(  );
    args.setArg( "Hell, World!" );

    TMessage message = new TMessage( "echoString", TMessageType.CALL, ThriftCodec.getSeqId() );

    protocol.writeMessageBegin( message );
    args.write( protocol );
    protocol.writeMessageEnd();
    protocol.getTransport().flush();
    int oldIndex = messageLength = bos.size();

    try{
        bos.setWriteIndex( ThriftCodec.MESSAGE_HEADER_LENGTH_INDEX );
        protocol.writeI16( ( short ) ( 0xffff & headerLength ) );
        bos.setWriteIndex( ThriftCodec.MESSAGE_LENGTH_INDEX );
        protocol.writeI32( messageLength );
    } finally {
        bos.setWriteIndex( oldIndex );
    }

    Object obj = codec.decode( ( Channel ) null, ChannelBuffers.wrappedBuffer(
        encodeFrame(bos.toByteArray())) );

    Assert.assertTrue( obj instanceof Request );

    obj = ( ( Request ) obj ).getData();

    Assert.assertTrue( obj instanceof RpcInvocation );

    RpcInvocation invocation = ( RpcInvocation ) obj;

    Assert.assertEquals( "echoString", invocation.getMethodName() );
    Assert.assertArrayEquals( new Class[] {String .class}, invocation.getParameterTypes() );
    Assert.assertArrayEquals( new Object[] { args.getArg() }, invocation.getArguments() );

}
 
Example 14
Source File: ThriftCodecTest.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@Test
public void testDecodeRequest() throws Exception {
    Request request = createRequest();
    // encode
    RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream( 1024 );

    TIOStreamTransport transport = new TIOStreamTransport( bos );

    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    int messageLength, headerLength;

    protocol.writeI16( ThriftCodec.MAGIC );
    protocol.writeI32( Integer.MAX_VALUE );
    protocol.writeI16( Short.MAX_VALUE );
    protocol.writeByte( ThriftCodec.VERSION );
    protocol.writeString(
            ( ( RpcInvocation ) request.getData() )
                    .getAttachment( Constants.INTERFACE_KEY) );
    protocol.writeI64( request.getId() );
    protocol.getTransport().flush();
    headerLength = bos.size();

    Demo.echoString_args args = new Demo.echoString_args(  );
    args.setArg( "Hell, World!" );

    TMessage message = new TMessage( "echoString", TMessageType.CALL, ThriftCodec.getSeqId() );

    protocol.writeMessageBegin( message );
    args.write( protocol );
    protocol.writeMessageEnd();
    protocol.getTransport().flush();
    int oldIndex = messageLength = bos.size();

    try{
        bos.setWriteIndex( ThriftCodec.MESSAGE_HEADER_LENGTH_INDEX );
        protocol.writeI16( ( short ) ( 0xffff & headerLength ) );
        bos.setWriteIndex( ThriftCodec.MESSAGE_LENGTH_INDEX );
        protocol.writeI32( messageLength );
    } finally {
        bos.setWriteIndex( oldIndex );
    }

    Object obj = codec.decode( ( Channel ) null, ChannelBuffers.wrappedBuffer(
        encodeFrame(bos.toByteArray())) );

    Assert.assertTrue( obj instanceof Request );

    obj = ( ( Request ) obj ).getData();

    Assert.assertTrue( obj instanceof RpcInvocation );

    RpcInvocation invocation = ( RpcInvocation ) obj;

    Assert.assertEquals( "echoString", invocation.getMethodName() );
    Assert.assertArrayEquals( new Class[] {String .class}, invocation.getParameterTypes() );
    Assert.assertArrayEquals( new Object[] { args.getArg() }, invocation.getArguments() );

}
 
Example 15
Source File: ThriftFunction.java    From armeria with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the type of this function.
 *
 * @return {@link TMessageType#CALL} or {@link TMessageType#ONEWAY}
 */
public byte messageType() {
    return isOneWay() ? TMessageType.ONEWAY : TMessageType.CALL;
}