篇文章對NIO進行了簡介,對Channel和Buffer接口的使用進行了說明,并舉了一個簡單的例子來說明其使用方法。
本篇則重點說明selector,Selector(選擇器)是Java NIO中能夠檢測一到多個NIO通道,并能夠知曉通道是否為諸如讀寫事件做好準備的元件。這樣,一個單獨的線程可以管理多個channel,進而管理多個網絡連接配接。
與selector聯系緊密的是ServerSocketChannel和SocketChannel,他們的使用與上篇文章描述的FileChannel的使用方法類似,然後與ServerSocket和Socket也有一些聯系。
本篇首先簡單的進selector進行說明,然後一個簡單的示例程式,來示範即時通訊。
Selector
使用傳統IO進行網絡程式設計,如下圖所示:
每一個到服務端的連接配接,都需要一個單獨的線程(或者線程池)來處理其對應的socket,當連接配接數多的時候,對服務端的壓力極大。并使用socket的getInputStream。Read方法來不斷的輪訓每個socket,效率可想而知。
而selector則可以在同一個線程中監聽多個channel的狀态,當某個channel有selector感興趣的事情發現,selector則被激活。即不會主動去輪詢。如下圖所示:
Selector使用如下示意:
[java] view plain copy
- public static void main(String[] args) throws IOException {
- Selector selector = Selector.open();//聲明selector
- ServerSocketChannel sc = ServerSocketChannel.open();
- sc.configureBlocking(false);//必須設定為異步
- sc.socket().bind(new InetSocketAddress(8081));//綁定端口
- //把channel 注冊到 selector上
- sc.register(selector, SelectionKey.OP_ACCEPT|SelectionKey.OP_CONNECT|SelectionKey.OP_READ|SelectionKey.OP_WRITE);
- while(true){
- selector.select();//阻塞,直到注冊的channel上某個感興趣的事情發生
- Set<SelectionKey> selectedKeys = selector.selectedKeys();
- Iterator<SelectionKey> keyIterator = selectedKeys.iterator();
- while(keyIterator.hasNext()) {
- SelectionKey key = keyIterator.next();
- if(key.isAcceptable()) {
- // a connection was accepted by a ServerSocketChannel.
- } else if (key.isConnectable()) {
- // a connection was established with a remote server.
- } else if (key.isReadable()) {
- // a channel is ready for reading
- } else if (key.isWritable()) {
- // a channel is ready for writing
- }
- keyIterator.remove();
- }
- }
- }
極簡即時通訊
本例子是是一個極為簡單的例子,很多地方都不完善,但是例子可以很好的說明selector的使用方法。
本例子包含服務端和用戶端兩個部分,其中服務端采用兩個selector,用來建立連接配接和資料的讀寫。兩個selector在兩個線程中。
服務端
[java] view plain copy
- public class ServerSocketChannelTest {
- private static final int SERVER_PORT = 8081;
- private ServerSocketChannel server;
- private volatile Boolean isStop = false;
- //負責建立連接配接的selector
- private Selector conn_Sel;
- //負責資料讀寫的selector
- private Selector read_Sel;
- // private ExecutorService sendService = Executors.newFixedThreadPool(3);
- //鎖,用來在建立連接配接後,喚醒read_Sel時使用的同步
- private Object lock = new Object();
- //注冊的使用者
- private Map<String, ClientInfo> clents = new HashMap<String, ClientInfo>();
- public void init() throws IOException {
- //建立ServerSocketChannel
- server = ServerSocketChannel.open();
- //綁定端口
- server.socket().bind(new InetSocketAddress(SERVER_PORT));
- server.configureBlocking(false);
- //定義兩個selector
- conn_Sel = Selector.open();
- read_Sel = Selector.open();
- //把channel注冊到selector上,第二個參數為興趣的事件
- server.register(conn_Sel, SelectionKey.OP_ACCEPT);
- }
- // 負責建立連接配接。
- private void beginListen() {
- System.out.println("--------開始監聽----------");
- while (!isStop) {
- try {
- conn_Sel.select();
- } catch (IOException e) {
- e.printStackTrace();
- continue;
- }
- Iterator<SelectionKey> it = conn_Sel.selectedKeys().iterator();
- while (it.hasNext()) {
- SelectionKey con = it.next();
- it.remove();
- if (con.isAcceptable()) {
- try {
- SocketChannel newConn = ((ServerSocketChannel) con
- .channel()).accept();
- handdleNewInConn(newConn);
- } catch (IOException e) {
- e.printStackTrace();
- continue;
- }
- } else if (con.isReadable()) {//廢代碼,執行不到。
- try {
- handleData((SocketChannel) con.channel());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- private void beginReceive(){
- System.out.println("---------begin receiver data-------");
- while (true) {
- synchronized (lock) {
- }
- try {
- read_Sel.select();
- } catch (IOException e) {
- e.printStackTrace();
- continue;
- }
- Iterator<SelectionKey> it = read_Sel.selectedKeys().iterator();
- while (it.hasNext()) {
- SelectionKey con = it.next();
- it.remove();
- if (con.isReadable()) {
- try {
- handleData((SocketChannel) con.channel());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- private void handdleNewInConn(SocketChannel newConn) throws IOException {
- newConn.configureBlocking(false);
- //這裡必須先喚醒read_Sel,然後加鎖,防止讀寫線程的中select方法再次鎖定。
- synchronized (lock) {
- read_Sel.wakeup();
- newConn.register(read_Sel, SelectionKey.OP_READ);
- }
- //newConn.register(conn_Sel, SelectionKey.OP_READ);
- }
- private void handleData(final SocketChannel data) throws IOException {
- ByteBuffer buffer = ByteBuffer.allocate(512);
- try {
- int size= data.read(buffer);
- if (size==-1) {
- System.out.println("-------連接配接斷開-----");
- //這裡暫時不處理,這裡可以移除已經注冊的用戶端
- }
- } catch (IOException e) {
- e.printStackTrace();
- return;
- }
- buffer.flip();
- byte[] msgByte = new byte[buffer.limit()];
- buffer.get(msgByte);
- Message msg = Message.getMsg(new String(msgByte));
- //這裡讀完資料其實已經可以另開線程了下一步的處理,理想情況下,根據不同的消息類型,建立不同的隊列,把待發送的消息放進隊列
- //當然也可以持久化。如果在資料沒有讀取前,另開線程的話,讀寫線程中 read_Sel.select(),會立刻傳回。可以把
- if (msg.getType().equals("0")) {// 注冊
- ClientInfo info = new ClientInfo(msg.getFrom(), data);
- clents.put(info.getClentID(), info);
- System.out.println(msg.getFrom() + "注冊成功");
- } else {// 轉發
- System.out.println("收到"+msg.getFrom()+"發給"+msg.getTo()+"的消息");
- ClientInfo to = clents.get(msg.getTo());
- buffer.rewind();
- if (to != null) {
- SocketChannel sendChannel = to.getChannel();
- try {
- while (buffer.hasRemaining()) {
- sendChannel.write(buffer);
- }
- } catch (Exception e) {
- }
- finally {
- buffer.clear();
- }
- }
- }
- }
- public static void main(String[] args) throws IOException {
- final ServerSocketChannelTest a = new ServerSocketChannelTest();
- a.init();
- new Thread("receive..."){
- public void run() {
- a.beginReceive();
- };
- }.start();
- a.beginListen();
- }
- }
用戶端
[java] view plain copy
- public class Client {
- private String self;
- private String to;
- //通道管理器
- private Selector selector;
- private ByteBuffer writeBuffer = ByteBuffer.allocate(512);
- private SocketChannel channel;
- private Object lock = new Object();
- private volatile boolean isInit = false;
- public Client(String self, String to) {
- super();
- this.self = self;
- this.to = to;
- }
- public void initClient(String ip,int port) throws IOException {
- // 獲得一個Socket通道
- channel = SocketChannel.open();
- // 設定通道為非阻塞
- channel.configureBlocking(false);
- // 獲得一個通道管理器
- this.selector = Selector.open();
- // 用戶端連接配接伺服器,其實方法執行并沒有實作連接配接,需要在listen()方法中調
- //用channel.finishConnect();才能完成連接配接
- channel.connect(new InetSocketAddress(ip,port));
- //将通道管理器和該通道綁定,并為該通道注冊SelectionKey.OP_CONNECT事件。
- channel.register(selector, SelectionKey.OP_CONNECT);
- }
- @SuppressWarnings("unchecked")
- public void listen() throws IOException {
- // 輪詢通路selector
- while (true) {
- synchronized (lock) {
- }
- selector.select();
- // 獲得selector中選中的項的疊代器
- Iterator<SelectionKey> ite = this.selector.selectedKeys().iterator();
- while (ite.hasNext()) {
- SelectionKey key = ite.next();
- // 删除已選的key,以防重複處理
- ite.remove();
- // 連接配接事件發生
- if (key.isConnectable()) {
- SocketChannel channel = (SocketChannel) key
- .channel();
- // 如果正在連接配接,則完成連接配接
- if(channel.isConnectionPending()){
- channel.finishConnect();
- }
- // 設定成非阻塞
- channel.configureBlocking(false);
- //在和服務端連接配接成功之後,為了可以接收到服務端的資訊,需要給通道設定讀的權限。
- channel.register(this.selector, SelectionKey.OP_READ);
- isInit = true;
- // 獲得了可讀的事件
- } else if (key.isReadable()) {
- read(key);
- }
- }
- }
- }
- public void read(SelectionKey key) throws IOException{
- SocketChannel data = (SocketChannel) key.channel();
- ByteBuffer buffer = ByteBuffer.allocate(512) ;
- try {
- data.read(buffer );
- } catch (IOException e) {
- e.printStackTrace();
- data.close();
- return;
- }
- buffer.flip();
- byte[] msgByte = new byte[buffer.limit()];
- buffer.get(msgByte);
- Message msg = Message.getMsg(new String(msgByte));
- System.out.println("---收到消息--"+msg+" 來自 "+msg.getFrom());
- }
- private void sendMsg(String content){
- writeBuffer.put(content.getBytes());
- writeBuffer.flip();
- try {
- while (writeBuffer.hasRemaining()) {
- channel.write(writeBuffer);
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- writeBuffer.clear();
- }
- public void start() throws IOException {
- initClient("localhost",8081);
- new Thread("reading"){
- public void run() {
- try {
- listen();
- } catch (IOException e) {
- e.printStackTrace();
- }
- };
- }.start();
- int time3 = 0;
- while(!isInit&&time3<3){
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- time3 ++;
- }
- System.out.println("--------開始注冊------");
- Message re = new Message("", self, "");
- sendMsg(re.toString());
- try {
- Thread.sleep(200);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("-----注冊成功----");
- String content ="";
- System.out.println("---- 請輸入要發送的消息,按回車發送,輸入 123 退出----------");
- Scanner s = new Scanner(System.in);
- while (!content.equals("123")&&s.hasNext()) {
- content = s.next();
- Message msg = new Message(content, self, to);
- msg.setType("1");
- sendMsg(msg.toString());
- if (content.equals("123")) {
- break;
- }
- System.out.println("---發送成功---");
- }
- channel.close();
- }
- }
用戶端測試
[java] view plain copy
- public class TestClient1 {
- public static void main(String[] args) throws IOException {
- Client c1 =new Client("1", "2");
- c1.start();
- }
- }
- public class TestClient2 {
- public static void main(String[] args) throws IOException {
- Client c2 =new Client("2", "1");
- c2.start();
- }
- }
結束
本文的例子極為簡單,但是都經過測試。在編碼的過程中,遇到的問題主要有兩點:
1. channel.register()方法阻塞
2. 使用線程池遇到問題。本文最後在服務端的讀寫線程中,沒有使用線程池,原因注釋說的比較明白,也說明了使用線程池的一種設想。