directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Directory Wiki] Update of "MinaTutorialInChinese" by donald
Date Sat, 06 Aug 2005 01:32:58 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Directory Wiki" for change notification.

The following page has been changed by donald:
http://wiki.apache.org/directory/MinaTutorialInChinese

------------------------------------------------------------------------------
    * sessionClosed: 当IO连接被关闭时被调用。
    * sessionIdle: 当在远程实体和用户程序之间没有数据传输的时候被调用。
    * exceptionCaught: 当IoAcceptor 或者你的IoHandler.中出现异常时被调用。
-   * dataRead: 当从远程实体读取数据时被调用。
+   * dataRead: 当从远程实体读取数据时被调用。
    * dataWritten: 当你想远程实体发出请求时被调用
  
  下面我们看看如何实现echo协议的IoHandler。
@@ -229, +229 @@

  
  === 实现 ProtocolProvider 以及启动代码 ===
  
+ 要实现反转协议要实作的唯一接口就是ProtocolProvider。它非常简单:
+ (注:Provider用于在一个统一的类中提供该协议相关的Handler、Decoder和Encoder。)
  
+ {{{
+ package org.apache.mina.examples.reverser;
  
+ import org.apache.mina.protocol.*;
+ 
+ /**
+  * {@link ProtocolProvider} implementation for reverser server protocol.
+ */
+ public class ReverseProtocolProvider implements ProtocolProvider
+ {
+     // Protocol handler is usually a singleton.
+     private static ProtocolHandler HANDLER =
+ new ReverseProtocolHandler();
+ 
+     // Codec factory is also usually a singleton.
+     private static ProtocolCodecFactory CODEC_FACTORY =
+ new ProtocolCodecFactory()
+     {
+         public ProtocolEncoder newEncoder()
+         {
+             // Create a new encoder.
+             return new TextLineEncoder();
+         }
+ 
+         public ProtocolDecoder newDecoder()
+         {
+             // Create a new decoder.
+             return new TextLineDecoder();
+         }
+     };
+ 
+     public ProtocolCodecFactory getCodecFactory()
+     {
+         return CODEC_FACTORY;
+     }
+ 
+     public ProtocolHandler getHandler()
+     {
+         return HANDLER;
+     }
+ }
+ }}}
+ 
+ 这样,反转协议就被完全实现了。启动的部分同echo server非常相似:
+ 
+ {{{
+ package org.apache.mina.examples.reverser;
+ 
+ import org.apache.mina.common.*;
+ import org.apache.mina.protocol.*;
+ import org.apache.mina.registry.*;
+ 
+ /**
+  * (<b>Entry point</b>) Reverser server which reverses all text lines from
+  * clients.
+  * 
+  * @author Trustin Lee (trustin@apache.org)
+  * @version $Rev: 165594 $, $Date: 2005-05-02 16:21:22 +0900 $,
+  */
+ public class Main
+ {
+     private static final int PORT = 8080;
+ 
+     public static void main( String[] args ) throws Exception
+     {
+         ServiceRegistry registry = new SimpleServiceRegistry();
+ 
+         // Bind
+         Service service = new Service( "reverse", TransportType.SOCKET, PORT );
+         registry.bind( service, new ReverseProtocolProvider() );
+ 
+         System.out.println( "Listening on port " + PORT );
+     }
+ }
+ }}}
+ 
+ === 添加 ProtocolFilters ===
+ 
+ ProtocolFilter 同IO层的IoFilter类似:
+ 
+ attachment:Arch4.gif
+ 
+ 添加IoLoggingFilter来记录底层IO事件是为了debug。我们可以用ProtocolLoggingFilter代替它来记录高层事件:
+ 
+ {{{
+     private static void addLogger( ServiceRegistry registry )
+     {
+         ProtocolAcceptor acceptor = registry.getProtocolAcceptor( TransportType.SOCKET );
+         acceptor.getFilterChain().addLast( "logger", new ProtocolLoggingFilter() );
+         System.out.println( "Logging ON" );
+     }
+ }}}
+ 
+ == 高级主题 ==
+ 
+ 在这里我们为MINA的高级用户讲解一些高级话题。
+ 
+ === ByteBuffers ===
+ 
+ MINA没有直接使用使用java NIO的ByteBuffer类。它使用一个自制的ByteBuffer来扩展java
NIO ByteBuffer的功能。
+ 以下是它们的一些区别:
+   * MINA ByteBuffer是一个抽象类,用户可以自由的扩展它
+   * MINA manages and pools MINA ByteBuffers. Users can control the point the buffers are
released by providing acquire() and release() methods. 
+   * MINA ByteBuffer提供很多便利的方法,如:无符号数值的getter和基于String的getter和putter
+ 如果你使用MINA,你将不需要直接使用NIO buffers,因为仅使用MINA buffers就可以完成大多数buffer操作。
+ 
+ ==== ByteBuffer 池====
+ 
+ MINA有一个全局的ByteBuffer池,它被在同一个虚拟机下的所有MINA应用共享。任何分配的buffers将在IO操作或者事件处理方法被执行之后被释放。所以你可以调用ByteBuffer.allocate()来从池中得到一个ByteBuffer而不需要将它返回到池中。请查阅ByteBuffer
JavaDocs获得更多信息。
+ 
+ === 线程模式 ===
+ 
+ MINA提供通过它灵活的filter机制来提供多种线程模型。没有线程池filter被使用时MINA运行在一个单线程模式。如果添加了一个IoThreadPoolFilter
到IoAcceptor,你将得到一个leader-follower模式的线程池。如果在添加一个ProtocolThreadPoolFilter,你的server将有两个线程池;一个(IoThreadPoolFilter)被用于对message对象进行转换,另外一个(ProtocolThreadPoolFilter)被用于处理业务逻辑。
+ 
+ SimpleServiceRegistry加上IoThreadPoolFilter和ProtocolThreadPoolFilter的缺省实现即可适用于需要高伸缩性的应用。如果你想使用自己的线程模型,请查看SimpleServiceRegistry的源代码,并且自己初始化Acceptor。显然,这是个繁琐的工作。
+ 
+ {{{
+ IoThreadPoolFilter threadPool = new IoThreadPoolFilter();
+ threadPool.start();
+ 
+ IoAcceptor acceptor = new SocketAcceptor();
+ acceptor.getFilterChain().addLast( "threadPool", threadPool );
+ 
+ ProtocolThreadPoolFilter threadPool2 = new ProtocolThreadPoolFilter();
+ threadPool2.start();
+ 
+ ProtocolAcceptor acceptor2 = new IoProtocolAcceptor( acceptor );
+ acceptor2.getFilterChain().addLast( "threadPool", threadPool2 );
+ 
+ ...
+ 
+ threadPool2.stop();
+ threadPool.stop();
+ }}}
+ 
+ === 更复杂的协议支持 ===
+ 
+ ‘Reverser’示例相对于其他复杂的协议来说仍然过于简单。要想让一个server工作,仍然有许多message类型和它们的转换的工作需要作。MINA提供了一下工具类来提供帮助:
+ 
+   * DemuxingProtocolHandler
+   * DemuxingProtocolCodecFactory
+ 
+ 更多细节请参考 JavaDocs 。
+ 
+ === VM 内部管道通讯===
+ 
+ 你一定已经知道协议层是建立在IO层之上的,但是有时也不一定。虽然我们通常使用协议层来包装IO层,但仍有一种特殊的协议层实现,称作:’
in-VM pipe communication’
+ 让我们假设你需要使用MINA实现一个SMTP server和一个Spam Filter server。SMTP
server可能需要同Spam Filter server通讯以便发现spam message或者RBL中列出的客户端。如果这两个server是在同一个java虚拟机中,一个IO层是多余的,你可以绕过message对象的编解码的过程。In-VM
pipe communication可以使你使用同样的代码而不管spam filter server是否在同一个虚拟机中。
+ 请查看随源码分发的’ Tennis’示例。
+ 
+ == How to Contribute ==
+ 
+ We want MINA to evolve actively, reacting to user requests, and therefore we need as much
feedback from you as possible. The Apache Directory team will strive to satisfy all possible
use cases of MINA. Please feel free to contact us. 
+ 
+ === How to Contact Us ===
+ 
+   * Mailing list: dev@directory.apache.org (Please use ‘[mina]’ prefix)
+   * Issue tracker: http://issues.apache.org/jira/browse/DIRMINA
+ 
+ === How to Report Bugs ===
+ 
+ You can report any bugs found from MINA to our issue tracker page.  Please attach any test
cases that can reproduce your issue if possible.
+ 
+ === How Issues ===
+ 
+ Any patches and comments are welcome!  You can browse the list of unresolved issues in JIRA:
+ 
+   * http://issues.apache.org/jira/browse/DIRMINA
+ 
+ Or, you could do some performance benchmarks on MINA and tune it.
+ 
+ == Acknowledgements ==
+ 
+ MINA couldn’t exist without strong support of many contributors:
+ 
+   * The Apache Directory team for letting me join the team
+   * All users of Netty2 forum and dev@directory.apache.org for great feedbacks
+   * Jan Andersson and his team for SSLFilter
+   * Vel Pandian for enabling client mode for SSLFilter
+   * Vinod Panicker for performance benchmark and active feedbacks
+ 

Mime
View raw message