List of usage examples for io.netty.channel EventLoopGroup shutdownGracefully
Future<?> shutdownGracefully();
From source file:com.im.socket.netty.http.HttpServer.java
License:Apache License
public boolean start() { if (start) {/*from ww w. ja va 2 s . co m*/ return start; } EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { start = true; server.group(bossGroup, workerGroup); server.channel(NioServerSocketChannel.class); server.handler(new LoggingHandler(LogLevel.WARN)); server.childHandler(new HttpServerInitializer(dispatcherServlet)); Channel ch = server.bind(port).sync().channel(); ch.closeFuture().sync(); } catch (InterruptedException ex) { start = false; logger.error("HttpServer?", ex); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } return start; }
From source file:com.im.socket.netty.tcp.SocketServer.java
License:Apache License
public boolean start() { if (start) {//from ww w.ja va2 s .c o m return start; } EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { start = true; server.group(bossGroup, workerGroup); server.channel(NioServerSocketChannel.class); server.handler(new LoggingHandler(LogLevel.INFO)); server.childHandler(new SocketServerInitializer()); Channel ch = server.bind(port).sync().channel(); ch.closeFuture().sync(); } catch (InterruptedException ex) { start = false; logger.error("WebSocketServer?", ex); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); System.out.println("WebSocketServer:-----shutdown---------"); } return start; }
From source file:com.im.socket.netty.web.WebSocketServer.java
License:Apache License
public boolean start() { if (start) {//w ww . jav a2s .c om return start; } EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { start = true; server.group(bossGroup, workerGroup); server.channel(NioServerSocketChannel.class); server.handler(new LoggingHandler(LogLevel.WARN)); server.childHandler(new WebSocketServerInitializer()); Channel ch = server.bind(port).sync().channel(); ch.closeFuture().sync(); } catch (InterruptedException ex) { start = false; logger.error("WebSocketServer?", ex); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } return start; }
From source file:com.im.test.WebSocketServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w w w . ja v a 2 s.c om*/ ServerBootstrap server = new ServerBootstrap(); server.group(bossGroup, workerGroup); server.channel(NioServerSocketChannel.class); server.handler(new LoggingHandler(LogLevel.INFO)); server.childHandler(new WebSocketServerInitializer()); Channel ch = server.bind(PORT).sync().channel(); System.out.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.jansegre.jwar.webapi.ApiServer.java
License:Open Source License
@Override public void start() { // Reference: http://netty.io/wiki/user-guide-for-4.x.html EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from w w w .j a v a2s. com ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // Add the text line codec combination first, pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); // the encoder and decoder are static as these are sharable pipeline.addLast("decoder", new StringDecoder()); pipeline.addLast("encoder", new StringEncoder()); // and then business logic. pipeline.addLast("handler", apiSocket); } }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); // Bind and start to accept incoming connections. ChannelFuture f = b.bind(apiPort).syncUninterruptibly(); log.info("ApiSocket started at port: {}", apiPort); // Also start parent super.start(); // Wait until the server socket is closed. f.channel().closeFuture().syncUninterruptibly(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.jason.netty.file.FileServer.java
License:Apache License
public void run(int port) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*www. j a 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("Server start at port : " + port); f.channel().closeFuture().sync(); } finally { // ? bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.jjzhk.Chapter10.xml.HttpXmlClient.java
License:Apache License
public void connect(int port) throws Exception { // ?NIO?/*ww w. j av a2 s. c o m*/ EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("http-decoder", new HttpResponseDecoder()); ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536)); // XML?? ch.pipeline().addLast("xml-decoder", new HttpXmlResponseDecoder(Order.class, true)); ch.pipeline().addLast("http-encoder", new HttpRequestEncoder()); ch.pipeline().addLast("xml-encoder", new HttpXmlRequestEncoder()); ch.pipeline().addLast("xmlClientHandler", new HttpXmlClientHandle()); } }); // ?? ChannelFuture f = b.connect(new InetSocketAddress(port)).sync(); // ? f.channel().closeFuture().sync(); } finally { // ?NIO? group.shutdownGracefully(); } }
From source file:com.jjzhk.Chapter10.xml.HttpXmlServer.java
License:Apache License
public void run(final int port) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/* w ww. j a v a 2 s. c o m*/ 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("xml-decoder", new HttpXmlRequestDecoder(Order.class, true)); ch.pipeline().addLast("http-encoder", new HttpResponseEncoder()); ch.pipeline().addLast("xml-encoder", new HttpXmlResponseEncoder()); ch.pipeline().addLast("xmlServerHandler", new HttpXmlServerHandler()); } }); ChannelFuture future = b.bind(new InetSocketAddress(port)).sync(); System.out.println("HTTP???? : " + "http://localhost:" + port); future.channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.jjzhk.Chapter11.websocket.WebSocketServer.java
License:Apache License
public void run(int port) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//ww w . j av a 2s .c o m ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("http-codec", new HttpServerCodec()); pipeline.addLast("aggregator", new HttpObjectAggregator(65536)); ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler()); pipeline.addLast("handler", new WebSocketServerHandler()); } }); Channel ch = b.bind(port).sync().channel(); System.out.println("Web socket server started at port " + port + '.'); System.out.println("Open your browser and navigate to http://localhost:" + port + '/'); ch.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.jjzhk.Chapter12.udp.ChineseProverbClient.java
License:Apache License
public void run(int port) throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try {// w w w . j ava 2s . c o m Bootstrap b = new Bootstrap(); b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true) .handler(new ChineseProverbClientHandler()); Channel ch = b.bind(0).sync().channel(); // ???????DP ch.writeAndFlush( new DatagramPacket(Unpooled.copiedBuffer("???", CharsetUtil.UTF_8), new InetSocketAddress("255.255.255.255", port))) .sync(); if (!ch.closeFuture().await(15000)) { System.out.println("?!"); } } finally { group.shutdownGracefully(); } }