各I/O模型優缺點
-
BIO通信模型
BIO主要的問題在于每當有一個新的客戶端請求接入時,服務端必須創建一個新的線程處理新接入的客戶端鏈路,一個線程只能處理一個客戶端連接
-
線程池I/O編程
假如所有可用線程都被阻塞,后續I/O都將在隊列中排隊
線程池采用阻塞隊列實現,隊列積滿之后,后續入隊列操作將被阻塞,新的客戶端請求被拒絕,發生大量連接超時 -
NIO編程
-
緩沖區Buffer
每一種Java基本類型都有對一種緩沖區
大多數標準I/O使用ByteBuffer -
通道Channel
Channel分為兩大類:用于網絡讀寫的SelectableChannel和用于文件操作的FileChannel
-
多路復用器Selector
多路復用器提供選擇已經就緒的任務的能力
-
-
NIO2.0 AIO
異步套接字通道不需要通過多路復用器(Selector)對注冊的通道進行輪詢操作即可實現異步讀寫
NIO實例分析
-
NIO服務端序列
- 步驟一:打開ServerSocketChannel,用于監聽客戶端的連接
- 步驟二:綁定監聽端口,設置連接為非阻塞模式
- 步驟三:創建Reactor線程,創建多路復用器并啟動線程
- 步驟四:將ServerSocketChannel注冊到Reactor線程的多路復用器Selector上,監聽ACCEPT事件
- 步驟五:多路復用器在線程run方法的無限循環體內輪休準備就緒的Key
- 步驟六:多路復用器監聽到有新的客戶端接入,處理新的計入請求,完成TCP三次握手,建立物理鏈路
- 步驟七:設置客戶端鏈路為非阻塞模式
- 步驟八:將新接入的客戶端連接注冊到Reactor線程的多路復用器上,監聽讀操作
- 步驟九:異步讀取客戶端請求消息到緩沖區
- 步驟十:對ByteBuffer進行編解碼,如果有半包消息指針reset,繼續讀取后續的報文,將解碼成功的消息封裝成Task,投遞到業務線程池中
- 步驟十一:將POJO對象encode成ByteBuffer,調用SocketChannel的異步write接口,將消息異步發送給客戶端
-
NIO客戶端序列
- 步驟一:打開SocketChannel,綁定客戶端本機地址
- 步驟二:設置SocketChannel為非阻塞模式,設置客戶端連接的TCP參數
- 步驟三:異步連接服務器
- 步驟四:判斷是否連接成功,如果連接成功,則直接注冊讀狀態位到多路復用器中,如果當前沒有連接成功
- 步驟五:向Reactor線程的多路復用器注冊OP_CONNECT狀態位,監聽服務端的TCP ACK應答
- 步驟六:創建Reactor線程,創建多路復用器并啟動線程
- 步驟七:多路復用器在線程run方法的無限循環體內輪詢準備就緒的Key
- 步驟八:接收connect事件進行處理
- 步驟九:判斷連接結果,如果連接成功,注冊讀事件到多路復用器
- 步驟十:注冊讀事件到多路復用器
- 步驟十一:異步讀客戶端請求消息到緩沖區
- 步驟十二:對ByteBuffer進行編解碼,如果有半包消息接收緩沖區Reset,繼續讀取后續的報文,將解碼成功的消息封裝成Task,投遞到業務線程池中,進行業務邏輯編排。
- 步驟十三:將POJO對象encode成ByteBuffer,調用SocketChannel的異步write接口,將消息異步發送給客戶端
NIO實例代碼
- 服務端
/**
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException{
int port = 8080;
if(args != null &&args.length >0){
try{
port = Integer.valueOf(args[0]);
}catch (NumberFormatException ex){
//采用默認值
}
}
MultiplexerTimeServer timeServer = new MultiplexerTimeServer(port);
new Thread(timeServer,"NIO-MultiplexerTimeServer-001").start();
}
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.Iterator;
import java.util.Set;
public class MultiplexerTimeServer implements Runnable {
private Selector selector;
private ServerSocketChannel serverChannel;
private volatile boolean stop;
/**
* 初始化多路復用器,綁定監聽端口
* @param port
*/
public MultiplexerTimeServer(int port){
try{
selector = Selector.open();//創建多路復用器
serverChannel = ServerSocketChannel.open();
serverChannel.configureBlocking(false);//設置為異步非阻塞模式
serverChannel.socket().bind(new InetSocketAddress(port),1024);//綁定端口
serverChannel.register(selector,SelectionKey.OP_ACCEPT);//注冊到Selector
System.out.println("The time server is start in port:" + port);
}catch (IOException e){
e.printStackTrace();
System.exit(1);
}
}
public void stop(){
this.stop = true;
}
public void run(){
while(!stop){
try{
selector.select(1000);//selector每隔1s都被喚醒一次
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 (Throwable t){
t.printStackTrace();
}
}
//多路復用器關閉后,所有注冊在上面的Channel和Pipe等資源都會被自動去注冊并關閉,所以不需要重復釋放資源
if(selector != null){
try{
selector.close();
}catch (IOException e){
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();//接收客戶端的連接請求,完成TCP三次握手
sc.configureBlocking(false);//設置為異步非阻塞
//Add the new connection to the selector
sc.register(selector,SelectionKey.OP_READ);
}
if(key.isReadable()){
//Read the data
SocketChannel sc = (SocketChannel)key.channel();
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
int readBytes = sc.read(readBuffer);
if(readBytes > 0 ){
readBuffer.flip();//將緩沖區當前的limit設置為position,position設置為0
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 java.util.Date(System.currentTimeMillis()).toString():"BAD ORDER";
doWrite(sc,currentTime);
}else if(readBytes <0){
//對端鏈路關閉
key.cancel();
sc.close();
}else{
//讀到0字節,忽略
}
}
}
}
/**
* 將應答消息異步發送給客戶端
* @param channel
* @param response
* @throws IOException
*/
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);
}
}
}
- 客戶端
/**
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException{
int port = 8080;
if(args != null &&args.length >0){
try{
port = Integer.valueOf(args[0]);
}catch (NumberFormatException ex){
//采用默認值
}
}
new Thread(new TimeClientHandle("127.0.0.1",port),"TimeClient-001").start();
}
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){
e.printStackTrace();
System.exit(1);
}
}
public void run(){
try{
doConnect();
}catch (IOException e){
e.printStackTrace();
System.exit(1);
}
while(!stop){
try{
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){
if(key != null){
key.cancel();
if(key.channel() !=null)
key.channel().close();
}
}
}
}catch (Exception e){
e.printStackTrace();
System.exit(1);
}
}
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();//將緩沖區當前的limit設置為position,position設置為0
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);
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[] bytes = "QUERY TIME ORDER".getBytes();
ByteBuffer writeBuffer = ByteBuffer.allocate(bytes.length);
writeBuffer.put(bytes);
writeBuffer.flip();
sc.write(writeBuffer);
if (!writeBuffer.hasRemaining())
System.out.println("Send order 2 server succeed.");
}
}
先啟動服務端,再啟動客戶端運行實例。
NIO2.0 AIO實例代碼
- 服務端
/**
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
int port = 8080;
if(args != null &&args.length >0){
try{
port = Integer.valueOf(args[0]);
}catch (NumberFormatException ex){
//采用默認值
}
}
AsyncTimeServerHandler timeServer = new AsyncTimeServerHandler(port);
new Thread(timeServer,"AIO-AsyncTimeServerHandler-001").start();
}
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.util.concurrent.CountDownLatch;
public class AsyncTimeServerHandler implements Runnable {
private int port;
CountDownLatch latch;
AsynchronousServerSocketChannel asynchronousServerSocketChannel;
public AsyncTimeServerHandler(int port){
this.port = port;
try{
//創建一個異步的服務端通道
asynchronousServerSocketChannel = AsynchronousServerSocketChannel.open();
//綁定端口
asynchronousServerSocketChannel.bind(new InetSocketAddress(port));
System.out.println("The time server is start in port:"+ port);
}catch (IOException e){
e.printStackTrace();
}
}
public void run(){
latch = new CountDownLatch(1);
doAccept();
try{
latch.await();//允許當前線程阻塞,防止服務端執行完退出
}catch (InterruptedException e){
e.printStackTrace();
}
}
public void doAccept(){
//傳遞一個CompletionHandler實例來接收通知
asynchronousServerSocketChannel.accept(this,new AcceptCompletionHandler());
}
}
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
public class AcceptCompletionHandler implements CompletionHandler<AsynchronousSocketChannel, AsyncTimeServerHandler> {
@Override
public void completed(AsynchronousSocketChannel result, AsyncTimeServerHandler attachment) {
//繼續接收
attachment.asynchronousServerSocketChannel.accept(attachment, this);
ByteBuffer buffer = ByteBuffer.allocate(1024);
result.read(buffer, buffer, new ReadCompletionHandler(result));
}
@Override
public void failed(Throwable exc, AsyncTimeServerHandler attachment) {
exc.printStackTrace();
attachment.latch.countDown();
}
}
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
public class ReadCompletionHandler implements CompletionHandler<Integer, ByteBuffer> {
private AsynchronousSocketChannel channel;
public ReadCompletionHandler(AsynchronousSocketChannel channel){
if(this.channel == null){
this.channel = channel;
}
}
@Override
public void completed(Integer result,ByteBuffer attachment){
attachment.flip();
byte[] body = new byte[attachment.remaining()];
attachment.get(body);
try{
String req = new String(body,"UTF-8");
System.out.println("The time server receive order:"+req);
String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(req)?
new java.util.Date(System.currentTimeMillis()).toString():"BAD ORDER";
doWrite(currentTime);
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}
}
private void doWrite(String currentTime){
if(currentTime !=null && currentTime.trim().length()>0){
byte[] bytes = (currentTime).getBytes();
final ByteBuffer writeBuffer = ByteBuffer.allocate(bytes.length);
writeBuffer.put(bytes);
writeBuffer.flip();
channel.write(writeBuffer, writeBuffer, new CompletionHandler<Integer, ByteBuffer>() {
@Override
public void completed(Integer result, ByteBuffer buffer) {
//如果沒有發送完成,繼續發送
if(buffer.hasRemaining())
channel.write(buffer,buffer,this);
}
@Override
public void failed(Throwable exc, ByteBuffer attachment) {
try{
channel.close();
}catch (IOException e){
//ingnore on close
}
}
});
}
}
public void failed(Throwable exc,ByteBuffer attachment){
try{
this.channel.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
- 客戶端
/**
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
int port = 8080;
if(args != null &&args.length >0){
try{
port = Integer.valueOf(args[0]);
}catch (NumberFormatException ex){
//采用默認值
}
}
new Thread(new AsyncTimeClientHandler("127.0.0.1",port),"AIO-AsyncTimeClientHandler-001").start();
}
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.CountDownLatch;
public class AsyncTimeClientHandler implements CompletionHandler<Void,AsyncTimeClientHandler>,Runnable {
private AsynchronousSocketChannel client;
private String host;
private int port;
private CountDownLatch latch;
public AsyncTimeClientHandler(String host,int port){
this.host = host;
this.port = port;
try{
client = AsynchronousSocketChannel.open();
}catch (IOException e){
e.printStackTrace();
}
}
@Override
public void run(){
latch = new CountDownLatch(1);
client.connect(new InetSocketAddress(host,port),this,this);
try{
latch.await();
}catch (InterruptedException el){
el.printStackTrace();
}
try{
client.close();
}catch (IOException e){
e.printStackTrace();
}
}
@Override
public void completed(Void result,AsyncTimeClientHandler attachment){
byte[] req = "QUERY TIME ORDER".getBytes();
ByteBuffer writeBuffer = ByteBuffer.allocate(req.length);
writeBuffer.put(req);
writeBuffer.flip();
client.write(writeBuffer, writeBuffer,
new CompletionHandler<Integer, ByteBuffer>() {
@Override
public void completed(Integer result, final ByteBuffer buffer) {
if(buffer.hasRemaining()){
client.write(buffer,buffer,this);
}else{
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
client.read(
readBuffer,
readBuffer,
new CompletionHandler<Integer, ByteBuffer>() {
@Override
public void completed(Integer result, ByteBuffer attachment) {
attachment.flip();
byte[] bytes = new byte[attachment.remaining()];
attachment.get(bytes);
String body;
try{
body = new String(bytes,"UTF-8");
System.out.println("Now is:"+body);
latch.countDown();
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}
}
@Override
public void failed(Throwable exc, ByteBuffer attachment) {
try{
client.close();
latch.countDown();
}catch (IOException e){
//ingnore on close
}
}
}
);
}
}
@Override
public void failed(Throwable exc, ByteBuffer attachment) {
try{
client.close();
latch.countDown();
}catch (IOException e){
//ingnore on close
}
}
});
}
@Override
public void failed(Throwable exc,AsyncTimeClientHandler attachment){
exc.printStackTrace();
try{
client.close();
latch.countDown();
}catch (IOException e){
e.printStackTrace();
}
}
}