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.gxkj.demo.netty.discard.DiscardServer.java

License:Apache License

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

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

        // 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.gxkj.demo.netty.echo.EchoServer.java

License:Apache License

public void run() throws Exception {
    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from w ww. j a v  a 2  s .com*/
        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 {
                        ch.pipeline().addLast(
                                //new LoggingHandler(LogLevel.INFO),
                                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:com.gxkj.demo.netty.proxy.HexDumpProxy.java

License:Apache License

public void run() throws Exception {
    System.err.println("Proxying *:" + localPort + " to " + remoteHost + ':' + remotePort + " ...");

    // Configure the bootstrap.
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//from  w  w  w . j  ava 2  s  .c  o  m
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new HexDumpProxyInitializer(remoteHost, remotePort))
                .childOption(ChannelOption.AUTO_READ, false).bind(localPort).sync().channel().closeFuture()
                .sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.gxkj.demo.netty.socksproxy.SocksServer.java

License:Apache License

public void run() throws Exception {
    System.err.println("Listening on*:" + localPort + "...");
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from   w w  w  .  java 2  s  .  c  o  m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new SocksServerInitializer());
        b.bind(localPort).sync().channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.gxkj.demo.netty.telnet.TelnetServer.java

License:Apache License

public void run() 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 TelnetServerInitializer());

        b.bind(port).sync().channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }

}

From source file:com.hazelcast.openshift.TunnelClient.java

License:Open Source License

public ServerBootstrap createBootstrap(int localPort) throws Exception {
    System.out.println(//www .  j a v a  2 s.com
            "Creating clientside plain-socket: (" + localPort + ") => (" + httpHost + ":" + httpPort + ")");
    return new ServerBootstrap().option(ChannelOption.SO_BACKLOG, 20).group(getBossGroup(), getWorkerGroup())
            .channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {

                @Override
                protected void initChannel(SocketChannel channel) throws Exception {
                    ChannelPipeline pipeline = channel.pipeline();
                    pipeline.addLast(new TunnelServerConnector(getWorkerGroup(), httpHost, httpPort, httpSsl));
                }
            });

}

From source file:com.hazelcast.openshift.TunnelServer.java

License:Open Source License

@Override
protected ServerBootstrap createBootstrap(int localPort) throws Exception {
    SslContext sslContext;/*ww  w. ja  va  2s.c o  m*/
    if (!ssl) {
        sslContext = null;

    } else {
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslContext = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    }

    System.out.println("Creating serverside http-socket: (" + localPort + ") => (" + forwardHost + ":"
            + forwardPort + ")");
    return new ServerBootstrap().option(ChannelOption.SO_BACKLOG, 20).group(getBossGroup(), getWorkerGroup())
            .channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {

                @Override
                protected void initChannel(SocketChannel channel) throws Exception {
                    ChannelPipeline pipeline = channel.pipeline();
                    if (sslContext != null) {
                        pipeline.addLast("ssl", sslContext.newHandler(channel.alloc()));
                    }
                    pipeline.addLast("http-codec", new HttpServerCodec());
                    pipeline.addLast(new TunnelClientAcceptor(getWorkerGroup(), forwardHost, forwardPort));
                }
            });

}

From source file:com.hazelcast.simulator.protocol.connector.AbstractServerConnector.java

License:Open Source License

private ServerBootstrap getServerBootstrap() {
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .localAddress(new InetSocketAddress(port)).childHandler(new ChannelInitializer<SocketChannel>() {
                @Override//from ww  w . j av  a2s .  c  o m
                public void initChannel(SocketChannel channel) {
                    configureServerPipeline(channel.pipeline(), AbstractServerConnector.this);
                }
            });
    return bootstrap;
}

From source file:com.heliosapm.tsdblite.Server.java

License:Apache License

/**
 * Creates a new Server/*www . ja v a 2 s  .com*/
 */
private Server() {
    log.info("Configuring Netty Server....");

    String serverLevel = ConfigurationHelper.getSystemThenEnvProperty(Constants.CONF_NETTY_SERVER_LOGLEVEL,
            Constants.DEFAULT_NETTY_SERVER_LOGLEVEL);
    loggingHandler = new LoggingHandler(getClass(), LogLevel.valueOf(serverLevel.trim().toUpperCase()));
    iface = ConfigurationHelper.getSystemThenEnvProperty(Constants.CONF_NETTY_IFACE,
            Constants.DEFAULT_NETTY_IFACE);
    port = ConfigurationHelper.getIntSystemThenEnvProperty(Constants.CONF_NETTY_PORT,
            Constants.DEFAULT_NETTY_PORT);
    int bossThreads = ConfigurationHelper.getIntSystemThenEnvProperty(Constants.CONF_NETTY_BOSS_THREADS,
            Constants.DEFAULT_NETTY_BOSS_THREADS);
    int workerThreads = ConfigurationHelper.getIntSystemThenEnvProperty(Constants.CONF_NETTY_WORKER_THREADS,
            Constants.DEFAULT_NETTY_WORKER_THREADS);
    int groupThreads = ConfigurationHelper.getIntSystemThenEnvProperty(Constants.CONF_NETTY_CGROUP_THREADS,
            Constants.DEFAULT_NETTY_CGROUP_THREADS);
    bossPool = new ManagedDefaultExecutorServiceFactory("bossPool").newExecutorService(bossThreads);
    //      ForkJoinPoolManager.register(bossPool, BOSS_POOL_ON);
    workerPool = new ManagedDefaultExecutorServiceFactory("workerPool").newExecutorService(workerThreads);
    //      ForkJoinPoolManager.register(workerPool, WORKER_POOL_ON);
    channelGroupPool = new ManagedDefaultExecutorServiceFactory("groupPool").newExecutorService(groupThreads);
    //      ForkJoinPoolManager.register(channelGroupPool, CGROUP_POOL_ON);
    bossGroup = new NioEventLoopGroup(bossThreads, bossPool, selectorProvider);
    workerGroup = new NioEventLoopGroup(bossThreads, workerPool, selectorProvider);
    bootStrap = new ServerBootstrap();
    groupExecutor = new DefaultEventExecutor(channelGroupPool);
    channelGroup = new DefaultChannelGroup("TSDBLite", groupExecutor);
    MetricCache.getInstance(); // fire up the metric cache before we start taking calls   
    log.info("Selector: {}", selectorProvider.getClass().getName());
    bootStrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).handler(loggingHandler)
            .childHandler(this);
    try {
        serverChannel = (NioServerSocketChannel) bootStrap.bind(iface, port).sync().channel();
    } catch (Exception ex) {
        stop();
        log.error("Failed to bind Netty server on [{}:{}]", iface, port, ex);
        throw new RuntimeException("Failed to bind Netty server", ex);
    }
    JMXHelper.registerMBean(this, OBJECT_NAME);
    log.info(
            "\n\t======================================\n\tNetty Server started on [{}:{}]\n\t======================================",
            iface, port);
}

From source file:com.heliosapm.webrpc.server.RPCServer.java

License:Apache License

private RPCServer(final Properties properties) {
    log.info(">>>>> RPCServer starting....");
    final int port = ConfigurationHelper.getIntSystemThenEnvProperty(CONF_PORT, DEFAULT_PORT, properties);
    final int workerThreads = ConfigurationHelper.getIntSystemThenEnvProperty(CONF_WORKERS, DEFAULT_WORKERS,
            properties);/*from w  w w  .j  a  va 2 s  .  c  o m*/
    final String iface = ConfigurationHelper.getSystemThenEnvProperty(CONF_BIND, DEFAULT_BIND, properties);
    socketAddress = new InetSocketAddress(iface, port);
    bossGroup = new NioEventLoopGroup(1);
    workerExecutor = new JMXManagedThreadPool(workerExecutorObjectName, "WorkerPool", workerThreads,
            workerThreads * 2, 1, 60000, 100, 99, true);
    workerGroup = new NioEventLoopGroup(workerThreads, (Executor) workerExecutor);
    bootstrap = new ServerBootstrap();
    bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .handler(new LoggingHandler(LogLevel.INFO)).childHandler(this);
    serverChannel = bootstrap.bind(socketAddress).syncUninterruptibly().channel();

    log.info("<<<<< Started RPCServer on [{}]", socketAddress);
}