In Eclipse: File --> New --> Other... (Ctrl+N) --> Dynamic Web Project...java
Project name: ThriftExampleapache
講項目轉換成maven項目api
在pom.xml中添加maven
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>ui
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.9.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>url
5.編輯文件example.thriftspa
namespace java example struct BeanExample { 1: bool booleanPrimive; 2: byte bytePrimive; 3: i16 shortPrimive; 4: i32 intPrimive; 5: i64 longPrimive; 6: double doublePrimive; 7: string stringObject; 8: binary byteArray; //ByteBuffer } service ServiceExample { BeanExample getBean(1: i32 anArg; 2: string anOther) }
下載thrift-0.9..3.exeserver
thrift-0.9.3 --gen java -out . example.thrift
將生成的包放到項目下面xml
編輯ServiceExampleImpl.javaip
package example;
import java.nio.ByteBuffer;
import org.apache.thrift.TException;
public class ServiceExampleImpl implements ServiceExample.Iface {
public BeanExample getBean(int anArg, String anOther) throws TException {
return new BeanExample(true, (byte) 2, (short) 3, 4, 5, 6.0,
"OK", ByteBuffer.wrap(new byte[] { 3, 1, 4 }));
}
}
package example;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
// import org.apache.thrift... etc.;
public class ClientExample {
private static final int PORT = 7911;
public static void main(String[] args) {
try {
TTransport transport = new TSocket("localhost", PORT);
TProtocol protocol = new TBinaryProtocol(transport);
ServiceExample.Client client = new ServiceExample.Client(protocol);
transport.open();
BeanExample bean = client.getBean(1, "string");
transport.close();
System.out.println(bean.getStringObject());
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
}
}
}
package example;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;
// import org.apache.thrift... etc.;
public class ServerExample implements Runnable {
private static final int PORT = 7911;
public void run() {
try {
TServerSocket serverTransport = new TServerSocket(PORT);
ServiceExample.Processor processor = new ServiceExample.Processor(new ServiceExampleImpl());
TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
System.out.println("Starting server on port " + PORT);
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Thread(new ServerExample()).run();
}
}
Execute the server (right-click ServerExample.java --> Run as --> Java Application) to see the message: Starting server on port 7911
Execute the client (right-click ClientExample.java --> Run as --> Java Application) to see the message: OK