莊周夢蝶

          生活、程序、未來
             :: 首頁 ::  ::  :: 聚合  :: 管理

          Yet another nio framework for java

          Posted on 2008-10-11 14:25 dennis 閱讀(2844) 評論(6)  編輯  收藏 所屬分類: javamy open-source
          項目名稱:Yanf4j(Yet another nio framework for java)
          項目地址:http://code.google.com/p/yanf4j/,當前版本0.30-beta1
          協(xié)議:Apache License, Version 2.0
          簡單描述:
              有這么多nio框架了,為什么要another?重復造輪子也罷,這框架脫胎于一個服務器項目的網絡層代碼,期間參考了cindy、grizzly等nio框架的實現(xiàn),加上自己的一些心得體會實現(xiàn)的。特點是簡單、小巧、超輕量級。項目沒有多大野心,目標是高效、簡單地實現(xiàn)非阻塞模式的Server(TCP、UDP)并且保證不錯的性能要求,不提供客戶端API 和阻塞模式。如果你的項目需要實現(xiàn)一個socket server并且不希望用太重量級的框架,yanf4j是個不錯的選擇。

              例子,在source archive中帶有例子,這里描述下tcp和udp的echo server的實現(xiàn)。
           一、先看TCP的Echo Server
          1、實現(xiàn)處理handler,繼承HandlerAdapter類,實現(xiàn)相應的回調方法,這與其他nio框架沒啥區(qū)別:
          import com.google.code.yanf4j.nio.Dispatcher;
          import com.google.code.yanf4j.nio.Session;
          import com.google.code.yanf4j.nio.impl.HandlerAdapter;
          import com.google.code.yanf4j.nio.util.DispatcherFactory;

          public class EchoHandler extends HandlerAdapter<String> {

              Dispatcher dispatcher 
          = DispatcherFactory.newDispatcher(4);

              @Override
              
          public void onException(Session session, Throwable t) {
                  t.printStackTrace();
              }

              @Override
              
          public void onMessageSent(Session session, String t) {
                  System.out.println("sent " + t + " to "
                              
          + session.getRemoteSocketAddress());
              }

              @Override
              
          public void onSessionStarted(Session session) {
                  System.out.println("session started");
                  session.setUseBlockingRead(
          true);
                  session.setUseBlockingWrite(
          false);
              }

              
          public void onSessionCreated(Session session) {
                  System.out.println(session.getRemoteSocketAddress().toString()
                              
          + " connected");
              }

              
          public void onSessionClosed(Session session) {
                  System.out.println(session.getRemoteSocketAddress().toString()
                              
          + " disconnected");

              }

              
          public void onReceive(final Session session, final String msg) {
                  System.out.println("recv:" + msg);
                  
          if (msg != null)
                      dispatcher.dispatch(
          new Runnable() {
                          
          public void run() {

                              
          if (msg.equals("q"))
                                  session.close();
                              session.send(msg);
                          }
                      });
              }

          }


          2、實現(xiàn)EchoServer,核心是TCPController類的使用:

                  Configuration configuration = new Configuration();
                  configuration.setStatisticsServer(
          true);
                  configuration.setTcpSessionReadBufferSize(
          256 * 1024); // 設置讀的緩沖區(qū)大小
                  AbstractController controller = new TCPController(configuration,
                          
          new StringCodecFactory());
                  controller.setPort(
          8080); // 設置端口
                  controller.setReadThreadCount(1); // 設置讀線程數(shù),通常為1
                  controller.setReceiveBufferSize(16 * 1024); // 設置socket接收緩沖區(qū)大小
                  controller.setReuseAddress(false); // 設置是否重用端口
                  controller.setHandler(new EchoHandler()); // 設置handler
                  controller.setHandleReadWriteConcurrently(true); // 設置是否允許讀寫并發(fā)處理
                  controller.addStateListener(new ServerStateListener());
                  controller.start();

          Configuration 默認會在classpath查找yanf4j.properties屬性文件,用于配置服務器屬性,然而,你也看到,可以編碼設置這些屬性,具體參考wiki。

          3、然后?沒然后了,一個TCP的echo server已經搞定了,你可以telnet到8080端口試試了。

          二、UDP的Echo server
          1、handler,可以復用前面的EchoHandler
          2、UDP的核心類是UDPController:

                  Configuration configuration = new Configuration();
                  configuration.setTcpPort(
          8090);
                  configuration.setTcpReuseAddress(
          false);
                  configuration.setStatisticsServer(
          true);
                  configuration.setTcpNoDelay(
          true);
                  configuration.setTcpReadThreadCount(
          1);
                  configuration.setTcpRecvBufferSize(
          16 * 1024);
                  UDPController controller 
          = new UDPController(configuration);
                  controller.setMaxDatagramPacketLength(
          1024);
                  controller.setHandler(
          new EchoHandler());
                  controller.start();

            更多細節(jié),請參考項目主頁上的wiki



          評論

          # re: Yet another nio framework for java  回復  更多評論   

          2008-10-15 09:48 by wavefly
          編程風格和Mina有些類似:pre-event,看看你的源碼先……

          # re: Yet another nio framework for java  回復  更多評論   

          2008-10-15 10:17 by dennis
          @wavefly
          嗯,多多指教。整體架構上并無多少新意,仍然是reactor+事件觸發(fā)機制,只是在cindy的基礎上揉和了grizzly的一些長處。私下里說,其實就是俺想鍛煉學習nio罷了。

          # re: Yet another nio framework for java  回復  更多評論   

          2009-02-18 18:11 by 菜鳥問道
          能否舉例說明客戶端API調用?讓我學一下??

          # re: Yet another nio framework for java  回復  更多評論   

          2009-02-18 23:22 by dennis
          @菜鳥問道
          本來沒有客戶端API的,yanf4j一開始定位在服務器框架上。不過我最近添加了客戶端API支持,請從svn獲取

          # re: Yet another nio framework for java  回復  更多評論   

          2009-02-20 00:59 by 菜鳥問道
          請從svn獲取?
          多謝大俠回答,問下SVN是什么意思?

          # re: Yet another nio framework for java  回復  更多評論   

          2010-03-22 12:23 by yonlist
          厲害,膜拜一下,學習一下
          主站蜘蛛池模板: 抚远县| 巩留县| 凯里市| 故城县| 左云县| 鄂温| 桐柏县| 巴彦县| 郓城县| 江山市| 连州市| 镇康县| 勐海县| 班戈县| 荃湾区| 基隆市| 黄陵县| 海南省| 田东县| 普格县| 康定县| 云浮市| 鞍山市| 东城区| 安化县| 通河县| 广安市| 万宁市| 屯留县| 天柱县| 肃宁县| 巴塘县| 九龙城区| 皋兰县| 平和县| 咸阳市| 平利县| 米泉市| 义乌市| 界首市| 东丰县|