Example usage for io.netty.bootstrap ServerBootstrap group

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

Introduction

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

Prototype

public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup) 

Source Link

Document

Set the EventLoopGroup for the parent (acceptor) and the child (client).

Usage

From source file:cc.changic.platform.etl.schedule.http.HttpServer.java

License:Apache License

public void bind() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*www .  ja  v  a  2 s.  c  om*/
        ServerBootstrap b = new ServerBootstrap();
        b.option(ChannelOption.SO_BACKLOG, 1024);
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new HttpServerInitializer());

        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:cc.ly.mc.core.server.io.SocketServer.java

License:Apache License

public void run() {
    // Configure the server.
    bossGroup = new NioEventLoopGroup(1);
    workerGroup = new NioEventLoopGroup();
    try {// w  ww .j a v a2s . c om
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).option(ChannelOption.SO_REUSEADDR, true)
                .option(ChannelOption.TCP_NODELAY, true)
                .childHandler(new SocketServerInitializer(connectedListeners, disconnectedListeners));
        // Start the server.
        // Wait until the server socket is closed.
        try {
            b.bind(port).sync().channel().closeFuture().sync();
        } catch (InterruptedException e) {
        }
    } finally {
        // Shut down all event loops to terminate all threads.
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:cc.ly.mc.server.netty.SocketServer.java

License:Apache License

public void run() {
    // Configure the server.
    bossGroup = new NioEventLoopGroup(1);
    workerGroup = new NioEventLoopGroup();
    try {/*from w ww .ja  va2 s.  c  o m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).option(ChannelOption.SO_REUSEADDR, true)
                .option(ChannelOption.TCP_NODELAY, true).childHandler(new SocketServerInitializer());
        // Start the server.
        // Wait until the server socket is closed.
        try {
            b.bind(port).sync().channel().closeFuture().sync();
        } catch (InterruptedException e) {
        }
    } finally {
        // Shut down all event loops to terminate all threads.
        close();
    }
}

From source file:cc.sharper.netty.TimeServer.java

License:Apache License

public void bind(int port) throws Exception {
    // ??NIO SocketChannel
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from  ww w  .  jav a 2s. c  o  m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChildChannelHandler());//io
        // ???
        ChannelFuture f = b.bind(port).sync();

        // ???
        f.channel().closeFuture().sync();
    } finally {
        // ?
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:ch.ethz.globis.distindex.middleware.net.IndexMiddleware.java

License:Open Source License

private ServerBootstrap initServerBootstrap(final IOHandler<K, V> handler) {
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {
                @Override/*w  w  w. j a v  a  2 s.  c  o m*/
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new MiddlewareMessageDecoder(),
                            new MiddlewareChannelHandler<K, V>(handler) {
                            });
                }
            });
    return b;
}

From source file:chapter10.WebSocketServer.java

License:Apache License

public void run(int port) throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//from  w  w w .  j a v a  2  s  .  c o m
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast("http-codec", new HttpServerCodec());
                        pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
                        ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
                        pipeline.addLast("handler", new WebSocketServerHandler());
                    }
                });

        Channel ch = b.bind(port).sync().channel();
        System.out.println("Web socket server started at port " + port + '.');
        System.out.println("Open your browser and navigate to http://localhost:" + port + '/');

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

From source file:cloudfoundry.norouter.f5.dropsonde.LineEventToMetronServer.java

License:Open Source License

@Autowired
public LineEventToMetronServer(@Qualifier("boss") EventLoopGroup boss,
        @Qualifier("worker") EventLoopGroup worker, RouteRegistrar routeRegistrar, MetronClient metronClient,
        F5Properties properties) {
    final ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(boss, worker).channel(NioServerSocketChannel.class)
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override//www .j  a v a2s  .  c o  m
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new LineBasedFrameDecoder(64 * 1024));
                    ch.pipeline().addLast(new LineEventDecoder());
                    ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
                        @Override
                        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
                                throws Exception {
                            LOGGER.warn("An error occurred processing logging events from the LTM.", cause);
                            ctx.close();
                        }

                        @Override
                        public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
                            LOGGER.info("New connection from {}", ctx.channel().remoteAddress());
                        }

                        @Override
                        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                            if (msg instanceof LogEvent) {
                                final LogEvent logEvent = (LogEvent) msg;
                                final RouteDetails routeDetails = routeRegistrar
                                        .getRouteByAddress(logEvent.getApplicationAddress());
                                if (routeDetails != null && routeDetails.getApplicationGuid() != null) {
                                    final String appGuid = routeDetails.getApplicationGuid().toString();
                                    final String message = logEvent.getMessage() + " app_id:" + appGuid;
                                    metronClient.createLogEmitter("RTR", logEvent.getLtmIdentifier())
                                            .emit(logEvent.getTimestamp(), appGuid, message);
                                }
                            } else {
                                super.channelRead(ctx, msg);
                            }
                        }
                    });
                }
            });
    bootstrap.bind(properties.getLoggingPort()).syncUninterruptibly();
    LOGGER.info("Listening for logging events from the LTM on port {}", properties.getLoggingPort());
}

From source file:club.jmint.crossing.server.CrossingServer.java

License:Apache License

public void start() {
    //load configuration
    //ConfigWizard cw = (ConfigWizard)WizardManager.getWizard("ConfigWizard");
    ServerConfig config = (ServerConfig) ConfigWizard.getConfig(Constants.CONFIG_SERVER);
    this.address = config.getItem("server.bind_address");
    this.port = Integer.parseInt(config.getItem("server.port"));
    this.backlog = Integer.parseInt(config.getItem("server.backlog"));
    this.ssl = Boolean.parseBoolean(config.getItem("server.ssl"));

    //Configure SSL
    if (ssl) {//from  w  ww.  j  a v a2 s . c o  m
        try {
            SelfSignedCertificate ssc = new SelfSignedCertificate();
            sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
        } catch (Exception e) {
            CrossLog.printStackTrace(e);
        }
    } else {
        sslCtx = null;
    }

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

        // Start the server.
        ChannelFuture f = b.bind(port).sync();
        CrossLog.logger.info("Crossing Server started......");

        // Wait until the server socket is closed.
        f.channel().closeFuture().sync();

    } catch (InterruptedException e) {
        CrossLog.printStackTrace(e);
    } finally {
        // Shut down all event loops to terminate all threads.
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
        CrossLog.logger.info("Crossing Server shutdown......");
    }
}

From source file:cn.david.main.EchoServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {/*from   w  ww . jav  a2  s  . c o 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.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        if (sslCtx != null) {
                            p.addLast(sslCtx.newHandler(ch.alloc()));
                        }
                        //p.addLast(new LoggingHandler(LogLevel.INFO));
                        p.addLast(new EchoServerHandler());
                    }
                });

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

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

From source file:cn.david.main.PersonServer.java

License:Apache License

public static void main(String[] args) throws Exception {

    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//from   w  w  w  .  j  a  v  a2  s  . co  m
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ServerInitializer());

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

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