Thrift(4)Java Server and Client

Thrift(4)JavaServerandClient

1.First,IdidaImplementationclassforBlogServiceInterface

packagecom.sillycat.easytalker.plugins.thrift.business;

importjava.util.ArrayList;

importjava.util.Date;

importjava.util.List;

importorg.apache.log4j.Logger;

importorg.apache.thrift.TException;

importcom.sillycat.easytalker.plugins.thrift.gen.code.Blog;

importcom.sillycat.easytalker.plugins.thrift.gen.code.BlogService;

publicclassBlogServiceImplimplementsBlogService.Iface{

privateLoggerlogger=Logger.getLogger(this.getClass());

publicStringcreateBlog(Blogblog)throwsTException{

logger.debug("MethodcreateBlogisinvoked!Parametersblog="+blog

+"topic="+blog.getTopic());

return"1";

}

publicList<String>batchCreateBlog(List<Blog>blogs)throwsTException{

logger.debug("MethodbatchCreateBlogisinvoked!Parametersblogs="

+blogs);

List<String>ids=newArrayList<String>();

ids.add("1");

ids.add("2");

ids.add("3");

returnids;

}

publicStringdeleteBlog(Stringid)throwsTException{

logger.debug("MethoddeleteBlogisinvoked!Parametersid="+id);

return"1";

}

publicList<Blog>listAll()throwsTException{

logger.debug("MethodlistAllisinvoked!");

List<Blog>blogs=newArrayList<Blog>();

Blogb1=newBlog();

Blogb2=newBlog();

b1.setContent("test1content".getBytes());

b1.setTopic("topic1");

b1.setCreatedTime(newDate().getTime());

b1.setIpAddress("127.0.0.1");

b1.setId("1");

b2.setContent("test2content".getBytes());

b2.setTopic("topic2");

b2.setCreatedTime(newDate().getTime());

b2.setIpAddress("127.0.0.1");

b2.setId("2");

blogs.add(b1);

blogs.add(b2);

returnblogs;

}

publicBloggetOne(Stringid)throwsTException{

logger.debug("MethodgetOneisinvoked!Parametersid="+id);

Blogb1=newBlog();

b1.setContent("test1content".getBytes());

b1.setTopic("topic1");

b1.setCreatedTime(newDate().getTime());

b1.setIpAddress("127.0.0.1");

b1.setId("1");

returnb1;

}

publicStringupdateBlog(Blogblog)throwsTException{

logger.debug("MethodupdateBlogisinvoked!Parametersblog="+blog

+"topic="+blog.getTopic());

return"1";

}

}

2.ThenItrytocreatetheThriftJavaServer

packagecom.sillycat.easytalker.plugins.thrift.server;

importjava.net.InetSocketAddress;

importorg.apache.thrift.protocol.TBinaryProtocol;

importorg.apache.thrift.server.TServer;

importorg.apache.thrift.server.TThreadPoolServer;

importorg.apache.thrift.server.TThreadPoolServer.Args;

importorg.apache.thrift.transport.TServerSocket;

importorg.apache.thrift.transport.TServerTransport;

importorg.apache.thrift.transport.TTransportFactory;

importcom.sillycat.easytalker.plugins.thrift.business.BlogServiceImpl;

importcom.sillycat.easytalker.plugins.thrift.gen.code.BlogService;

publicclassThriftJavaServer{

publicstaticvoidmain(String[]args){

BlogService.Processor<BlogServiceImpl>processor=newBlogService.Processor<BlogServiceImpl>(

newBlogServiceImpl());

try{

TServerTransportserverTransport=newTServerSocket(

newInetSocketAddress("0.0.0.0",9813));

ArgstrArgs=newArgs(serverTransport);

trArgs.processor(processor);

trArgs.protocolFactory(newTBinaryProtocol.Factory(true,true));

trArgs.transportFactory(newTTransportFactory());

TServerserver=newTThreadPoolServer(trArgs);

System.out.println("serverbegin......................");

server.serve();

System.out.println("---------------------------------------");

server.stop();

}catch(Exceptione){

thrownewRuntimeException("thriftserverstartfailed!!"+"/n"

+e.getMessage());

}

}

}

3.OurTestJavaClientclass

packagecom.sillycat.easytalker.plugins.thrift.client;

importjava.util.ArrayList;

importjava.util.Date;

importjava.util.List;

importorg.apache.thrift.TException;

importorg.apache.thrift.protocol.TBinaryProtocol;

importorg.apache.thrift.protocol.TProtocol;

importorg.apache.thrift.transport.TSocket;

importorg.apache.thrift.transport.TTransport;

importcom.sillycat.easytalker.plugins.thrift.gen.code.Blog;

importcom.sillycat.easytalker.plugins.thrift.gen.code.BlogService;

publicclassThriftJavaClient{

publicstaticvoidmain(String[]args)throwsTException{

longstart=System.currentTimeMillis();

TTransporttransport=newTSocket("127.0.0.1",9813);

TProtocolprotocol=newTBinaryProtocol(transport);

BlogService.Clientclient=newBlogService.Client(

protocol);

transport.open();

//createBlog

Blogb1=newBlog();

b1.setContent("test1content".getBytes());

b1.setTopic("topic1");

b1.setCreatedTime(newDate().getTime());

b1.setIpAddress("127.0.0.1");

Stringresult_CreateBlog=client.createBlog(b1);

System.out.println("result_CreateBlog="+result_CreateBlog);

//batchCreateBlog

List<Blog>blogs=newArrayList<Blog>();

Blogb2=newBlog();

b2.setContent("test2content".getBytes());

b2.setTopic("topic2");

b2.setCreatedTime(newDate().getTime());

b2.setIpAddress("127.0.0.1");

b2.setId("2");

blogs.add(b1);

blogs.add(b2);

List<String>result_ids=client.batchCreateBlog(blogs);

System.out.println("result_ids="+result_ids);

//deleteBlog

Stringresult_deleteBlog=client.deleteBlog("1");

System.out.println("result_deleteBlog="+result_deleteBlog);

//getOne

Blogresult_GetOne=client.getOne("id");

System.out.println("result_GetOne="+result_GetOne);

//listAll

List<Blog>result_ListAll=client.listAll();

System.out.println("result_ListAll="+result_ListAll);

//updateBlog

Stringresult_UpdateBlog=client.updateBlog(b1);

System.out.println("result_UpdateBlog="+result_UpdateBlog);

transport.close();

System.out.println((System.currentTimeMillis()-start));

System.out.println("clientsucess!");

}

}

references:

http://li3huo.com/2011/10/creating-a-thrift-service-step-by-step/

相关推荐