Example usage for io.netty.bootstrap ServerBootstrap ServerBootstrap

List of usage examples for io.netty.bootstrap ServerBootstrap ServerBootstrap

Introduction

In this page you can find the example usage for io.netty.bootstrap ServerBootstrap ServerBootstrap.

Prototype

public ServerBootstrap() 

Source Link

Usage

From source file:com.example.http.hello.HttpHelloServer.java

License:Apache License

private void run() throws InterruptedException {
    final NioEventLoopGroup bossGroup = new NioEventLoopGroup();
    final NioEventLoopGroup workerGroup = new NioEventLoopGroup();
    try (final GracefulShutdown ignored = GracefulShutdown.gracefulShutdown(bossGroup::shutdownGracefully,
            workerGroup::shutdownGracefully)) {
        final ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.option(ChannelOption.SO_BACKLOG, 1024);
        serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(ServerChannelInitializationConfigurer.channelInitializer());

        final Channel channel = serverBootstrap.bind(port).sync().channel();

        log.info("server started: http://localhost:{}/", port);

        channel.closeFuture().sync();/*from  w ww  .j  ava 2s  . c o  m*/
    }
}

From source file:com.example.server.ServerMain.java

License:Apache License

public void run() throws InterruptedException {
    final NioEventLoopGroup parentGroup = new NioEventLoopGroup();
    final NioEventLoopGroup workerGroup = new NioEventLoopGroup();

    try (final GracefulShutdown ignored = GracefulShutdown.gracefulShutdown(parentGroup::shutdownGracefully,
            workerGroup::shutdownGracefully)) {
        final ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(parentGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(ServerChannelInitializationConfigurer.channelInitializer())
                .option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
        log.info("server start");
        final ChannelFuture channelFuture = bootstrap.bind(port).sync();
        channelFuture.channel().closeFuture().sync();
    }/*w w  w  . jav  a  2  s.com*/
}

From source file:com.example.spring.boot.netty.TcpServer.java

License:Apache License

public void bind() {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//from  www.  j ava 2s  .c om
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 65535).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        //p.addLast(new LoggingHandler(LogLevel.INFO));
                        p.addLast(new LineBasedFrameDecoder(1024));
                        p.addLast(new StringDecoder());
                        p.addLast(TCP_SERVER_HANDLER);
                    }
                });

        // Start the server.
        ChannelFuture f = b.bind(port).sync();

        // Wait until the server socket is closed.
        f.channel().closeFuture().sync();
    } catch (Throwable e) {
        e.printStackTrace();
    } finally {
        // Shut down all event loops to terminate all threads.
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.fanavard.challenge.server.websocket.server.WebSocketServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {//from ww w  . ja  va 2 s. co m
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
    } else {
        sslCtx = null;
    }

    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new WebSocketServerInitializer(sslCtx));

        Channel ch = b.bind(PORT).sync().channel();

        System.out.println("Open your web browser and navigate to " + (SSL ? "https" : "http") + "://127.0.0.1:"
                + PORT + '/');

        ch.closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.farsunset.cim.sdk.server.handler.CIMNioSocketAcceptor.java

License:Apache License

public void bind() throws IOException {

    /**/*from   w w  w. ja  v a 2s . c o m*/
     * websocket??
     */
    innerHandlerMap.put(WEBSOCKET_HANDLER_KEY, new WebsocketHandler());

    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup());
    bootstrap.childOption(ChannelOption.TCP_NODELAY, true);
    bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
    bootstrap.channel(NioServerSocketChannel.class);
    bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch) throws Exception {

            ch.pipeline().addLast(new ServerMessageDecoder());
            ch.pipeline().addLast(new ServerMessageEncoder());
            ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));
            ch.pipeline().addLast(new IdleStateHandler(READ_IDLE_TIME, WRITE_IDLE_TIME, 0));
            ch.pipeline().addLast(CIMNioSocketAcceptor.this);
        }
    });

    bootstrap.bind(port);
}

From source file:com.fizzed.rocker.bin.NettyMain.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {// w  ww  . j  ava 2 s . co m
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
    } else {
        sslCtx = null;
    }

    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.option(ChannelOption.SO_BACKLOG, 1024);
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new HttpHelloWorldServerInitializer(sslCtx));

        Channel ch = b.bind(PORT).sync().channel();

        System.err.println("Open your web browser and navigate to " + (SSL ? "https" : "http") + "://127.0.0.1:"
                + PORT + '/');

        ch.closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.fizzed.stork.demo.hellod.Hellod.java

License:Apache License

static public void main(String[] args) throws Exception {
    // was a port specified?
    port = 8888;//  w w w  . ja v  a 2 s.  co  m
    if (args.length > 0) {
        String portString = args[0];
        port = Integer.parseInt(portString);
    }

    startedAt = new Date();
    lines = createLines(args);

    // start server
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.option(ChannelOption.SO_BACKLOG, 1024);
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                //.handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new HttpHelloWorldServerInitializer());

        Channel ch = b.bind(port).sync().channel();

        // what interfaces
        System.out.println("In your browser visit (all possible options):");
        Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface netint : Collections.list(nets)) {
            Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
            for (InetAddress inetAddress : Collections.list(inetAddresses)) {
                System.out.println("  http://" + inetAddress.getHostAddress() + ":" + port);
            }
        }

        // on shutdown...
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                System.out.println("Shutting down.");
            }
        });

        ch.closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.fizzed.stork.sample.HelloDaemon.java

License:Apache License

static public void main(String[] args) throws Exception {
    // call same as hello world
    HelloConsole.main(args);//from  w  w w  . ja v a 2 s  .  c  o  m

    // was a port specified?
    port = 8888;
    if (args.length > 0) {
        String portString = args[0];
        port = Integer.parseInt(portString);
    }

    startedAt = new Date();
    lines = createLines(args);

    // start server
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.option(ChannelOption.SO_BACKLOG, 1024);
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                //.handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new HttpHelloWorldServerInitializer());

        Channel ch = b.bind(port).sync().channel();

        // what interfaces
        System.out.println("In your browser visit (all possible options):");
        Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface netint : Collections.list(nets)) {
            Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
            for (InetAddress inetAddress : Collections.list(inetAddresses)) {
                System.out.println("  http://" + inetAddress.getHostAddress() + ":" + port);
            }
        }

        // on shutdown...
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                System.out.println("Shutting down.");
            }
        });

        ch.closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.fjn.helper.frameworkex.netty.v4.discardserver.DiscardServer.java

License:Apache License

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//w  w w.j a  v a 2  s .  c  o  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 DiscardServerHandler());
                    }
                }).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)

        // 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();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}

From source file:com.fjn.helper.frameworkex.netty.v4.echotest.EchoServer.java

License:Apache License

public void start() throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {//from  w  w  w.j  a v  a 2 s.  c o m
        // Bootstraps the server
        ServerBootstrap b = new ServerBootstrap();

        // Specifies NIO transport, local socket address
        b.group(group);
        b.childGroup();
        b.channel(NioServerSocketChannel.class);
        b.localAddress(new InetSocketAddress(port));

        // Adds handler to channel pipeline
        b.childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(new EchoServerHandler());
            }
        });

        // Binds server, waits for server to close, and releases resources
        ChannelFuture f = b.bind().sync();
        System.out.println(EchoServer.class.getName() + " started and listen on " + f.channel().localAddress());
        f.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully().sync();
    }

}