NIO 与 QUEUE 的结合

本文介绍了使用Java NIO结合队列进行高效通信的方法,包括如何用队列处理信息的发送和接收,解决心跳协议的问题,并利用多线程实现数据处理的异步方式。

写了一段时间的NIO通信部分,在不断的摸索中提升,但是也有很多不足的地方,下面是一些在写的过程解决的小方法。
QUEUE的认识让我在写程序中如鱼得水,在NIO通信中经常利用到
Queue queue = new LinkedList();
1: 在NIO通信中利用QUEUE来处理信息的发送和接受

java 代码
  1. while (selector.select() > 0) {   
  2.   Set     readyKeys =  selector.selectedKeys();   
  3.   Iterator  it = readyKeys.iterator();   
  4.          while (it.hasNext()) {    
  5.     try {   
  6.          SelectionKey   key = (SelectionKey) it.next();   
  7.          it.remove();   
  8.   
  9.          if (key.isAcceptable()) {             
  10.                 ServerSocketChannel ssc = (ServerSocketChannel) key.channel();   
  11.                 SocketChannel socketChannel = (SocketChannel) ssc.accept();   
  12.                 socketChannel.configureBlocking(false);                
  13.                 socketChannel.register(selector, SelectionKey.OP_READ   
  14.                 | SelectionKey.OP_WRITE);   
  15.          }   
  16.        if (key.isReadable()) {   
  17.             receive(key);   
  18.        }   
  19.        if ( key.isWritable()) {   
  20.             send(key);   
  21.        }   
  22.     } catch (IOException e) {   
  23.             e.printStackTrace();   
  24.             try {   
  25.                   if (key != null) {   
  26.         key.cancel();// 释放和关闭key   
  27.         key.channel().close();   
  28.         }   
  29.             } catch (Exception ex) {   
  30.         logger.error(ex);   
  31.             }   }   
  32.    }// #while   
  33. }  

定义相关相关队列

java 代码
  1. 定义接受和发送队列   
  2. Queue<string></string>   reciveQueue = new LinkedList<string></string>();   
  3. Queue<string></string>   sendQueue = new LinkedList<string></string>();  


关于revceive(key)就可以这样处理了

java 代码
  1. public void receive(SelectionKey key) throws IOException {   
  2.     SocketChannel socketChannel = (SocketChannel) key.channel();   
  3.   
  4.     socketChannel.read(readBuff);   
  5.     readBuff.flip();   
  6.     String rmsg = decode(readBuff);   
  7.     //将接受到的数据放到队列中   
  8.                                           reciveQueue.offer(rmsg );   
  9.     ByteBuffer tempBuffer = encode(msg);   
  10.     readBuff.position(tempBuffer.limit());   
  11.     readBuff.compact();   
  12. }  

关于Send(key)

java 代码
  1. public void send(SelectionKey key) throws IOException {   
  2.         SocketChannel socketChannel = (SocketChannel) key.channel();   
  3.         String msg = null;   
  4.         ByteBuffer reBuffer = null;   
  5.                                           //队列中取出需要发送的信息   
  6.         while ((msg = sendQueue.poll()) != null) {   
  7.             reBuffer = encode(msg);   
  8.             writeBuff.put(reBuffer);   
  9.             writeBuff.flip();   
  10.             while (writeBuff.hasRemaining())   
  11.                 socketChannel.write(writeBuff);   
  12.   
  13.             writeBuff.position(reBuffer.limit());   
  14.             writeBuff.compact();   
  15.         }   
  16.     }  

2:关于心跳协议
    以前在处理这部分时很烦恼,在网上搜了很多也问了一些人。遇到QUEUE后,我开始利用QUEUE来处理消息,也解决了心跳包的发送烦恼;可以利用定时器在固定时间内象队列中offer心跳包;

3:因为QUEUE是线程安全的,所以在QUEUE的处理上带来了很多方便,可以在利用多线程完成通信和数据的处理异步方式,利用NIO完成数据的接受,然后将reciveQueue注入到另一个数据处理线程,来完成异步处理的效果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值