thrift和java交互案例和结果

代码结构:




1>>>>>>  demoHello.thrift:


namespace java xdg.luozhonghua.thrift.service



/*
 struct UserProfile {
 1: i32 uid = 1,
 2: string name = "User1",
 3: string blurb,
 4: list<i32> subNodeList,
      5: map<i32,string> subNodeMap,
      6: set<i32> subNodeSet
  }


service  HelloWorldService {
  UserStruct addUser(1: UserStruct userStruct)
  UserStruct getUser(1: i32 userId)
  list<UserStruct> findAllUser()
  list<UserStruct> findUser(1: map<string,string> parameterMap)
  string sayHello(1:string username)
  string getAge(1:i32 agr)
  set<i32> subNodeSet1(1: UserStruct userStruct)
  map<i32,string> subNodeMap1(1: UserStruct userStruct)
  list<i32> subNodeList1(1: UserStruct userStruct)
}
*/


struct Blog {
 1:string topic
 2:binary content
 3:i64 createTime
 4:string id
 5:string ipAddress
 6:map<string,string> props
}


service HelloWorldService {
    string sayHello(1:string username)
    
i32 testCase1(1:i32 num1, 2:i32 num2,3:string num3)


list<string> testCase2(1:map<string,string> num1)


void testCase3()


void testCase4(1:list<Blog> 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<String> testCase2(Map<String, String> num1) throws TException {
//System.out.print("testCase2");
List<String> list=new ArrayList<String>();
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> 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&lt;HelloWorldService.Iface&gt;
// tprocessor =
// new HelloWorldService.Processor&lt;HelloWorldService.Iface&gt;(
// 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<String,String> map=new HashMap<String,String>();
map.put("aaa", "1111");
map.put("bbb", "2222");

 
List<Blog> list=new ArrayList<Blog>();
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]


posted @ 2015-03-22 23:08  172257861  阅读(142)  评论(0编辑  收藏  举报