List of usage examples for io.netty.bootstrap ServerBootstrap childOption
public <T> ServerBootstrap childOption(ChannelOption<T> childOption, T value)
From source file:com.netty.HttpHelloWorldServer.java
License:Apache License
public void start(String[] args) throws Exception { initSpringContext(args);// www . j a va 2s . c om // Configure SSL. final SslContext sslCtx; if (SSL) { SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); } else { sslCtx = null; } // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(1000); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) // .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new HttpHelloWorldServerInitializer(applicationContext, sslCtx)); b.option(ChannelOption.SO_BACKLOG, 128); // (5) b.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); b.childOption(ChannelOption.SO_KEEPALIVE, true); b.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024); b.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024); 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.onion.worker.WorkerDataServer.java
License:Apache License
private ServerBootstrap createServerBootstrap() { final ServerBootstrap boot = new ServerBootstrap(); // If number of worker threads is 0, Netty creates (#processors * 2) threads by default. final EventLoopGroup bossGroup = new NioEventLoopGroup(1); final EventLoopGroup workerGroup = new NioEventLoopGroup(0); boot.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class); // use pooled buffers boot.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); boot.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); // set write buffer // this is the default, but its recommended to set it in case of change in future netty. boot.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32768); boot.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8192); return boot;/* w ww. j a v a 2 s . c om*/ }
From source file:com.openddal.server.NettyServer.java
License:Apache License
private ServerBootstrap configServer() { bossGroup = new NioEventLoopGroup(args.bossThreads, new DefaultThreadFactory("NettyBossGroup", true)); workerGroup = new NioEventLoopGroup(args.workerThreads, new DefaultThreadFactory("NettyWorkerGroup", true)); userExecutor = createUserThreadExecutor(); Authenticator authenticator = createAuthenticator(); ProcessorFactory processorFactory = createProcessorFactory(); RequestFactory requestFactory = createRequestFactory(); ResponseFactory responseFactory = createResponseFactory(); final ProtocolHandler protocolHandler = new ProtocolHandler(authenticator, processorFactory, requestFactory, responseFactory, userExecutor); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childOption(ChannelOption.SO_REUSEADDR, true).childOption(ChannelOption.SO_KEEPALIVE, true) .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(ChannelOption.TCP_NODELAY, true); if (args.socketTimeoutMills > 0) { b.childOption(ChannelOption.SO_TIMEOUT, args.socketTimeoutMills); }//from w ww . j a v a2 s . c o m if (args.recvBuff > 0) { b.childOption(ChannelOption.SO_RCVBUF, args.recvBuff); } if (args.sendBuff > 0) { b.childOption(ChannelOption.SO_SNDBUF, args.sendBuff); } b.childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(createProtocolDecoder(), /* createProtocolEncoder(), */ protocolHandler); } }); return b; }
From source file:com.quavo.osrs.network.NetworkExecutor.java
License:Open Source License
/** * Starts the network for a {@link Server}. * // ww w .j a v a 2 s . c o m * @param server The {@link Server} to use for building the network. * @return <True> If the network started successfully. */ public static void start() { EventLoopGroup boss = new NioEventLoopGroup(); EventLoopGroup worker = new NioEventLoopGroup(); ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(boss, worker); bootstrap.channel(NioServerSocketChannel.class); bootstrap.childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("decoder", new ConnectionDecoder()); pipeline.addLast("encoder", new ConnectionEncoder()); pipeline.addLast("adapter", new NetworkMessageHandler()); } }); bootstrap.childOption(ChannelOption.TCP_NODELAY, true); try { bootstrap.bind(Constants.HOST_NAME, Constants.HOST_PORT).sync(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Server successfully bootstrapped on port " + Constants.HOST_PORT + " and address " + Constants.HOST_NAME + "."); }
From source file:com.relayrides.pushy.apns.MockApnsServer.java
License:Open Source License
public synchronized void start() throws InterruptedException { final ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(this.eventLoopGroup); bootstrap.channel(NioServerSocketChannel.class); bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true); final MockApnsServer server = this; bootstrap.childHandler(new ChannelInitializer<SocketChannel>() { @Override//from ww w .j av a 2 s.com protected void initChannel(final SocketChannel channel) throws Exception { channel.pipeline().addLast("ssl", new SslHandler(SSLTestUtil.createSSLEngineForMockServer())); channel.pipeline().addLast("encoder", new ApnsErrorEncoder()); channel.pipeline().addLast("decoder", new ApnsPushNotificationDecoder()); channel.pipeline().addLast("handler", new MockApnsServerHandler(server)); } }); this.channel = bootstrap.bind(this.port).await().channel(); }
From source file:com.Server.java
License:Apache License
/** * This is passive mode server//from w w w. j a va2s. co m * @param fs FTP Session Handler * @param host Server IP address * @param port Passive port no. */ public Server(String host, int port, int mode, String fileName) { InetSocketAddress inSocketAddress = new InetSocketAddress(host, port); try { ServerBootstrap bootStrap = new ServerBootstrap(); bootStrap.group(bossGroup, workerGroup); bootStrap.channel(NioServerSocketChannel.class); bootStrap.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 1); bootStrap.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 1); bootStrap.childHandler(new MyChannelInitializer(this, mode, fileName)); bootStrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); bootStrap.bind(inSocketAddress); System.out.println("Server started"); } catch (Exception eg) { eg.printStackTrace(); stop(); } }
From source file:com.spotify.ffwd.protocol.ProtocolServersImpl.java
License:Apache License
private AsyncFuture<ProtocolConnection> bindTCP(final Logger log, final Protocol protocol, ProtocolServer server, RetryPolicy policy) { final ServerBootstrap b = new ServerBootstrap(); b.group(boss, worker);//from w w w . j av a 2 s .com b.channel(NioServerSocketChannel.class); b.childHandler(server.initializer()); b.option(ChannelOption.SO_BACKLOG, 128); if (protocol.getReceiveBufferSize() != null) { b.childOption(ChannelOption.SO_RCVBUF, protocol.getReceiveBufferSize()); } b.childOption(ChannelOption.SO_KEEPALIVE, true); final String host = protocol.getAddress().getHostString(); final int port = protocol.getAddress().getPort(); final RetryingProtocolConnection connection = new RetryingProtocolConnection(async, timer, log, policy, new ProtocolChannelSetup() { @Override public ChannelFuture setup() { return b.bind(host, port); } @Override public String toString() { return String.format("bind tcp://%s:%d", host, port); } }); return connection.getInitialFuture(); }
From source file:com.titilink.camel.rest.server.CamelServer.java
License:LGPL
private static void run(int port, boolean supportSSL) { LOGGER.info("CamelServer run({}, {})", port, supportSSL); EventLoopGroup bossGroup = new NioEventLoopGroup(DEFAULT_BOSS_NIO_EVENT_NUM); BOSS_GROUP_LIST.add(bossGroup);/*from ww w . j av a 2 s . com*/ EventLoopGroup workerGroup = new NioEventLoopGroup(DEFAULT_NIO_EVENT_NUM); WORKER_GROUP_LIST.add(workerGroup); String host = AdapterRestletUtil.getProperty(SERVER_REST_IP, LOCAL_HOST); HttpConnector httpConnector = new RestletConnector(host, port, supportSSL); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new HttpServerInitializer(supportSSL, httpConnector)); b.childOption(ChannelOption.SO_KEEPALIVE, true); b.childOption(ChannelOption.TCP_NODELAY, true); b.option(ChannelOption.SO_BACKLOG, DEFAULT_SO_BACKLOG); Channel ch = b.bind(new InetSocketAddress(host, port)).sync().channel(); CHANNEL_LIST.add(ch); ch.closeFuture().sync(); } catch (InterruptedException e) { LOGGER.error("InterruptedException while start server."); } catch (Throwable t) { LOGGER.error("CamelServer run throws error."); } finally { LOGGER.info("CamelServer run() : shutdown"); bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.tongbanjie.tarzan.rpc.netty.NettyRpcServer.java
License:Apache License
@Override public void start() { this.defaultEventExecutorGroup = new DefaultEventExecutorGroup(// nettyServerConfig.getServerWorkerThreads(), // new ThreadFactory() { private AtomicInteger threadIndex = new AtomicInteger(0); @Override/*from ww w. ja v a 2 s .co m*/ public Thread newThread(Runnable r) { return new Thread(r, "NettyServerCodecThread_" + this.threadIndex.incrementAndGet()); } }); ServerBootstrap childHandler = // this.serverBootstrap.group(this.eventLoopGroupBoss, this.eventLoopGroupSelector) .channel(NioServerSocketChannel.class) // .option(ChannelOption.SO_BACKLOG, 1024) // .option(ChannelOption.SO_REUSEADDR, true) // .option(ChannelOption.SO_KEEPALIVE, false) // .childOption(ChannelOption.TCP_NODELAY, true) // .option(ChannelOption.SO_SNDBUF, nettyServerConfig.getServerSocketSndBufSize()) // .option(ChannelOption.SO_RCVBUF, nettyServerConfig.getServerSocketRcvBufSize()) // .localAddress(new InetSocketAddress(this.nettyServerConfig.getListenPort())) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast( // defaultEventExecutorGroup, // new NettyEncoder(), // new NettyDecoder(), // new IdleStateHandler(0, 0, nettyServerConfig.getServerChannelMaxIdleTimeSeconds()), // new NettyConnectManageHandler(), // new NettyServerHandler()); } }); if (nettyServerConfig.isServerPooledByteBufAllocatorEnable()) { childHandler.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); } try { ChannelFuture sync = this.serverBootstrap.bind().sync(); InetSocketAddress addr = (InetSocketAddress) sync.channel().localAddress(); this.port = addr.getPort(); } catch (InterruptedException e1) { throw new RuntimeException("this.serverBootstrap.bind().sync() InterruptedException", e1); } if (this.channelEventListener != null) { this.nettyEventExecutor.start(); } this.timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { try { NettyRpcServer.this.scanResponseTable(); } catch (Exception e) { LOGGER.error("scanResponseTable exception", e); } } }, 1000 * 3, 1000); LOGGER.info("Rpc server [port:{}] start success", port); }
From source file:com.turn.ttorrent.client.io.PeerServer.java
License:Apache License
/** * Create and start a new listening service for our torrents, reporting * with our peer ID on the given address. * * <p>// ww w . j ava 2 s .c o m * This binds to the first available port in the client port range * PORT_RANGE_START to PORT_RANGE_END. * </p> */ public void start() throws Exception { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(environment.getEventService()); bootstrap.channel(environment.getEventLoopType().getServerChannelType()); bootstrap.option(ChannelOption.SO_BACKLOG, 128); bootstrap.option(ChannelOption.SO_REUSEADDR, true); bootstrap.option(ChannelOption.TCP_NODELAY, true); bootstrap.childHandler(new ChannelInitializer() { @Override protected void initChannel(Channel ch) throws Exception { ch.pipeline().addLast(new PeerServerHandshakeHandler(torrents)); } }); bootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true); // bootstrap.childOption(ChannelOption.SO_TIMEOUT, (int) TimeUnit.MINUTES.toMillis(CLIENT_KEEP_ALIVE_MINUTES)); // TODO: Derive from PieceHandler.DEFAULT_BLOCK_SIZE // bootstrap.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 1024 * 1024); // bootstrap.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024); SocketAddress address = environment.getLocalPeerListenAddress(); if (address != null) { future = bootstrap.bind(address).sync(); } else { BIND: { Exception x = new IOException("No available port for the BitTorrent client!"); for (int i = PORT_RANGE_START; i <= PORT_RANGE_END; i++) { try { future = bootstrap.bind(i).sync(); break BIND; } catch (InterruptedException e) { throw e; } catch (Exception e) { x = e; } } throw new IOException("Failed to find an address to bind in range [" + PORT_RANGE_START + "," + PORT_RANGE_END + "]", x); } } }