PpcHandler用到RpcResponseCallback,RpcResponseCallback是RPC结果的回调函数,有onSuccess和onFailure两个方法。
onSuccess方法当成功从服务器序列化结果时调用。当onSuccess调用返回时,参数ByteBuffer response会被回收,它的内容会变得不再有效。如果在onSucess方法返回之后还要使用ByteBuffer的内容,请把ByteBuffer的内容拷贝一份。
onFailure传递从服务端传过来的异常信息,或者客户端抛出的异常信息。
/**
* Callback for the result of a single RPC. This will be invoked once with either success or
* failure.
*/
public interface RpcResponseCallback {
/**
* Successful serialized result from server.
*
* After `onSuccess` returns, `response` will be recycled and its content will become invalid.
* Please copy the content of `response` if you want to use it after `onSuccess` returns.
*/
void onSuccess(ByteBuffer response);
/** Exception either propagated from server or raised on client side. */
void onFailure(Throwable e);
}
OneWayRpcCallBack实现了OneWayRpcCallback,在onSuccess和onFailure方法中,在log中打印一条警告日志。
private static class OneWayRpcCallback implements RpcResponseCallback {
private final Logger logger = LoggerFactory.getLogger(OneWayRpcCallback.class);
@Override
public void onSuccess(ByteBuffer response) {
logger.warn("Response provided for one-way RPC.");
}
@Override
public void onFailure(Throwable e) {
logger.error("Error response provided for one-way RPC.", e);
}
}
RpcResponseCallback 处理从TransportClient 调用sendRPC()发送过来的信息。
onReceive方法:接收一个RPC信息。当发生异常时,这个方法中的异常信息会以一个字符串的格式被发送回客户端。一个TransportClient对象中,这个方法不能被多线程调用。
getStreamManager方法:返回StreamMaager,这个StreamManager包含哪个流正在被TransportClient读的状态信息。
channelActive():当客户端所在的通道活动时调用。
channelInactive(): 当客户端所在的通道不活动时调用。
excpetionCautht(): 异常信息
/**
* Handler for sendRPC() messages sent by {@link org.apache.spark.network.client.TransportClient}s.
*/
public abstract class RpcHandler {
private static final RpcResponseCallback ONE_WAY_CALLBACK = new OneWayRpcCallback();
/**
* Receive a single RPC message. Any exception thrown while in this method will be sent back to
* the client in string form as a standard RPC failure.
*
* This method will not be called in parallel for a single TransportClient (i.e., channel).
*
* @param client A channel client which enables the handler to make requests back to the sender
* of this RPC. This will always be the exact same object for a particular channel.
* @param message The serialized bytes of the RPC.
* @param callback Callback which should be invoked exactly once upon success or failure of the
* RPC.
*/
public abstract void receive(
TransportClient client,
ByteBuffer message,
RpcResponseCallback callback);
/**
* Returns the StreamManager which contains the state about which streams are currently being
* fetched by a TransportClient.
*/
public abstract StreamManager getStreamManager();
/**
* Receives an RPC message that does not expect a reply. The default implementation will
* call "{@link #receive(TransportClient, ByteBuffer, RpcResponseCallback)}" and log a warning if
* any of the callback methods are called.
*
* @param client A channel client which enables the handler to make requests back to the sender
* of this RPC. This will always be the exact same object for a particular channel.
* @param message The serialized bytes of the RPC.
*/
public void receive(TransportClient client, ByteBuffer message) {
receive(client, message, ONE_WAY_CALLBACK);
}
/**
* Invoked when the channel associated with the given client is active.
*/
public void channelActive(TransportClient client) { }
/**
* Invoked when the channel associated with the given client is inactive.
* No further requests will come from this client.
*/
public void channelInactive(TransportClient client) { }
public void exceptionCaught(Throwable cause, TransportClient client) { }
}