各I/O模型優(yōu)缺點(diǎn)
-
BIO通信模型
BIO主要的問(wèn)題在于每當(dāng)有一個(gè)新的客戶端請(qǐng)求接入時(shí)赡模,服務(wù)端必須創(chuàng)建一個(gè)新的線程處理新接入的客戶端鏈路罢猪,一個(gè)線程只能處理一個(gè)客戶端連接
-
線程池I/O編程
假如所有可用線程都被阻塞,后續(xù)I/O都將在隊(duì)列中排隊(duì)
線程池采用阻塞隊(duì)列實(shí)現(xiàn)醋界,隊(duì)列積滿之后竟宋,后續(xù)入隊(duì)列操作將被阻塞,新的客戶端請(qǐng)求被拒絕形纺,發(fā)生大量連接超時(shí) -
NIO編程
-
緩沖區(qū)Buffer
每一種Java基本類型都有對(duì)一種緩沖區(qū)
大多數(shù)標(biāo)準(zhǔn)I/O使用ByteBuffer -
通道Channel
Channel分為兩大類:用于網(wǎng)絡(luò)讀寫(xiě)的SelectableChannel和用于文件操作的FileChannel
-
多路復(fù)用器Selector
多路復(fù)用器提供選擇已經(jīng)就緒的任務(wù)的能力
-
-
NIO2.0 AIO
異步套接字通道不需要通過(guò)多路復(fù)用器(Selector)對(duì)注冊(cè)的通道進(jìn)行輪詢操作即可實(shí)現(xiàn)異步讀寫(xiě)
NIO實(shí)例分析
-
NIO服務(wù)端序列
- 步驟一:打開(kāi)ServerSocketChannel丘侠,用于監(jiān)聽(tīng)客戶端的連接
- 步驟二:綁定監(jiān)聽(tīng)端口,設(shè)置連接為非阻塞模式
- 步驟三:創(chuàng)建Reactor線程逐样,創(chuàng)建多路復(fù)用器并啟動(dòng)線程
- 步驟四:將ServerSocketChannel注冊(cè)到Reactor線程的多路復(fù)用器Selector上蜗字,監(jiān)聽(tīng)ACCEPT事件
- 步驟五:多路復(fù)用器在線程run方法的無(wú)限循環(huán)體內(nèi)輪休準(zhǔn)備就緒的Key
- 步驟六:多路復(fù)用器監(jiān)聽(tīng)到有新的客戶端接入,處理新的計(jì)入請(qǐng)求脂新,完成TCP三次握手挪捕,建立物理鏈路
- 步驟七:設(shè)置客戶端鏈路為非阻塞模式
- 步驟八:將新接入的客戶端連接注冊(cè)到Reactor線程的多路復(fù)用器上,監(jiān)聽(tīng)讀操作
- 步驟九:異步讀取客戶端請(qǐng)求消息到緩沖區(qū)
- 步驟十:對(duì)ByteBuffer進(jìn)行編解碼争便,如果有半包消息指針reset级零,繼續(xù)讀取后續(xù)的報(bào)文,將解碼成功的消息封裝成Task滞乙,投遞到業(yè)務(wù)線程池中
- 步驟十一:將POJO對(duì)象encode成ByteBuffer奏纪,調(diào)用SocketChannel的異步write接口,將消息異步發(fā)送給客戶端
-
NIO客戶端序列
- 步驟一:打開(kāi)SocketChannel斩启,綁定客戶端本機(jī)地址
- 步驟二:設(shè)置SocketChannel為非阻塞模式序调,設(shè)置客戶端連接的TCP參數(shù)
- 步驟三:異步連接服務(wù)器
- 步驟四:判斷是否連接成功,如果連接成功兔簇,則直接注冊(cè)讀狀態(tài)位到多路復(fù)用器中发绢,如果當(dāng)前沒(méi)有連接成功
- 步驟五:向Reactor線程的多路復(fù)用器注冊(cè)O(shè)P_CONNECT狀態(tài)位硬耍,監(jiān)聽(tīng)服務(wù)端的TCP ACK應(yīng)答
- 步驟六:創(chuàng)建Reactor線程,創(chuàng)建多路復(fù)用器并啟動(dòng)線程
- 步驟七:多路復(fù)用器在線程run方法的無(wú)限循環(huán)體內(nèi)輪詢準(zhǔn)備就緒的Key
- 步驟八:接收connect事件進(jìn)行處理
- 步驟九:判斷連接結(jié)果朴摊,如果連接成功默垄,注冊(cè)讀事件到多路復(fù)用器
- 步驟十:注冊(cè)讀事件到多路復(fù)用器
- 步驟十一:異步讀客戶端請(qǐng)求消息到緩沖區(qū)
- 步驟十二:對(duì)ByteBuffer進(jìn)行編解碼,如果有半包消息接收緩沖區(qū)Reset甚纲,繼續(xù)讀取后續(xù)的報(bào)文口锭,將解碼成功的消息封裝成Task,投遞到業(yè)務(wù)線程池中介杆,進(jìn)行業(yè)務(wù)邏輯編排鹃操。
- 步驟十三:將POJO對(duì)象encode成ByteBuffer,調(diào)用SocketChannel的異步write接口春哨,將消息異步發(fā)送給客戶端
NIO實(shí)例代碼
- 服務(wù)端
/**
*
* @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){
//采用默認(rèn)值
}
}
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;
/**
* 初始化多路復(fù)用器荆隘,綁定監(jiān)聽(tīng)端口
* @param port
*/
public MultiplexerTimeServer(int port){
try{
selector = Selector.open();//創(chuàng)建多路復(fù)用器
serverChannel = ServerSocketChannel.open();
serverChannel.configureBlocking(false);//設(shè)置為異步非阻塞模式
serverChannel.socket().bind(new InetSocketAddress(port),1024);//綁定端口
serverChannel.register(selector,SelectionKey.OP_ACCEPT);//注冊(cè)到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();
}
}
//多路復(fù)用器關(guān)閉后,所有注冊(cè)在上面的Channel和Pipe等資源都會(huì)被自動(dòng)去注冊(cè)并關(guān)閉赴背,所以不需要重復(fù)釋放資源
if(selector != null){
try{
selector.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
private void handleInput(SelectionKey key) throws IOException{
if(key.isValid()){
//處理新接入的請(qǐng)求消息
if(key.isAcceptable()){
//Accept the new connection
ServerSocketChannel ssc = (ServerSocketChannel)key.channel();
SocketChannel sc = ssc.accept();//接收客戶端的連接請(qǐng)求椰拒,完成TCP三次握手
sc.configureBlocking(false);//設(shè)置為異步非阻塞
//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();//將緩沖區(qū)當(dāng)前的limit設(shè)置為position,position設(shè)置為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){
//對(duì)端鏈路關(guān)閉
key.cancel();
sc.close();
}else{
//讀到0字節(jié),忽略
}
}
}
}
/**
* 將應(yīng)答消息異步發(fā)送給客戶端
* @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){
//采用默認(rèn)值
}
}
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);//連接失敗凰荚,進(jìn)程退出
}
}
if(key.isReadable()) {
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
int readBytes = sc.read(readBuffer);
if (readBytes > 0) {
readBuffer.flip();//將緩沖區(qū)當(dāng)前的limit設(shè)置為position,position設(shè)置為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) {
//對(duì)端鏈路關(guān)閉
key.cancel();
sc.close();
} else {
//讀到0字節(jié)燃观,忽略
}
}
}
}
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.");
}
}
先啟動(dòng)服務(wù)端,再啟動(dòng)客戶端運(yùn)行實(shí)例便瑟。
NIO2.0 AIO實(shí)例代碼
- 服務(wù)端
/**
*
* @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){
//采用默認(rèn)值
}
}
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{
//創(chuàng)建一個(gè)異步的服務(wù)端通道
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();//允許當(dāng)前線程阻塞缆毁,防止服務(wù)端執(zhí)行完退出
}catch (InterruptedException e){
e.printStackTrace();
}
}
public void doAccept(){
//傳遞一個(gè)CompletionHandler實(shí)例來(lái)接收通知
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) {
//繼續(xù)接收
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) {
//如果沒(méi)有發(fā)送完成,繼續(xù)發(fā)送
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){
//采用默認(rèn)值
}
}
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();
}
}
}