Example usage for io.netty.channel.nio NioEventLoopGroup NioEventLoopGroup

List of usage examples for io.netty.channel.nio NioEventLoopGroup NioEventLoopGroup

Introduction

In this page you can find the example usage for io.netty.channel.nio NioEventLoopGroup NioEventLoopGroup.

Prototype

public NioEventLoopGroup(ThreadFactory threadFactory) 

Source Link

Document

Create a new instance using the default number of threads, the given ThreadFactory and the SelectorProvider which is returned by SelectorProvider#provider() .

Usage

From source file:com.github.spapageo.jannel.client.JannelClient.java

License:Open Source License

public JannelClient(int ioThreads) {
    this(new NioEventLoopGroup(ioThreads), NioSocketChannel.class);
}

From source file:com.github.spapageo.jannel.client.JannelClient.java

License:Open Source License

public JannelClient(EventLoopGroup eventLoopGroup, Class<? extends Channel> channelClass) {
    this(eventLoopGroup, channelClass, new NioEventLoopGroup(Runtime.getRuntime().availableProcessors()));
}

From source file:com.github.unafraid.signer.server.ServerManager.java

License:Apache License

private void init() {
    SslContext sslCtx = null;//from   w  w  w .  j a  v  a2  s.  co  m
    if (SSL) {
        try {
            final SelfSignedCertificate ssc = new SelfSignedCertificate("localhost");
            sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
        } catch (Exception e) {
            LOGGER.warn(e.getMessage(), e);
        }
    }

    InetAddress listenAddress;
    try {
        listenAddress = Inet4Address.getByName(HOSTNAME);
    } catch (Exception e) {
        LOGGER.warn("Incorrect listen ip specified: {} using localhost instead!", HOSTNAME);
        listenAddress = Inet4Address.getLoopbackAddress();
    }
    final EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    final EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        final ServerBootstrap b = new ServerBootstrap();
        b.option(ChannelOption.SO_BACKLOG, 1024);
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ServerInitializer(sslCtx));

        // Start listening
        final Channel ch = b.bind(listenAddress, PORT).sync().channel();

        LOGGER.info("Open your web browser and navigate to {}://{}{}/", (SSL ? "https" : "http"),
                listenAddress.getHostAddress(), (PORT != 443 && PORT != 80 ? ":" + PORT : ""));

        // Block til closed
        ch.closeFuture().sync();

    } catch (Exception e) {
        LOGGER.warn("Failed to initialize server: ", e);
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.github.wolf480pl.ircd.netty.NettyServer.java

License:Open Source License

public ChannelFuture start() {
    if (!started.compareAndSet(false, true)) {
        return null;
    }//from  w  w w  . j  a  va 2 s . c  om

    ServerBootstrap bootstrap = new ServerBootstrap();
    bossGroup = new NioEventLoopGroup(1);
    workerGroup = new NioEventLoopGroup();

    IRCChannelInitializer initializer = new IRCChannelInitializer(handler);

    bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(initializer)
            .childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true);

    ChannelFuture future = bootstrap.bind(bindAddress);
    this.channel = future.channel();
    return future;
}

From source file:com.google.devtools.build.lib.remote.blobstore.http.HttpBlobStoreTest.java

License:Open Source License

private ServerSocketChannel startServer(ChannelHandler handler) throws Exception {
    EventLoopGroup eventLoop = new NioEventLoopGroup(1);
    ServerBootstrap sb = new ServerBootstrap().group(eventLoop).channel(NioServerSocketChannel.class)
            .childHandler(new ChannelInitializer<NioSocketChannel>() {
                @Override/*from   w w w . jav  a2  s  . c  o  m*/
                protected void initChannel(NioSocketChannel ch) {
                    ch.pipeline().addLast(new HttpServerCodec());
                    ch.pipeline().addLast(new HttpObjectAggregator(1000));
                    ch.pipeline().addLast(handler);
                }
            });
    return ((ServerSocketChannel) sb.bind("localhost", 0).sync().channel());
}

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

License:Open Source License

AgentConnector(ConcurrentMap<String, ResponseFuture> futureMap, SimulatorAddress localAddress, int port,
        Agent agent, WorkerJvmManager workerJvmManager, ConnectionManager connectionManager,
        int threadPoolSize) {
    super(futureMap, localAddress, port);

    this.group = new NioEventLoopGroup(threadPoolSize);

    RemoteExceptionLogger exceptionLogger = new RemoteExceptionLogger(localAddress, AGENT_EXCEPTION, this);
    this.processor = new AgentOperationProcessor(exceptionLogger, agent, workerJvmManager);

    this.futureMap = futureMap;

    this.localAddress = localAddress;
    this.addressIndex = localAddress.getAddressIndex();

    this.connectionManager = connectionManager;
    this.workerJvmManager = workerJvmManager;
}

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 ww 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);
}

From source file:com.heren.turtle.entry.channel.MessageReceiveServer.java

License:Open Source License

public void bind(int port, boolean needToFilter) {
    System.out.println("netty start!");
    EventLoopGroup bossGroup = new NioEventLoopGroup(6);
    EventLoopGroup workGroup = new NioEventLoopGroup(12);
    try {/*from  www .jav  a 2s. c o m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 1024)
                .childHandler(new MessageReceiveInitializer(needToFilter));
        //bound port,waiting for a successful synchronization
        ChannelFuture f = b.bind(port).sync();
        //waiting for the server listening port closed
        f.channel().closeFuture().sync();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        bossGroup.shutdownGracefully();
        workGroup.shutdownGracefully();
    }
}

From source file:com.hiido.eagle.hes.network.FileServer.java

License:Apache License

public void run() throws Exception {
    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from   w  w  w  .j  av a 2s  . 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 ChunkedWriteHandler());
                        ch.pipeline().addLast(new FileHandler());
                    }
                });

        // 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.hiido.eagle.hes.transfer.FileTransferServer.java

License:Apache License

public void start() throws Exception {

    logger.info("Start server at host:{} port:{}, the number of work:{}", host, port, workNum);

    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup(workNum);
    try {// w  w  w .jav a 2  s  .c o m
        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 FileHandler());
                    }
                });

        ChannelFuture f = null;
        if (host == null) {
            f = b.bind(port).sync();
        } else {
            f = b.bind(host, port).sync();
        }

        logger.info("Server bound host:{} port:{} successfully", host, port);

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