eclipse thrift java_eclipse 配置thrift

In Eclipse: File --> New --> Other... (Ctrl+N) --> Dynamic Web Project...

Project name: ThriftExample

讲项目转换成maven项目

在pom.xml中添加

http://maven.apache.org

UTF-8

junit

junit

3.8.1

test

org.apache.thrift

libthrift

0.9.3

org.slf4j

slf4j-api

1.7.5

org.slf4j

slf4j-simple

1.7.5

org.slf4j

slf4j-log4j12

1.7.5

log4j

log4j

1.2.17

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值