Search in sources :

Example 91 with ChannelFutureListener

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);
}
Also used : Header(pers.cy.iris.commons.network.protocol.Header) Command(pers.cy.iris.commons.network.protocol.Command) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ErrorResponse(pers.cy.iris.commons.network.protocol.response.ErrorResponse)

Example 92 with ChannelFutureListener

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);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelFutureListener(io.netty.channel.ChannelFutureListener) TimeoutException(java.util.concurrent.TimeoutException) SaslException(javax.security.sasl.SaslException) IOException(java.io.IOException)

Aggregations

ChannelFutureListener (io.netty.channel.ChannelFutureListener)92 ChannelFuture (io.netty.channel.ChannelFuture)87 Channel (io.netty.channel.Channel)29 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)24 Bootstrap (io.netty.bootstrap.Bootstrap)20 Test (org.junit.Test)19 ByteBuf (io.netty.buffer.ByteBuf)18 ClosedChannelException (java.nio.channels.ClosedChannelException)17 CountDownLatch (java.util.concurrent.CountDownLatch)16 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)15 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)15 IOException (java.io.IOException)12 ChannelPromise (io.netty.channel.ChannelPromise)11 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)11 ConnectException (java.net.ConnectException)11 InetSocketAddress (java.net.InetSocketAddress)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 AbstractChannel (io.netty.channel.AbstractChannel)6 EventLoopGroup (io.netty.channel.EventLoopGroup)6 ChannelPipeline (io.netty.channel.ChannelPipeline)5