List of usage examples for io.netty.bootstrap ServerBootstrap ServerBootstrap
public ServerBootstrap()
From source file:com.ctrip.xpipe.redis.console.health.netty.EchoServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {/*from w ww.j a v a2 s . co m*/ SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); } else { sslCtx = null; } // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).option(ChannelOption.TCP_NODELAY, true) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); } //p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new EchoServerHandler()); } }); // 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.demo.netty.echo.EchoServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {/* w w w.j a va 2 s. com*/ SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); } else { sslCtx = null; } //EventLoopGroup is abstraction of Notification and Thread loop EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { 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(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); } //p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new EchoServerHandler()); } }); // 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.digisky.innerproxy.server.HttpServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. ProcessServerManager.getInstance().init(); final SslContext sslCtx; if (SSL) {/*from ww w . ja v a 2 s .c om*/ SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey()); } else { sslCtx = null; } // Configure the server. 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 HttpServerInitializer(sslCtx)); 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.digitalpetri.modbus.slave.ModbusTcpSlave.java
License:Apache License
public CompletableFuture<ModbusTcpSlave> bind(String host, int port) { CompletableFuture<ModbusTcpSlave> bindFuture = new CompletableFuture<>(); ServerBootstrap bootstrap = new ServerBootstrap(); ChannelInitializer<SocketChannel> initializer = new ChannelInitializer<SocketChannel>() { @Override/* www .ja v a2 s .c om*/ protected void initChannel(SocketChannel channel) throws Exception { channelCounter.inc(); logger.info("channel initialized: {}", channel); channel.pipeline().addLast(new LoggingHandler(LogLevel.TRACE)); channel.pipeline() .addLast(new ModbusTcpCodec(new ModbusResponseEncoder(), new ModbusRequestDecoder())); channel.pipeline().addLast(new ModbusTcpSlaveHandler(ModbusTcpSlave.this)); channel.closeFuture().addListener(future -> channelCounter.dec()); } }; config.getBootstrapConsumer().accept(bootstrap); bootstrap.group(config.getEventLoop()).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.DEBUG)).childHandler(initializer) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); bootstrap.bind(host, port).addListener((ChannelFuture future) -> { if (future.isSuccess()) { Channel channel = future.channel(); serverChannels.put(channel.localAddress(), channel); bindFuture.complete(ModbusTcpSlave.this); } else { bindFuture.completeExceptionally(future.cause()); } }); return bindFuture; }
From source file:com.dingwang.netty.server.DiscardServer.java
License:Open Source License
public void run() { //NioEventLoopGroup ??I/O?Netty????EventLoopGroup //????????2NioEventLoopGroup //???boss?????worker??? //boss?worker??? //Channels??EventLoopGroup??? EventLoopGroup bossGroup = new NioEventLoopGroup(5); EventLoopGroup workerGroup = new NioEventLoopGroup(20); //ServerBootstrap ?NIO????Channel?? //????/* w ww.j a v a 2 s.c o m*/ ServerBootstrap b = new ServerBootstrap(); //NioServerSocketChannel?Channel? b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) //?????ChannelChannelInitializer? //?Channel?DiscardServerHandle?? //ChannelChannelPipeline??????? //?pipline?????? .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new TimeEncoder(), new DiscardServerHandler()); } }) //????TCP/IP??socket? //tcpNoDelaykeepAlive?ChannelOptionChannelConfig?? //ChannelOptions .option(ChannelOption.SO_BACKLOG, 128) //option()childOption()?option()??NioServerSocketChannel?? //childOption()???ServerChannel?NioServerSocketChannel .childOption(ChannelOption.SO_KEEPALIVE, true); try { //?????8080? //?bind()(???) ChannelFuture f = b.bind(port).sync(); f.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.dingwang.netty.server.PersonServer.java
License:Open Source License
public void run() { //NioEventLoopGroup ??I/O?Netty????EventLoopGroup //????????2NioEventLoopGroup //???boss?????worker??? //boss?worker??? //Channels??EventLoopGroup??? EventLoopGroup bossGroup = new NioEventLoopGroup(5); EventLoopGroup workerGroup = new NioEventLoopGroup(20); //ServerBootstrap ?NIO????Channel?? //????//from w w w .j a v a 2 s . c o m ServerBootstrap b = new ServerBootstrap(); //NioServerSocketChannel?Channel? b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) //?????ChannelChannelInitializer? //?Channel?DiscardServerHandle?? //ChannelChannelPipeline??????? //?pipline?????? .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new PersonDecoder(), new PersonEncoder(), new PersonOutHandler()); } }) //????TCP/IP??socket? //tcpNoDelaykeepAlive?ChannelOptionChannelConfig?? //ChannelOptions .option(ChannelOption.SO_BACKLOG, 128) //option()childOption()?option()??NioServerSocketChannel?? //childOption()???ServerChannel?NioServerSocketChannel .childOption(ChannelOption.SO_KEEPALIVE, true); try { //?????8080? //?bind()(???) ChannelFuture f = b.bind(port).sync(); f.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.dingwang.rpc.server.RpcServer.java
License:Open Source License
@Override public void afterPropertiesSet() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from w w w . j a v a 2 s . c o m ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel channel) throws Exception { channel.pipeline().addLast(new RpcDecoder(RpcRequest.class)) // RPC ?? .addLast(new RpcEncoder(RpcResponse.class)) // RPC ??? // .addLast(new ProviderProxy()); // ? RPC .addLast(new RpcHandler(handlerMap)); } }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); String[] array = serverAddress.split(":"); String host = array[0]; int port = Integer.parseInt(array[1]); ChannelFuture future = bootstrap.bind(host, port).sync(); LOGGER.debug("server started on port {}", port); if (serviceRegistry != null) { serviceRegistry.register(serverAddress); // ?? } future.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.dinstone.jrpc.transport.netty4.NettyAcceptance.java
License:Apache License
@Override public Acceptance bind() { bossGroup = new NioEventLoopGroup(1, new DefaultThreadFactory("N4A-Boss")); workGroup = new NioEventLoopGroup(transportConfig.getNioProcessorCount(), new DefaultThreadFactory("N4A-Work")); ServerBootstrap boot = new ServerBootstrap().group(bossGroup, workGroup); boot.channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() { @Override/*from w w w . ja va2 s . c om*/ public void initChannel(SocketChannel ch) throws Exception { TransportProtocolDecoder decoder = new TransportProtocolDecoder(); decoder.setMaxObjectSize(transportConfig.getMaxSize()); TransportProtocolEncoder encoder = new TransportProtocolEncoder(); encoder.setMaxObjectSize(transportConfig.getMaxSize()); ch.pipeline().addLast("TransportProtocolDecoder", decoder); ch.pipeline().addLast("TransportProtocolEncoder", encoder); int intervalSeconds = transportConfig.getHeartbeatIntervalSeconds(); ch.pipeline().addLast("IdleStateHandler", new IdleStateHandler(intervalSeconds * 2, 0, 0)); ch.pipeline().addLast("NettyServerHandler", new NettyServerHandler()); } }); boot.option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_BACKLOG, 128); boot.childOption(ChannelOption.SO_RCVBUF, 16 * 1024).childOption(ChannelOption.SO_SNDBUF, 16 * 1024) .childOption(ChannelOption.TCP_NODELAY, true); try { boot.bind(serviceAddress).sync(); int processorCount = transportConfig.getBusinessProcessorCount(); if (processorCount > 0) { NamedThreadFactory threadFactory = new NamedThreadFactory("N4A-BusinessProcessor"); executorService = Executors.newFixedThreadPool(processorCount, threadFactory); } } catch (Exception e) { throw new RuntimeException("can't bind service on " + serviceAddress, e); } LOG.info("netty acceptance bind on {}", serviceAddress); return this; }
From source file:com.dinstone.jrpc.transport.netty5.NettyAcceptance.java
License:Apache License
@Override public Acceptance bind() { bossGroup = new NioEventLoopGroup(1, new DefaultExecutorServiceFactory("N5A-Boss")); workGroup = new NioEventLoopGroup(transportConfig.getNioProcessorCount(), new DefaultExecutorServiceFactory("N5A-Work")); ServerBootstrap boot = new ServerBootstrap(); boot.group(bossGroup, workGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override/*w w w .j av a2 s . c o m*/ public void initChannel(SocketChannel ch) throws Exception { TransportProtocolDecoder decoder = new TransportProtocolDecoder(); decoder.setMaxObjectSize(transportConfig.getMaxSize()); TransportProtocolEncoder encoder = new TransportProtocolEncoder(); encoder.setMaxObjectSize(transportConfig.getMaxSize()); ch.pipeline().addLast("TransportProtocolDecoder", decoder); ch.pipeline().addLast("TransportProtocolEncoder", encoder); int intervalSeconds = transportConfig.getHeartbeatIntervalSeconds(); ch.pipeline().addLast("IdleStateHandler", new IdleStateHandler(intervalSeconds * 2, 0, 0)); ch.pipeline().addLast("NettyServerHandler", new NettyServerHandler()); } }); boot.option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_BACKLOG, 128); boot.childOption(ChannelOption.SO_RCVBUF, 16 * 1024).childOption(ChannelOption.SO_SNDBUF, 16 * 1024) .childOption(ChannelOption.TCP_NODELAY, true); try { boot.bind(serviceAddress).sync(); int processorCount = transportConfig.getBusinessProcessorCount(); if (processorCount > 0) { NamedThreadFactory threadFactory = new NamedThreadFactory("N5A-BusinssProcessor"); executorService = Executors.newFixedThreadPool(processorCount, threadFactory); } } catch (Exception e) { throw new RuntimeException("can't bind service on " + serviceAddress, e); } LOG.info("netty5 acceptance bind on {}", serviceAddress); return this; }
From source file:com.dinstone.netty.Server.java
License:Apache License
public static void main(String[] args) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_REUSEADDR, true).childHandler(new ChannelInitializer<NioSocketChannel>() { @Override/*from w ww . j a v a2s . c o m*/ protected void initChannel(NioSocketChannel ch) throws Exception { ch.pipeline().addLast(new SimpleServerHandler()); } }); b.bind(8090).sync().channel().closeFuture().sync(); }