天天看點

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<SelectionKey> selectedKeys = selector.selectedKeys();
		Iterator<SelectionKey> 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<SelectionKey> selectedKeys = selector.selectedKeys();
		Iterator<SelectionKey> 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程式設計

用戶端:

03-netty基礎-NIO程式設計

 9.總結:

1.NIO編碼比同步阻塞BIO編碼難度大很多,以上并沒有考慮半包讀,半包寫的問題。

2.用戶端發起的連接配接操作是異步的,可以通過在多路複用器注冊 OP_CONNECT 等待後續結果,不需要像之前的用戶端那樣被同步阻塞。

3.SocketChannel 的讀寫操作都是異步的,如果沒有可讀可寫的資料它不會同步等待,直接傳回,這樣I/O通信線程就可以處理其它的鍊路,不需要同步等待這個鍊路可用。

4.線程模型的優化,JDK的Selector 在linux作業系統上通過epoll實作,沒有連接配接句柄數限制,隻受限于作業系統的最大句柄數或者對單個線程的句柄數限制,這意味着一個Selector 可以同時處理成千上萬個用戶端連接配接,而且性能不會随着用戶端的增加而線性下降。