Example usage for io.netty.channel ChannelOption SO_KEEPALIVE

List of usage examples for io.netty.channel ChannelOption SO_KEEPALIVE

Introduction

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

Prototype

ChannelOption SO_KEEPALIVE

To view the source code for io.netty.channel ChannelOption SO_KEEPALIVE.

Click Source Link

Usage

From source file:com.ibm.crail.datanode.netty.client.NettyEndpointGroup.java

License:Apache License

public NettyEndpointGroup() {
    workerGroup = new NioEventLoopGroup();
    boot = new Bootstrap();
    boot.group(workerGroup);//from ww w.  ja  va 2  s .  co  m
    boot.channel(NioSocketChannel.class);
    boot.option(ChannelOption.SO_KEEPALIVE, true);
    final NettyEndpointGroup thisGroup = this;
    boot.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            /* outgoing pipeline */
            //ch.pipeline().addLast(new RdmaEncoderTx());
            /* incoming pipeline */
            ch.pipeline().addLast(new RdmaDecoderRx(), new IncomingResponseHandler(thisGroup));
        }
    });
    activeClients = new ArrayList<NettyEndpoint>();
    slot = new AtomicLong(0);
    inFlightOps = new ConcurrentHashMap<Long, NettyIOResult>();
}

From source file:com.ibm.crail.datanode.netty.server.NettyServer.java

License:Apache License

public void run() {
    /* start the netty server */
    EventLoopGroup acceptGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/* w  w w .  j  a va 2 s .  co  m*/
        ServerBootstrap boot = new ServerBootstrap();
        boot.group(acceptGroup, workerGroup);
        /* we use sockets */
        boot.channel(NioServerSocketChannel.class);
        /* for new incoming connection */
        boot.childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                LOG.info("TID: " + Thread.currentThread().getId()
                        + " , a new client connection has arrived from : " + ch.remoteAddress().toString());
                /* incoming pipeline */
                ch.pipeline().addLast(new RdmaDecoderRx(), /* this makes full RDMA messages */
                        new IncomingRequestHandler(ch, dataNode));
                /* outgoing pipeline */
                //ch.pipeline().addLast(new RdmaEncoderTx());
            }
        });
        /* general optimization settings */
        boot.option(ChannelOption.SO_BACKLOG, 1024);
        boot.childOption(ChannelOption.SO_KEEPALIVE, true);

        /* now we bind the server and start */
        ChannelFuture f = boot.bind(this.inetSocketAddress.getAddress(), this.inetSocketAddress.getPort())
                .sync();
        LOG.info("Datanode binded to : " + this.inetSocketAddress);
        /* at this point we are binded and ready */
        f.channel().closeFuture().sync();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        workerGroup.shutdownGracefully();
        acceptGroup.shutdownGracefully();
        LOG.info("Datanode at " + this.inetSocketAddress + " is shutdown");
    }
}

From source file:com.ibm.crail.namenode.rpc.netty.client.NettyRPCNamenodeClientGroup.java

License:Apache License

public NettyRPCNamenodeClientGroup() {
    workerGroup = new NioEventLoopGroup();
    boot = new Bootstrap();
    boot.group(workerGroup);/*  www  . ja  v  a  2  s  . c  o  m*/
    boot.channel(NioSocketChannel.class);
    boot.option(ChannelOption.SO_KEEPALIVE, true);
    final NettyRPCNamenodeClientGroup thisGroup = this;
    boot.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            /* outgoing pipeline */
            ch.pipeline().addLast(new RequestEncoder());
            /* incoming pipeline */
            ch.pipeline().addLast(new ResponseDecoder(thisGroup));
        }
    });

    slot = new AtomicLong(1);
    inFlightOps = new ConcurrentHashMap<Long, NettyResponse>();
    activeClients = new ArrayList<NettyRPCNamenodeClient>();
}

From source file:com.ibm.crail.namenode.rpc.netty.NettyNameNode.java

License:Apache License

public void run(final RpcNameNodeService service) {
    /* here we run the incoming RPC service */
    InetSocketAddress inetSocketAddress = CrailUtils.getNameNodeAddress();
    LOG.info("Starting the NettyNamenode service at : " + inetSocketAddress);
    /* start the netty server */
    EventLoopGroup acceptGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//from ww  w .  ja v a 2 s.  co  m
        ServerBootstrap boot = new ServerBootstrap();
        boot.group(acceptGroup, workerGroup);
        /* we use sockets */
        boot.channel(NioServerSocketChannel.class);
        /* for new incoming connection */
        boot.childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                LOG.info("A new connection has arrived from : " + ch.remoteAddress().toString());
                /* incoming pipeline */
                ch.pipeline().addLast("RequestDecoder", new RequestDecoder());
                ch.pipeline().addLast("NNProcessor", new NamenodeProcessor(service));
                /* outgoing pipeline */
                ch.pipeline().addLast("ResponseEncoder", new ResponseEncoder());
            }
        });
        /* general optimization settings */
        boot.option(ChannelOption.SO_BACKLOG, 1024);
        boot.childOption(ChannelOption.SO_KEEPALIVE, true);

        /* now we bind the server and start */
        ChannelFuture f = boot.bind(inetSocketAddress.getAddress(), inetSocketAddress.getPort()).sync();
        /* at this point we are binded and ready */
        f.channel().closeFuture().sync();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        workerGroup.shutdownGracefully();
        acceptGroup.shutdownGracefully();
        LOG.info("Netty namenode at " + inetSocketAddress + " is shutdown");
    }
}

From source file:com.ibm.mqlight.api.impl.network.NettyNetworkService.java

License:Apache License

/**
 * Request a {@link Bootstrap} for obtaining a {@link Channel} and track
 * that the workerGroup is being used.//from   ww  w.j  av a  2  s.com
 *
 * @param secure
 *            a {@code boolean} indicating whether or not a secure channel
 *            will be required
 * @param sslEngine
 *            an {@link SSLEngine} if one should be used to secure the channel
 * @param handler a {@link ChannelHandler} to use for serving the requests.
 * @return a netty {@link Bootstrap} object suitable for obtaining a
 *         {@link Channel} from
 */
private static synchronized Bootstrap getBootstrap(final boolean secure, final SSLEngine sslEngine,
        final ChannelHandler handler) {
    final String methodName = "getBootstrap";
    logger.entry(methodName, secure, sslEngine);

    ++useCount;
    if (useCount == 1) {
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        bootstrap = new Bootstrap();
        bootstrap.group(workerGroup);
        bootstrap.channel(NioSocketChannel.class);
        bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
        bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30000);
        bootstrap.handler(handler);
    }

    final Bootstrap result;
    if (secure) {
        result = bootstrap.clone();
        result.handler(handler);
    } else {
        result = bootstrap;
    }

    logger.exit(methodName, result);

    return result;
}

From source file:com.imaginarycode.minecraft.bungeejson.impl.httpserver.NettyBootstrap.java

License:Open Source License

public void initialize() {
    group = new NioEventLoopGroup(5, factory);
    int port = 7432; // CONFIG
    ServerBootstrap b = new ServerBootstrap();
    b.group(group).channel(NioServerSocketChannel.class)
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override//  w  w w.  j  ava2 s . co  m
                public void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();
                    pipeline.addLast("messageCodec", new HttpServerCodec());
                    pipeline.addLast("messageHandler", new HttpServerHandler());
                }
            }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
    channelFuture = b.bind(port).addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture channelFuture) throws Exception {
            BungeeJSONPlugin.getPlugin().getLogger()
                    .info("BungeeJSON server started on " + channelFuture.channel().localAddress());
        }
    });
}

From source file:com.jansegre.jwar.webapi.ApiServer.java

License:Open Source License

@Override
public void start() {
    // Reference: http://netty.io/wiki/user-guide-for-4.x.html
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//from w  w  w . j  a  va 2 s .  c om
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();

                        // Add the text line codec combination first,
                        pipeline.addLast("framer",
                                new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
                        // the encoder and decoder are static as these are sharable
                        pipeline.addLast("decoder", new StringDecoder());
                        pipeline.addLast("encoder", new StringEncoder());

                        // and then business logic.
                        pipeline.addLast("handler", apiSocket);

                    }
                }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);

        // Bind and start to accept incoming connections.
        ChannelFuture f = b.bind(apiPort).syncUninterruptibly();
        log.info("ApiSocket started at port: {}", apiPort);

        // Also start parent
        super.start();

        // Wait until the server socket is closed.
        f.channel().closeFuture().syncUninterruptibly();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}

From source file:com.kingmed.dp.lisclient.demo.DiscardServer.java

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//from w  ww . j  a v a2  s .  com
        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(new DiscardServerHandler());
                    }
                }).option(ChannelOption.SO_BACKLOG, 128).option(ChannelOption.SO_KEEPALIVE, true);
        ChannelFuture f = b.bind(port).sync();
        f.channel().closeFuture().sync();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}

From source file:com.kixeye.kixmpp.client.KixmppClient.java

License:Apache License

/**
 * Creates a new {@link KixmppClient}./*  w  ww  . jav a  2  s.  c o m*/
 * 
 * @param eventLoopGroup
 * @param eventEngine
 * @param sslContext
 * @param type
 */
public KixmppClient(EventLoopGroup eventLoopGroup, KixmppEventEngine eventEngine, SslContext sslContext,
        Type type) {
    if (sslContext != null) {
        assert sslContext.isClient() : "The given SslContext must be a client context.";
    }

    if (eventLoopGroup == null) {
        if (OS.indexOf("nux") >= 0) {
            eventLoopGroup = new EpollEventLoopGroup();
        } else {
            eventLoopGroup = new NioEventLoopGroup();
        }
    }

    this.type = type;

    this.sslContext = sslContext;
    this.eventEngine = eventEngine;

    // set modules to be registered
    this.modulesToRegister.add(MucKixmppClientModule.class.getName());
    this.modulesToRegister.add(PresenceKixmppClientModule.class.getName());
    this.modulesToRegister.add(MessageKixmppClientModule.class.getName());
    this.modulesToRegister.add(ErrorKixmppClientModule.class.getName());

    if (eventLoopGroup instanceof EpollEventLoopGroup) {
        this.bootstrap = new Bootstrap().group(eventLoopGroup).channel(EpollSocketChannel.class)
                .option(ChannelOption.TCP_NODELAY, false).option(ChannelOption.SO_KEEPALIVE, true);
    } else {
        this.bootstrap = new Bootstrap().group(eventLoopGroup).channel(NioSocketChannel.class)
                .option(ChannelOption.TCP_NODELAY, false).option(ChannelOption.SO_KEEPALIVE, true);
    }

    switch (type) {
    case TCP:
        bootstrap.handler(new KixmppClientChannelInitializer());
        break;
    case WEBSOCKET:
        bootstrap.handler(new KixmppClientWebSocketChannelInitializer());
        break;
    }
}

From source file:com.kixeye.kixmpp.p2p.node.NodeClient.java

License:Apache License

public void initialize(final String host, int port, final EventLoopGroup workerGroup,
        final MessageRegistry messageRegistry, final ChannelInboundHandlerAdapter channelListener)
        throws InterruptedException {
    // prepare connection
    Bootstrap boot = new Bootstrap();
    boot.group(workerGroup);// w  ww  . ja  va2s.c  o  m
    boot.channel(NioSocketChannel.class);
    boot.option(ChannelOption.SO_KEEPALIVE, true);
    boot.option(ChannelOption.TCP_NODELAY, true);
    boot.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();

            //p.addLast(new LoggingHandler());

            // encoders
            p.addLast(new LengthFieldPrepender(4));
            p.addLast(new ProtostuffEncoder(messageRegistry));

            // decoders
            p.addLast(new LengthFieldBasedFrameDecoder(0x100000, 0, 4, 0, 4));
            p.addLast(new ProtostuffDecoder(messageRegistry));
            p.addLast(channelListener);
        }
    });

    // connect
    channel = boot.connect(host, port).sync().channel();
}