03-netty基础-NIO编程

1.NIO简称:

有人称之为New I/O,因为相对于之前的I/O是新增的。这是官方叫法。但是,更多的人喜欢称之为非阻塞I/O(Non-block I/O)。

2.与Socket类和ServerSocket类相对应,NIO提供了SocketChannel和ServerSocketChannel两种不同套接字通道实现。支持阻塞和非阻塞两种方式,阻塞模式使用简单,但是性能和可靠性都不好,非阻塞模式正好相反,开发人员可以根据需求进行开发。

3.缓冲区 Buffer概念:

NIO类库中加入了Buffer对象,在面向流的I/O中,可以将数据直接写入或者将数据直接读到Stream对象中。在NIO类库中,所有数据都是用缓冲区处理,在读取数据时,它是直接读到缓冲区中的,在写入数据时,写入到缓冲区中。缓冲区实质上是一个数组。最常用的是ByteBuffer缓冲区。

4.通道Channel概念:

Channel是一个通道,就像自来水管一样,网络数据通过Channel进行读取和写入,通道与流的不同之处在于通道是双向的,流只在一个方向移动,而通道可以用于读写,或者两者同时进行。

5.多路复用器 Selector概念:

多路复用器Selector 是Java NIO 编程的基础,多路复用器提供选择已经就绪的任务的能力。Selector 会不断轮询注册在其上的Channel,如果某个Channel上发生读或者写事件,这个Channel就处于就绪状态,会被Selector 轮询出来,然后通过SelectionKey获取就绪的Channel集合,然后进行后续的IO操作。

一个多路复用器Selector可以同时轮询多个Channel,由于 JDK使用了epoll() 代替传统的select实现,所以它并没有最大连接句柄1024/2048的限制。这也意味着只需要一个线程的负责Selector的轮询,就可以接入成千上万的客户端,这是个非常巨大的进步。

 

6.NIO创建TimeServer.java源码:

package com.pats.file.nio;

public class TimeServer {
 
	public static void main(String[] args) {
	int port = 8080;
	if(args != null&& args.length > 0 ) {
     port = Integer.valueOf(args[0]);		
	}
	MultiplexerTimeServer timeServer = new MultiplexerTimeServer(port);

	new Thread(timeServer,"NIO-MultiplexerTimeServer-001").start();
}
}

MultiplexerTimeServer.java

package com.pats.file.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;

public class MultiplexerTimeServer implements Runnable{

	private Selector selector;
	private ServerSocketChannel serverSocketChannel;
	private volatile boolean stop;

	/** 
	  * 初始化多路复用器,绑定监听端口;
	 */
	public MultiplexerTimeServer(int port){
		try {
			selector = Selector.open();
			serverSocketChannel = ServerSocketChannel.open();
			serverSocketChannel.configureBlocking(false);
			serverSocketChannel.socket().bind(new InetSocketAddress(port),1024);
			serverSocketChannel.register(selector,SelectionKey.OP_ACCEPT);
			System.out.println("The time server is start in port : " + port);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void stop() {
	this.stop = true;	
	}
	@Override
	public void run() {
	 while(!stop){
    	try {
		selector.select(1000);
		Set selectedKeys = selector.selectedKeys();
		Iterator iterator = selectedKeys.iterator();
		SelectionKey key = null;
		while(iterator.hasNext()) {
		key = iterator.next();
		iterator.remove();
		try {
		handleInput(key);	
		} catch (Exception e) {
		 if(key != null) {
			 key.cancel();
			 if(key.channel() != null) {
				 key.channel().close(); 
			 }
		 }
		}
        	
		
		}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		}
	 //:多路复用器关闭后所有注册在上面的Channel和Pipe等资源都会被自动去注册并关闭,所以不需要重复释放资源
	 if( selector != null) {
		 try {
			selector.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }
	 
	 
	}

	private void handleInput(SelectionKey key) throws IOException {
        if(key.isValid()) {
        //:处理新接入的请求消息	
           if(key.isAcceptable()) {
        	//accept the new connection
           ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
           SocketChannel sc = ssc.accept();
           sc.configureBlocking(false);
           //add the new cinnection to the selector
           sc.register(selector, SelectionKey.OP_READ);
           }
           if(key.isReadable()) {
           //read to data
        	SocketChannel sc = (SocketChannel) key.channel();
        	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("The time server receive order : "+ body);
        		String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body)? new Date(System.currentTimeMillis()).toString() : "BAD ORDER";
        		doWrite(sc,currentTime);
        	}else if(readBytes < 0) {
        	//:对端链路关闭
        	key.cancel();
        	sc.close();
        	} else {
        		;//:读到0字节 忽略
        	}
        	
           }
        }
		
	}

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

}

由于SocketChannel 是异步非阻塞的,并不能够保证一次性将需要发送的字节数组全部发送完毕,此时会出现写半包问题。我们需要注册写操作,不断轮询Selector将没有发送完的ByteBuffer发送完毕,可以通过ByteBuffer的hasRemain()方法判断消息是否发送完成。此处未演示如何处理写半包问题。

7. NIO创建TimeClient.java 源码:

package com.pats.file.nio;


public class TimeClient {

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

TimeClientHandle.java

package com.pats.file.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

public class TimeClientHandle implements Runnable{

	private String host;
	private int port;
	private Selector selector;
	private SocketChannel socketChannel;
	private volatile boolean stop;
	
	public TimeClientHandle(String host, int port) {
    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 Auto-generated catch block
		e.printStackTrace();
		System.exit(1);
	}
   
	}
	
	@Override
	public void run() {

		try {
			doConnect();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		while(!stop) {
		try {
		selector.select(1000);
		Set selectedKeys = selector.selectedKeys();
		Iterator iterator = selectedKeys.iterator();
		SelectionKey key = null;
		
		while(iterator.hasNext()) {
			key = iterator.next();
			iterator.remove();
			try {
			handleInput(key);	
			} catch (Exception e) {
			 if(key != null) {
				 key.cancel();
				 if(key.channel() != null) {
					 key.channel().close(); 
				 }
			 }
			}
	        	
			
			}
		
		} catch (Exception e) {
			System.exit(1);
		}	
		}
		
		 //:多路复用器关闭后所有注册在上面的Channel和Pipe等资源都会被自动去注册并关闭,所以不需要重复释放资源
		 if( selector != null) {
			 try {
				selector.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				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 time 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 2 server succeed.");  
      }
		
	}

	

	
}

8.运行代码如下:

服务端:

03-netty基础-NIO编程_第1张图片

客户端:

03-netty基础-NIO编程_第2张图片

 9.总结:

1.NIO编码比同步阻塞BIO编码难度大很多,以上并没有考虑半包读,半包写的问题。

2.客户端发起的连接操作是异步的,可以通过在多路复用器注册 OP_CONNECT 等待后续结果,不需要像之前的客户端那样被同步阻塞。

3.SocketChannel 的读写操作都是异步的,如果没有可读可写的数据它不会同步等待,直接返回,这样I/O通信线程就可以处理其它的链路,不需要同步等待这个链路可用。

4.线程模型的优化,JDK的Selector 在linux操作系统上通过epoll实现,没有连接句柄数限制,只受限于操作系统的最大句柄数或者对单个线程的句柄数限制,这意味着一个Selector 可以同时处理成千上万个客户端连接,而且性能不会随着客户端的增加而线性下降。

 

 

 

你可能感兴趣的:(netty,NIO编程,netty)