List of usage examples for io.netty.bootstrap ServerBootstrap group
public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup)
From source file:com.vip.netty.basic.TimeServer.java
License:Apache License
public void bind(int port) throws Exception { // ??NIO// ww w . j av a 2s . c o m EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChildChannelHandler()); // ??? ChannelFuture f = b.bind(port).sync(); // ??? f.channel().closeFuture().sync(); } finally { // ? bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.vip.netty.protocol.file.FileServer.java
License:Apache License
public void run(int port) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from www .ja v a 2 s. c o m ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).childHandler(new ChannelInitializer<SocketChannel>() { /* * (non-Javadoc) * * @see * io.netty.channel.ChannelInitializer#initChannel(io * .netty.channel.Channel) */ public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8), new LineBasedFrameDecoder(1024), new StringDecoder(CharsetUtil.UTF_8), new FileServerHandler()); } }); ChannelFuture f = b.bind(port).sync(); System.out.println("Start file server at port : " + port); f.channel().closeFuture().sync(); } finally { // ? bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.vip.netty.protocol.http.fileServer.HttpFileServer.java
License:Apache License
public void run(final int port, final String url) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//w ww .jav a 2 s.c om ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("http-decoder", new HttpRequestDecoder()); ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536)); ch.pipeline().addLast("http-encoder", new HttpResponseEncoder()); ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler()); ch.pipeline().addLast("fileServerHandler", new HttpFileServerHandler(url)); } }); ChannelFuture future = b.bind("192.168.0.106", port).sync(); System.out.println( "HTTP??? : " + "http://192.168.0.106:" + port + url); future.channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.vmware.dcp.common.http.netty.NettyHttpListener.java
License:Open Source License
public void start(int port, String bindAddress) throws Throwable { ExecutorServiceFactory f = (tc) -> { return Executors.newFixedThreadPool(EVENT_LOOP_THREAD_COUNT, r -> new Thread(r, this.host.getUri().toString() + "/netty-listener/" + this.host.getId())); };/*from ww w . j a v a 2 s.c o m*/ this.parentGroup = new NioEventLoopGroup(EVENT_LOOP_THREAD_COUNT, f); this.childGroup = new NioEventLoopGroup(EVENT_LOOP_THREAD_COUNT, f); if (this.childChannelHandler == null) { this.childChannelHandler = new NettyHttpServerInitializer(this.host, this.sslContext); } ServerBootstrap b = new ServerBootstrap(); b.group(this.parentGroup, this.childGroup).channel(NioServerSocketChannel.class) .childHandler(this.childChannelHandler); InetSocketAddress addr = null; if (bindAddress != null) { addr = new InetSocketAddress(bindAddress, port); } else { this.host.log(Level.WARNING, "*** Binding to all interfaces, please supply a bindAddress instead ***"); addr = new InetSocketAddress(port); } this.serverChannel = b.bind(addr).sync().channel(); this.serverChannel.config().setOption(ChannelOption.SO_LINGER, 0); this.port = ((InetSocketAddress) this.serverChannel.localAddress()).getPort(); }
From source file:com.weibo.api.motan.transport.netty4.http.Netty4HttpServer.java
License:Apache License
@Override public boolean open() { if (isAvailable()) { return true; }//from w w w.ja v a 2 s. co m if (channel != null) { channel.close(); } if (bossGroup == null) { bossGroup = new NioEventLoopGroup(); workerGroup = new NioEventLoopGroup(); } boolean shareChannel = url.getBooleanParameter(URLParamType.shareChannel.getName(), URLParamType.shareChannel.getBooleanValue()); // TODO max connection protect int maxServerConnection = url.getIntParameter(URLParamType.maxServerConnection.getName(), URLParamType.maxServerConnection.getIntValue()); int workerQueueSize = url.getIntParameter(URLParamType.workerQueueSize.getName(), 500); int minWorkerThread = 0, maxWorkerThread = 0; if (shareChannel) { minWorkerThread = url.getIntParameter(URLParamType.minWorkerThread.getName(), MotanConstants.NETTY_SHARECHANNEL_MIN_WORKDER); maxWorkerThread = url.getIntParameter(URLParamType.maxWorkerThread.getName(), MotanConstants.NETTY_SHARECHANNEL_MAX_WORKDER); } else { minWorkerThread = url.getIntParameter(URLParamType.minWorkerThread.getName(), MotanConstants.NETTY_NOT_SHARECHANNEL_MIN_WORKDER); maxWorkerThread = url.getIntParameter(URLParamType.maxWorkerThread.getName(), MotanConstants.NETTY_NOT_SHARECHANNEL_MAX_WORKDER); } final int maxContentLength = url.getIntParameter(URLParamType.maxContentLength.getName(), URLParamType.maxContentLength.getIntValue()); final NettyHttpRequestHandler handler = new NettyHttpRequestHandler(this, messageHandler, new ThreadPoolExecutor(minWorkerThread, maxWorkerThread, 15, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(workerQueueSize))); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("http-decoder", new HttpRequestDecoder()); ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(maxContentLength)); ch.pipeline().addLast("http-encoder", new HttpResponseEncoder()); ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler()); ch.pipeline().addLast("serverHandler", handler); } }).option(ChannelOption.SO_BACKLOG, 1024).childOption(ChannelOption.SO_KEEPALIVE, false); ChannelFuture f; try { f = b.bind(url.getPort()).sync(); channel = f.channel(); } catch (InterruptedException e) { LoggerUtil.error("init http server fail.", e); return false; } state = ChannelState.ALIVE; StatsUtil.registryStatisticCallback(this); LoggerUtil.info("Netty4HttpServer ServerChannel finish Open: url=" + url); return true; }
From source file:com.weibo.yar.yarserver.NettyYarServer.java
License:Apache License
public void start(int port) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//w ww. ja v a 2 s . c om ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("http-decoder", new HttpRequestDecoder()); ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536)); ch.pipeline().addLast("http-encoder", new HttpResponseEncoder()); ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler()); ch.pipeline().addLast("serverHandler", new HttpServerHandler()); } }).option(ChannelOption.SO_BACKLOG, 1024).childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture f = b.bind(port).sync(); f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.whizzosoftware.hobson.hub.websockets.WebSocketsPlugin.java
License:Open Source License
@Override public void onStartup(PropertyContainer config) { bossGroup = new NioEventLoopGroup(1); workerGroup = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new WebSocketServerInitializer(clientChannels, getAccessManager())); b.bind(PORT).addListener(new GenericFutureListener<ChannelFuture>() { @Override//from w w w. ja v a 2 s . co m public void operationComplete(ChannelFuture future) throws Exception { WebSocketsPlugin.this.channel = future.channel(); logger.debug("WebSocket server started at port {}", PORT); getHubManager().getLocalManager().setWebSocketInfo("ws", PORT, null); } }); }
From source file:com.wuma.file.FileServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. // final SslContext sslCtx; // if (SSL) { // SelfSignedCertificate ssc = new SelfSignedCertificate(); // sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); // } else { // sslCtx = null; // }/* w ww .j a v a 2s . c om*/ // 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).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 StringEncoder(CharsetUtil.UTF_8), new LineBasedFrameDecoder(8192), new StringDecoder(CharsetUtil.UTF_8), new ChunkedWriteHandler(), new FileServerHandler()); } }); // 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.wx3.galacdecks.networking.NettyWebSocketServer.java
License:Open Source License
public void start() { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from w w w. j a v a 2 s. c om ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new WebSocketServerInitializer(this, null)); Channel ch = b.bind(port).sync().channel(); // This blocks until the channel is closed: ch.closeFuture().sync(); } catch (InterruptedException e) { // What do we do with an interupted exception? e.printStackTrace(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.xiovr.unibot.utils.EchoServer.java
License:Open Source License
public void startServer() { if (bStarted) { return;/*from www . ja va 2 s. c o m*/ } // Configure the server. bossGroup = new NioEventLoopGroup(1); 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(); p.addLast(new EchoServerHandler()); } }); // Start the server. cf = b.bind(addr).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { // System.out.println("Echo server started"); EchoServer.this.bStarted = true; } }); } catch (Exception e) { e.printStackTrace(); } }