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 topic2:binary content3:i64 createTime4:string id5:string ipAddress6: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 stubreturn "Hi," + username + " welcome to my blog blog.csdn.net/luozhonghua2014";}@Overridepublic int testCase1(int num1, int num2, String num3) throws TException {// TODO Auto-generated method stubreturn num1+num2;}@Overridepublic 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;}@Overridepublic void testCase3() throws TException {// TODO Auto-generated method stubSystem.out.print("testCase3");}@Overridepublic 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

人只要不失去方向,就不会失去自己

thrift和java交互案例和结果

相关文章:

你感兴趣的文章:

标签云: