List of usage examples for io.netty.bootstrap ServerBootstrap group
public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup)
From source file:com.nettyhttpserver.server.NettyServer.java
public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/* w w w .j a va 2 s. c om*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new NettyServerInitializer()).option(ChannelOption.SO_BACKLOG, 1024) .childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture cf = b.bind(port).sync(); cf.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.newlandframework.rpc.netty.MessageRecvExecutor.java
License:Apache License
public void start() { try {// w ww . j av a2 s.com ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(boss, worker).channel(NioServerSocketChannel.class) .childHandler(new MessageRecvChannelInitializer(handlerMap) .buildRpcSerializeProtocol(serializeProtocol)) .option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); String[] ipAddr = serverAddress.split(MessageRecvExecutor.DELIMITER); if (ipAddr.length == 2) { String host = ipAddr[0]; int port = Integer.parseInt(ipAddr[1]); ChannelFuture future = null; future = bootstrap.bind(host, port).sync(); System.out.printf( "[author tangjie] Netty RPC Server start success!\nip:%s\nport:%d\nprotocol:%s\n\n", host, port, serializeProtocol); future.channel().closeFuture().sync(); } else { System.out.printf("[author tangjie] Netty RPC Server start fail!\n"); } } catch (InterruptedException e) { e.printStackTrace(); } }
From source file:com.nextcont.ecm.fileengine.http.nettyServer.HttpUploadServer.java
License:Apache License
@Override protected void doStart() throws Exception { final SslContext sslCtx; if (SSL) {/*from w w w.ja v a 2 s. co m*/ SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); } else { sslCtx = null; } boss = new NioEventLoopGroup(); worker = new NioEventLoopGroup(); try { ServerBootstrap server = new ServerBootstrap(); server.group(boss, worker); server.channel(NioServerSocketChannel.class); server.handler(new LoggingHandler(LogLevel.INFO)); server.childHandler(new HttpUploadServerInitializer(sslCtx, fileService, fileRecordManager)); Channel ch = server.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 { boss.shutdownGracefully(); worker.shutdownGracefully(); } }
From source file:com.nitesh.netty.snoop.server.HttpSnoopServer.java
License:Apache License
public void run() throws Exception { // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from w ww. j av a 2 s.c o m ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new HttpSnoopServerInitializer()); Channel ch = b.bind(port).sync().channel(); ch.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.nus.mazegame.server.GameServer.java
public static void init(int port, int x, int y, int treasure, boolean isSlave) throws Exception { GameService.gameService = new GameService(x, y, treasure, isSlave); // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from www. j a va 2 s. c om 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 { ChannelPipeline p = ch.pipeline(); // Decoders p.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4)); p.addLast("bytesDecoder", new ByteArrayDecoder()); // Encoder p.addLast("frameEncoder", new LengthFieldPrepender(4)); p.addLast("bytesEncoder", new ByteArrayEncoder()); // p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new GameServerHandler()); } }); // Start the server. ChannelFuture f = b.bind(port).sync(); Logger.getLogger(GameServerHandler.class.getName()).log(Level.INFO, "Server started..."); // 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.ogarproject.ogar.server.net.NetworkManager.java
License:Open Source License
public void start() throws IOException, InterruptedException { bossGroup = new NioEventLoopGroup(); workerGroup = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ClientInitializer(server)); channel = b.bind(server.getConfig().server.port).sync().channel(); OgarServer.log.info("Server started on port " + server.getConfig().server.port + "."); }
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 . jav a2s . co m*/ }
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 w w .j a v a2 s .c om 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.ottogroup.bi.asap.server.emitter.websocket.WebSocketServer.java
License:Apache License
public static void main(String[] args) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/* ww w .j av a2s . c o m*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new WebSocketServerInitializer()); Channel ch = b.bind(9898).sync().channel(); ch.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.ottogroup.bi.spqr.websocket.server.SPQRWebSocketServer.java
License:Apache License
/** * Initializes and executes the server components. Although the other {@link SPQRWebSocketServer#run(String)} * could do the same work this one allows better testing as the {@link InputStream} parameter allows * to inject a variable which may be controlled much better. In the case it would be necessary to * provide a reference to a temporary file which is controlled by the use case ... somehow too much work ;-) * This one is easier. //from w w w . j a v a 2 s. c om * @param configurationFileStream stream holding configuration file content * @throws IOException * @throws InterruptedException */ protected void run(final InputStream configurationFileStream) throws IOException, InterruptedException { /////////////////////////////////////////////////////////////////// // parse out configuration from provided input stream ObjectMapper jsonMapper = new ObjectMapper(); SPQRWebSocketServerConfiguration cfg = jsonMapper.readValue(configurationFileStream, SPQRWebSocketServerConfiguration.class); // /////////////////////////////////////////////////////////////////// PropertyConfigurator.configure(cfg.getLog4jConfigurationFile()); EventLoopGroup bossGroup = new NioEventLoopGroup(cfg.getBossEventGroupThreads()); EventLoopGroup workerGroup = new NioEventLoopGroup(cfg.getWorkerEventGroupThreads()); logger.info( "websocket server [port=" + cfg.getPort() + ", bossGroupThreads=" + cfg.getBossEventGroupThreads() + ", workerGroupThreads=" + cfg.getWorkerEventGroupThreads() + "]"); try { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new SPQRWebSocketServerInitializer()); Channel channel = bootstrap.bind(cfg.getPort()).sync().channel(); channel.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }