Java IO多路復用技術簡介
/**
* @author qifuguang
* @date 15-2-4 下午2:07
*/
public class TimeServerMain {
public static void main(String[] args) throws Exception {
// 啟動時間服務器
new Thread(new SelectorTimeServer()).start();
}
}
/**
* @author qifuguang
* @date 15-2-4 下午2:09
*/
public class TimeClientMain {
public static void main(String[] args) throws Exception {
// 創建100個客戶端連接到服務器
for (int i = 0; i < 100; i++) {
new Thread(new SelectorTimeClient(i + 1)).start();
}
}
}
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.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
/**
* @author qifuguang
* @date 15-2-4 下午1:21
*/
public class SelectorTimeServer implements Runnable {
private static final String TIME_ORDER = "Query Time";
private Selector selector;
private ServerSocketChannel serverChannel;
private volatile boolean stop = false;
/**
* 創建Selector, 創建ServerSocketChannel,並設置為非阻塞模式, 注冊到selector.
*
* @throws Exception
*/
public SelectorTimeServer() throws Exception {
selector = Selector.open();
serverChannel = ServerSocketChannel.open();
serverChannel.socket().bind(new InetSocketAddress(8080));
serverChannel.configureBlocking(false);
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
}
/**
* 輪詢監聽selector.
*/
@Override
public void run() {
try {
System.out.println("時間服務器啟動!");
while (!stop) {
selector.select(1000);
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
iterator.remove();
handleKey(key);
}
}
if (selector != null) {
selector.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 處理每一種selector感興趣的事件.
*
* @param key 輪詢監聽得到的SelectionKey.
*/
private void handleKey(SelectionKey key) {
try {
if (key.isValid()) { // 如果連接成功
if (key.isAcceptable()) { // 監聽到有新客戶端連接
SocketChannel accept = ((ServerSocketChannel) key.channel()).accept(); // 建立與客戶端的連接
accept.configureBlocking(false); // 設置該連接為非阻塞模式
accept.register(selector, SelectionKey.OP_READ); // 將該連接注冊到selector
System.out.println("發現有新客戶端連接...");
}
if (key.isReadable()) { // 監聽到有客戶端發送請求
SocketChannel channel = (SocketChannel) key.channel();
// 讀取客戶端發來的請求
ByteBuffer buff = ByteBuffer.allocate(1024);
int size = channel.read(buff);
if (size > 0) {
byte[] b = new byte[size];
buff.flip();
buff.get(b);
String order = new String(b, "UTF-8");
System.out.println("收到客戶端命令:" + order);
String content = "";
if (order.equalsIgnoreCase(TIME_ORDER)) {
content = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
} else {
content = "命令錯誤";
}
// 根據客戶端發來的請求做出相應的動作,並將處理結果返回給客戶端
doWrite(channel, content);
} else if (size < 0) {
channel.close();
key.cancel();
} else {
;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 向指定的SocketChannel發送指定的消息。
*
* @param sc 需要向哪一個SocketChannel發送消息
* @param content 需要發送的消息
* @throws Exception
*/
private void doWrite(SocketChannel sc, String content) throws Exception {
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.put(content.getBytes("UTF-8"));
buffer.flip();
sc.write(buffer);
if (!buffer.hasRemaining()) {
System.out.println("下發消息給客戶端:" + content);
}
}
}
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;
/**
* @author qifuguang
* @date 15-2-4 下午1:21
*/
public class SelectorTimeClient implements Runnable {
private static final String TIME_ORDER = "Query Time";
private SocketChannel channel;
private Selector selector;
private volatile boolean stop = false;
private Integer index;
/**
* 創建Selector, SocketChannel.
*
* @param index 客戶端編號.
* @throws Exception
*/
public SelectorTimeClient(Integer index) throws Exception {
selector = Selector.open();
channel = SocketChannel.open();
channel.configureBlocking(false);
this.index = index;
}
/**
* 輪詢監聽selector剛興趣的事件.
*/
@Override
public void run() {
try {
System.out.println("第" + index + "個客戶端啟動!");
// 先嘗試異步連接服務器, 如果連接成功,則只需要把channel注冊到selector的READ事件,
// 讀取服務器返回的結果. 如果不成功(客戶端已經向服務器發送了sync包,但是服務器沒有返回ack包, 物理鏈路還沒建立成功)
// 則把該channel注冊到selector的CONNECT事件, 等待服務器返回的ack包.
if (channel.connect(new InetSocketAddress(8080))) {
channel.register(selector, SelectionKey.OP_READ);
doWrite(channel, TIME_ORDER);
} else {
channel.register(selector, SelectionKey.OP_CONNECT);
}
while (!stop) {
selector.select(1000);
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
SocketChannel sc = (SocketChannel) key.channel();
iterator.remove();
if (key.isValid()) {
if (key.isReadable()) { // 監聽到可讀事件, 讀取服務器返回的處理結果.
ByteBuffer buff = ByteBuffer.allocate(1024);
int size = sc.read(buff);
if (size > 0) {
byte[] b = new byte[size];
buff.flip();
buff.get(b);
System.out.println("第" + index + "個客戶端獲取服務器返回時間:" + new String(b));
stop = true;
} else if (size < 0) {
sc.close();
key.cancel();
} else {
;
}
}
if (key.isConnectable()) { //監聽到服務器返回了ack包, 准備完成連接的建立
if (sc.finishConnect()) { // 調用此方法完成物理鏈路連接的建立
sc.register(selector, SelectionKey.OP_READ); // 建立連接之後注冊監聽READ事件
doWrite(sc, TIME_ORDER);
} else {
System.exit(1); //否則,程序退出
}
}
}
}
}
if (selector != null) {
selector.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 向指定的channel發送指定的消息.
*
* @param channel 向哪一個channel發送消息
* @param content 需要發送的消息
* @throws Exception
*/
private void doWrite(SocketChannel channel, String content) throws Exception {
ByteBuffer buff = ByteBuffer.allocate(1024);
buff.put(content.getBytes("UTF-8"));
buff.flip();
channel.write(buff);
if (!buff.hasRemaining()) {
System.out.println("第" + index + "個客戶端成功發送請求到服務器:" + content);
}
}
}