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.lxz.talk.websocketx.server.WebSocketServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {/*from w ww. j a  v a 2s.  c  o  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.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.mapple.http.HttpHelloWorldServer.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 ww  . j a va  2  s  .  c  o m*/
        ServerBootstrap b = new ServerBootstrap();
        b.option(ChannelOption.SO_BACKLOG, 32);
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new HttpHelloWorldServerInitializer());

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

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

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

From source file:com.mapr.franz.netty.FranzServer.java

License:Apache License

public void run() throws Exception {
    // Configure the server.
    ServerBootstrap b = new ServerBootstrap();
    try {//w  w w  .ja v a  2 s .co  m
        b.group(new NioEventLoopGroup(), new NioEventLoopGroup()).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).localAddress(new InetSocketAddress(port))
                .childOption(ChannelOption.TCP_NODELAY, true).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 FranzServerHandler());
                    }
                });

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

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

From source file:com.mastfrog.scamper.SctpServer.java

License:Open Source License

public ChannelFuture start(AtomicReference<ChannelFuture> connectFutureReceiver) throws InterruptedException {
    // Configure the server.
    ServerBootstrap b = new ServerBootstrap();
    config.init(b);//from w ww.ja  v  a2  s . c  o m
    b.handler(new LoggingHandler(LogLevel.INFO));

    logger.log(Level.FINE, "Start server on {0}", port);
    // Start the server.
    ChannelFuture f = b.bind(port);
    if (logger.isLoggable(Level.FINE)) {
        f.addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                logger.log(Level.FINE, "Listening for connections on {0}", port);
            }

        });
    }
    f.sync();
    logger.log(Level.FINER, "Thread proceeding", Thread.currentThread());
    // For tests and things that need to delay execution until a connection
    // has been opened
    if (connectFutureReceiver != null) {
        connectFutureReceiver.set(f);
    }
    synchronized (this) {
        return future = f.channel().closeFuture();
    }
}

From source file:com.mc.netty.server.NettyServer.java

License:Open Source License

private ServerBootstrap getDefaultServerBootstrap() {
    ServerBootstrap bootStrap = new ServerBootstrap();
    bootStrap.group(bossGroup, workerGroup).option(ChannelOption.SO_BACKLOG, 1000)
            // ???
            .option(ChannelOption.SO_SNDBUF, 32 * 1024).option(ChannelOption.SO_RCVBUF, 32 * 1024)
            .option(ChannelOption.TCP_NODELAY, true)
            // ???
            .option(ChannelOption.RCVBUF_ALLOCATOR, AdaptiveRecvByteBufAllocator.DEFAULT)
            .channel(NioServerSocketChannel.class).childOption(ChannelOption.SO_KEEPALIVE, true);

    return bootStrap;
}

From source file:com.metasoft.empire.net.websocket.WebSocketServer.java

License:Apache License

public static void start() throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {/*from   www. j  av a  2  s  .  c o m*/
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    } 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.miko.s4netty.config.WorkerNettyConfig.java

License:Open Source License

@SuppressWarnings("unchecked")
@Bean(name = "serverBootstrap")
public ServerBootstrap bootstrap() {
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup(), workerGroup()).channel(NioServerSocketChannel.class).childHandler(workerInitializer);
    Map<ChannelOption<?>, Object> tcpChannelOptions = tcpChannelOptions();
    Set<ChannelOption<?>> keySet = tcpChannelOptions.keySet();
    for (@SuppressWarnings("rawtypes")
    ChannelOption option : keySet) {/*  w  w  w . ja  v  a2  s. c o m*/
        b.option(option, tcpChannelOptions.get(option));
    }
    return b;
}

From source file:com.mnxfst.stream.server.StreamAnalyzerServer.java

License:Apache License

public void run(final String configurationFilename, final int port) throws Exception {

    ObjectMapper mapper = new ObjectMapper();
    StreamAnalyzerConfiguration streamAnalyzerConfiguration = mapper.readValue(new File(configurationFilename),
            StreamAnalyzerConfiguration.class);

    // set up  the actor runtime environment
    this.rootActorSystem = ActorSystem.create("streamanalyzer");

    this.componentRegistryRef = componentRegistryInitialization();
    pipelineInitialization(streamAnalyzerConfiguration.getPipelines());
    dispatcherInitialization(streamAnalyzerConfiguration.getDispatchers(), componentRegistryRef);
    listenerInitialization(streamAnalyzerConfiguration.getListeners(), componentRegistryRef);

    EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*w  w  w.j a va2s  .  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 StreamAnalyzerStatsHandler());
                    }
                }).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.moshi.receptionist.remoting.netty.NettyRemotingServer.java

License:Apache License

public NettyRemotingServer(final NettyServerConfig nettyServerConfig,
        final ChannelEventListener channelEventListener) {
    super(nettyServerConfig.getServerOnewaySemaphoreValue(), nettyServerConfig.getServerAsyncSemaphoreValue());
    this.serverBootstrap = new ServerBootstrap();
    this.nettyServerConfig = nettyServerConfig;
    this.channelEventListener = channelEventListener;

    int publicThreadNums = nettyServerConfig.getServerCallbackExecutorThreads();
    if (publicThreadNums <= 0) {
        publicThreadNums = 4;/*from w  w  w .j ava2  s.c o  m*/
    }

    this.publicExecutor = Executors.newFixedThreadPool(publicThreadNums, new ThreadFactory() {
        private AtomicInteger threadIndex = new AtomicInteger(0);

        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, "NettyServerPublicExecutor_" + this.threadIndex.incrementAndGet());
        }
    });

    this.eventLoopGroup = new NioEventLoopGroup(nettyServerConfig.getServerSelectorThreads());
}

From source file:com.mpcc.springmvc.configuration.Start.java

public void run() throws Exception {
    EventLoopGroup boss = new NioEventLoopGroup();
    EventLoopGroup worker = new NioEventLoopGroup();
    try {/*  w  w w  . j  ava2 s.co  m*/
        ServerBootstrap server = new ServerBootstrap();
        server.group(boss, worker).channel(NioServerSocketChannel.class).childHandler(new ServerInitializer());
        Channel ch = server.bind(PORT).sync().channel();
        ch.closeFuture().sync();
    } finally {
        //            boss.shutdownGracefully();
        //            worker.shutdownGracefully();
        shutdownWorkers(boss, worker);
    }
}