eclipse 配置thrift
2016-05-19 00:00
423 查看
In Eclipse: File --> New --> Other... (Ctrl+N) --> Dynamic Web Project...
Project name: ThriftExample
讲项目转换成maven项目
在pom.xml中添加
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<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>
5.编辑文件example.thrift
下载thrift-0.9..3.exe
将生成的包放到项目下面
编辑ServiceExampleImpl.java
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 }));
}
}
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();
}
}
}
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
Project name: ThriftExample
讲项目转换成maven项目
在pom.xml中添加
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<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>
5.编辑文件example.thrift
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.exe
thrift-0.9.3 --gen java -out . example.thrift
将生成的包放到项目下面
编辑ServiceExampleImpl.java
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 }));
}
}
ClientExample.java
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();
}
}
}
ServerExample.java
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
相关文章推荐
- spring线程池ThreadPoolExecutor配置并且得到任务执行的结果
- Java多线程之初体验
- [javaSE] 类型转换(1加1等于几)
- 善用Eclipse的代码模板(Templates)功能
- 多线程学习之(一)线程安全性
- Spring Boot Memory Performance
- 在Java中内部类的访问形式和内部类的作用分别是什么?
- Spring Boot Memory Performance
- Error: java.lang.NumberFormatException: For input string: ""
- [javaSE] 变量的传值与传址
- Java关键字之static
- 多图详解Spring框架的设计理念与设计模式
- spring实现动态数据源切换
- java之String
- Eclipse相关
- JAVA大数处理(BigInteger,BigDecimal)
- Java垃圾回收机制--入门
- Java之字符串详解
- java多线程
- 算法(第四版)笔记<一>-------动态队列的数组实现(Java语言)