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();
            }
        }
    }
     
  • 相关阅读:
    线性筛素数
    m个苹果放入n个盘子问题
    幸运的袋子
    [HNOI2013]消毒
    [SDOI2016]数字配对
    [SCOI2015]小凸玩矩阵
    [JLOI2008]将军
    [HEOI2016/TJOI2016]游戏
    [洛谷4329/COCI2006-2007#1] Bond
    [BZOJ1324]Exca王者之剑
  • 原文地址:https://www.cnblogs.com/fubinhnust/p/11940249.html
Copyright © 2011-2022 走看看