可以看到,client的端的调用是很简单。
可以简单的来看一下代码:
public Writable call(Writable param, ConnectionId remoteId),此方法是调用入口,代码分析:
/**
* 创建一个call,并得到连接,之后在连接中保存call,之后向连接的输出流写入请求
* 并返回, 底层使用的是oio(即blocking io),采用什么样的io与异步消息机制没有
* 必然联系.
**/
Call call = new Call(param);
Connection connection = getConnection(remoteId, call);
connection.sendParam(call);
// 接口是同步的,异步变同步的操作再这里
synchronized (call) {
while (!call.done) {
try {
call.wait(); // wait for the result
} catch (InterruptedException ie) {
// save the fact that we were interrupted
interrupted = true;
}
}
……
}
对应的,可以看到Connection类的receiveResponse方法里处理从server里读到的结果:
int id = in.readInt(); // try to read an id
Call call = calls.get(id);
Writable value = ReflectionUtils.newInstance(valueClass, conf);
value.readFields(in); // read value
call.setValue(value);
calls.remove(id);
Call调用setValue方式,会执行notify操作.
备注: