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.xx_dev.apn.socks.remote.SocksServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from www . j a  v a  2 s. c  o m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler("NET_LOGGER", LogLevel.DEBUG))
                .childHandler(new SocksServerInitializer());
        b.bind(RemoteConfig.ins().getListenPort()).sync().channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.xx_dev.port_forwared.HexDumpProxy.java

License:Apache License

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

    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();

    List<HexDumpForwardRule> ruleList = new ArrayList<HexDumpForwardRule>();
    HexDumpForwardRule r3 = new HexDumpForwardRule(9000, "imap.gmail.com", 993, true);
    ruleList.add(r3);//from ww  w.jav a  2  s  .c  o  m
    HexDumpForwardRule r4 = new HexDumpForwardRule(9001, "smtp.gmail.com", 465, true);
    ruleList.add(r4);

    List<ChannelFuture> bindFutureList = new ArrayList<ChannelFuture>();

    try {
        for (HexDumpForwardRule r : ruleList) {

            ServerBootstrap b = new ServerBootstrap();
            ChannelFuture bindFuture = b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                    .handler(new LoggingHandler("BYTE_LOGGER", LogLevel.DEBUG))
                    .childHandler(
                            new HexDumpProxyInitializer(r.getRemoteHost(), r.getRemotePort(), r.isRemoteSsl()))
                    .childOption(ChannelOption.AUTO_READ, false).bind(r.getLocalPort());

            bindFutureList.add(bindFuture);

        }
        for (ChannelFuture f : bindFutureList) {
            f.sync().channel().closeFuture().sync();
        }
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }

}

From source file:com.xyz.rpc.netty4.server.Netty4Server.java

License:Apache License

public void start(int listenPort, final ExecutorService ignore) throws Exception {
    if (!startFlag.compareAndSet(false, true)) {
        return;//from   w  w  w .j a  va  2s  . c o m
    }
    bossGroup = new NioEventLoopGroup();
    ioGroup = new NioEventLoopGroup();
    businessGroup = new DefaultEventExecutorGroup(businessThreads);

    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, ioGroup).channel(NioServerSocketChannel.class)
            .childOption(ChannelOption.TCP_NODELAY,
                    Boolean.parseBoolean(System.getProperty("nfs.rpc.tcp.nodelay", "true")))
            .childOption(ChannelOption.SO_REUSEADDR,
                    Boolean.parseBoolean(System.getProperty("nfs.rpc.tcp.reuseaddress", "true")))
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast("decoder", new Netty4ProtocolDecoder());
                    ch.pipeline().addLast("encoder", new Netty4ProtocolEncoder());
                    ch.pipeline().addLast(businessGroup, "handler", new Netty4ServerHandler());
                }
            });
    b.bind(new InetSocketAddress("127.0.0.1", listenPort)).sync();
    LOGGER.warn("Server started,listen at: " + listenPort + ", businessThreads is " + businessThreads);
}

From source file:com.yahoo.ads.pb.network.netty.NettyPistachioServer.java

License:Open Source License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {/*from www . j  av  a2  s  . 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 NettyPistachioServerInitializer(sslCtx));

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

From source file:com.yahoo.pulsar.broker.service.BrokerService.java

License:Apache License

public void start() throws Exception {
    this.producerNameGenerator = new DistributedIdGenerator(pulsar.getZkClient(), producerNameGeneratorPath,
            pulsar.getConfiguration().getClusterName());

    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    bootstrap.group(acceptorGroup, workerGroup);
    bootstrap.childOption(ChannelOption.TCP_NODELAY, true);
    bootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR,
            new AdaptiveRecvByteBufAllocator(1024, 16 * 1024, 1 * 1024 * 1024));

    if (workerGroup instanceof EpollEventLoopGroup) {
        bootstrap.channel(EpollServerSocketChannel.class);
        bootstrap.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
    } else {//from w w w.  j  a  v a2  s .  c  om
        bootstrap.channel(NioServerSocketChannel.class);
    }

    ServiceConfiguration serviceConfig = pulsar.getConfiguration();

    bootstrap.childHandler(new PulsarChannelInitializer(this, serviceConfig, false));
    // Bind and start to accept incoming connections.
    bootstrap.bind(new InetSocketAddress(pulsar.getBindAddress(), port)).sync();
    log.info("Started Pulsar Broker service on port {}", port);

    if (serviceConfig.isTlsEnabled()) {
        ServerBootstrap tlsBootstrap = bootstrap.clone();
        tlsBootstrap.childHandler(new PulsarChannelInitializer(this, serviceConfig, true));
        tlsBootstrap.bind(new InetSocketAddress(pulsar.getBindAddress(), tlsPort)).sync();
        log.info("Started Pulsar Broker TLS service on port {}", tlsPort);
    }

    // start other housekeeping functions
    this.startStatsUpdater();
    this.startInactivityMonitor();
    this.startMessageExpiryMonitor();
    this.startBacklogQuotaChecker();
}

From source file:com.yahoo.pulsar.client.api.MockBrokerService.java

License:Apache License

public void startMockBrokerService() throws Exception {
    ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("mock-pulsar-%s").build();
    final int numThreads = 2;

    final int MaxMessageSize = 5 * 1024 * 1024;
    EventLoopGroup eventLoopGroup;//from  w w  w .  j a v a2s.com

    try {
        if (SystemUtils.IS_OS_LINUX) {
            try {
                eventLoopGroup = new EpollEventLoopGroup(numThreads, threadFactory);
            } catch (UnsatisfiedLinkError e) {
                eventLoopGroup = new NioEventLoopGroup(numThreads, threadFactory);
            }
        } else {
            eventLoopGroup = new NioEventLoopGroup(numThreads, threadFactory);
        }
        workerGroup = eventLoopGroup;

        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(workerGroup, workerGroup);
        if (workerGroup instanceof EpollEventLoopGroup) {
            bootstrap.channel(EpollServerSocketChannel.class);
        } else {
            bootstrap.channel(NioServerSocketChannel.class);
        }

        bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast("frameDecoder",
                        new LengthFieldBasedFrameDecoder(MaxMessageSize, 0, 4, 0, 4));
                ch.pipeline().addLast("handler", new MockServerCnx());
            }
        });
        // Bind and start to accept incoming connections.
        bootstrap.bind(brokerServicePort).sync();
    } catch (Exception e) {
        throw e;
    }
}