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:org.midonet.util.netty.ClientFrontEnd.java

License:Apache License

@Override
protected void doStart() {
    log.info("Starting Netty client for {}:{}", host, port);
    try {//from  ww w  .j  a v  a 2 s .c om
        Bootstrap boot = new Bootstrap();
        boot.group(wrkr).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true)
                .handler(adapter);
        sock = boot.connect(host, port).sync();
        log.info("Netty client started");
        notifyStarted();
    } catch (InterruptedException e) {
        log.warn("Netty client start interrupted");
        Thread.currentThread().interrupt();
        notifyFailed(e);
    }
}

From source file:org.midonet.util.netty.ServerFrontEnd.java

License:Apache License

@Override
protected void doStart() {

    try {//from w  w w  . j a  v  a 2s .  com
        if (datagram) {
            log.info("Starting Netty UDP server on port {}", port);
            Bootstrap boot = new Bootstrap();
            boot.group(wrkr).channel(NioDatagramChannel.class).handler(adapter);
            if (rcvbufSize != null)
                boot.option(ChannelOption.SO_RCVBUF, rcvbufSize).option(ChannelOption.RCVBUF_ALLOCATOR,
                        new FixedRecvByteBufAllocator(rcvbufSize));
            sock = boot.bind(port).sync();
        } else {
            log.info("Starting Netty TCP server on port {}", port);
            ServerBootstrap boot = new ServerBootstrap();
            boot.group(boss, wrkr).channel(NioServerSocketChannel.class).childHandler(adapter)
                    .option(ChannelOption.SO_BACKLOG, 128).option(ChannelOption.SO_KEEPALIVE, true);
            sock = boot.bind(port).sync();
        }
        log.info("Netty server started");
        notifyStarted();
    } catch (InterruptedException e) {
        log.warn("Netty server start interrupted");
        Thread.currentThread().interrupt();
        notifyFailed(e);
    }
}

From source file:org.mobicents.media.server.ctrl.rtsp.stack.RtspClientStackImpl.java

License:Open Source License

public void start() throws IOException {

    Bootstrap b = new Bootstrap();
    b.group(workerGroup);/*  w  w  w .  ja v a  2 s  .  c o m*/
    b.channel(NioSocketChannel.class);
    b.option(ChannelOption.SO_KEEPALIVE, true);
    b.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            // httpResponse??HttpResponseDecoder?
            ch.pipeline().addLast(new RtspResponseDecoder());
            // ??httprequest?HttpRequestEncoder?
            ch.pipeline().addLast(new RtspRequestEncoder());
            // ch.pipeline().addLast(new HttpObjectAggregator(1024 * 64));
            ch.pipeline().addLast("handler", new RtspResponseHandler(RtspClientStackImpl.this));
        }
    });

    // Start the client.
    ChannelFuture f = b.connect(getHost(), getPort());
    try {
        f.sync();
        channel = f.channel();

        InetSocketAddress bindAddress = new InetSocketAddress(this.host, this.port);

        logger.info("Mobicents RTSP Client started and bound to " + bindAddress.toString());

    } catch (InterruptedException e) {
        throw new IOException(f.cause());
    }
}

From source file:org.mobicents.media.server.rtsp.stack.RtspClientStackImpl.java

License:Open Source License

public void connect() throws IOException {

    Bootstrap b = new Bootstrap();
    b.group(workerGroup);//from   www.  j  a v a  2  s. c  o  m
    b.channel(NioSocketChannel.class);
    b.option(ChannelOption.SO_KEEPALIVE, true);
    b.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            // ??httpResponse???HttpResponseDecoder?
            ch.pipeline().addLast(new RtspResponseDecoder());
            // ?ttprequest?HttpRequestEncoder?
            ch.pipeline().addLast(new RtspRequestEncoder());
            // ch.pipeline().addLast(new HttpObjectAggregator(1024 * 64));
            ch.pipeline().addLast("handler", new RtspResponseHandler(RtspClientStackImpl.this));
        }
    });

    // Start the client.
    ChannelFuture f = b.connect(getHost(), getPort());
    try {
        f.sync();
        channel = f.channel();

        InetSocketAddress bindAddress = new InetSocketAddress(this.host, this.port);

        logger.info("Mobicents RTSP Client started and bound to " + bindAddress.toString());

        RtspResponseHandler handler = (RtspResponseHandler) f.channel().pipeline().get("handler");
        handler.connect();
    } catch (InterruptedException e) {
        throw new IOException(f.cause());
    }
}

From source file:org.msgpack.rpc.impl.netty.NettyTcpClientTransport.java

License:Apache License

NettyTcpClientTransport(final TcpClientConfig config, final Session session, final NettyEventLoop loop) {
    // TODO check session.getAddress() instanceof IPAddress
    final RpcMessageHandler handler = new RpcMessageHandler(session);

    _bootstrap = new Bootstrap().group(new NioEventLoopGroup(/*2*/)).channel(NioSocketChannel.class)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Math.round((float) config.getConnectTimeout()))
            .option(ChannelOption.TCP_NODELAY,
                    !Boolean.FALSE.equals(config.getOption(ChannelOption.TCP_NODELAY.name())))
            .option(ChannelOption.SO_KEEPALIVE,
                    !Boolean.FALSE.equals(config.getOption(ChannelOption.SO_KEEPALIVE.name())))
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override/*from  ww w.  j a  va 2s .co  m*/
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new MessagePackDecoder(loop.getObjectMapper()),
                            new MessageHandler(handler), new MessagePackEncoder(loop.getObjectMapper()));
                }
            });

    _session = session;
    _writables = new ConcurrentLinkedQueue<>();
}

From source file:org.msgpack.rpc.impl.netty.NettyTcpServerTransport.java

License:Apache License

NettyTcpServerTransport(final TcpServerConfig config, final Server server, final NettyEventLoop loop)
        throws InterruptedException {

    if (server == null) {
        throw new IllegalArgumentException("Server must not be null");
    }//from  w w w  .  ja v  a 2  s  .  co  m

    final Address address = config.getListenAddress();
    final RpcMessageHandler handler = new RpcMessageHandler(server);

    handler.useThread(true);

    final EventLoopGroup bossGroup = new NioEventLoopGroup(/*1*/); // (1)
    final EventLoopGroup workerGroup = new NioEventLoopGroup(/*4*/);
    final ServerBootstrap b = new ServerBootstrap().group(bossGroup, workerGroup)
            .channel(NioServerSocketChannel.class) // (3)
            .childHandler(new ChannelInitializer<SocketChannel>() { // (4)
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new MessagePackDecoder(loop.getObjectMapper()),
                            new MessageHandler(handler), new MessagePackEncoder(loop.getObjectMapper()));
                }
            }).option(ChannelOption.SO_BACKLOG, 128) // (5)
            .childOption(ChannelOption.TCP_NODELAY,
                    !Boolean.FALSE.equals(config.getOption(ChannelOption.TCP_NODELAY.name())))
            .childOption(ChannelOption.SO_KEEPALIVE,
                    !Boolean.FALSE.equals(config.getOption(ChannelOption.SO_KEEPALIVE.name())));

    // Bind and start to accept incoming connections.
    channelFuture = b.bind(address.getSocketAddress()).sync(); // (7)
}

From source file:org.nmrfx.server.Server.java

@Override
public void run() {
    EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//from  w w w. j  av  a2 s  .  co  m
        ServerBootstrap b = new ServerBootstrap(); // (2)
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) // (3)
                .childHandler(new ChannelInitializer<SocketChannel>() { // (4)
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new ServerHandler(consumer));
                    }
                }).option(ChannelOption.SO_BACKLOG, 128) // (5)
                .childOption(ChannelOption.SO_KEEPALIVE, true); // (6)

        // Bind and start to accept incoming connections.
        ChannelFuture f = b.bind(port).sync(); // (7)

        System.out.println("NMRFx server started using port " + port);

        // Wait until the server socket is closed.
        // In this example, this does not happen, but you can do that to gracefully
        // shut down your server.
        f.channel().closeFuture().sync();
    } catch (InterruptedException ex) {
        Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}

From source file:org.onlab.netty.NettyMessaging.java

License:Apache License

private void startAcceptingConnections() throws InterruptedException {
    ServerBootstrap b = new ServerBootstrap();
    b.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024);
    b.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024);
    b.option(ChannelOption.SO_RCVBUF, 1048576);
    b.option(ChannelOption.TCP_NODELAY, true);
    b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    b.group(serverGroup, clientGroup);//from  w  w w. j  av  a 2s.c o m
    b.channel(serverChannelClass);
    if (enableNettyTLS) {
        b.childHandler(new SSLServerCommunicationChannelInitializer());
    } else {
        b.childHandler(new OnosCommunicationChannelInitializer());
    }
    b.option(ChannelOption.SO_BACKLOG, 128);
    b.childOption(ChannelOption.SO_KEEPALIVE, true);

    // Bind and start to accept incoming connections.
    b.bind(localEp.port()).sync().addListener(future -> {
        if (future.isSuccess()) {
            log.info("{} accepting incoming connections on port {}", localEp.host(), localEp.port());
        } else {
            log.warn("{} failed to bind to port {}", localEp.host(), localEp.port(), future.cause());
        }
    });
}

From source file:org.onlab.netty.NettyMessagingService.java

License:Apache License

private void startAcceptingConnections() throws InterruptedException {
    ServerBootstrap b = new ServerBootstrap();
    b.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024);
    b.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024);
    b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    b.group(serverGroup, clientGroup).channel(serverChannelClass)
            .childHandler(new OnosCommunicationChannelInitializer()).option(ChannelOption.SO_BACKLOG, 128)
            .childOption(ChannelOption.SO_KEEPALIVE, true);

    // Bind and start to accept incoming connections.
    b.bind(localEp.port()).sync();/*from   w w w.ja va  2  s .c o m*/
}

From source file:org.onosproject.artemis.impl.moas.MoasClientController.java

License:Apache License

/**
 * Bootstrap netty socket.//from   w  w  w .j  ava  2  s . co  m
 *
 * @return bootstrap
 */
private Bootstrap createBootstrap() {
    workerGroup = new NioEventLoopGroup();
    return new Bootstrap().group(workerGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE,
            true);
}