文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Java Thrift Server Sample

Java Thrift Server Sample

时间:2010-04-20  来源:剑胆琴心

Thrift 是facebook的核心技术之一,使用不同开发语言开发的系统可以通过该框架实现彼此间的通讯,开发者只需编辑一份thrift脚本,即可自动获得其它开发语言的代码(比如 c++ java python ruby c# haskell ocmal erlang cocoa php squeak).同时提供了完整的rpc service framework,可以很方便的直接构建服务。
简单实现一个User Activity的log服务和客户端,只是作简单的演示,所以没有实现异步调用等缓冲机制(比如Queue),具体实现可以根据自己的情况定制。 目标:实现一个user activity log 服务。
step 1:安装Thrift please refer to http://incubator.apache.org/thrift/ step 2:编写Thrift脚本,存为DataReceiver.thrift


namespace java thrift.test

struct Activity{
  1: i64 uid,
  2: i32 type,
  3: i64 timestamp,
  4: map<string,string> maps,
}

service DataReceiver{
        oneway void postData(1: Activity ac),
        oneway void postDataArray(1: list<Activity> arg),
}


step 3:生成代码

thrift --gen java DataReceiver.thrift


step 4:根据需要,编写自己的代码 a.server实现类

package com.happyelements.DataCenter;

import java.util.List;

import org.apache.thrift.TException;

public class DataServerImpl implements DataReceiver.Iface{

    @Override
    public void postData(Activity ac) throws TException {
        // TODO Auto-generated method stub
        //这里写自己的定制代码
        
    }

    @Override
    public void postDataArray(List<Activity> arg) throws TException {
        // TODO Auto-generated method stub
       //这里写自己的定制代码
        long time = System.currentTimeMillis();
        System.out.println("postDataArray called timestamp: " + time);
        List<Activity> activities = arg;
        System.out.println("len = "+activities.size());
        for(int i=0; i<activities.size(); i++){
            Activity ac = activities.get(i);
            System.out.println(ac.getUid());
            System.out.println(ac.getType());
            System.out.println(ac.getMaps().get("foo"));
        }
    }
}

b.服务器

package com.happyelements.DataCenter;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TBinaryProtocol.Factory;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;

public class DataServer {

     private void start(){
              try {
              TServerSocket serverTransport = new TServerSocket(8811);
              DataReceiver.Processor processor = new DataReceiver.Processor(new DataServerImpl());
              Factory protFactory = new TBinaryProtocol.Factory(true, true);
              TServer server = new TThreadPoolServer(processor, serverTransport, protFactory);
              System.out.println("Starting server on port 8811 ...");
              server.serve();
              } catch (TTransportException e) {
              e.printStackTrace();
              } catch (Exception e) {
              e.printStackTrace();
              }
              }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        DataServer srv = new DataServer();
        srv.start();
    }

}

c.客户端

package com.happyelements.DataCenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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 org.apache.thrift.transport.TTransportException;

import com.happyelements.DataCenter.DataReceiver.Client;

public class TestClient {
    private void start() {
        TTransport transport;
        try {
            transport = new TSocket("localhost", 8811);
            TProtocol protocol = new TBinaryProtocol(transport);
            Client client = new Client(protocol);
            transport.open();
            List<Activity> la = new ArrayList<Activity>();
            Activity ac = new Activity();
            ac.setType(1);
            ac.setUid(12345);
            Map<String, String> map = new HashMap<String, String>();
            map.put("foo", "blablabla...");
            ac.setMaps(map);
            
            la.add(ac);
            client.postDataArray(la);
            transport.close();
        } catch (TTransportException e) {
            e.printStackTrace();
        } catch (TException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        TestClient c = new TestClient();
            c.start();
    }
}


以上代码需要的jar包: libthrift.jar slf4j-api-1.5.8.jar slf4j-log4j12-1.5.8.jar log4j-1.2.15.jar

ok. thats all.
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载