From e254d1948c636787f59c59c37cd6339bade67c41 Mon Sep 17 00:00:00 2001 From: yongshun Date: Tue, 7 Mar 2017 23:11:47 +0800 Subject: [PATCH 1/2] Add ThriftDemo. --- ThriftDemo/pom.xml | 81 ++ .../java/com/xys/thrift/HelloWorldImpl.java | 13 + .../com/xys/thrift/HelloWorldService.java | 959 ++++++++++++++++++ .../xys/thrift/NonblockingHelloClient.java | 33 + .../xys/thrift/NonblockingHelloServer.java | 31 + .../com/xys/thrift/SimpleHelloClient.java | 32 + .../com/xys/thrift/SimpleHelloServer.java | 30 + .../com/xys/thrift/ThreadPoolHelloServer.java | 31 + ThriftDemo/src/main/resources/log4j2.xml | 24 + ThriftDemo/src/main/resources/test.thrift | 5 + 10 files changed, 1239 insertions(+) create mode 100644 ThriftDemo/pom.xml create mode 100644 ThriftDemo/src/main/java/com/xys/thrift/HelloWorldImpl.java create mode 100644 ThriftDemo/src/main/java/com/xys/thrift/HelloWorldService.java create mode 100644 ThriftDemo/src/main/java/com/xys/thrift/NonblockingHelloClient.java create mode 100644 ThriftDemo/src/main/java/com/xys/thrift/NonblockingHelloServer.java create mode 100644 ThriftDemo/src/main/java/com/xys/thrift/SimpleHelloClient.java create mode 100644 ThriftDemo/src/main/java/com/xys/thrift/SimpleHelloServer.java create mode 100644 ThriftDemo/src/main/java/com/xys/thrift/ThreadPoolHelloServer.java create mode 100644 ThriftDemo/src/main/resources/log4j2.xml create mode 100644 ThriftDemo/src/main/resources/test.thrift diff --git a/ThriftDemo/pom.xml b/ThriftDemo/pom.xml new file mode 100644 index 0000000..3e7e0d7 --- /dev/null +++ b/ThriftDemo/pom.xml @@ -0,0 +1,81 @@ + + + 4.0.0 + + com.xys + thrift.demo + 1.0-SNAPSHOT + + + UTF-8 + 1.7.21 + 2.6 + + + + + + org.apache.thrift + libthrift + 0.10.0 + + + + + + + org.apache.logging.log4j + log4j-api + ${log4j2.version} + + + org.apache.logging.log4j + log4j-core + ${log4j2.version} + + + + + org.apache.logging.log4j + log4j-slf4j-impl + ${log4j2.version} + + + + + org.apache.logging.log4j + log4j-jcl + ${log4j2.version} + + + + + org.apache.logging.log4j + log4j-web + ${log4j2.version} + + + + org.slf4j + slf4j-api + ${slf4j.version} + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + 1.8 + 1.8 + + + + + \ No newline at end of file diff --git a/ThriftDemo/src/main/java/com/xys/thrift/HelloWorldImpl.java b/ThriftDemo/src/main/java/com/xys/thrift/HelloWorldImpl.java new file mode 100644 index 0000000..76f12f4 --- /dev/null +++ b/ThriftDemo/src/main/java/com/xys/thrift/HelloWorldImpl.java @@ -0,0 +1,13 @@ +package com.xys.thrift; + +import org.apache.thrift.TException; + +public class HelloWorldImpl implements HelloWorldService.Iface { + public HelloWorldImpl() { + } + + @Override + public String sayHello(String name) throws TException { + return "Hello, " + name; + } +} \ No newline at end of file diff --git a/ThriftDemo/src/main/java/com/xys/thrift/HelloWorldService.java b/ThriftDemo/src/main/java/com/xys/thrift/HelloWorldService.java new file mode 100644 index 0000000..be50b9e --- /dev/null +++ b/ThriftDemo/src/main/java/com/xys/thrift/HelloWorldService.java @@ -0,0 +1,959 @@ +/** + * Autogenerated by Thrift Compiler (0.10.0) + * + * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING + * @generated + */ +package com.xys.thrift; + +@SuppressWarnings({"cast", "rawtypes", "serial", "unchecked", "unused"}) +@javax.annotation.Generated(value = "Autogenerated by Thrift Compiler (0.10.0)", date = "2017-03-07") +public class HelloWorldService { + + public interface Iface { + + public java.lang.String sayHello(java.lang.String name) throws org.apache.thrift.TException; + + } + + public interface AsyncIface { + + public void sayHello(java.lang.String name, org.apache.thrift.async.AsyncMethodCallback resultHandler) throws org.apache.thrift.TException; + + } + + public static class Client extends org.apache.thrift.TServiceClient implements Iface { + public static class Factory implements org.apache.thrift.TServiceClientFactory { + public Factory() {} + public Client getClient(org.apache.thrift.protocol.TProtocol prot) { + return new Client(prot); + } + public Client getClient(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TProtocol oprot) { + return new Client(iprot, oprot); + } + } + + public Client(org.apache.thrift.protocol.TProtocol prot) + { + super(prot, prot); + } + + public Client(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TProtocol oprot) { + super(iprot, oprot); + } + + public java.lang.String sayHello(java.lang.String name) throws org.apache.thrift.TException + { + send_sayHello(name); + return recv_sayHello(); + } + + public void send_sayHello(java.lang.String name) throws org.apache.thrift.TException + { + sayHello_args args = new sayHello_args(); + args.setName(name); + sendBase("sayHello", args); + } + + public java.lang.String recv_sayHello() throws org.apache.thrift.TException + { + sayHello_result result = new sayHello_result(); + receiveBase(result, "sayHello"); + if (result.isSetSuccess()) { + return result.success; + } + throw new org.apache.thrift.TApplicationException(org.apache.thrift.TApplicationException.MISSING_RESULT, "sayHello failed: unknown result"); + } + + } + public static class AsyncClient extends org.apache.thrift.async.TAsyncClient implements AsyncIface { + public static class Factory implements org.apache.thrift.async.TAsyncClientFactory { + private org.apache.thrift.async.TAsyncClientManager clientManager; + private org.apache.thrift.protocol.TProtocolFactory protocolFactory; + public Factory(org.apache.thrift.async.TAsyncClientManager clientManager, org.apache.thrift.protocol.TProtocolFactory protocolFactory) { + this.clientManager = clientManager; + this.protocolFactory = protocolFactory; + } + public AsyncClient getAsyncClient(org.apache.thrift.transport.TNonblockingTransport transport) { + return new AsyncClient(protocolFactory, clientManager, transport); + } + } + + public AsyncClient(org.apache.thrift.protocol.TProtocolFactory protocolFactory, org.apache.thrift.async.TAsyncClientManager clientManager, org.apache.thrift.transport.TNonblockingTransport transport) { + super(protocolFactory, clientManager, transport); + } + + public void sayHello(java.lang.String name, org.apache.thrift.async.AsyncMethodCallback resultHandler) throws org.apache.thrift.TException { + checkReady(); + sayHello_call method_call = new sayHello_call(name, resultHandler, this, ___protocolFactory, ___transport); + this.___currentMethod = method_call; + ___manager.call(method_call); + } + + public static class sayHello_call extends org.apache.thrift.async.TAsyncMethodCall { + private java.lang.String name; + public sayHello_call(java.lang.String name, org.apache.thrift.async.AsyncMethodCallback resultHandler, org.apache.thrift.async.TAsyncClient client, org.apache.thrift.protocol.TProtocolFactory protocolFactory, org.apache.thrift.transport.TNonblockingTransport transport) throws org.apache.thrift.TException { + super(client, protocolFactory, transport, resultHandler, false); + this.name = name; + } + + public void write_args(org.apache.thrift.protocol.TProtocol prot) throws org.apache.thrift.TException { + prot.writeMessageBegin(new org.apache.thrift.protocol.TMessage("sayHello", org.apache.thrift.protocol.TMessageType.CALL, 0)); + sayHello_args args = new sayHello_args(); + args.setName(name); + args.write(prot); + prot.writeMessageEnd(); + } + + public java.lang.String getResult() throws org.apache.thrift.TException { + if (getState() != org.apache.thrift.async.TAsyncMethodCall.State.RESPONSE_READ) { + throw new java.lang.IllegalStateException("Method call not finished!"); + } + org.apache.thrift.transport.TMemoryInputTransport memoryTransport = new org.apache.thrift.transport.TMemoryInputTransport(getFrameBuffer().array()); + org.apache.thrift.protocol.TProtocol prot = client.getProtocolFactory().getProtocol(memoryTransport); + return (new Client(prot)).recv_sayHello(); + } + } + + } + + public static class Processor extends org.apache.thrift.TBaseProcessor implements org.apache.thrift.TProcessor { + private static final org.slf4j.Logger _LOGGER = org.slf4j.LoggerFactory.getLogger(Processor.class.getName()); + public Processor(I iface) { + super(iface, getProcessMap(new java.util.HashMap>())); + } + + protected Processor(I iface, java.util.Map> processMap) { + super(iface, getProcessMap(processMap)); + } + + private static java.util.Map> getProcessMap(java.util.Map> processMap) { + processMap.put("sayHello", new sayHello()); + return processMap; + } + + public static class sayHello extends org.apache.thrift.ProcessFunction { + public sayHello() { + super("sayHello"); + } + + public sayHello_args getEmptyArgsInstance() { + return new sayHello_args(); + } + + protected boolean isOneway() { + return false; + } + + public sayHello_result getResult(I iface, sayHello_args args) throws org.apache.thrift.TException { + sayHello_result result = new sayHello_result(); + result.success = iface.sayHello(args.name); + return result; + } + } + + } + + public static class AsyncProcessor extends org.apache.thrift.TBaseAsyncProcessor { + private static final org.slf4j.Logger _LOGGER = org.slf4j.LoggerFactory.getLogger(AsyncProcessor.class.getName()); + public AsyncProcessor(I iface) { + super(iface, getProcessMap(new java.util.HashMap>())); + } + + protected AsyncProcessor(I iface, java.util.Map> processMap) { + super(iface, getProcessMap(processMap)); + } + + private static java.util.Map> getProcessMap(java.util.Map> processMap) { + processMap.put("sayHello", new sayHello()); + return processMap; + } + + public static class sayHello extends org.apache.thrift.AsyncProcessFunction { + public sayHello() { + super("sayHello"); + } + + public sayHello_args getEmptyArgsInstance() { + return new sayHello_args(); + } + + public org.apache.thrift.async.AsyncMethodCallback getResultHandler(final org.apache.thrift.server.AbstractNonblockingServer.AsyncFrameBuffer fb, final int seqid) { + final org.apache.thrift.AsyncProcessFunction fcall = this; + return new org.apache.thrift.async.AsyncMethodCallback() { + public void onComplete(java.lang.String o) { + sayHello_result result = new sayHello_result(); + result.success = o; + try { + fcall.sendResponse(fb, result, org.apache.thrift.protocol.TMessageType.REPLY,seqid); + } catch (org.apache.thrift.transport.TTransportException e) { + _LOGGER.error("TTransportException writing to internal frame buffer", e); + fb.close(); + } catch (java.lang.Exception e) { + _LOGGER.error("Exception writing to internal frame buffer", e); + onError(e); + } + } + public void onError(java.lang.Exception e) { + byte msgType = org.apache.thrift.protocol.TMessageType.REPLY; + org.apache.thrift.TSerializable msg; + sayHello_result result = new sayHello_result(); + if (e instanceof org.apache.thrift.transport.TTransportException) { + _LOGGER.error("TTransportException inside handler", e); + fb.close(); + return; + } else if (e instanceof org.apache.thrift.TApplicationException) { + _LOGGER.error("TApplicationException inside handler", e); + msgType = org.apache.thrift.protocol.TMessageType.EXCEPTION; + msg = (org.apache.thrift.TApplicationException)e; + } else { + _LOGGER.error("Exception inside handler", e); + msgType = org.apache.thrift.protocol.TMessageType.EXCEPTION; + msg = new org.apache.thrift.TApplicationException(org.apache.thrift.TApplicationException.INTERNAL_ERROR, e.getMessage()); + } + try { + fcall.sendResponse(fb,msg,msgType,seqid); + } catch (java.lang.Exception ex) { + _LOGGER.error("Exception writing to internal frame buffer", ex); + fb.close(); + } + } + }; + } + + protected boolean isOneway() { + return false; + } + + public void start(I iface, sayHello_args args, org.apache.thrift.async.AsyncMethodCallback resultHandler) throws org.apache.thrift.TException { + iface.sayHello(args.name,resultHandler); + } + } + + } + + public static class sayHello_args implements org.apache.thrift.TBase, java.io.Serializable, Cloneable, Comparable { + private static final org.apache.thrift.protocol.TStruct STRUCT_DESC = new org.apache.thrift.protocol.TStruct("sayHello_args"); + + private static final org.apache.thrift.protocol.TField NAME_FIELD_DESC = new org.apache.thrift.protocol.TField("name", org.apache.thrift.protocol.TType.STRING, (short)1); + + private static final org.apache.thrift.scheme.SchemeFactory STANDARD_SCHEME_FACTORY = new sayHello_argsStandardSchemeFactory(); + private static final org.apache.thrift.scheme.SchemeFactory TUPLE_SCHEME_FACTORY = new sayHello_argsTupleSchemeFactory(); + + public java.lang.String name; // required + + /** The set of fields this struct contains, along with convenience methods for finding and manipulating them. */ + public enum _Fields implements org.apache.thrift.TFieldIdEnum { + NAME((short)1, "name"); + + private static final java.util.Map byName = new java.util.HashMap(); + + static { + for (_Fields field : java.util.EnumSet.allOf(_Fields.class)) { + byName.put(field.getFieldName(), field); + } + } + + /** + * Find the _Fields constant that matches fieldId, or null if its not found. + */ + public static _Fields findByThriftId(int fieldId) { + switch(fieldId) { + case 1: // NAME + return NAME; + default: + return null; + } + } + + /** + * Find the _Fields constant that matches fieldId, throwing an exception + * if it is not found. + */ + public static _Fields findByThriftIdOrThrow(int fieldId) { + _Fields fields = findByThriftId(fieldId); + if (fields == null) throw new java.lang.IllegalArgumentException("Field " + fieldId + " doesn't exist!"); + return fields; + } + + /** + * Find the _Fields constant that matches name, or null if its not found. + */ + public static _Fields findByName(java.lang.String name) { + return byName.get(name); + } + + private final short _thriftId; + private final java.lang.String _fieldName; + + _Fields(short thriftId, java.lang.String fieldName) { + _thriftId = thriftId; + _fieldName = fieldName; + } + + public short getThriftFieldId() { + return _thriftId; + } + + public java.lang.String getFieldName() { + return _fieldName; + } + } + + // isset id assignments + public static final java.util.Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> metaDataMap; + static { + java.util.Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> tmpMap = new java.util.EnumMap<_Fields, org.apache.thrift.meta_data.FieldMetaData>(_Fields.class); + tmpMap.put(_Fields.NAME, new org.apache.thrift.meta_data.FieldMetaData("name", org.apache.thrift.TFieldRequirementType.DEFAULT, + new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.STRING))); + metaDataMap = java.util.Collections.unmodifiableMap(tmpMap); + org.apache.thrift.meta_data.FieldMetaData.addStructMetaDataMap(sayHello_args.class, metaDataMap); + } + + public sayHello_args() { + } + + public sayHello_args( + java.lang.String name) + { + this(); + this.name = name; + } + + /** + * Performs a deep copy on other. + */ + public sayHello_args(sayHello_args other) { + if (other.isSetName()) { + this.name = other.name; + } + } + + public sayHello_args deepCopy() { + return new sayHello_args(this); + } + + @Override + public void clear() { + this.name = null; + } + + public java.lang.String getName() { + return this.name; + } + + public sayHello_args setName(java.lang.String name) { + this.name = name; + return this; + } + + public void unsetName() { + this.name = null; + } + + /** Returns true if field name is set (has been assigned a value) and false otherwise */ + public boolean isSetName() { + return this.name != null; + } + + public void setNameIsSet(boolean value) { + if (!value) { + this.name = null; + } + } + + public void setFieldValue(_Fields field, java.lang.Object value) { + switch (field) { + case NAME: + if (value == null) { + unsetName(); + } else { + setName((java.lang.String)value); + } + break; + + } + } + + public java.lang.Object getFieldValue(_Fields field) { + switch (field) { + case NAME: + return getName(); + + } + throw new java.lang.IllegalStateException(); + } + + /** Returns true if field corresponding to fieldID is set (has been assigned a value) and false otherwise */ + public boolean isSet(_Fields field) { + if (field == null) { + throw new java.lang.IllegalArgumentException(); + } + + switch (field) { + case NAME: + return isSetName(); + } + throw new java.lang.IllegalStateException(); + } + + @Override + public boolean equals(java.lang.Object that) { + if (that == null) + return false; + if (that instanceof sayHello_args) + return this.equals((sayHello_args)that); + return false; + } + + public boolean equals(sayHello_args that) { + if (that == null) + return false; + if (this == that) + return true; + + boolean this_present_name = true && this.isSetName(); + boolean that_present_name = true && that.isSetName(); + if (this_present_name || that_present_name) { + if (!(this_present_name && that_present_name)) + return false; + if (!this.name.equals(that.name)) + return false; + } + + return true; + } + + @Override + public int hashCode() { + int hashCode = 1; + + hashCode = hashCode * 8191 + ((isSetName()) ? 131071 : 524287); + if (isSetName()) + hashCode = hashCode * 8191 + name.hashCode(); + + return hashCode; + } + + @Override + public int compareTo(sayHello_args other) { + if (!getClass().equals(other.getClass())) { + return getClass().getName().compareTo(other.getClass().getName()); + } + + int lastComparison = 0; + + lastComparison = java.lang.Boolean.valueOf(isSetName()).compareTo(other.isSetName()); + if (lastComparison != 0) { + return lastComparison; + } + if (isSetName()) { + lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.name, other.name); + if (lastComparison != 0) { + return lastComparison; + } + } + return 0; + } + + public _Fields fieldForId(int fieldId) { + return _Fields.findByThriftId(fieldId); + } + + public void read(org.apache.thrift.protocol.TProtocol iprot) throws org.apache.thrift.TException { + scheme(iprot).read(iprot, this); + } + + public void write(org.apache.thrift.protocol.TProtocol oprot) throws org.apache.thrift.TException { + scheme(oprot).write(oprot, this); + } + + @Override + public java.lang.String toString() { + java.lang.StringBuilder sb = new java.lang.StringBuilder("sayHello_args("); + boolean first = true; + + sb.append("name:"); + if (this.name == null) { + sb.append("null"); + } else { + sb.append(this.name); + } + first = false; + sb.append(")"); + return sb.toString(); + } + + public void validate() throws org.apache.thrift.TException { + // check for required fields + // check for sub-struct validity + } + + private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException { + try { + write(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(out))); + } catch (org.apache.thrift.TException te) { + throw new java.io.IOException(te); + } + } + + private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException { + try { + read(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(in))); + } catch (org.apache.thrift.TException te) { + throw new java.io.IOException(te); + } + } + + private static class sayHello_argsStandardSchemeFactory implements org.apache.thrift.scheme.SchemeFactory { + public sayHello_argsStandardScheme getScheme() { + return new sayHello_argsStandardScheme(); + } + } + + private static class sayHello_argsStandardScheme extends org.apache.thrift.scheme.StandardScheme { + + public void read(org.apache.thrift.protocol.TProtocol iprot, sayHello_args struct) throws org.apache.thrift.TException { + org.apache.thrift.protocol.TField schemeField; + iprot.readStructBegin(); + while (true) + { + schemeField = iprot.readFieldBegin(); + if (schemeField.type == org.apache.thrift.protocol.TType.STOP) { + break; + } + switch (schemeField.id) { + case 1: // NAME + if (schemeField.type == org.apache.thrift.protocol.TType.STRING) { + struct.name = iprot.readString(); + struct.setNameIsSet(true); + } else { + org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type); + } + break; + default: + org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type); + } + iprot.readFieldEnd(); + } + iprot.readStructEnd(); + + // check for required fields of primitive type, which can't be checked in the validate method + struct.validate(); + } + + public void write(org.apache.thrift.protocol.TProtocol oprot, sayHello_args struct) throws org.apache.thrift.TException { + struct.validate(); + + oprot.writeStructBegin(STRUCT_DESC); + if (struct.name != null) { + oprot.writeFieldBegin(NAME_FIELD_DESC); + oprot.writeString(struct.name); + oprot.writeFieldEnd(); + } + oprot.writeFieldStop(); + oprot.writeStructEnd(); + } + + } + + private static class sayHello_argsTupleSchemeFactory implements org.apache.thrift.scheme.SchemeFactory { + public sayHello_argsTupleScheme getScheme() { + return new sayHello_argsTupleScheme(); + } + } + + private static class sayHello_argsTupleScheme extends org.apache.thrift.scheme.TupleScheme { + + @Override + public void write(org.apache.thrift.protocol.TProtocol prot, sayHello_args struct) throws org.apache.thrift.TException { + org.apache.thrift.protocol.TTupleProtocol oprot = (org.apache.thrift.protocol.TTupleProtocol) prot; + java.util.BitSet optionals = new java.util.BitSet(); + if (struct.isSetName()) { + optionals.set(0); + } + oprot.writeBitSet(optionals, 1); + if (struct.isSetName()) { + oprot.writeString(struct.name); + } + } + + @Override + public void read(org.apache.thrift.protocol.TProtocol prot, sayHello_args struct) throws org.apache.thrift.TException { + org.apache.thrift.protocol.TTupleProtocol iprot = (org.apache.thrift.protocol.TTupleProtocol) prot; + java.util.BitSet incoming = iprot.readBitSet(1); + if (incoming.get(0)) { + struct.name = iprot.readString(); + struct.setNameIsSet(true); + } + } + } + + private static S scheme(org.apache.thrift.protocol.TProtocol proto) { + return (org.apache.thrift.scheme.StandardScheme.class.equals(proto.getScheme()) ? STANDARD_SCHEME_FACTORY : TUPLE_SCHEME_FACTORY).getScheme(); + } + } + + public static class sayHello_result implements org.apache.thrift.TBase, java.io.Serializable, Cloneable, Comparable { + private static final org.apache.thrift.protocol.TStruct STRUCT_DESC = new org.apache.thrift.protocol.TStruct("sayHello_result"); + + private static final org.apache.thrift.protocol.TField SUCCESS_FIELD_DESC = new org.apache.thrift.protocol.TField("success", org.apache.thrift.protocol.TType.STRING, (short)0); + + private static final org.apache.thrift.scheme.SchemeFactory STANDARD_SCHEME_FACTORY = new sayHello_resultStandardSchemeFactory(); + private static final org.apache.thrift.scheme.SchemeFactory TUPLE_SCHEME_FACTORY = new sayHello_resultTupleSchemeFactory(); + + public java.lang.String success; // required + + /** The set of fields this struct contains, along with convenience methods for finding and manipulating them. */ + public enum _Fields implements org.apache.thrift.TFieldIdEnum { + SUCCESS((short)0, "success"); + + private static final java.util.Map byName = new java.util.HashMap(); + + static { + for (_Fields field : java.util.EnumSet.allOf(_Fields.class)) { + byName.put(field.getFieldName(), field); + } + } + + /** + * Find the _Fields constant that matches fieldId, or null if its not found. + */ + public static _Fields findByThriftId(int fieldId) { + switch(fieldId) { + case 0: // SUCCESS + return SUCCESS; + default: + return null; + } + } + + /** + * Find the _Fields constant that matches fieldId, throwing an exception + * if it is not found. + */ + public static _Fields findByThriftIdOrThrow(int fieldId) { + _Fields fields = findByThriftId(fieldId); + if (fields == null) throw new java.lang.IllegalArgumentException("Field " + fieldId + " doesn't exist!"); + return fields; + } + + /** + * Find the _Fields constant that matches name, or null if its not found. + */ + public static _Fields findByName(java.lang.String name) { + return byName.get(name); + } + + private final short _thriftId; + private final java.lang.String _fieldName; + + _Fields(short thriftId, java.lang.String fieldName) { + _thriftId = thriftId; + _fieldName = fieldName; + } + + public short getThriftFieldId() { + return _thriftId; + } + + public java.lang.String getFieldName() { + return _fieldName; + } + } + + // isset id assignments + public static final java.util.Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> metaDataMap; + static { + java.util.Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> tmpMap = new java.util.EnumMap<_Fields, org.apache.thrift.meta_data.FieldMetaData>(_Fields.class); + tmpMap.put(_Fields.SUCCESS, new org.apache.thrift.meta_data.FieldMetaData("success", org.apache.thrift.TFieldRequirementType.DEFAULT, + new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.STRING))); + metaDataMap = java.util.Collections.unmodifiableMap(tmpMap); + org.apache.thrift.meta_data.FieldMetaData.addStructMetaDataMap(sayHello_result.class, metaDataMap); + } + + public sayHello_result() { + } + + public sayHello_result( + java.lang.String success) + { + this(); + this.success = success; + } + + /** + * Performs a deep copy on other. + */ + public sayHello_result(sayHello_result other) { + if (other.isSetSuccess()) { + this.success = other.success; + } + } + + public sayHello_result deepCopy() { + return new sayHello_result(this); + } + + @Override + public void clear() { + this.success = null; + } + + public java.lang.String getSuccess() { + return this.success; + } + + public sayHello_result setSuccess(java.lang.String success) { + this.success = success; + return this; + } + + public void unsetSuccess() { + this.success = null; + } + + /** Returns true if field success is set (has been assigned a value) and false otherwise */ + public boolean isSetSuccess() { + return this.success != null; + } + + public void setSuccessIsSet(boolean value) { + if (!value) { + this.success = null; + } + } + + public void setFieldValue(_Fields field, java.lang.Object value) { + switch (field) { + case SUCCESS: + if (value == null) { + unsetSuccess(); + } else { + setSuccess((java.lang.String)value); + } + break; + + } + } + + public java.lang.Object getFieldValue(_Fields field) { + switch (field) { + case SUCCESS: + return getSuccess(); + + } + throw new java.lang.IllegalStateException(); + } + + /** Returns true if field corresponding to fieldID is set (has been assigned a value) and false otherwise */ + public boolean isSet(_Fields field) { + if (field == null) { + throw new java.lang.IllegalArgumentException(); + } + + switch (field) { + case SUCCESS: + return isSetSuccess(); + } + throw new java.lang.IllegalStateException(); + } + + @Override + public boolean equals(java.lang.Object that) { + if (that == null) + return false; + if (that instanceof sayHello_result) + return this.equals((sayHello_result)that); + return false; + } + + public boolean equals(sayHello_result that) { + if (that == null) + return false; + if (this == that) + return true; + + boolean this_present_success = true && this.isSetSuccess(); + boolean that_present_success = true && that.isSetSuccess(); + if (this_present_success || that_present_success) { + if (!(this_present_success && that_present_success)) + return false; + if (!this.success.equals(that.success)) + return false; + } + + return true; + } + + @Override + public int hashCode() { + int hashCode = 1; + + hashCode = hashCode * 8191 + ((isSetSuccess()) ? 131071 : 524287); + if (isSetSuccess()) + hashCode = hashCode * 8191 + success.hashCode(); + + return hashCode; + } + + @Override + public int compareTo(sayHello_result other) { + if (!getClass().equals(other.getClass())) { + return getClass().getName().compareTo(other.getClass().getName()); + } + + int lastComparison = 0; + + lastComparison = java.lang.Boolean.valueOf(isSetSuccess()).compareTo(other.isSetSuccess()); + if (lastComparison != 0) { + return lastComparison; + } + if (isSetSuccess()) { + lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.success, other.success); + if (lastComparison != 0) { + return lastComparison; + } + } + return 0; + } + + public _Fields fieldForId(int fieldId) { + return _Fields.findByThriftId(fieldId); + } + + public void read(org.apache.thrift.protocol.TProtocol iprot) throws org.apache.thrift.TException { + scheme(iprot).read(iprot, this); + } + + public void write(org.apache.thrift.protocol.TProtocol oprot) throws org.apache.thrift.TException { + scheme(oprot).write(oprot, this); + } + + @Override + public java.lang.String toString() { + java.lang.StringBuilder sb = new java.lang.StringBuilder("sayHello_result("); + boolean first = true; + + sb.append("success:"); + if (this.success == null) { + sb.append("null"); + } else { + sb.append(this.success); + } + first = false; + sb.append(")"); + return sb.toString(); + } + + public void validate() throws org.apache.thrift.TException { + // check for required fields + // check for sub-struct validity + } + + private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException { + try { + write(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(out))); + } catch (org.apache.thrift.TException te) { + throw new java.io.IOException(te); + } + } + + private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException { + try { + read(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(in))); + } catch (org.apache.thrift.TException te) { + throw new java.io.IOException(te); + } + } + + private static class sayHello_resultStandardSchemeFactory implements org.apache.thrift.scheme.SchemeFactory { + public sayHello_resultStandardScheme getScheme() { + return new sayHello_resultStandardScheme(); + } + } + + private static class sayHello_resultStandardScheme extends org.apache.thrift.scheme.StandardScheme { + + public void read(org.apache.thrift.protocol.TProtocol iprot, sayHello_result struct) throws org.apache.thrift.TException { + org.apache.thrift.protocol.TField schemeField; + iprot.readStructBegin(); + while (true) + { + schemeField = iprot.readFieldBegin(); + if (schemeField.type == org.apache.thrift.protocol.TType.STOP) { + break; + } + switch (schemeField.id) { + case 0: // SUCCESS + if (schemeField.type == org.apache.thrift.protocol.TType.STRING) { + struct.success = iprot.readString(); + struct.setSuccessIsSet(true); + } else { + org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type); + } + break; + default: + org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type); + } + iprot.readFieldEnd(); + } + iprot.readStructEnd(); + + // check for required fields of primitive type, which can't be checked in the validate method + struct.validate(); + } + + public void write(org.apache.thrift.protocol.TProtocol oprot, sayHello_result struct) throws org.apache.thrift.TException { + struct.validate(); + + oprot.writeStructBegin(STRUCT_DESC); + if (struct.success != null) { + oprot.writeFieldBegin(SUCCESS_FIELD_DESC); + oprot.writeString(struct.success); + oprot.writeFieldEnd(); + } + oprot.writeFieldStop(); + oprot.writeStructEnd(); + } + + } + + private static class sayHello_resultTupleSchemeFactory implements org.apache.thrift.scheme.SchemeFactory { + public sayHello_resultTupleScheme getScheme() { + return new sayHello_resultTupleScheme(); + } + } + + private static class sayHello_resultTupleScheme extends org.apache.thrift.scheme.TupleScheme { + + @Override + public void write(org.apache.thrift.protocol.TProtocol prot, sayHello_result struct) throws org.apache.thrift.TException { + org.apache.thrift.protocol.TTupleProtocol oprot = (org.apache.thrift.protocol.TTupleProtocol) prot; + java.util.BitSet optionals = new java.util.BitSet(); + if (struct.isSetSuccess()) { + optionals.set(0); + } + oprot.writeBitSet(optionals, 1); + if (struct.isSetSuccess()) { + oprot.writeString(struct.success); + } + } + + @Override + public void read(org.apache.thrift.protocol.TProtocol prot, sayHello_result struct) throws org.apache.thrift.TException { + org.apache.thrift.protocol.TTupleProtocol iprot = (org.apache.thrift.protocol.TTupleProtocol) prot; + java.util.BitSet incoming = iprot.readBitSet(1); + if (incoming.get(0)) { + struct.success = iprot.readString(); + struct.setSuccessIsSet(true); + } + } + } + + private static S scheme(org.apache.thrift.protocol.TProtocol proto) { + return (org.apache.thrift.scheme.StandardScheme.class.equals(proto.getScheme()) ? STANDARD_SCHEME_FACTORY : TUPLE_SCHEME_FACTORY).getScheme(); + } + } + +} diff --git a/ThriftDemo/src/main/java/com/xys/thrift/NonblockingHelloClient.java b/ThriftDemo/src/main/java/com/xys/thrift/NonblockingHelloClient.java new file mode 100644 index 0000000..1af5f9b --- /dev/null +++ b/ThriftDemo/src/main/java/com/xys/thrift/NonblockingHelloClient.java @@ -0,0 +1,33 @@ +package com.xys.thrift; + +import org.apache.thrift.protocol.TBinaryProtocol; +import org.apache.thrift.protocol.TProtocol; +import org.apache.thrift.transport.TFastFramedTransport; +import org.apache.thrift.transport.TSocket; +import org.apache.thrift.transport.TTransport; + +public class NonblockingHelloClient { + public static final String SERVER_IP = "localhost"; + public static final int SERVER_PORT = 8080; + public static final int TIMEOUT = 30000; + + public void startClient(String userName) throws Exception { + TTransport transport = null; + + transport = new TFastFramedTransport(new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT)); + // 协议要和服务端一致 + TProtocol protocol = new TBinaryProtocol(transport); + HelloWorldService.Client client = new HelloWorldService.Client( + protocol); + transport.open(); + String result = client.sayHello(userName); + System.out.println("Result: " + result); + + transport.close(); + } + + public static void main(String[] args) throws Exception { + NonblockingHelloClient client = new NonblockingHelloClient(); + client.startClient("XYS"); + } +} \ No newline at end of file diff --git a/ThriftDemo/src/main/java/com/xys/thrift/NonblockingHelloServer.java b/ThriftDemo/src/main/java/com/xys/thrift/NonblockingHelloServer.java new file mode 100644 index 0000000..737bc23 --- /dev/null +++ b/ThriftDemo/src/main/java/com/xys/thrift/NonblockingHelloServer.java @@ -0,0 +1,31 @@ +package com.xys.thrift; + +import org.apache.thrift.TProcessor; +import org.apache.thrift.protocol.TBinaryProtocol; +import org.apache.thrift.server.TNonblockingServer; +import org.apache.thrift.server.TServer; +import org.apache.thrift.transport.TNonblockingServerSocket; + + +public class NonblockingHelloServer { + public static final int SERVER_PORT = 8080; + + public static void main(String[] args) throws Exception { + NonblockingHelloServer server = new NonblockingHelloServer(); + server.startServer(); + } + + public void startServer() throws Exception { + TProcessor tprocessor = new HelloWorldService.Processor( + new HelloWorldImpl()); + + TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(SERVER_PORT); + TNonblockingServer.Args tArgs = new TNonblockingServer.Args(serverTransport); + tArgs.processor(tprocessor); + tArgs.protocolFactory(new TBinaryProtocol.Factory()); +// tArgs.transportFactory(new TFastFramedTransport.Factory()); + + TServer server = new TNonblockingServer(tArgs); + server.serve(); + } +} \ No newline at end of file diff --git a/ThriftDemo/src/main/java/com/xys/thrift/SimpleHelloClient.java b/ThriftDemo/src/main/java/com/xys/thrift/SimpleHelloClient.java new file mode 100644 index 0000000..6d890c9 --- /dev/null +++ b/ThriftDemo/src/main/java/com/xys/thrift/SimpleHelloClient.java @@ -0,0 +1,32 @@ +package com.xys.thrift; + +import org.apache.thrift.protocol.TBinaryProtocol; +import org.apache.thrift.protocol.TProtocol; +import org.apache.thrift.transport.TSocket; +import org.apache.thrift.transport.TTransport; + +public class SimpleHelloClient { + public static final String SERVER_IP = "localhost"; + public static final int SERVER_PORT = 8080; + public static final int TIMEOUT = 30000; + + public void startClient(String userName) throws Exception { + TTransport transport = null; + + transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT); + // 协议要和服务端一致 + TProtocol protocol = new TBinaryProtocol(transport); + HelloWorldService.Client client = new HelloWorldService.Client( + protocol); + transport.open(); + String result = client.sayHello(userName); + System.out.println("Result: " + result); + + transport.close(); + } + + public static void main(String[] args) throws Exception { + SimpleHelloClient client = new SimpleHelloClient(); + client.startClient("XYS"); + } +} \ No newline at end of file diff --git a/ThriftDemo/src/main/java/com/xys/thrift/SimpleHelloServer.java b/ThriftDemo/src/main/java/com/xys/thrift/SimpleHelloServer.java new file mode 100644 index 0000000..3de6a26 --- /dev/null +++ b/ThriftDemo/src/main/java/com/xys/thrift/SimpleHelloServer.java @@ -0,0 +1,30 @@ +package com.xys.thrift; + +import org.apache.thrift.TProcessor; +import org.apache.thrift.protocol.TBinaryProtocol; +import org.apache.thrift.server.TServer; +import org.apache.thrift.server.TSimpleServer; +import org.apache.thrift.transport.TServerSocket; + +public class SimpleHelloServer { + public static final int SERVER_PORT = 8080; + + public static void main(String[] args) throws Exception { + SimpleHelloServer server = new SimpleHelloServer(); + server.startServer(); + } + + public void startServer() throws Exception { + TProcessor tprocessor = new HelloWorldService.Processor( + new HelloWorldImpl()); + + TServerSocket serverTransport = new TServerSocket(SERVER_PORT); + TSimpleServer.Args tArgs = new TSimpleServer.Args(serverTransport); + tArgs.processor(tprocessor); + tArgs.protocolFactory(new TBinaryProtocol.Factory()); + + TServer server = new TSimpleServer(tArgs); + + server.serve(); + } +} \ No newline at end of file diff --git a/ThriftDemo/src/main/java/com/xys/thrift/ThreadPoolHelloServer.java b/ThriftDemo/src/main/java/com/xys/thrift/ThreadPoolHelloServer.java new file mode 100644 index 0000000..6ac961b --- /dev/null +++ b/ThriftDemo/src/main/java/com/xys/thrift/ThreadPoolHelloServer.java @@ -0,0 +1,31 @@ +package com.xys.thrift; + +import org.apache.thrift.TProcessor; +import org.apache.thrift.protocol.TBinaryProtocol; +import org.apache.thrift.server.TServer; +import org.apache.thrift.server.TThreadPoolServer; +import org.apache.thrift.transport.TServerSocket; + + + +public class ThreadPoolHelloServer { + public static final int SERVER_PORT = 8080; + + public static void main(String[] args) throws Exception { + ThreadPoolHelloServer server = new ThreadPoolHelloServer(); + server.startServer(); + } + + public void startServer() throws Exception { + TProcessor tprocessor = new HelloWorldService.Processor( + new HelloWorldImpl()); + + TServerSocket serverTransport = new TServerSocket(SERVER_PORT); + TThreadPoolServer.Args tArgs = new TThreadPoolServer.Args(serverTransport); + tArgs.processor(tprocessor); + tArgs.protocolFactory(new TBinaryProtocol.Factory()); + + TServer server = new TThreadPoolServer(tArgs); + server.serve(); + } +} \ No newline at end of file diff --git a/ThriftDemo/src/main/resources/log4j2.xml b/ThriftDemo/src/main/resources/log4j2.xml new file mode 100644 index 0000000..5fa5e08 --- /dev/null +++ b/ThriftDemo/src/main/resources/log4j2.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ThriftDemo/src/main/resources/test.thrift b/ThriftDemo/src/main/resources/test.thrift new file mode 100644 index 0000000..dde0e62 --- /dev/null +++ b/ThriftDemo/src/main/resources/test.thrift @@ -0,0 +1,5 @@ +namespace java com.xys.thrift + +service HelloWorldService { + string sayHello(1:string name); +} \ No newline at end of file From 7a4304400388ed01bf0b2b918e012835e0e7f367 Mon Sep 17 00:00:00 2001 From: yongshun Date: Tue, 7 Mar 2017 23:45:47 +0800 Subject: [PATCH 2/2] Add NonblockingAsyncHelloClient. --- .../thrift/NonblockingAsyncHelloClient.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 ThriftDemo/src/main/java/com/xys/thrift/NonblockingAsyncHelloClient.java diff --git a/ThriftDemo/src/main/java/com/xys/thrift/NonblockingAsyncHelloClient.java b/ThriftDemo/src/main/java/com/xys/thrift/NonblockingAsyncHelloClient.java new file mode 100644 index 0000000..8c38aa2 --- /dev/null +++ b/ThriftDemo/src/main/java/com/xys/thrift/NonblockingAsyncHelloClient.java @@ -0,0 +1,46 @@ +package com.xys.thrift; + +import org.apache.thrift.async.AsyncMethodCallback; +import org.apache.thrift.async.TAsyncClientManager; +import org.apache.thrift.protocol.TBinaryProtocol; +import org.apache.thrift.protocol.TProtocolFactory; +import org.apache.thrift.transport.TNonblockingSocket; +import org.apache.thrift.transport.TNonblockingTransport; + +public class NonblockingAsyncHelloClient { + public static final String SERVER_IP = "localhost"; + public static final int SERVER_PORT = 8080; + public static final int TIMEOUT = 30000; + + public void startClient(String userName) throws Exception { + TAsyncClientManager clientManager = new TAsyncClientManager(); + TNonblockingTransport transport = new TNonblockingSocket(SERVER_IP, + SERVER_PORT, TIMEOUT); + + // 协议要和服务端一致 + TProtocolFactory protocolFactory = new TBinaryProtocol.Factory(); + HelloWorldService.AsyncClient client = new HelloWorldService.AsyncClient( + protocolFactory, clientManager, transport); + + client.sayHello(userName, new AsyncHandler()); + + Thread.sleep(500); + } + + class AsyncHandler implements AsyncMethodCallback { + @Override + public void onComplete(String response) { + System.out.println("Got result: " + response); + } + + @Override + public void onError(Exception exception) { + System.out.println("Got error: " + exception.getMessage()); + } + } + + public static void main(String[] args) throws Exception { + NonblockingAsyncHelloClient client = new NonblockingAsyncHelloClient(); + client.startClient("XYS"); + } +} \ No newline at end of file