Example usage for io.netty.channel ChannelFutureListener ChannelFutureListener

List of usage examples for io.netty.channel ChannelFutureListener ChannelFutureListener

Introduction

In this page you can find the example usage for io.netty.channel ChannelFutureListener ChannelFutureListener.

Prototype

ChannelFutureListener

Source Link

Usage

From source file:com.spotify.ffwd.protocol.RetryingProtocolConnection.java

License:Apache License

@Override
public void send(Object message) {
    final Channel c = channel.get();

    if (c == null) {
        return;//from w  w w. ja v a2 s.c om
    }

    c.writeAndFlush(message).addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (!future.isSuccess()) {
                log.error("failed to send metric", future.cause());
            }
        }
    });
}

From source file:com.spotify.ffwd.protocol.RetryingProtocolConnection.java

License:Apache License

@Override
public AsyncFuture<Void> sendAll(Collection<? extends Object> batch) {
    final Channel c = channel.get();

    if (c == null) {
        return async.failed(new IllegalStateException("not connected"));
    }//from  w w  w.  j  ava  2s  . c  om

    final ResolvableFuture<Void> future = async.future();

    c.writeAndFlush(batch).addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture f) throws Exception {
            try {
                future.resolve(f.get());
            } catch (ExecutionException e) {
                future.fail(e);
            }
        }
    });

    return future;
}

From source file:com.spotify.folsom.client.DefaultRawMemcacheClient.java

License:Apache License

public static ListenableFuture<RawMemcacheClient> connect(final HostAndPort address,
        final int outstandingRequestLimit, final boolean binary, final Executor executor,
        final long timeoutMillis) {

    final ChannelInboundHandler decoder;
    if (binary) {
        decoder = new BinaryMemcacheDecoder();
    } else {//from   www  .ja  v a2  s . co  m
        decoder = new AsciiMemcacheDecoder();
    }

    final ChannelHandler initializer = new ChannelInitializer<Channel>() {
        @Override
        protected void initChannel(final Channel ch) throws Exception {
            ch.pipeline().addLast(new TcpTuningHandler(), decoder,

                    // Downstream
                    new MemcacheEncoder());
        }
    };

    final SettableFuture<RawMemcacheClient> clientFuture = SettableFuture.create();

    final Bootstrap bootstrap = new Bootstrap().group(EVENT_LOOP_GROUP).handler(initializer)
            .channel(NioSocketChannel.class)
            .option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, SimpleSizeEstimator.INSTANCE);

    final ChannelFuture connectFuture = bootstrap
            .connect(new InetSocketAddress(address.getHostText(), address.getPort()));

    connectFuture.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(final ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                // Create client
                final RawMemcacheClient client = new DefaultRawMemcacheClient(address, future.channel(),
                        outstandingRequestLimit, executor, timeoutMillis);
                clientFuture.set(client);
            } else {
                clientFuture.setException(future.cause());
            }
        }
    });

    return onExecutor(clientFuture, executor);
}

From source file:com.spotify.heroic.consumer.collectd.Server.java

License:Apache License

public static AsyncFuture<Server> setup(final AsyncFramework async, final CollectdChannelHandler handler,
        final InetAddress host, final int port) {
    final EventLoopGroup group = new NioEventLoopGroup();
    final Bootstrap b = new Bootstrap();

    b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true).handler(handler);

    final ResolvableFuture<Server> future = async.future();

    b.bind(host, port).addListener(new ChannelFutureListener() {
        @Override/*from  w ww. ja  v  a2s  . co m*/
        public void operationComplete(final ChannelFuture f) throws Exception {
            if (f.isSuccess()) {
                future.resolve(new Server(async, f.channel()));
            } else {
                future.fail(f.cause() != null ? f.cause() : new RuntimeException("Failed to bind"));
            }
        }
    });

    return future;
}

From source file:com.spotify.heroic.rpc.nativerpc.NativeRpcClient.java

License:Apache License

private <R> ChannelFutureListener handleConnect(final NativeRpcRequest request,
        final ResolvableFuture<R> future, final AtomicReference<Timeout> heartbeatTimeout,
        final Timeout requestTimeout) {
    return new ChannelFutureListener() {
        @Override/*  w ww.j  a  v a 2 s  .  c om*/
        public void operationComplete(ChannelFuture f) throws Exception {
            if (!f.isSuccess()) {
                future.fail(f.cause());
                return;
            }

            f.channel().writeAndFlush(request)
                    .addListener(handleRequestSent(future, heartbeatTimeout, requestTimeout));
        }
    };
}

From source file:com.spotify.heroic.rpc.nativerpc.NativeRpcClient.java

License:Apache License

private <R> ChannelFutureListener handleRequestSent(final ResolvableFuture<R> future,
        final AtomicReference<Timeout> heartbeatTimeout, final Timeout requestTimeout) {
    return new ChannelFutureListener() {
        @Override/* www . j  av  a2 s  .  co  m*/
        public void operationComplete(ChannelFuture f) throws Exception {
            requestTimeout.cancel();

            if (!f.isSuccess()) {
                future.fail(f.cause());
                return;
            }

            final Timeout timeout = timer.newTimeout(heartbeatTimeout(f.channel(), future), heartbeatInterval,
                    TimeUnit.MILLISECONDS);

            heartbeatTimeout.set(timeout);
        }
    };
}

From source file:com.spotify.heroic.rpc.nativerpc.NativeRpcProtocolServer.java

License:Apache License

private AsyncFuture<Void> start() {
    final ServerBootstrap s = new ServerBootstrap();
    s.channel(NioServerSocketChannel.class);
    s.group(bossGroup, workerGroup);/*from  w ww  .  j  ava2s  . c  o m*/
    s.childHandler(new NativeRpcServerSession(timer, mapper, container, maxFrameSize, encoding));

    final ChannelFuture bind = s.bind(address);

    bind.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(final ChannelFuture f) throws Exception {
            if (!f.isSuccess()) {
                bindFuture.fail(f.cause());
                return;
            }

            serverChannel.set(f.channel());
            final InetSocketAddress address = (InetSocketAddress) f.channel().localAddress();
            bindFuture.resolve(address);
        }
    });

    return bindFuture.directTransform(a -> null);
}

From source file:com.supermy.im.netty.TCPServer.java

License:Apache License

/**
 * 3??/*w  w  w.j  a  v  a2s .  co m*/
 */
protected void doBind() {
    if (closed) {
        return;
    }

    serverBootstrap.bind(tcpPort).addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture f) throws Exception {
            if (f.isSuccess()) {
                System.out.println("Started Tcp Server: " + tcpPort);
            } else {
                System.out.println("Started Tcp Server Failed: " + tcpPort);

                // f.channel().eventLoop().schedule(() -> doBind(), 3, TimeUnit.SECONDS);

                f.channel().eventLoop().schedule(new Runnable() {
                    @Override
                    public void run() {
                        doBind();
                    }
                }, 3, TimeUnit.SECONDS);
            }
        }
    });
}

From source file:com.tc.websocket.server.handler.ProxyBackendHandler.java

License:Apache License

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
    ByteBuf buf = (ByteBuf) msg;//from   w ww. j a va 2 s  . c  o  m
    String data = new String(ByteBufUtil.getBytes(buf));
    ByteBuf bufData = buf;

    if (Config.getInstance().isEncrypted() && data.contains(StringCache.HTTP)) {
        data = data.replace(StringCache.HTTP, StringCache.HTTPS);
        bufData = Unpooled.wrappedBuffer(data.getBytes());
    }

    //ProxyFrontendHandler.writeToFile("backend", ByteBufUtil.getBytes(bufData));

    inboundChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) {
            if (future.isSuccess()) {
                ctx.channel().read();

            } else {
                future.channel().close();
            }
        }
    });

}

From source file:com.tc.websocket.server.handler.ProxyFrontendHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) {
    final Channel inboundChannel = ctx.channel();

    this.handler = new ProxyBackendHandler(inboundChannel);

    // Start the connection attempt.
    Bootstrap b = new Bootstrap();
    b.group(inboundChannel.eventLoop()).channel(ctx.channel().getClass()).handler(this.handler)
            .option(ChannelOption.AUTO_READ, false);

    ChannelFuture f = b.connect(remoteHost, remotePort);
    outboundChannel = f.channel();/*from w ww.ja v a 2  s.c o  m*/

    f.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) {
            if (future.isSuccess()) {
                // connection complete start to read first data
                inboundChannel.read();

            } else {
                // Close the connection if the connection attempt has failed.
                inboundChannel.close();
            }
        }
    });

}