[TOC]
TProcessor
public interface TProcessor {
public boolean process(TProtocol in, TProtocol out) throws TException;
}
TProcessor定义了一个接口,负责从输入中获取请求信息,调用用户自己实现的服务,并将结果写入到输出中。
Thrift提供了两个实现了TProcessor接口的抽象类:TBaseProcessor & TBaseAsyncProcessor,本文只介绍TBaseProcessor。
ProcessFunction
在介绍TBaseProcessor之前,需要先介绍ProcessFunction抽象类,因为这个类和其子类是TBaseProcessor功能的实现者。
Thrift将IDL中定义的每个方法抽象为一个类,即ProcessFunction类。该类负责从输入中读取参数,调用用户服务(TBaseProcessor通过ProcessFunction完成对用户服务的调用),将响应写回到输出中。
其有两个主要方法:
public final void process(int seqid, TProtocol iprot, TProtocol oprot, I iface) throws TException {
// 1. 读取参数
args.read(iprot);
// 2. 调用用户服务
result = getResult(iface, args);
// 3. 输出
if(!isOneway()) { // 标记为oneway的请求不需要返回结果
oprot.writeMessageBegin(new TMessage(getMethodName(), msgType, seqid));
result.write(oprot);
oprot.writeMessageEnd();
oprot.getTransport().flush();
}
}
// 在getResult中完成对用户服务的调用,具体调用什么方法由子类指定。
public abstract TBase getResult(I iface, T args) throws TException;
Thrift编译IDL文件生成java代码时,为各个方法自动生成相应的ProcessFunction子类。例如IDL文件中定义了如下方法:
void waveHand();
其对应的ProcessFunction子类如下所示:
public static class Processor<I extends Iface> extends org.apache.thrift.TBaseProcessor<I> implements org.apache.thrift.TProcessor {
...
...
...
// 用户方法对应的ProcessFunction类定义在该用户服务对应的TBaseProcessor子类中,TBaseProcessor子类也是在编译IDL文件时由Thrift自动生成的
public static class waveHand<I extends Iface> extends org.apache.thrift.ProcessFunction<I, waveHand_args> {
public waveHand() {
super("waveHand");
}
public waveHand_args getEmptyArgsInstance() {
return new waveHand_args();
}
protected boolean isOneway() {
return false;
}
@Override
protected boolean handleRuntimeExceptions() {
return false;
}
public waveHand_result getResult(I iface, waveHand_args args) throws org.apache.thrift.TException {
waveHand_result result = new waveHand_result();
// 调用用户服务
iface.waveHand();
return result;
}
}
...
...
...
}
TBaseProcessor
讲完用户方法对应的ProcessFunction,在回过头来看 TBaseProcessor。其实TBaseProcessor和ProcessFunction的关系与用户定义在IDL中的service和方法的关系类似,这点在swift代码的实现中有更明显的表现。
TBaseProcessor维护了两个变量,一个是用户服务接口,一个是用户定义方法名与其对应的ProcessFunction子类的映射关系。从这里也可以看出,Thrift不支持方法的重载
。
private final I iface;
private final Map<String,ProcessFunction<I, ? extends TBase>> processMap;
TBaseProcessor实现了TProcessor接口,先从输入中获取方法名,然后找到相应的ProcessFunction实例,通过ProcessFunction实例完成对请求的处理。
@Override
public boolean process(TProtocol in, TProtocol out) throws TException {
// 获取方法名
TMessage msg = in.readMessageBegin();
// 获取相应的ProcessFunction实例
ProcessFunction fn = processMap.get(msg.name);
// 不存在则跑出方法不存在的异常
if (fn == null) {
TProtocolUtil.skip(in, TType.STRUCT);
in.readMessageEnd();
TApplicationException x = new TApplicationException(TApplicationException.UNKNOWN_METHOD, "Invalid method name: '"+msg.name+"'");
out.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid));
x.write(out);
out.writeMessageEnd();
out.getTransport().flush();
return true;
}
// 用ProcessFunction实例完成对请求的处理
fn.process(msg.seqid, in, out, iface);
return true;
}
Processor
在编译IDL文件的时候,Thrift为每个用户定义的服务生成一个TBaseProcessor的子类Processor。
Processor中包含各个用户定义方法对应的ProcessFunction子类,并在构造方法中完成对用户定义方法名与其对应的ProcessFunction子类实例的映射。
public Processor(I iface) {
super(iface, getProcessMap(new java.util.HashMap<java.lang.String, org.apache.thrift.ProcessFunction<I, ? extends org.apache.thrift.TBase>>()));
}
protected Processor(I iface, java.util.Map<java.lang.String, org.apache.thrift.ProcessFunction<I, ? extends org.apache.thrift.TBase>> processMap) {
super(iface, getProcessMap(processMap));
}
private static <I extends Iface> java.util.Map<java.lang.String, org.apache.thrift.ProcessFunction<I, ? extends org.apache.thrift.TBase>> getProcessMap(java.util.Map<java.lang.String, org.apache.thrift.ProcessFunction<I, ? extends org.apache.thrift.TBase>> processMap) {
processMap.put("sayHello", new sayHello());
processMap.put("waveHand", new waveHand());
processMap.put("findPerson", new findPerson());
processMap.put("showPerson", new showPerson());
return processMap;
}