Thrift源码分析(八)--总结加一个完整的可运行的Thrift例子

本文作为Thrift源码分析系列的总结,通过一个可运行的Thrift实例,展示从IDL定义到客户端调用的全过程,帮助读者巩固对Thrift的理解。
摘要由CSDN通过智能技术生成

前面七篇文章分析了Thrfit的方方面面,看到这里时应该对Thrift有了深入的理解。

Thrift源码分析(一)-- 基本概念

Thrift源码分析(二)-- 协议和编解码

Thrift源码分析(三)-- IDL和生成代码分析

Thrift源码分析(四)-- 方法调用模型分析

Thrift源码分析(五)-- FrameBuffer类分析

Thrift源码分析(六)-- Transport传输层分析

Thrift源码分析(七)-- TServer服务器分析

下面通过一个实际可以运行的例子来跑一跑Thrift,结束这个主题


1. 通过IDL来定义接口:   DemoService.thrift

namespace java com.thrift.test

service DemoService{
	string sayHi(1:string name);
}

2. 根据IDL自动生成代码

thrift -r --gen java DemoService.thrift

3. 生成的代码供服务器和客户端调用

/**
 * Autogenerated by Thrift Compiler (0.8.0)
 *
 * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
 *  @generated
 */
package com.thrift.test;

import org.apache.thrift.scheme.IScheme;
import org.apache.thrift.scheme.SchemeFactory;
import org.apache.thrift.scheme.StandardScheme;

import org.apache.thrift.scheme.TupleScheme;
import org.apache.thrift.protocol.TTupleProtocol;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.EnumMap;
import java.util.Set;
import java.util.HashSet;
import java.util.EnumSet;
import java.util.Collections;
import java.util.BitSet;
import java.nio.ByteBuffer;
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DemoService {

  public interface Iface {

    public String sayHi(String name) throws org.apache.thrift.TException;

  }

  public interface AsyncIface {

    public void sayHi(String name, org.apache.thrift.async.AsyncMethodCallback<AsyncClient.sayHi_call> resultHandler) throws org.apache.thrift.TException;

  }

  public static class Client extends org.apache.thrift.TServiceClient implements Iface {
    public static class Factory implements org.apache.thrift.TServiceClientFactory<Client> {
      public Factory() {}
      public Client getClient(org.apache.thrift.protocol.TProtocol prot) {
        return new Client(prot);
      }
      public Client getClient(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TProtocol oprot) {
        return new Client(iprot, oprot);
      }
    }

    public Client(org.apache.thrift.protocol.TProtocol prot)
    {
      super(prot, prot);
    }

    public Client(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TProtocol oprot) {
      super(iprot, oprot);
    }

    public String sayHi(String name) throws org.apache.thrift.TException
    {
      send_sayHi(name);
      return recv_sayHi();
    }

    public void send_sayHi(String name) throws org.apache.thrift.TException
    {
      sayHi_args args = new sayHi_args();
      args.setName(name);
      sendBase("sayHi", args);
    }

    public String recv_sayHi() throws org.apache.thrift.TException
    {
      sayHi_result result = new sayHi_result();
      receiveBase(result, "sayHi");
      if (result.isSetSuccess()) {
        return result.success;
      }
      throw new org.apache.thrift.TApplicationException(org.apache.thrift.TApplicationException.MISSING_RESULT, "sayHi failed: unknown result");
    }

  }
  public static class AsyncClient extends org.apache.thrift.async.TAsyncClient implements AsyncIface {
    public static class Factory implements org.apache.thrift.async.TAsyncClientFactory<AsyncClient> {
      private org.apache.thrift.async.TAsyncClientManager clientManager;
      private org.apache.thrift.protocol.TProtocolFactory protocolFactory;
      public Factory(org.apache.thrift.async.TAsyncClientManager clientManager, org.apache.thrift.protocol.TProtocolFactory protocolFactory) {
        this.clientManager = clientManager;
        this.protocolFactory = protocolFactory;
      }
      public AsyncClient getAsyncClient(org.apache.thrift.transport.TNonblockingTransport transport) {
        return new AsyncClient(protocolFactory, clientManager, transport);
      }
    }

    public AsyncClient(org.apache.thrift.protocol.TProtocolFactory protocolFactory, org.apache.thrift.async.TAsyncClientManager clientManager, org.apache.thrift.transport.TNonblockingTransport transport) {
      super(protocolFactory, clientManager, transport);
    }

    public void sayHi(String name, org.apache.thrift.async.AsyncMethodCallback<sayHi_call> resultHandler) throws org.apache.thrift.TException {
      checkReady();
      sayHi_call method_call = new sayHi_call(name, resultHandler, this, ___protocolFactory, ___transport);
      this.___currentMethod = method_call;
      ___manager.call(method_call);
    }

    public static class sayHi_call extends org.apache.thrift.async.TAsyncMethodCall {
      private String name;
      public sayHi_call(String name, org.apache.thrift.async.AsyncMethodCallback<sayHi_call> resultHandler, org.apache.thrift.async.TAsyncClient client, org.apache.thrift.protocol.TProtocolFactory protocolFactory, org.apache.thrift.transport.TNonblockingTransport transport) throws org.apache.thrift.TException {
        super(client, protocolFactory, transport, resultHandler, false);
        this.name = name;
      }

      public void write_args(org.apache.thrift.protocol.TProtocol prot) throws org.apache.thrift.TException {
        prot.writeMessageBegin(new org.apache.thrift.protocol.TMessage("sayHi", org.apache.thrift.protocol.TMessageType.CALL, 0));
        sayHi_args args = new sayHi_args();
        args.setName(name);
        args.write(prot);
        prot.writeMessageEnd();
      }

      public String
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值