項目一直有用到thrift這東西,thrift的介紹網上也挺多的。
今天寫了個servlet伺服器端的http請求與as3用戶端請求的列子。這裡做個筆記哈。
test.thrift
namespace java com.xzw.thrift
namespace as3 com.xzw.thrift
struct User{
1:i32 userId,
2:string loginName,
3:string password,
4:string name
}
exception UserNotFound{
1:string msg //as3中由于message是關鍵字,是以避免使用message關鍵字。
service UserService{
User getUser(1:string loginName) throws (1:UserNotFound unf),
list<User> getUsers()
通過指令生成java代碼
>thrift --gen java test.thrift
以上指令會生成gen-java的目錄拷貝裡面的代碼到你的項目中。如圖com.xzw.thrift就是
工具生成java類。
<a href="http://blog.51cto.com/attachment/201304/162207662.jpg" target="_blank"></a>
其中我們需要去實作UserService類中的IFace接口。代碼如下:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.xzw.service;
import com.xzw.thrift.User;
import com.xzw.thrift.UserNotFound;
import com.xzw.thrift.UserService;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.apache.thrift.TException;
/**
*
* @author xzw
publicclass UserServiceHandler implements UserService.Iface{
public User getUser(String loginName) throws UserNotFound, TException {
if(!"xuzhiwei".equals(loginName)){
UserNotFound e = new UserNotFound("使用者無法找到!");
throw e;
}
User user = new User();
user.setUserId(100);
user.setLoginName("xuzhiwei");
user.setPassword("123456");
user.setName("user1");
Logger.getLogger("user=>"+user.toString());
return user;
}
public List<User> getUsers() throws TException {
List<User> list = new ArrayList<User>();
User user = new User();
user.setUserId(100);
user.setLoginName("xuzhiwei");
user.setName("user1");
list.add(user);
User user2 = new User();
user2.setUserId(200);
user2.setLoginName("login2");
user2.setPassword("pwd2");
user2.setName("user2");
list.add(user2);
Logger.getLogger("user list=>"+list.toString());
return list;
編寫servlet,servlet需要繼承thrift提供的類包TServlet即可,不需要去重寫doGet,doPost方法。
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServlet;
publicclass UserServlet extends TServlet{
public UserServlet(){
super(new UserService.Processor(new UserServiceHandler()),new TBinaryProtocol.Factory(true, true));
}
以上就完成伺服器端的編寫了。
可以使用junit測試一下:
import java.util.logging.Level;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.THttpClient;
import org.junit.*;
publicclass TestServlet {
@Test
publicvoid test(){
String serveltUrl = "http://localhost:8080/test_thrift/userServlet";
try {
THttpClient thc = new THttpClient(serveltUrl);
TProtocol lopFactory = new TBinaryProtocol(thc);
UserService.Client client = new UserService.Client(lopFactory);
List users = client.getUsers();
System.out.println("-->>"+users.toString());
} catch (TException ex) {
Logger.getLogger(TestServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
publicvoid test2(){
User user = client.getUser("xuzhiwei");
System.out.println("-->"+user.toString());
接下來生成as3代碼
>thrift --gen as3 test.thrift
以上代碼會生成gen-as3的目錄,拷貝目錄下的代碼到你的工程中。
<a href="http://blog.51cto.com/attachment/201304/162916325.jpg" target="_blank"></a>
編寫as3用戶端類:
package
{
import com.xzw.thrift.User;
import com.xzw.thrift.UserNotFound;
import com.xzw.thrift.UserService;
import com.xzw.thrift.UserServiceImpl;
import flash.display.Sprite;
import flash.net.URLRequest;
import flash.text.TextField;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.THttpClient;
public class testthrift extends Sprite
{
private const SERVER_URL:String = "http://localhost:8080/test_thrift/userServlet";
private var textField:TextField;
publicfunction testthrift()
{
init();
connServer();
private function init():void
{
textField = new TextField();
textField.width = 300;
textField.height = 500;
textField.autoSize = "left";
addChild(textField);
private function connServer():void
{ //執行個體化URLRequest
var urlRequest:URLRequest = new URLRequest(SERVER_URL);
//執行個體化THttpClient
var thc:THttpClient = new THttpClient(urlRequest);
var protocol:TProtocol = new TBinaryProtocol(thc);
var usImpl:UserService = new UserServiceImpl(protocol);
usImpl.getUser("xuzhiwei",onError,onUserSuccess);
//usImpl.getUsers(onError,onSuccess);
private function onError(e:UserNotFound):void{
trace(e);
private function onUserSuccess(user:User):void{
trace("success:");
trace(user);
textField.text = user.toString();
private function onSuccess(user:Array):void{
寫完了,測試結果
<a href="http://blog.51cto.com/attachment/201304/163408638.jpg" target="_blank"></a>
thrift目前越來越多人在使用它,但是as3的一直找不到比較完整的例子。今天寫了與大家分享哈。
歡迎拍磚。
本文轉自xuzw13 51CTO部落格,原文連結:http://blog.51cto.com/xuzhiwei/1184540,如需轉載請自行聯系原作者