Thrift(4)Java Server and Client

Thrift(4)Java Server and Client

1. First, I did a Implementation class for BlogService Interface
package com.sillycat.easytalker.plugins.thrift.business;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.thrift.TException;

import com.sillycat.easytalker.plugins.thrift.gen.code.Blog;
import com.sillycat.easytalker.plugins.thrift.gen.code.BlogService;

public class BlogServiceImpl implements BlogService.Iface {

private Logger logger = Logger.getLogger(this.getClass());

public String createBlog(Blog blog) throws TException {
logger.debug("Method createBlog is invoked! Parameters blog = " + blog
+ " topic = " + blog.getTopic());
return "1";
}

public List<String> batchCreateBlog(List<Blog> blogs) throws TException {
logger.debug("Method batchCreateBlog is invoked! Parameters blogs = "
+ blogs);
List<String> ids = new ArrayList<String>();
ids.add("1");
ids.add("2");
ids.add("3");
return ids;
}

public String deleteBlog(String id) throws TException {
logger.debug("Method deleteBlog is invoked! Parameters id = " + id);
return "1";
}

public List<Blog> listAll() throws TException {
logger.debug("Method listAll is invoked!");
List<Blog> blogs = new ArrayList<Blog>();
Blog b1 = new Blog();
Blog b2 = new Blog();
b1.setContent("test1 content".getBytes());
b1.setTopic("topic1");
b1.setCreatedTime(new Date().getTime());
b1.setIpAddress("127.0.0.1");
b1.setId("1");

b2.setContent("test2 content".getBytes());
b2.setTopic("topic2");
b2.setCreatedTime(new Date().getTime());
b2.setIpAddress("127.0.0.1");
b2.setId("2");

blogs.add(b1);
blogs.add(b2);
return blogs;
}

public Blog getOne(String id) throws TException {
logger.debug("Method getOne is invoked! Parameters id = " + id);
Blog b1 = new Blog();
b1.setContent("test1 content".getBytes());
b1.setTopic("topic1");
b1.setCreatedTime(new Date().getTime());
b1.setIpAddress("127.0.0.1");
b1.setId("1");
return b1;
}

public String updateBlog(Blog blog) throws TException {
logger.debug("Method updateBlog is invoked! Parameters blog = " + blog
+ " topic = " + blog.getTopic());
return "1";
}

}

2. Then I try to create the ThriftJavaServer
package com.sillycat.easytalker.plugins.thrift.server;

import java.net.InetSocketAddress;  

import org.apache.thrift.protocol.TBinaryProtocol;  
import org.apache.thrift.server.TServer;  
import org.apache.thrift.server.TThreadPoolServer;  
import org.apache.thrift.server.TThreadPoolServer.Args;  
import org.apache.thrift.transport.TServerSocket;  
import org.apache.thrift.transport.TServerTransport;  
import org.apache.thrift.transport.TTransportFactory;

import com.sillycat.easytalker.plugins.thrift.business.BlogServiceImpl;
import com.sillycat.easytalker.plugins.thrift.gen.code.BlogService;

public class ThriftJavaServer {

public static void main(String[] args) {

BlogService.Processor<BlogServiceImpl> processor = new BlogService.Processor<BlogServiceImpl>(
new BlogServiceImpl());
try {
TServerTransport serverTransport = new TServerSocket(
new InetSocketAddress("0.0.0.0", 9813));
Args trArgs = new Args(serverTransport);
trArgs.processor(processor);
trArgs.protocolFactory(new TBinaryProtocol.Factory(true, true));
trArgs.transportFactory(new TTransportFactory());
TServer server = new TThreadPoolServer(trArgs);
System.out.println("server begin ......................");
server.serve();
System.out.println("---------------------------------------");
server.stop();
} catch (Exception e) {
throw new RuntimeException("thrift server start failed!!" + "/n"
+ e.getMessage());
}

}

}

3. Our Test Java Client class
package com.sillycat.easytalker.plugins.thrift.client;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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 com.sillycat.easytalker.plugins.thrift.gen.code.Blog;
import com.sillycat.easytalker.plugins.thrift.gen.code.BlogService;

public class ThriftJavaClient {

public static void main(String[] args) throws TException {
long start = System.currentTimeMillis();

TTransport transport = new TSocket("127.0.0.1", 9813);
TProtocol protocol = new TBinaryProtocol(transport);
BlogService.Client client = new BlogService.Client(
protocol);
transport.open();

//createBlog
Blog b1 = new Blog();
b1.setContent("test1 content".getBytes());
b1.setTopic("topic1");
b1.setCreatedTime(new Date().getTime());
b1.setIpAddress("127.0.0.1");
String result_CreateBlog = client.createBlog(b1);
System.out.println("result_CreateBlog = " + result_CreateBlog);

//batchCreateBlog
List<Blog> blogs = new ArrayList<Blog>();
Blog b2 = new Blog();
b2.setContent("test2 content".getBytes());
b2.setTopic("topic2");
b2.setCreatedTime(new Date().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
String result_deleteBlog = client.deleteBlog("1");
System.out.println("result_deleteBlog = " + result_deleteBlog);

//getOne
Blog result_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
String result_UpdateBlog = client.updateBlog(b1);
System.out.println("result_UpdateBlog = " + result_UpdateBlog);

transport.close();
System.out.println((System.currentTimeMillis() - start));
System.out.println("client sucess!");

}

}


references:
http://li3huo.com/2011/10/creating-a-thrift-service-step-by-step/
http://gemantic.iteye.com/blog/1199214



猜你喜欢

转载自sillycat.iteye.com/blog/1586422