Example usage for io.netty.channel ChannelFuture channel

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

Introduction

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

Prototype

Channel channel();

Source Link

Document

Returns a channel where the I/O operation associated with this future takes place.

Usage

From source file:cn.wantedonline.puppy.httpserver.component.ContextAttachment.java

License:Apache License

@Override
public void operationComplete(ChannelFuture future) throws Exception {
    running = false;//ww w  .  j a v a2  s. co  m
    if (closeAfterOperationComplete) {
        future.channel().close();
    }
    if (AssertUtil.isNotNull(request)) {
        request.clean();
    }
}

From source file:cn.yesway.demo.book.protocol.http.fileServer.HttpFileServer.java

License:Apache License

public void run(final int port, final String url) throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from w  ww . j a  v  a2 s. co  m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast("http-decoder", new HttpRequestDecoder());
                        ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536));
                        ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());
                        ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
                        ch.pipeline().addLast("fileServerHandler", new HttpFileServerHandler(url));
                    }
                });
        ChannelFuture future = b.bind("127.0.0.1", port).sync();
        System.out.println("HTTP??? : " + "http://127.0.0.1:" + port + url);
        future.channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:cn.yesway.demo.book.protocol.http.xml.client.HttpXmlClient.java

License:Apache License

public void connect(int port) throws Exception {
    // ?NIO//from w w w.java  2 s .  com
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast("http-decoder", new HttpResponseDecoder());
                        ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536));
                        // XML?
                        ch.pipeline().addLast("xml-decoder", new HttpXmlResponseDecoder(Order.class, true));
                        ch.pipeline().addLast("http-encoder", new HttpRequestEncoder());
                        ch.pipeline().addLast("xml-encoder", new HttpXmlRequestEncoder());
                        ch.pipeline().addLast("xmlClientHandler", new HttpXmlClientHandle());
                    }
                });

        // ??
        ChannelFuture f = b.connect(new InetSocketAddress(port)).sync();

        // 
        f.channel().closeFuture().sync();
    } finally {
        // NIO
        group.shutdownGracefully();
    }
}

From source file:cn.yesway.demo.book.protocol.http.xml.server.HttpXmlServer.java

License:Apache License

public void run(final int port) throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {// w ww.java 2s.co m
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast("http-decoder", new HttpRequestDecoder());
                        ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536));
                        ch.pipeline().addLast("xml-decoder", new HttpXmlRequestDecoder(Order.class, true));
                        ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());
                        ch.pipeline().addLast("xml-encoder", new HttpXmlResponseEncoder());
                        ch.pipeline().addLast("xmlServerHandler", new HttpXmlServerHandler());
                    }
                });
        ChannelFuture future = b.bind(new InetSocketAddress(port)).sync();
        System.out.println("HTTP??? : " + "http://localhost:" + port);
        future.channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:cn.zyf.ObjectStorageServer.java

License:Open Source License

private void run(String host, int port, int packageSize) {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    EventLoopGroup backendGroup = new NioEventLoopGroup();

    try {/*from ww  w.j  av  a 2s .  c o  m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast("http-decoder", new HttpRequestDecoder());
                        ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(packageSize));
                        ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
                        ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());
                        ch.pipeline().addLast("oss-decoder", new ParseRequestHandler());
                        ch.pipeline().addLast("oss-filter", new FilterRequestHandler());
                        ch.pipeline().addLast("oss-authen", new AuthenticationHandler());
                        ch.pipeline().addLast("oss-author", new AuthorizationHandler());
                        ch.pipeline().addLast(backendGroup, "oss-backend", new ObjectStorageHandler());
                    }
                });

        ChannelFuture future = b.bind(host, port).sync();
        LOG.info("start Http Server with hostname=" + host + ":" + port);
        future.channel().closeFuture().sync();
    } catch (InterruptedException e) {
        LOG.error("occur InterruptedException ", e);
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.addthis.hydra.query.aggregate.MeshSourceAggregator.java

License:Apache License

private void safelyRemoveSelfFromPipeline(ChannelFuture future) {
    try {/*from w w w  .  j  av a  2 s .  c  o m*/
        future.channel().pipeline().remove(this);
    } catch (Exception e) {
        log.warn("unexpected error while trying to remove mesh source aggregator from the pipeline on success",
                e);
    }
}

From source file:com.addthis.hydra.query.loadbalance.NextQueryTask.java

License:Apache License

@Override
public void run() {
    QueryRequest request;//from   www  . j  a va 2 s . c o m
    try {
        request = queryQueue.takeQuery();
    } catch (InterruptedException ignored) {
        log.info("Frame reader thread interrupted -- halting query processing");
        return;
    }
    try {
        final ChannelFuture queryFuture = HttpQueryCallHandler.handleQuery(request.querySource, request.kv,
                request.request, request.ctx, executor);
        queryFuture.addListener(this);
        queryFuture.channel().closeFuture().addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (queryFuture.cancel(false)) {
                    log.warn("cancelling query due to closed output channel");
                }
            }
        });
    } catch (Exception e) {
        log.warn("Exception caught before mesh query master added to pipeline", e);
        if (request.ctx.channel().isActive()) {
            HttpUtils.sendError(request.ctx, new HttpResponseStatus(500, e.getMessage()));
        }
    }
}

From source file:com.addthis.hydra.query.loadbalance.NextQueryTask.java

License:Apache License

private static void safelyHandleQueryFailure(ChannelFuture future) {
    try {// ww w  .j  a va2  s . c  o m
        ChannelPipeline pipeline = future.channel().pipeline();
        ChannelHandlerContext lastContext = cleanPipelineAndGetLastContext(pipeline);
        if (lastContext != null) {
            sendDetailedError(lastContext, future.cause());
        } else {
            logAndFormatErrorDetail(future.cause());
        }
    } catch (Throwable error) {
        log.warn("unexpected error while trying to report to user; closing channel", error);
        safelyTryChannelClose(future);
    }
}

From source file:com.addthis.hydra.query.loadbalance.NextQueryTask.java

License:Apache License

private static void safelyTryChannelClose(ChannelFuture future) {
    try {//from w  ww  . j  ava 2  s  . c  o m
        Channel channel = future.channel();
        channel.close();
    } catch (Throwable error) {
        log.error("unexpected error while trying to close channel; ignoring to keep frame reader alive", error);
    }
}

From source file:com.addthis.meshy.MeshyClient.java

License:Apache License

/**
 * client//from w w  w  .j  a  v  a 2s . c om
 */
public MeshyClient(InetSocketAddress address) throws IOException {
    super();
    /* block session creation until connection is fully established */
    try {
        clientInitGate.acquire();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    ChannelFuture clientConnect = connect(address);
    clientConnect.awaitUninterruptibly();
    if (!clientConnect.isSuccess()) {
        close();
        throw new IOException("connection fail to " + address);
    }
    clientChannelCloseFuture = clientConnect.channel().closeFuture();
    /* re-acquire after connection comes up, which releases the lock */
    try {
        clientInitGate.acquire();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    if (log.isDebugEnabled()) {
        log.debug("client [{}] connected to {}", getUUID(), address);
    }
}