zoukankan      html  css  js  c++  java
  • Netty Java原生和Netty网络的API比较

     

    Java原生阻塞IO

    public class PlainOioServer {
        public void serve(int port) throws IOException {
            final ServerSocket socket = new ServerSocket(port);
            try {
                for(;;) {
                    final Socket clientSocket = socket.accept();
                    System.out.println(
                            "Accepted connection from " + clientSocket);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            OutputStream out;
                            try {
                                out = clientSocket.getOutputStream();
                                out.write("Hi!
    ".getBytes(
                                        Charset.forName("UTF-8")));
                                out.flush();
                                clientSocket.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            } finally {
                                try {
                                    clientSocket.close();
                                } catch (IOException ex) {
                                    // ignore on close
                                }
                            }
                        }
                    }).start();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    Java原生NIO

    nio:选择并处理状态的变化

    public class PlainNioServer {
        public void serve(int port) throws IOException {
            ServerSocketChannel serverChannel = ServerSocketChannel.open();
            serverChannel.configureBlocking(false);
            ServerSocket ss = serverChannel.socket();
            InetSocketAddress address = new InetSocketAddress(port);
            ss.bind(address);
            Selector selector = Selector.open();
            serverChannel.register(selector, SelectionKey.OP_ACCEPT);
            final ByteBuffer msg = ByteBuffer.wrap("Hi!
    ".getBytes());
            for (;;){
                try {
                    selector.select();
                } catch (IOException ex) {
                    ex.printStackTrace();
                    //handle exception
                    break;
                }
                Set<SelectionKey> readyKeys = selector.selectedKeys();
                Iterator<SelectionKey> iterator = readyKeys.iterator();
                while (iterator.hasNext()) {
                    SelectionKey key = iterator.next();
                    iterator.remove();
                    try {
                        if (key.isAcceptable()) {
                            ServerSocketChannel server =
                                    (ServerSocketChannel) key.channel();
                            SocketChannel client = server.accept();
                            client.configureBlocking(false);
                            client.register(selector, SelectionKey.OP_WRITE |
                                    SelectionKey.OP_READ, msg.duplicate());
                            System.out.println(
                                    "Accepted connection from " + client);
                        }
                        if (key.isWritable()) {
                            SocketChannel client =
                                    (SocketChannel) key.channel();
                            ByteBuffer buffer =
                                    (ByteBuffer) key.attachment();
                            while (buffer.hasRemaining()) {
                                if (client.write(buffer) == 0) {
                                    break;
                                }
                            }
                            client.close();
                        }
                    } catch (IOException ex) {
                        key.cancel();
                        try {
                            key.channel().close();
                        } catch (IOException cex) {
                            // ignore on close
                        }
                    }
                }
            }
        }
    }

    只有NIO和Epoll支持零拷贝

    • 针对于Linux,自Linux内核版本 2.5.44后,引入epoll——一个高度可扩展的I/O事件通知特性,提供了比旧的POSIX select和poll系统调用更好的性能,Linux JDK NIO API使用了这些epoll调用

    用法简单,只需要将NioEventLoopGroup替换为EpollEventGroup,并且把NioServerSocketChannel.class替换为EpollServerSocketChannel.class即可。

    String os = System.getProperty("os.name");  
    if(os.toLowerCase().startsWith("win")){  
      System.out.println(os + " can't gunzip");  
    }

    Netty实现阻塞IO

    OIO的处理逻辑

    public class NettyOioServer {
        public void server(int port)
                throws Exception {
            final ByteBuf buf =
                    Unpooled.unreleasableBuffer(Unpooled.copiedBuffer("Hi!
    ", Charset.forName("UTF-8")));
            EventLoopGroup group = new OioEventLoopGroup();
            try {
                ServerBootstrap b = new ServerBootstrap();
                b.group(group)
                        .channel(OioServerSocketChannel.class)
                        .localAddress(new InetSocketAddress(port))
                        .childHandler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            public void initChannel(SocketChannel ch)
                                    throws Exception {
                                    ch.pipeline().addLast(
                                        new ChannelInboundHandlerAdapter() {
                                            @Override
                                            public void channelActive(
                                                    ChannelHandlerContext ctx)
                                                    throws Exception {
                                                ctx.writeAndFlush(buf.duplicate())
                                                        .addListener(
                                                                ChannelFutureListener.CLOSE);
                                            }
                                        });
                            }
                        });
                ChannelFuture f = b.bind().sync();
                f.channel().closeFuture().sync();
            } finally {
                group.shutdownGracefully().sync();
            }
        }
    }

    Netty实现NIO

    public class NettyNioServer {
        public void server(int port) throws Exception {
            final ByteBuf buf =
                    Unpooled.unreleasableBuffer(Unpooled.copiedBuffer("Hi!
    ",
                            Charset.forName("UTF-8")));
            NioEventLoopGroup group = new NioEventLoopGroup();
            try {
                ServerBootstrap b = new ServerBootstrap();
                b.group(group).channel(NioServerSocketChannel.class)
                        .localAddress(new InetSocketAddress(port))
                        .childHandler(new ChannelInitializer<SocketChannel>() {
                                          @Override
                                          public void initChannel(SocketChannel ch)
                                                  throws Exception {
                                                  ch.pipeline().addLast(
                                                      new ChannelInboundHandlerAdapter() {
                                                          @Override
                                                          public void channelActive(
                                                                  ChannelHandlerContext ctx) throws Exception {
                                                                    ctx.writeAndFlush(buf.duplicate())
                                                                      .addListener(
                                                                              ChannelFutureListener.CLOSE);
                                                          }
                                                      });
                                          }
                                      }
                        );
                ChannelFuture f = b.bind().sync();
                f.channel().closeFuture().sync();
            } finally {
                group.shutdownGracefully().sync();
            }
        }
    }
     
  • 相关阅读:
    C# 从服务器下载文件
    不能使用联机NuGet 程序包
    NPOI之Excel——合并单元格、设置样式、输入公式
    jquery hover事件中 fadeIn和fadeOut 效果不能及时停止
    UVA 10519 !! Really Strange !!
    UVA 10359 Tiling
    UVA 10940 Throwing cards away II
    UVA 10079 Pizze Cutting
    UVA 763 Fibinary Numbers
    UVA 10229 Modular Fibonacci
  • 原文地址:https://www.cnblogs.com/fubinhnust/p/11940249.html
Copyright © 2011-2022 走看看