天天看点

thrift的使用—servlet服务器端与as3客户端通信

 项目一直有用到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=&gt;"+user.toString());

return user;  

   }

public List&lt;User&gt; getUsers() throws TException {

      List&lt;User&gt; list = new ArrayList&lt;User&gt;();  

       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=&gt;"+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("--&gt;&gt;"+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("--&gt;"+user.toString());

接下来生成as3代码

&gt;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,如需转载请自行联系原作者