rpcHooks是一个泛型为rpcHook的集合:
protected List<RPCHook> rpcHooks = new ArrayList<RPCHook>();
进入RPCHook:
org.apache.rocketmq.remoting.RPCHook
public interface RPCHook {
void doBeforeRequest(final String remoteAddr, final RemotingCommand request);
void doAfterResponse(final String remoteAddr, final RemotingCommand request,
final RemotingCommand response);
}
可以看到接口内部有两个方法,分别是请求前执行和响应后执行。接口的实现类的实例化对象作为一个对象存入rpcHooks集合中,后期会调用并统一执行。
@Override
public void registerProcessor(int requestCode, NettyRequestProcessor processor, ExecutorService executor) {
ExecutorService executorThis = executor;
if (null == executor) {
executorThis = this.publicExecutor;
}
Pair<NettyRequestProcessor, ExecutorService> pair = new Pair<NettyRequestProcessor, ExecutorService>(processor, executorThis);
this.processorTable.put(requestCode, pair);
}
注册处理器,其实就是把请求码,处理器和执行器注册到NettyRemotingServer的processorTable中。执行器如果为空的话,会选择默认的publicExecutor
processorTable:
protected final HashMap<Integer/* request code */, Pair<NettyRequestProcessor, ExecutorService>> processorTable =
new HashMap<Integer, Pair<NettyRequestProcessor, ExecutorService>>(64);
继续往下看:
@Override
public RemotingCommand invokeSync(final Channel channel, final RemotingCommand request, final long timeoutMillis)
throws InterruptedException, RemotingSendRequestException, RemotingTimeoutException {
return this.invokeSyncImpl(channel, request, timeoutMillis);
}
同步引用,继续看调用的方法:
public RemotingCommand invokeSyncImpl(final Channel channel, final RemotingCommand request,
final long timeoutMillis)
throws InterruptedException, RemotingSendRequestException, RemotingTimeoutException {
final int opaque = request.getOpaque();
try {
final ResponseFuture responseFuture = new ResponseFuture(channel, opaque, timeoutMillis, null, null);
this.responseTable.put(opaque, responseFuture);
final SocketAddress addr = channel.remoteAddress();
进来之后可以看到,首先把同步调用的responseFuture包好放到responseTable里,然后准备发送请求,请求之后再用这个responseFuture去接结果,可以认为是留了个钩子在调用方了。