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

项目一直有用到thrift这东西,thrift的介绍网上也挺多的。

描述:thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml 这些编程语言间无缝结合的、高效的服务。

今天写了个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 getUsers()

}

通过命令生成java代码

>thrift --gen java test.thrift

以上命令会生成gen-java的目录拷贝里面的代码到你的项目中。如图com.xzw.thrift就是

工具生成java类。

3f732ad37977eed6ccfc0f9096e24191.png

其中我们需要去实现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 getUsers() throws TException {

List list = new ArrayList();

User user = new User();

user.setUserId(100);

user.setLoginName("xuzhiwei");

user.setPassword("123456");

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方法。

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package com.xzw.service;

import com.xzw.thrift.UserService;

import org.apache.thrift.protocol.TBinaryProtocol;

import org.apache.thrift.server.TServlet;

/**

*

* @author xzw

*/

publicclass UserServlet extends TServlet{

public UserServlet(){

super(new UserService.Processor(new UserServiceHandler()),new TBinaryProtocol.Factory(true, true));

}

}

以上就完成服务器端的编写了。

可以使用junit测试一下:

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

import com.xzw.thrift.User;

import com.xzw.thrift.UserService;

import java.util.List;

import java.util.logging.Level;

import java.util.logging.Logger;

import org.apache.thrift.TException;

import org.apache.thrift.protocol.TBinaryProtocol;

import org.apache.thrift.protocol.TProtocol;

import org.apache.thrift.transport.THttpClient;

import org.junit.*;

/**

*

* @author xzw

*/

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);

}

}

@Test

publicvoid test2(){

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);

User user = client.getUser("xuzhiwei");

System.out.println("-->"+user.toString());

} catch (TException ex) {

Logger.getLogger(TestServlet.class.getName()).log(Level.SEVERE, null, ex);

}

}

}

接下来生成as3代码

>thrift --gen as3 test.thrift

以上代码会生成gen-as3的目录,拷贝目录下的代码到你的工程中。

280b7c0deb0d919b0167866f312bd5e9.png

编写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{

trace(user);

textField.text = user.toString();

}

}

}

写完了,测试结果

60adc53ce04d8a887c30688a27823582.png

thrift目前越来越多人在使用它,但是as3的一直找不到比较完整的例子。今天写了与大家分享哈。

欢迎拍砖。

参考:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值