Java NIO Reactor模式

来源:互联网 发布:微pe windows安装器 编辑:程序博客网 时间:2024/05/12 00:39

Java代码  收藏代码
  1. package com.zzq.nio.reactor;  
  2.   
  3. import java.io.IOException;  
  4. import java.net.InetSocketAddress;  
  5. import java.nio.ByteBuffer;  
  6. import java.nio.channels.SelectionKey;  
  7. import java.nio.channels.Selector;  
  8. import java.nio.channels.ServerSocketChannel;  
  9. import java.nio.channels.SocketChannel;  
  10. import java.util.Iterator;  
  11.   
  12. public class Reactor implements Runnable {  
  13.   
  14.     private ServerSocketChannel serverSocketChannel = null;  
  15.   
  16.     private Selector            selector            = null;  
  17.   
  18.     public Reactor() {  
  19.         try {  
  20.             selector = Selector.open();  
  21.             serverSocketChannel = ServerSocketChannel.open();  
  22.             serverSocketChannel.configureBlocking(false);  
  23.             serverSocketChannel.socket().bind(new InetSocketAddress(8888));  
  24.             SelectionKey selectionKey = serverSocketChannel.register(selector,  
  25.                 SelectionKey.OP_ACCEPT);  
  26.             selectionKey.attach(new Acceptor());  
  27.             System.out.println("服务器启动正常!");  
  28.         } catch (IOException e) {  
  29.             System.out.println("启动服务器时出现异常!");  
  30.             e.printStackTrace();  
  31.         }  
  32.     }  
  33.   
  34.     public void run() {  
  35.         while (true) {  
  36.             try {  
  37.                 selector.select();  
  38.   
  39.                 Iterator<SelectionKey> iter = selector.selectedKeys().iterator();  
  40.                 while (iter.hasNext()) {  
  41.                     SelectionKey selectionKey = iter.next();  
  42.                     dispatch((Runnable) selectionKey.attachment());  
  43.                     iter.remove();  
  44.                 }  
  45.             } catch (IOException e) {  
  46.                 e.printStackTrace();  
  47.             }  
  48.         }  
  49.     }  
  50.   
  51.     public void dispatch(Runnable runnable) {  
  52.         if (runnable != null) {  
  53.             runnable.run();  
  54.         }  
  55.     }  
  56.   
  57.     public static void main(String[] args) {  
  58.         new Thread(new Reactor()).start();  
  59.     }  
  60.   
  61.     class Acceptor implements Runnable {  
  62.         public void run() {  
  63.             try {  
  64.                 SocketChannel socketChannel = serverSocketChannel.accept();  
  65.                 if (socketChannel != null) {  
  66.                     System.out.println("接收到来自客户端("  
  67.                                        + socketChannel.socket().getInetAddress().getHostAddress()  
  68.                                        + ")的连接");  
  69.                     new Handler(selector, socketChannel);  
  70.                 }  
  71.   
  72.             } catch (IOException e) {  
  73.                 e.printStackTrace();  
  74.             }  
  75.         }  
  76.     }  
  77. }  
  78.   
  79. class Handler implements Runnable {  
  80.   
  81.     private static final int READ_STATUS  = 1;  
  82.   
  83.     private static final int WRITE_STATUS = 2;  
  84.   
  85.     private SocketChannel    socketChannel;  
  86.   
  87.     private SelectionKey     selectionKey;  
  88.   
  89.     private int              status       = READ_STATUS;  
  90.   
  91.     public Handler(Selector selector, SocketChannel socketChannel) {  
  92.         this.socketChannel = socketChannel;  
  93.         try {  
  94.             socketChannel.configureBlocking(false);  
  95.             selectionKey = socketChannel.register(selector, 0);  
  96.             selectionKey.interestOps(SelectionKey.OP_READ);  
  97.             selectionKey.attach(this);  
  98.             selector.wakeup();  
  99.         } catch (IOException e) {  
  100.             e.printStackTrace();  
  101.         }  
  102.     }  
  103.   
  104.     public void run() {  
  105.         try {  
  106.             if (status == READ_STATUS) {  
  107.                 read();  
  108.                 selectionKey.interestOps(SelectionKey.OP_WRITE);  
  109.                 status = WRITE_STATUS;  
  110.             } else if (status == WRITE_STATUS) {  
  111.                 process();  
  112.                 selectionKey.cancel();  
  113.                 System.out.println("服务器发送消息成功!");  
  114.             }  
  115.         } catch (IOException e) {  
  116.             e.printStackTrace();  
  117.         }  
  118.     }  
  119.   
  120.     public void read() throws IOException {  
  121.         ByteBuffer buffer = ByteBuffer.allocate(1024);  
  122.         socketChannel.read(buffer);  
  123.         System.out.println("接收到来自客户端(" + socketChannel.socket().getInetAddress().getHostAddress()  
  124.                            + ")的消息:" + new String(buffer.array()));  
  125.     }  
  126.   
  127.     public void process() throws IOException {  
  128.         String content = "Hello World!";  
  129.         ByteBuffer buffer = ByteBuffer.wrap(content.getBytes());  
  130.         socketChannel.write(buffer);  
  131.     }  
  132. }  
0 0
原创粉丝点击