thrift java例子_thrift和java交互案例和结果

代码结构:

d58167f4dd838983331312c8e7fe110d.png

1>>>>>> demoHello.thrift:

namespace java xdg.luozhonghua.thrift.service

/*

struct UserProfile {

1: i32 uid = 1,

2: string name = "User1",

3: string blurb,

4: list subNodeList,

5: map subNodeMap,

6: set subNodeSet

}

service HelloWorldService {

UserStruct addUser(1: UserStruct userStruct)

UserStruct getUser(1: i32 userId)

list findAllUser()

list findUser(1: map parameterMap)

string sayHello(1:string username)

string getAge(1:i32 agr)

set subNodeSet1(1: UserStruct userStruct)

map subNodeMap1(1: UserStruct userStruct)

list subNodeList1(1: UserStruct userStruct)

}

*/

struct Blog {

1:string topic

2:binary content

3:i64 createTime

4:string id

5:string ipAddress

6:map props

}

service HelloWorldService {

string sayHello(1:string username)

i32 testCase1(1:i32 num1, 2:i32 num2,3:string num3)

list testCase2(1:map num1)

void testCase3()

void testCase4(1:list blog)

}

2>>>>>>生成文件:

Blog.java

HelloWorldService.java

3>>>>>>HelloWorldImpl .java:

package xdg.luozhonghua.thrift.demo;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

import org.apache.thrift.TException;

import xdg.luozhonghua.thrift.service.Blog;

import xdg.luozhonghua.thrift.service.HelloWorldService.Iface;

public class HelloWorldImpl implements Iface{

public String sayHello(String username) throws TException {

// TODO Auto-generated method stub

return "Hi," + username + " welcome to my blog blog.csdn.net/luozhonghua2014";

}

@Override

public int testCase1(int num1, int num2, String num3) throws TException {

// TODO Auto-generated method stub

return num1+num2;

}

@Override

public List testCase2(Map num1) throws TException {

//System.out.print("testCase2");

List list=new ArrayList();

for(String str:num1.keySet()){

list.add(str);

}

return list;

}

@Override

public void testCase3() throws TException {

// TODO Auto-generated method stub

System.out.print("testCase3");

}

@Override

public void testCase4(List blog) throws TException {

System.out.print("testCase4 "+blog.size());

}

}

4>>>>>>HelloServerDemo.java

package xdg.luozhonghua.thrift.test;

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;

import xdg.luozhonghua.thrift.demo.HelloWorldImpl;

import xdg.luozhonghua.thrift.service.HelloWorldService;

public class HelloServerDemo {

public static final int SERVER_PORT = 8090;

public void startServer() {

try {

System.out.println("HelloWorld TSimpleServer start ....");

TProcessor tprocessor = new HelloWorldService.Processor(new HelloWorldImpl());

// HelloWorldService.Processor<HelloWorldService.Iface>

// tprocessor =

// new HelloWorldService.Processor<HelloWorldService.Iface>(

// new HelloWorldImpl());

// 简单的单线程服务模型,一般用于测试

TServerSocket serverTransport = new TServerSocket(SERVER_PORT);

TServer.Args tArgs = new TServer.Args(serverTransport);

tArgs.processor(tprocessor);

tArgs.protocolFactory(new TBinaryProtocol.Factory());

// tArgs.protocolFactory(new TCompactProtocol.Factory());

// tArgs.protocolFactory(new TJSONProtocol.Factory());

TServer server = new TSimpleServer(tArgs);

server.serve();

} catch (Exception e) {

System.out.println("Server start error!!!");

e.printStackTrace();

}

}

/**

* @param args

*/

public static void main(String[] args) {

HelloServerDemo server = new HelloServerDemo();

server.startServer();

}

}

5>>>>>>HelloClientDemo.java

package xdg.luozhonghua.thrift.test;

import java.util.ArrayList;

import java.util.Date;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

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 xdg.luozhonghua.thrift.service.Blog;

import xdg.luozhonghua.thrift.service.HelloWorldService;

public class HelloClientDemo {

public static final String SERVER_IP = "localhost";

public static final int SERVER_PORT = 8090;

public static final int TIMEOUT = 30000;

/**

*

* @param userName

*/

public void startClient(String userName) {

TTransport transport = null;

try {

transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);

// 协议要和服务端一致

TProtocol protocol = new TBinaryProtocol(transport);

// TProtocol protocol = new TCompactProtocol(transport);

// TProtocol protocol = new TJSONProtocol(transport);

HelloWorldService.Client client = new HelloWorldService.Client(

protocol);

transport.open();

String result = client.sayHello(userName);

int t=client.testCase1(1, 2, "aaaa");

Map map=new HashMap();

map.put("aaa", "1111");

map.put("bbb", "2222");

List list=new ArrayList();

for(int i=0;i<5;i++){

Blogb=new Blog();

b.setId(i+"");

b.setTopic("afdaf"+i);

b.setCreateTime(new Date().getTime());

list.add(b);

}

client.testCase4(list);

System.out.println(t+"\t\n Thrify client result =: " + result);

System.out.println(client.testCase2(map));

} catch (TTransportException e) {

e.printStackTrace();

} catch (TException e) {

e.printStackTrace();

} finally {

if (null != transport) {

transport.close();

}

}

}

/**

* @param args

*/

public static void main(String[] args) {

HelloClientDemo client = new HelloClientDemo();

client.startClient("嘻嘻嘻");

}

}

运行结果:

server:

HelloWorld TSimpleServer start ....

testCase4 5

client:

3

Thrify client result =: Hi,嘻嘻嘻 welcome to my blog blog.csdn.net/luozhonghua2014

[bbb, aaa]

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2015-03-22 23:08

浏览 36

评论

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值