List of usage examples for org.apache.thrift.protocol TMessageType CALL
byte CALL
To view the source code for org.apache.thrift.protocol TMessageType CALL.
Click Source Link
From source file:com.alibaba.dubbo.rpc.protocol.swift.ThriftCodec.java
License:Open Source License
private Object decode(String serviceName, TProtocol protocol) throws IOException { TMessage message;//from w w w .j av a 2 s .co m try { message = protocol.readMessageBegin(); } catch (TException e) { throw new IOException(e.getMessage(), e); } if (message.type == TMessageType.CALL) { RpcInvocation result = new RpcInvocation(); result.setAttachment(Constants.INTERFACE_KEY, serviceName); result.setMethodName(message.name); String argsClassName = ExtensionLoader.getExtensionLoader(ClassNameGenerator.class) .getExtension(ThriftClassNameGenerator.NAME).generateArgsClassName(serviceName, message.name); if (StringUtils.isEmpty(argsClassName)) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, "The specified interface name incorrect."); } Class clazz = cachedClass.get(argsClassName); if (clazz == null) { try { clazz = ClassHelper.forNameWithThreadContextClassLoader(argsClassName); cachedClass.putIfAbsent(argsClassName, clazz); } catch (ClassNotFoundException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } } TBase args; try { args = (TBase) clazz.newInstance(); } catch (InstantiationException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } try { args.read(protocol); protocol.readMessageEnd(); } catch (TException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } List<Object> parameters = new ArrayList<Object>(); List<Class<?>> parameterTypes = new ArrayList<Class<?>>(); int index = 1; while (true) { TFieldIdEnum fieldIdEnum = args.fieldForId(index++); if (fieldIdEnum == null) { break; } String fieldName = fieldIdEnum.getFieldName(); String getMethodName = ThriftUtils.generateGetMethodName(fieldName); Method getMethod; try { getMethod = clazz.getMethod(getMethodName); } catch (NoSuchMethodException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } parameterTypes.add(getMethod.getReturnType()); try { parameters.add(getMethod.invoke(args)); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (InvocationTargetException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } } result.setArguments(parameters.toArray()); result.setParameterTypes(parameterTypes.toArray(new Class[parameterTypes.size()])); Request request = new Request(message.seqid); request.setData(result); cachedRequest.putIfAbsent(message.seqid, RequestData.create(message.seqid, serviceName, message.name)); return request; } else if (message.type == TMessageType.EXCEPTION) { TApplicationException exception; try { exception = TApplicationException.read(protocol); protocol.readMessageEnd(); } catch (TException e) { throw new IOException(e.getMessage(), e); } RpcResult result = new RpcResult(); result.setException(new RpcException(exception.getMessage())); Response response = new Response(); response.setResult(result); response.setId(message.seqid); return response; } else if (message.type == TMessageType.REPLY) { String resultClassName = ExtensionLoader.getExtensionLoader(ClassNameGenerator.class) .getExtension(ThriftClassNameGenerator.NAME).generateResultClassName(serviceName, message.name); if (StringUtils.isEmpty(resultClassName)) { throw new IllegalArgumentException(new StringBuilder(32) .append("Could not infer service result class name from service name ").append(serviceName) .append(", the service name you specified may not generated by thrift idl compiler") .toString()); } Class<?> clazz = cachedClass.get(resultClassName); if (clazz == null) { try { clazz = ClassHelper.forNameWithThreadContextClassLoader(resultClassName); cachedClass.putIfAbsent(resultClassName, clazz); } catch (ClassNotFoundException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } } TBase<?, ? extends TFieldIdEnum> result; try { result = (TBase<?, ?>) clazz.newInstance(); } catch (InstantiationException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } try { result.read(protocol); protocol.readMessageEnd(); } catch (TException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } Object realResult = null; int index = 0; while (true) { TFieldIdEnum fieldIdEnum = result.fieldForId(index++); if (fieldIdEnum == null) { break; } Field field; try { field = clazz.getDeclaredField(fieldIdEnum.getFieldName()); field.setAccessible(true); } catch (NoSuchFieldException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } try { realResult = field.get(result); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } if (realResult != null) { break; } } Response response = new Response(); response.setId(message.seqid); RpcResult rpcResult = new RpcResult(); if (realResult instanceof Throwable) { rpcResult.setException((Throwable) realResult); } else { rpcResult.setValue(realResult); } response.setResult(rpcResult); return response; } else { // Impossible throw new IOException(); } }
From source file:com.alibaba.dubbo.rpc.protocol.swift.ThriftCodec.java
License:Open Source License
private void encodeRequest(Channel channel, ChannelBuffer buffer, Request request) throws IOException { RpcInvocation inv = (RpcInvocation) request.getData(); int seqId = new Long(request.getId()).intValue(); String serviceName = inv.getAttachment(Constants.INTERFACE_KEY); if (StringUtils.isEmpty(serviceName)) { throw new IllegalArgumentException( new StringBuilder(32).append("Could not find service name in attachment with key ") .append(Constants.INTERFACE_KEY).toString()); }//from w ww. jav a2 s . c o m TMessage message = new TMessage(inv.getMethodName(), TMessageType.CALL, seqId); ExtensionLoader<ClassNameGenerator> loader = ExtensionLoader.getExtensionLoader(ClassNameGenerator.class); String name = channel.getUrl().getParameter(ThriftConstants.CLASS_NAME_GENERATOR_KEY, ThriftClassNameGenerator.NAME); ClassNameGenerator generator = loader.getExtension(name); String methodName = inv.getMethodName(); String methodArgs = generator.generateArgsClassName(serviceName, methodName); if (StringUtils.isEmpty(methodArgs)) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, new StringBuilder(32) .append("Could not encode request, the specified interface may be incorrect.").toString()); } Class<?> clazz = cachedClass.get(methodArgs); if (clazz == null) { try { clazz = ClassHelper.forNameWithThreadContextClassLoader(methodArgs); cachedClass.putIfAbsent(methodArgs, clazz); } catch (ClassNotFoundException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } } TBase args; try { args = (TBase) clazz.newInstance(); } catch (InstantiationException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } for (int i = 0; i < inv.getArguments().length; i++) { Object obj = inv.getArguments()[i]; if (obj == null) { continue; } TFieldIdEnum field = args.fieldForId(i + 1); String setMethodName = ThriftUtils.generateSetMethodName(field.getFieldName()); Method method; try { method = clazz.getMethod(setMethodName, inv.getParameterTypes()[i]); } catch (NoSuchMethodException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } try { method.invoke(args, obj); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (InvocationTargetException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } } RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream(1024); TIOStreamTransport transport = new TIOStreamTransport(bos); TBinaryProtocol protocol = new TBinaryProtocol(transport); try { protocol.writeMessageBegin(message); args.write(protocol); protocol.writeMessageEnd(); protocol.getTransport().flush(); } catch (TException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } buffer.writeBytes(bos.toByteArray()); }
From source file:com.alibaba.dubbo.rpc.protocol.thrift.ThriftCodec.java
License:Open Source License
private Object decode(TProtocol protocol) throws IOException { // version/*w w w .ja v a2 s .c o m*/ String serviceName; long id; TMessage message; try { protocol.readI16(); protocol.readByte(); serviceName = protocol.readString(); id = protocol.readI64(); message = protocol.readMessageBegin(); } catch (TException e) { throw new IOException(e.getMessage(), e); } if (message.type == TMessageType.CALL) { RpcInvocation result = new RpcInvocation(); result.setAttachment(Constants.INTERFACE_KEY, serviceName); result.setMethodName(message.name); String argsClassName = ExtensionLoader.getExtensionLoader(ClassNameGenerator.class) .getExtension(ThriftClassNameGenerator.NAME).generateArgsClassName(serviceName, message.name); if (StringUtils.isEmpty(argsClassName)) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, "The specified interface name incorrect."); } Class clazz = cachedClass.get(argsClassName); if (clazz == null) { try { clazz = ClassHelper.forNameWithThreadContextClassLoader(argsClassName); cachedClass.putIfAbsent(argsClassName, clazz); } catch (ClassNotFoundException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } } TBase args; try { args = (TBase) clazz.newInstance(); } catch (InstantiationException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } try { args.read(protocol); protocol.readMessageEnd(); } catch (TException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } List<Object> parameters = new ArrayList<Object>(); List<Class<?>> parameterTypes = new ArrayList<Class<?>>(); int index = 1; while (true) { TFieldIdEnum fieldIdEnum = args.fieldForId(index++); if (fieldIdEnum == null) { break; } String fieldName = fieldIdEnum.getFieldName(); String getMethodName = ThriftUtils.generateGetMethodName(fieldName); Method getMethod; try { getMethod = clazz.getMethod(getMethodName); } catch (NoSuchMethodException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } parameterTypes.add(getMethod.getReturnType()); try { parameters.add(getMethod.invoke(args)); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (InvocationTargetException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } } result.setArguments(parameters.toArray()); result.setParameterTypes(parameterTypes.toArray(new Class[parameterTypes.size()])); Request request = new Request(id); request.setData(result); cachedRequest.putIfAbsent(id, RequestData.create(message.seqid, serviceName, message.name)); return request; } else if (message.type == TMessageType.EXCEPTION) { TApplicationException exception; try { exception = TApplicationException.read(protocol); protocol.readMessageEnd(); } catch (TException e) { throw new IOException(e.getMessage(), e); } RpcResult result = new RpcResult(); result.setException(new RpcException(exception.getMessage())); Response response = new Response(); response.setResult(result); response.setId(id); return response; } else if (message.type == TMessageType.REPLY) { String resultClassName = ExtensionLoader.getExtensionLoader(ClassNameGenerator.class) .getExtension(ThriftClassNameGenerator.NAME).generateResultClassName(serviceName, message.name); if (StringUtils.isEmpty(resultClassName)) { throw new IllegalArgumentException(new StringBuilder(32) .append("Could not infer service result class name from service name ").append(serviceName) .append(", the service name you specified may not generated by thrift idl compiler") .toString()); } Class<?> clazz = cachedClass.get(resultClassName); if (clazz == null) { try { clazz = ClassHelper.forNameWithThreadContextClassLoader(resultClassName); cachedClass.putIfAbsent(resultClassName, clazz); } catch (ClassNotFoundException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } } TBase<?, ? extends TFieldIdEnum> result; try { result = (TBase<?, ?>) clazz.newInstance(); } catch (InstantiationException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } try { result.read(protocol); protocol.readMessageEnd(); } catch (TException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } Object realResult = null; int index = 0; while (true) { TFieldIdEnum fieldIdEnum = result.fieldForId(index++); if (fieldIdEnum == null) { break; } Field field; try { field = clazz.getDeclaredField(fieldIdEnum.getFieldName()); field.setAccessible(true); } catch (NoSuchFieldException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } try { realResult = field.get(result); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } if (realResult != null) { break; } } Response response = new Response(); response.setId(id); RpcResult rpcResult = new RpcResult(); if (realResult instanceof Throwable) { rpcResult.setException((Throwable) realResult); } else { rpcResult.setValue(realResult); } response.setResult(rpcResult); return response; } else { // Impossible throw new IOException(); } }
From source file:com.alibaba.dubbo.rpc.protocol.thrift.ThriftCodec.java
License:Open Source License
private void encodeRequest(Channel channel, OutputStream output, Request request) throws IOException { RpcInvocation inv = (RpcInvocation) request.getData(); int seqId = nextSeqId(); String serviceName = inv.getAttachment(Constants.INTERFACE_KEY); if (StringUtils.isEmpty(serviceName)) { throw new IllegalArgumentException( new StringBuilder(32).append("Could not find service name in attachment with key ") .append(Constants.INTERFACE_KEY).toString()); }//from w w w .j av a 2 s.c om TMessage message = new TMessage(inv.getMethodName(), TMessageType.CALL, seqId); String methodArgs = ExtensionLoader .getExtensionLoader(ClassNameGenerator.class).getExtension(channel.getUrl() .getParameter(ThriftConstants.CLASS_NAME_GENERATOR_KEY, ThriftClassNameGenerator.NAME)) .generateArgsClassName(serviceName, inv.getMethodName()); if (StringUtils.isEmpty(methodArgs)) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, new StringBuilder(32) .append("Could not encode request, the specified interface may be incorrect.").toString()); } Class<?> clazz = cachedClass.get(methodArgs); if (clazz == null) { try { clazz = ClassHelper.forNameWithThreadContextClassLoader(methodArgs); cachedClass.putIfAbsent(methodArgs, clazz); } catch (ClassNotFoundException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } } TBase args; try { args = (TBase) clazz.newInstance(); } catch (InstantiationException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } for (int i = 0; i < inv.getArguments().length; i++) { Object obj = inv.getArguments()[i]; if (obj == null) { continue; } TFieldIdEnum field = args.fieldForId(i + 1); String setMethodName = ThriftUtils.generateSetMethodName(field.getFieldName()); Method method; try { method = clazz.getMethod(setMethodName, inv.getParameterTypes()[i]); } catch (NoSuchMethodException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } try { method.invoke(args, obj); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (InvocationTargetException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } } RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream(1024); TIOStreamTransport transport = new TIOStreamTransport(bos); TBinaryProtocol protocol = new TBinaryProtocol(transport); int headerLength, messageLength; byte[] bytes = new byte[4]; try { // magic protocol.writeI16(MAGIC); // message length placeholder protocol.writeI32(Integer.MAX_VALUE); // message header length placeholder protocol.writeI16(Short.MAX_VALUE); // version protocol.writeByte(VERSION); // service name protocol.writeString(serviceName); // dubbo request id protocol.writeI64(request.getId()); protocol.getTransport().flush(); // header size headerLength = bos.size(); // message body protocol.writeMessageBegin(message); args.write(protocol); protocol.writeMessageEnd(); protocol.getTransport().flush(); int oldIndex = messageLength = bos.size(); // fill in message length and header length try { TFramedTransport.encodeFrameSize(messageLength, bytes); bos.setWriteIndex(MESSAGE_LENGTH_INDEX); protocol.writeI32(messageLength); bos.setWriteIndex(MESSAGE_HEADER_LENGTH_INDEX); protocol.writeI16((short) (0xffff & headerLength)); } finally { bos.setWriteIndex(oldIndex); } } catch (TException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } output.write(bytes); bos.writeTo(output); output.flush(); }
From source file:com.alibaba.dubbo.rpc.protocol.thrift.ThriftCodecTest.java
License:Open Source License
@Test public void testEncodeRequest() throws Exception { Request request = createRequest(); ByteArrayOutputStream output = new ByteArrayOutputStream(1024); codec.encode(channel, output, request); byte[] bytes = output.toByteArray(); ByteArrayInputStream bis = new ByteArrayInputStream(bytes); TTransport transport = new TIOStreamTransport(bis); TBinaryProtocol protocol = new TBinaryProtocol(transport); // frame//from w w w .j a v a 2s .c om byte[] length = new byte[4]; transport.read(length, 0, 4); if (bis.markSupported()) { bis.mark(0); } // magic Assert.assertEquals(ThriftCodec.MAGIC, protocol.readI16()); // message length int messageLength = protocol.readI32(); Assert.assertEquals(messageLength + 4, bytes.length); // header length short headerLength = protocol.readI16(); // version Assert.assertEquals(ThriftCodec.VERSION, protocol.readByte()); // service name Assert.assertEquals(Demo.Iface.class.getName(), protocol.readString()); // dubbo request id Assert.assertEquals(request.getId(), protocol.readI64()); // test message header length if (bis.markSupported()) { bis.reset(); bis.skip(headerLength); } TMessage message = protocol.readMessageBegin(); Demo.echoString_args args = new Demo.echoString_args(); args.read(protocol); protocol.readMessageEnd(); Assert.assertEquals("echoString", message.name); Assert.assertEquals(TMessageType.CALL, message.type); Assert.assertEquals("Hello, World!", args.getArg()); }
From source file:com.alibaba.dubbo.rpc.protocol.thrift.ThriftCodecTest.java
License:Open Source License
@Test public void testDecodeRequest() throws Exception { Request request = createRequest(); // encode//from w w w. jav a2 s . co m 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, new ByteArrayInputStream(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()); }
From source file:com.ebay.nest.io.sede.dynamic_type.DynamicSerDeFunction.java
License:Apache License
@Override public byte getType() { return TMessageType.CALL; }
From source file:com.facebook.nifty.core.TestThriftFrameDecoder.java
License:Apache License
private void writeTestMessages(TBinaryProtocol protocol, int count) throws TException { for (int i = 0; i < count; i++) { protocol.writeMessageBegin(new TMessage("testmessage" + i, TMessageType.CALL, i)); {//from w w w. j a va2 s. c o m protocol.writeStructBegin(new TStruct()); { protocol.writeFieldBegin(new TField("i32field", TType.I32, (short) 1)); protocol.writeI32(123); protocol.writeFieldEnd(); } { protocol.writeFieldBegin(new TField("strfield", TType.STRING, (short) 2)); protocol.writeString("foo"); protocol.writeFieldEnd(); } { protocol.writeFieldBegin(new TField("boolfield", TType.BOOL, (short) 3)); protocol.writeBool(true); protocol.writeFieldEnd(); } protocol.writeFieldStop(); protocol.writeStructEnd(); } protocol.writeMessageEnd(); protocol.getTransport().flush(); } }
From source file:com.facebook.swift.service.ThriftServiceProcessor.java
License:Apache License
@Override @SuppressWarnings("PMD.EmptyCatchBlock") public ListenableFuture<Boolean> process(final TProtocol in, TProtocol out, RequestContext requestContext) throws TException { String methodName = null;//w w w .jav a 2s .co m int sequenceId = 0; try { final SettableFuture<Boolean> resultFuture = SettableFuture.create(); TMessage message = in.readMessageBegin(); methodName = message.name; sequenceId = message.seqid; // lookup method ThriftMethodProcessor method = methods.get(methodName); if (method == null) { TProtocolUtil.skip(in, TType.STRUCT); createAndWriteApplicationException(out, requestContext, methodName, sequenceId, UNKNOWN_METHOD, "Invalid method name: '" + methodName + "'", null); return Futures.immediateFuture(true); } switch (message.type) { case TMessageType.CALL: case TMessageType.ONEWAY: // Ideally we'd check the message type here to make the presence/absence of // the "oneway" keyword annotating the method matches the message type. // Unfortunately most clients send both one-way and two-way messages as CALL // message type instead of using ONEWAY message type, and servers ignore the // difference. break; default: TProtocolUtil.skip(in, TType.STRUCT); createAndWriteApplicationException(out, requestContext, methodName, sequenceId, INVALID_MESSAGE_TYPE, "Received invalid message type " + message.type + " from client", null); return Futures.immediateFuture(true); } // invoke method final ContextChain context = new ContextChain(eventHandlers, method.getQualifiedName(), requestContext); ListenableFuture<Boolean> processResult = method.process(in, out, sequenceId, context); Futures.addCallback(processResult, new FutureCallback<Boolean>() { @Override public void onSuccess(Boolean result) { context.done(); resultFuture.set(result); } @Override public void onFailure(Throwable t) { LOG.error(t, "Failed to process method [" + method.getName() + "] of service [" + method.getServiceName() + "]"); context.done(); resultFuture.setException(t); } }); return resultFuture; } catch (TApplicationException e) { // If TApplicationException was thrown send it to the client. // This could happen if for example, some of event handlers method threw an exception. writeApplicationException(out, requestContext, methodName, sequenceId, e); return Futures.immediateFuture(true); } catch (Exception e) { return Futures.immediateFailedFuture(e); } }
From source file:com.flipkart.phantom.thrift.impl.ProxyServiceClient.java
License:Apache License
/** * Overriden super class method. Simply delegates the call to super type implementation * @see org.apache.thrift.TServiceClient#sendBase(String, org.apache.thrift.TBase) *//*from ww w . jav a 2 s. co m*/ public void sendBase(String methodName, TBase args, int sequenceId) throws TException { this.seqid_ = sequenceId; oprot_.writeMessageBegin(new TMessage(methodName, TMessageType.CALL, this.seqid_)); args.write(oprot_); oprot_.writeMessageEnd(); oprot_.getTransport().flush(); }