public int start(final int port, final ChannelInitializer<? extends Channel> pipelineFactory) { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(group) .channel(NioServerSocketChannel.class) .childHandler(pipelineFactory); try { future = bootstrap.bind(port).sync(); SocketAddress socketAddress = future.channel().localAddress(); return ((InetSocketAddress) socketAddress).getPort(); } catch (InterruptedException e) { throw new MocoException(e); } }
private void groupsEpoll(final ServerBootstrap bootstrap) { workerGroup = new EpollEventLoopGroup(); bootstrap.group(bossGroup, workerGroup) .channel(EpollServerSocketChannel.class) .option(EpollChannelOption.SO_BACKLOG, 128) .option(EpollChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(8 * 1024 * 1024, 16 * 1024 * 1024)) .option(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(EpollChannelOption.TCP_NODELAY, true) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ServerHandlerInitializer()); }
private ServerBootstrap createServerBootstrap( ServerConfigurationProvider configurationProvider, ProtocolInitializer protocolInitializer ) { return new ServerBootstrap() .group( bossGroup, selectorGroup ) .channel( configurationProvider.getChannelClass() ) .option( ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT ) .option( ChannelOption.SO_REUSEADDR, true ) .childOption( ChannelOption.SO_KEEPALIVE, true ) .childHandler( protocolInitializer.channelInitializer() ); } }
private void groupsEpoll(final ServerBootstrap bootstrap) { workerGroup = new EpollEventLoopGroup(); bootstrap.group(bossGroup, workerGroup) .channel(EpollServerSocketChannel.class) .option(EpollChannelOption.SO_BACKLOG, 128) .option(EpollChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(8 * 1024 * 1024, 16 * 1024 * 1024)) .option(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(EpollChannelOption.TCP_NODELAY, true) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ServerHandlerInitializer()); }
/** * Listing 8.6 Bootstrapping and using ChannelInitializer * */ public void bootstrap() throws InterruptedException { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup()) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializerImpl()); ChannelFuture future = bootstrap.bind(new InetSocketAddress(8080)); future.sync(); }
private void groupsEpoll(final ServerBootstrap bootstrap, final int workThreads) { workerGroup = new EpollEventLoopGroup(workThreads); bootstrap.group(bossGroup, workerGroup) .channel(EpollServerSocketChannel.class) .option(EpollChannelOption.TCP_CORK, true) .option(EpollChannelOption.SO_KEEPALIVE, true) .option(EpollChannelOption.SO_BACKLOG, 100) .option(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(nettyServerHandlerInitializer); }
@Override protected int initServer() throws InterruptedException { bossGroup = new NioEventLoopGroup(1); workerGroup = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.option(ChannelOption.SO_BACKLOG, 1024); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<Channel>() { @Override protected void initChannel(final Channel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new HttpServerCodec()); p.addLast(new TracingDispatchHandler()); p.addLast(new HelloWorldHandler()); } }); Channel ch = b.bind(0).sync().channel(); return ((InetSocketAddress) ch.localAddress()).getPort(); }
@Override protected ServerBootstrap getServerBootstrap() { ServerBootstrap b = new ServerBootstrap(); b.group(_bossGroup, _workerGroup).channel(NioServerSocketChannel.class).childHandler(createChannelInitializer()) .option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); return b; }
private void groupsNio(final ServerBootstrap bootstrap) { workerGroup = new NioEventLoopGroup(); bootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 128) .option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(8 * 1024 * 1024, 16 * 1024 * 1024)) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(ChannelOption.TCP_NODELAY, true) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ServerHandlerInitializer()); } }
private void groupsNio(final ServerBootstrap bootstrap) { workerGroup = new NioEventLoopGroup(); bootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 128) .option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(8 * 1024 * 1024, 16 * 1024 * 1024)) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(ChannelOption.TCP_NODELAY, true) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ServerHandlerInitializer()); } }
public ChannelFuture start(InetSocketAddress address) { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(group) .channel(NioServerSocketChannel.class) .childHandler(createInitializer(channelGroup)); ChannelFuture future = bootstrap.bind(address); future.syncUninterruptibly(); channel = future.channel(); return future; }
/** * start server, bind port */ public void start() throws Throwable { if (!started.compareAndSet(false, true)) { return; } boss = new NioEventLoopGroup(0, new DefaultThreadFactory("qos-boss", true)); worker = new NioEventLoopGroup(0, new DefaultThreadFactory("qos-worker", true)); ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(boss, worker); serverBootstrap.channel(NioServerSocketChannel.class); serverBootstrap.childOption(ChannelOption.TCP_NODELAY, true); serverBootstrap.childOption(ChannelOption.SO_REUSEADDR, true); serverBootstrap.childHandler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ch.pipeline().addLast(new QosProcessHandler(welcome, acceptForeignIp)); } }); try { serverBootstrap.bind(port).sync(); logger.info("qos-server bind localhost:" + port); } catch (Throwable throwable) { logger.error("qos-server can not bind localhost:" + port, throwable); throw throwable; } }
private void groupsNio(final ServerBootstrap bootstrap, final int workThreads) { workerGroup = new NioEventLoopGroup(workThreads); bootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(EpollChannelOption.TCP_CORK, true) .option(EpollChannelOption.SO_KEEPALIVE, true) .option(EpollChannelOption.SO_BACKLOG, 100) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(nettyServerHandlerInitializer); }
/** * start server, bind port */ public void start() throws Throwable { if (!started.compareAndSet(false, true)) { return; } boss = new NioEventLoopGroup(0, new DefaultThreadFactory("qos-boss", true)); worker = new NioEventLoopGroup(0, new DefaultThreadFactory("qos-worker", true)); ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(boss, worker); serverBootstrap.channel(NioServerSocketChannel.class); serverBootstrap.childOption(ChannelOption.TCP_NODELAY, true); serverBootstrap.childOption(ChannelOption.SO_REUSEADDR, true); serverBootstrap.childHandler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ch.pipeline().addLast(new QosProcessHandler(welcome, acceptForeignIp)); } }); try { serverBootstrap.bind(port).sync(); logger.info("qos-server bind localhost:" + port); } catch (Throwable throwable) { logger.error("qos-server can not bind localhost:" + port, throwable); throw throwable; } }
private void initializeSSLTCPTransport(String host, int port, final SSLContext sslContext) { ServerBootstrap b = new ServerBootstrap(); b.group(serverBossGroup, serverWorkerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); try { pipeline.addLast("ssl", createSslHandler(sslContext, Config.getInstance().getBooleanValue("server.ssl.needsClientAuth", false))); ch.pipeline().addLast(new ProxyMessageDecoder(MAX_FRAME_LENGTH, LENGTH_FIELD_OFFSET, LENGTH_FIELD_LENGTH, LENGTH_ADJUSTMENT, INITIAL_BYTES_TO_STRIP)); ch.pipeline().addLast(new ProxyMessageEncoder()); ch.pipeline().addLast(new IdleCheckHandler(IdleCheckHandler.READ_IDLE_TIME, IdleCheckHandler.WRITE_IDLE_TIME, 0)); ch.pipeline().addLast(new ServerChannelHandler()); } catch (Throwable th) { logger.error("Severe error during pipeline creation", th); throw th; } } }); try { // Bind and start to accept incoming connections. ChannelFuture f = b.bind(host, port); f.sync(); logger.info("proxy ssl server start on port {}", port); } catch (InterruptedException ex) { logger.error("An interruptedException was caught while initializing server", ex); } }
public void start() throws IOException { LOG.info("Starting LlapOutputFormatService"); int portFromConf = HiveConf.getIntVar(conf, HiveConf.ConfVars.LLAP_DAEMON_OUTPUT_SERVICE_PORT); int sendBufferSize = HiveConf.getIntVar(conf, HiveConf.ConfVars.LLAP_DAEMON_OUTPUT_SERVICE_SEND_BUFFER_SIZE); // Netty defaults to no of processors * 2. Can be changed via -Dio.netty.eventLoopThreads eventLoopGroup = new NioEventLoopGroup(); serverBootstrap = new ServerBootstrap(); serverBootstrap.group(eventLoopGroup); serverBootstrap.channel(NioServerSocketChannel.class); serverBootstrap.childHandler(new LlapOutputFormatServiceChannelHandler(sendBufferSize)); try { listeningChannelFuture = serverBootstrap.bind(portFromConf).sync(); this.port = ((InetSocketAddress) listeningChannelFuture.channel().localAddress()).getPort(); LOG.info("LlapOutputFormatService: Binding to port: {} with send buffer size: {} ", this.port, sendBufferSize); } catch (InterruptedException err) { throw new IOException("LlapOutputFormatService: Error binding to port " + portFromConf, err); } }
private ChannelFuture setupServerBootstrap(int port, ChannelInitializer channelInitializer) throws InterruptedException ServerBootstrap serverBootstrap = new ServerBootstrap().group( serverGroup.clientToProxyBossPool, serverGroup.clientToProxyWorkerPool); serverBootstrap = serverBootstrap.channel(EpollServerSocketChannel.class); channelOptions.put(EpollChannelOption.TCP_DEFER_ACCEPT, Integer.valueOf(-1)); serverBootstrap = serverBootstrap.channel(NioServerSocketChannel.class); serverBootstrap.childHandler(channelInitializer); serverBootstrap.validate(); return serverBootstrap.bind(port).sync();
/** * Netty HTTP server initialization. */ public void init() { try { LOG.info("NettyServer Initializing..."); bossGroup = new NioEventLoopGroup(); LOG.debug("NettyServer bossGroup created"); workerGroup = new NioEventLoopGroup(); LOG.debug("NettyServer workGroup created"); btsServer = new ServerBootstrap(); LOG.debug("NettyServer ServerBootstrap created"); ChannelInitializer<SocketChannel> serverInit = configureInitializer(); LOG.debug("NettyServer InitClass instance created"); LOG.debug("NettyServer InitClass instance init()"); btsServer.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .childHandler(serverInit) .option(ChannelOption.SO_REUSEADDR, true); LOG.debug("NettyServer ServerBootstrap group initialized"); bindChannel = btsServer.bind(bindAddress, bindPort).sync().channel(); } catch (Exception exception) { LOG.error("NettyHttpServer init() failed", exception); } }
/** * Listing 8.4 Bootstrapping a server * */ public void bootstrap() { NioEventLoopGroup group = new NioEventLoopGroup(); ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(group) .channel(NioServerSocketChannel.class) .childHandler(new SimpleChannelInboundHandler<ByteBuf>() { @Override protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception { System.out.println("Received data"); } }); ChannelFuture future = bootstrap.bind(new InetSocketAddress(8080)); future.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) throws Exception { if (channelFuture.isSuccess()) { System.out.println("Server bound"); } else { System.err.println("Bind attempt failed"); channelFuture.cause().printStackTrace(); } } }); } }
public void start() throws IOException { LOG.info("Starting LlapOutputFormatService"); int portFromConf = HiveConf.getIntVar(conf, HiveConf.ConfVars.LLAP_DAEMON_OUTPUT_SERVICE_PORT); int sendBufferSize = HiveConf.getIntVar(conf, HiveConf.ConfVars.LLAP_DAEMON_OUTPUT_SERVICE_SEND_BUFFER_SIZE); eventLoopGroup = new NioEventLoopGroup(1); serverBootstrap = new ServerBootstrap(); serverBootstrap.group(eventLoopGroup); serverBootstrap.channel(NioServerSocketChannel.class); serverBootstrap.childHandler(new LlapOutputFormatServiceChannelHandler(sendBufferSize)); try { listeningChannelFuture = serverBootstrap.bind(portFromConf).sync(); this.port = ((InetSocketAddress) listeningChannelFuture.channel().localAddress()).getPort(); LOG.info("LlapOutputFormatService: Binding to port: {} with send buffer size: {} ", this.port, sendBufferSize); } catch (InterruptedException err) { throw new IOException("LlapOutputFormatService: Error binding to port " + portFromConf, err); } }