use of io.netty.channel.ChannelFutureListener in project iris by chicc999.
the class HandlerTask method run.
@Override
public void run() {
// 处理请求命令
Header header = request.getHeader();
Command response = null;
try {
response = handler.process(ctx, request);
} catch (Throwable e) {
//如果请求需要答复
if (request.getHeader().getAcknowledge() != Acknowledge.ACK_NO) {
//写出响应,如果出现异常则调用exceptionCaught打印异常关闭连接
ctx.writeAndFlush(new ErrorResponse(-1, e.getMessage(), request.getRequestId())).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
}
}
response.getHeader().setRequestId(request.getRequestId());
ChannelFutureListener listenner = response.getListenner() == null ? ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE : response.getListenner();
//服务端不处理commandCallback,直接在DispatcherHandler处理对应type.客户端则在发送时指定回调函数.
ctx.writeAndFlush(response).addListener(listenner);
}
use of io.netty.channel.ChannelFutureListener in project hive by apache.
the class Rpc method call.
/**
* Send an RPC call to the remote endpoint and returns a future that can be used to monitor the
* operation.
*
* @param msg RPC call to send.
* @param retType Type of expected reply.
* @return A future used to monitor the operation.
*/
public <T> Future<T> call(final Object msg, Class<T> retType) {
Preconditions.checkArgument(msg != null);
Preconditions.checkState(channel.isActive(), "RPC channel is closed.");
try {
final long id = rpcId.getAndIncrement();
final Promise<T> promise = createPromise();
final ChannelFutureListener listener = new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture cf) {
if (!cf.isSuccess() && !promise.isDone()) {
LOG.warn("Failed to send RPC, closing connection.", cf.cause());
promise.setFailure(cf.cause());
dispatcher.discardRpc(id);
close();
}
}
};
dispatcher.registerRpc(id, promise, msg.getClass().getName());
channel.eventLoop().submit(new Runnable() {
@Override
public void run() {
channel.write(new MessageHeader(id, Rpc.MessageType.CALL)).addListener(listener);
channel.writeAndFlush(msg).addListener(listener);
}
});
return promise;
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
Aggregations