thrift和java交互案例和结果

代码结构:

thrift和java交互案例和结果_第1张图片



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++){
Blog b=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]


你可能感兴趣的:(thrift和java交互案例和结果)