netty学习笔记04——初识NIO

文章目录

  • netty学习笔记04——初识NIO
    • 介绍
    • NIO的时间服务器的简单demo

netty学习笔记04——初识NIO

介绍

  1. java提供了一系列改进的输入输出新特性,被统称为NIO,是同步非阻塞的
  2. NIO有三大核心部分:Channel,Buffer,Selector
  3. NIO是面向缓冲区,或者面向块编程的。数据读取到一个它稍后处理的缓冲区,需要时可在缓冲区中前后移动,这就增加了处理过程中的灵活性,使用它可以提供非阻塞式的高伸缩性网络。即先把数据写入buffer,然后从buffer读数据;
  4. Java NIO的非阻塞模式,使一个线程从某通道发送请求或者读取数据,但是它进能得到目前可用的数据,如果目前没有数据可用时,就什么都不会获取,而不是保持线程阻塞,所以直至数据变的可以读取之前,该线程可以继续做其他的事情,非阻塞写也是如果,一个线程请求写入一些数据到某通道,但不需要等待它完全写入,这个线程可以去做别的事情。
  5. NIO可以做到一个线程来处理多个操作的。假设有10000个请求过来,根据实际情况,可以分配50个或者100个线程来处理。不像之前的阻塞IO那样,非得分配10000个。
  6. HTTP2.0使用了多路复用技术,做到同一个连接并发处理多个请求,而且并发请求的数量比HTTP1.1大了好几个数量级

NIO的时间服务器的简单demo

下面是时间服务器的代码

NIOTimeServer:

public class NIOTimeServer {
	public static void main(String[] args){
        //绑定一个用来监听的端口
		int port = 8080;
		if(args!=null && args.length > 0 ){
			try{
				port = Integer.valueOf(args[0]);
			}catch(NumberFormatException e){
				port = 8080;
			}
		}
        //建立一个线程
		NIOMultiplexerTimeServer nioMultiplexerTimeServer = new NIOMultiplexerTimeServer(port);
		new Thread(nioMultiplexerTimeServer).start();
	}
}

NIOMultiplexerTimeServer:

public class NIOMultiplexerTimeServer implements Runnable {
    
	private Selector selector;
	private ServerSocketChannel serverSocketChannel;
	private volatile boolean stop;

	public NIOMultiplexerTimeServer(int port) {
		// TODO Auto-generated constructor stub
		try {
            //打开一个多路复用器
			this.selector = Selector.open();
            //打开一个通道
			this.serverSocketChannel = ServerSocketChannel.open();
            //把通道设置成为非阻塞IO
			this.serverSocketChannel.configureBlocking(false);
            //给通道绑定端口
			this.serverSocketChannel.bind(new InetSocketAddress(port), 1024);
            //把通道的向对应的多路复用器注册,并且注册对应的事件
			this.serverSocketChannel.register(this.selector, SelectionKey.OP_ACCEPT);
			System.out.println("The time server is start in port: " + port);
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
			System.exit(1);
		}
	}

	public void stop() {
		this.stop = true;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (!stop) {//不断轮询多路复用器
			try {
				this.selector.select(1000);
                //获取事件
				Set<SelectionKey> selectionKeys = selector.selectedKeys();
				//迭代事件
                Iterator<SelectionKey> it = selectionKeys.iterator();
				SelectionKey key = null;
				while (it.hasNext()) {
					key = it.next();
					it.remove();
					try {
                        //处理事件
						handleInput(key);
					} catch (Exception e) {
						// TODO: handle exception
						if (key != null) {
							key.cancel();
							if (key.channel() != null)
								key.channel().close();
						}
					}
				}
			} catch (Throwable t) {
				t.printStackTrace();
			}
		}
	}

	private void handleInput(SelectionKey key) throws IOException {
		if (key.isValid()) {
            //判断事件类型
			if (key.isAcceptable()) {
                //获取事件对应的ServerSocketChannel
				ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
				//利用ServerSocketChannel创建SocketChannel
                SocketChannel sc = ssc.accept();
                //设置成为非阻塞模式
				sc.configureBlocking(false);
                //将SocketChannel注册进多路复用器
				sc.register(this.selector, SelectionKey.OP_READ);
			}
			if (key.isReadable()) {
                //如果是可读事件
				SocketChannel socketChannel = (SocketChannel) key.channel();
				//分配一个ByteBuffer
                ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                //把读取内容写入ByteBuffer
				int readBytes = socketChannel.read(byteBuffer);
				if (readBytes > 0) {
                    //读写切换
					byteBuffer.flip();
                    //建立读取缓存区
					byte[] bytes = new byte[byteBuffer.remaining()];
                    //读取信息
					byteBuffer.get(bytes);
					String body = new String(bytes, "UTF-8");
					System.out.println("The Time server receive order: " + body);
					String currentTime = "QUERY TIME ORDER".equals(body)
							? new Date(System.currentTimeMillis()).toString() : "BAD ORDER";// 判断输入是否合法
                    //写入响应信息
					doWrite(socketChannel, currentTime);
				} else if (readBytes < 0) {
					key.cancel();
					socketChannel.close();
				} else {
					;
				}
			}
		}
	}

	private void doWrite(SocketChannel socketChannel, String response) throws IOException {
		if (response != null && response.trim().length() > 0) {
			byte[] bytes = response.getBytes();
			ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length);
			byteBuffer.put(bytes);
			byteBuffer.flip();
			socketChannel.write(byteBuffer);
		}
	}
}

NIOTimeClient:

public class NIOTimeClient {
	public static void main(String[] args){
		int port = 8080;
		if(args!=null && args.length > 0 ){
			try{
				port = Integer.valueOf(args[0]);
			}catch(NumberFormatException e){
				port = 8080;
			}
		}
		new Thread(new NIOTimeClientHandler("127.0.0.1", port), "TimeClient-001")
		.start();
	}
}

NIOTimeClientHandler:

public class NIOTimeClientHandler implements Runnable {
	private String host;
	private int port;
	private Selector selector;
	private SocketChannel socketChannel;
	private volatile boolean stop;
	
	public NIOTimeClientHandler(String host, int port) {
		// TODO Auto-generated constructor stub
		this.host = host==null? "127.0.0.1":host;
		this.port = port;
		try{
			selector = Selector.open();
			socketChannel = SocketChannel.open();
			socketChannel.configureBlocking(false);
		}catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
			System.exit(1);
		}
	}
	
    @Override
    public void run() {
		try {
			doConnect();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
	while (!stop) {
	    try {
		selector.select(1000);
		Set<SelectionKey> selectedKeys = selector.selectedKeys();
		Iterator<SelectionKey> it = selectedKeys.iterator();
		SelectionKey key = null;
		while (it.hasNext()) {
		    key = it.next();
		    it.remove();
		    try {
			handleInput(key);
		    } catch (Exception e) {
			if (key != null) {
			    key.cancel();
			    if (key.channel() != null)
				key.channel().close();
			}
		    }
		}
	    } catch (Exception e) {
		e.printStackTrace();
		System.exit(1);
	    }
	}

	// 多路复用器关闭后,所有注册在上面的Channel和Pipe等资源都会被自动去注册并关闭,所以不需要重复释放资源
	if (selector != null)
	    try {
		selector.close();
	    } catch (IOException e) {
		e.printStackTrace();
	    }

    }

    private void handleInput(SelectionKey key) throws IOException {

	if (key.isValid()) {
	    // 判断是否连接成功
	    SocketChannel sc = (SocketChannel) key.channel();
	    if (key.isConnectable()) {
		if (sc.finishConnect()) {
		    sc.register(selector, SelectionKey.OP_READ);
		    doWrite(sc);
		} else
		    System.exit(1);// 连接失败,进程退出
	    }
	    if (key.isReadable()) {
		ByteBuffer readBuffer = ByteBuffer.allocate(1024);
		int readBytes = sc.read(readBuffer);
		if (readBytes > 0) {
		    readBuffer.flip();
		    byte[] bytes = new byte[readBuffer.remaining()];
		    readBuffer.get(bytes);
		    String body = new String(bytes, "UTF-8");
		    System.out.println("Now is : " + body);
		    this.stop = true;
		} else if (readBytes < 0) {
		    // 对端链路关闭
		    key.cancel();
		    sc.close();
		} else
		    ; // 读到0字节,忽略
	    }
	}

    }

    private void doConnect() throws IOException {
	// 如果直接连接成功,则注册到多路复用器上,发送请求消息,读应答
	if (socketChannel.connect(new InetSocketAddress(host, port))) {
	    socketChannel.register(selector, SelectionKey.OP_READ);
	    doWrite(socketChannel);
	} else
	    socketChannel.register(selector, SelectionKey.OP_CONNECT);
    }

    private void doWrite(SocketChannel sc) throws IOException {
	byte[] req = "QUERY TIME ORDER".getBytes();
	ByteBuffer writeBuffer = ByteBuffer.allocate(req.length);
	writeBuffer.put(req);
	writeBuffer.flip();
	sc.write(writeBuffer);
	if (!writeBuffer.hasRemaining())
	    System.out.println("Send order to server succeed.");
    }
	
}

你可能感兴趣的:(java学习,netty学习)