Java的NIO多线程服务器1.3版

Java的NIO多线程服务器1.3版

反应堆模式的JAVA NIO多线程服务器,这是比较完善的一版了。的Java的NIO的网络模型实在是不好用,还是使用现成的好。

公共  NIOServer  实现了 Runnable  

    私有 静态 最后 登录日志   LogFactory.getLog(NIOServer. ); 

    私人 ExecutorService的执行人
    私人 最终 选择器SEL; 
    私人 最终 的ServerSocketChannel SSC的; 
    私人 豪HandleUtil; 

    公共 NIOServer( 端口号,HandleUtil 









     浩的ExecutorService 
























                                            SK  
 it.next();                         it.remove(); 

                        (sk.isAcceptable()| | sk.isReadable的()| | sk.isWritable())
                                                    Runnable的ŗ  
 (Runnable接口)sk.attachment();                             ŗ运行();                         }


                     }
                 }
             }
         }
        的catch (IOException异常   InterruptedException的E)         {log.info(ExceptionUtil.getExceptionMessage(E));}
     }

     接受器  实现 了Runnable  
            @覆盖

        公共 无效 的run()  
        
            尝试
                            的SocketChannel 





                    SK 
sc.register(SEL,SelectionKey.OP_READ);                     sk.attach(
新的 阅读器(SK));                     sel.wakeup();                 }


             }
            的catch (IOException异常E)  {log.info(ExceptionUtil.getExceptionMessage(e)条); }
         }
     }

     读者  实现了 Runnable  
    
        私人 字节[]字节 字节]; 
        私人 的SelectionKey SK 

        公共 阅读器(的SelectionKeyŠK)
        
            SK SK;         }


         @覆盖
        公共 无效 的run()
        
            试图
                            的SocketChannel SC  
 (的SocketChannel)sk.channel();                 处理 




ho.getParameters ho.getParameterTypes(),());                 handle.setSocketChannel(SC);                 的ByteBuffer 














































         SK 
        私人 ByteBuffer的输出; 

        公共 作家(SelectionKey的SK,ByteBuffer的输出)
        
            SK SK 
            输出输出;         }


        公共 作家(SK未来的SelectionKey 字节[]  未来)  抛出 InterruptedException的,

                    SC  
 (SocketChannel实例)












            五)
                            sk.cancel();                 log.info(ExceptionUtil.getExceptionMessage(五));             }



         }
     }

    公共 无效 发送的SocketChannel SC(字节[]字节)  抛出 ClosedChannelException的
            SelectionKey的SK 
sc.register(SEL的SelectionKey。 OP_WRITE);         sk.attach(
 编剧(SK,ByteBuffer.wrap(字节)));         sel.wakeup();     }


 }

标签