Apache MINA 快速入门指南-时间服务器

最近用到Socket套接字编程,在服务器监听方面还没有具体思路,朋友推荐了Apahce Mina,就在官方看了一下快速入门文档。原文是英文的,学习之余就将它翻译出来和大家共享!关于Mina的中文简介内容不多就摘抄了一些。

 

    “MINA是一个Socket的网络框架,但是它提供了方便的Protocol支持,通过它的Encoder和Decoder,你将你的应用可以方便的扩展并支持各种基于Socket的网络协议,比如HTTP服务器、FTP服务器(当然,这很复杂)、Telnet服务器等等。基于MINA用户可以容易地开发高性能和高伸缩性的网络应用程序。”

 

下面是对官方入门文档的中文译版,由于完全参照官方文档在程序具体运行时遇到一些import不足的情况,在示例源码的import是我在亲自实践后修改过的,其他的地方都和原文一样。

 

Apache MINA 快速入门

Added by Mark Webb, last edited by Trustin Lee on Apr 16, 2007    (view change)

 

 

1    简介

 

建立一个基于MINA的时间服务器,下面的内容需要先准备好。

1,Mina 1.1 Core
2,JDK 1.5 or greater
3,SLF4J 1.3.0 or greater :
   Log4J 1.2 users: slf4j-api.jar, slf4j-log4j12.jar, and Log4J 1.2.x
   Log4J 1.3 users: slf4j-api.jar, slf4j-log4j13.jar, and Log4J 1.3.x
   java.util.logging users: slf4j-api.jar and slf4j-jdk14.jar


   注意:首先请确认你使用了正确的slf4j-*.jar 。比如说slf4j-log4j12.jar 和log4j-1.3.x.jar 是不能在一起使用的将会出现功能障碍。


 (我用了1.2系列,那么所有的软件包就是:
 log4j-1.2.14.jar;
 mina-core-1.1.2.jar;
 slf4j-api-1.2.jar;
 slf4j-jdk14-1.2.jar;
 slf4j-log4j12-1.2.jar)

 

这个程序只测试了Windows2000pro 和 Linux系统,并且在做的时候没有依赖于一些开发平台的环境。

 

2    编写MINA时间服务

 

下面先建立一个文件MinaTimeServer.java,代码如下:

 

Java代码 复制代码
  1. public class MinaTimeServer {   
  2.     public static void main(String[] args) {   
  3.       // code will go here next   
  4.     }   
  5. }  
  1. <SPAN style="FONT-FAMILY: andale mono, times"><SPAN style="FONT-SIZE: medium">public class MinaTimeServer {  
  2.     public static void main(String[] args) {  
  3.       // code will go here next   
  4.     }  
  5. }  
  6.   
  7. </SPAN></SPAN>  
<span style="font-family:andale mono, times;"><span style="font-size:14px;">public class MinaTimeServer {
    public static void main(String[] args) {
      // code will go here next
    }
}

</span></span>

 

下面会慢慢将这个类写完,这里定义一个main用于启动程序。这一步结束后,还需要一个监听连接的对象,因为这个程序是基于TCP/IP的,这里将增加一个SocketAcceptor。

  

Java代码 复制代码
  1. import org.apache.mina.common.IoAcceptor;   
  2. import org.apache.mina.transport.socket.nio.SocketAcceptor;   
  3.   
  4. public class MinaTimeServer {   
  5.     public static void main(String[] args) {   
  6.       // The following two lines change the default buffer type to 'heap',   
  7.       // which yields better performance.   
  8.       ByteBuffer.setUseDirectBuffers(false);   
  9.       ByteBuffer.setAllocator(new SimpleByteBufferAllocator());   
  10.       IoAcceptor acceptor = new SocketAcceptor();   
  11.     }   
  12. }  
  1. <SPAN style="FONT-FAMILY: andale mono, times"><SPAN style="FONT-SIZE: medium">import org.apache.mina.common.IoAcceptor;  
  2. import org.apache.mina.transport.socket.nio.SocketAcceptor;  
  3.   
  4. public class MinaTimeServer {  
  5.     public static void main(String[] args) {  
  6.       // The following two lines change the default buffer type to 'heap',   
  7.       // which yields better performance.   
  8.       ByteBuffer.setUseDirectBuffers(false);  
  9.       ByteBuffer.setAllocator(new <SPAN class=hilite1><SPAN style="BACKGROUND-COLOR: #ffff00">SimpleByteBufferAllocator</SPAN></SPAN>());  
  10.       IoAcceptor acceptor = new SocketAcceptor();  
  11.     }  
  12. }  
  13.   
  14. </SPAN></SPAN>  
<span style="FONT-FAMILY: andale mono, times"><span style="font-size:14px;">import org.apache.mina.common.IoAcceptor;
import org.apache.mina.transport.socket.nio.SocketAcceptor;

public class MinaTimeServer {
    public static void main(String[] args) {
      // The following two lines change the default buffer type to 'heap',
      // which yields better performance.
      ByteBuffer.setUseDirectBuffers(false);
      ByteBuffer.setAllocator(new <span class="hilite1"><span style="BACKGROUND-COLOR: #ffff00">SimpleByteBufferAllocator</span></span>());
      IoAcceptor acceptor = new SocketAcceptor();
    }
}

</span></span>

  

通过这里的SocketAcceptor类,下面将把它绑定到一个端口上,如果你想增加一个线程模型到该类的话,参考"配置线程模型"部分。

  

   现在我们为SocketAcceptor添加设置。它允许我们为Socekt配置明确套接字用于允许来自客户端的连接。

  

Java代码 复制代码
  1. import java.io.IOException;   
  2. import java.net.InetSocketAddress;   
  3. import org.apache.mina.common.IoAcceptor;   
  4. import org.apache.mina.transport.socket.nio.SocketAcceptor;   
  5.   
  6. public class MinaTimeServer {   
  7.   
  8.     private static final int PORT = 9123;   
  9.   
  10.     public static void main(String[] args) throws IOException {   
  11.         ByteBuffer.setUseDirectBuffers(false);   
  12.         ByteBuffer.setAllocator(new SimpleByteBufferAllocator());   
  13.   
  14.         IoAcceptor acceptor = new SocketAcceptor();   
  15.   
  16.         SocketAcceptorConfig cfg = new SocketAcceptorConfig();   
  17.         cfg.getSessionConfig().setReuseAddress( true );   
  18.         cfg.getFilterChain().addLast( "logger"new LoggingFilter() );   
  19.         cfg.getFilterChain().addLast( "codec"new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ))));   
  20.   
  21.     }   
  22. }  
  1. <SPAN style="FONT-FAMILY: andale mono, times"><SPAN style="FONT-SIZE: medium">import java.io.IOException;  
  2. import java.net.InetSocketAddress;  
  3. import org.apache.mina.common.IoAcceptor;  
  4. import org.apache.mina.transport.socket.nio.SocketAcceptor;  
  5.   
  6. public class MinaTimeServer {  
  7.   
  8.     private static final int PORT = 9123;  
  9.   
  10.     public static void main(String[] args) throws IOException {  
  11.         ByteBuffer.setUseDirectBuffers(false);  
  12.         ByteBuffer.setAllocator(new <SPAN class=hilite1><SPAN style="BACKGROUND-COLOR: #ffff00">SimpleByteBufferAllocator</SPAN></SPAN>());  
  13.   
  14.         IoAcceptor acceptor = new SocketAcceptor();  
  15.   
  16.         SocketAcceptorConfig cfg = new SocketAcceptorConfig();  
  17.         cfg.getSessionConfig().setReuseAddress( true );  
  18.         cfg.getFilterChain().addLast( "logger"new LoggingFilter() );  
  19.         cfg.getFilterChain().addLast( "codec"new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ))));  
  20.   
  21.     }  
  22. }  
  23.   
  24.   
  25. </SPAN></SPAN>  
<span style="FONT-FAMILY: andale mono, times"><span style="font-size:14px;">import java.io.IOException;
import java.net.InetSocketAddress;
import org.apache.mina.common.IoAcceptor;
import org.apache.mina.transport.socket.nio.SocketAcceptor;

public class MinaTimeServer {

    private static final int PORT = 9123;

    public static void main(String[] args) throws IOException {
        ByteBuffer.setUseDirectBuffers(false);
        ByteBuffer.setAllocator(new <span class="hilite1"><span style="BACKGROUND-COLOR: #ffff00">SimpleByteBufferAllocator</span></span>());

        IoAcceptor acceptor = new SocketAcceptor();

        SocketAcceptorConfig cfg = new SocketAcceptorConfig();
        cfg.getSessionConfig().setReuseAddress( true );
        cfg.getFilterChain().addLast( "logger", new LoggingFilter() );
        cfg.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ))));

    }
}


</span></span>

 

 

   SocketAcceptorConfig类的实例用于当我们每次准备启动acceptor的时候进入这个acceptor。


    首先,我们设置一个重用地址标识。从 JDK Documentation可以了解更多关于这里的知识。

 

     然后我们在配置中添加一个过滤器。这个过滤器filter将记录所有信息,例如最近创建的sessions,收到的消息,发送

     的消息,session关闭。

 

    下一个过滤器是一个ProtocolCodecFilter。这个过滤器可以将二进制数据或编码协议数据转换成消息对象和代替法。

 下面的部分来为acceptor绑定端口。这个方法标志着服务器进程的启动,如果这个方法没有被调用,服务器将不会与客户端进行连接

   

Java代码 复制代码
  1. import java.io.IOException;   
  2. import java.net.InetSocketAddress;   
  3. import java.nio.charset.Charset;   
  4.   
  5. import org.apache.mina.common.ByteBuffer;   
  6. import org.apache.mina.common.IoAcceptor;   
  7. import org.apache.mina.common.SimpleByteBufferAllocator;    
  8. import org.apache.mina.transport.socket.nio.SocketAcceptor;   
  9. import org.apache.mina.filter.LoggingFilter;   
  10. import org.apache.mina.filter.codec.ProtocolCodecFilter;   
  11. import org.apache.mina.filter.codec.textline.TextLineCodecFactory;   
  12. import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;   
  13.   
  14. public class MinaTimeServer {   
  15.   
  16.     private static final int PORT = 9123;   
  17.   
  18.     public static void main(String[] args) throws IOException {   
  19.         ByteBuffer.setUseDirectBuffers(false);   
  20.         ByteBuffer.setAllocator(new SimpleByteBufferAllocator());   
  21.   
  22.         IoAcceptor acceptor = new SocketAcceptor();   
  23.   
  24.         SocketAcceptorConfig cfg = new SocketAcceptorConfig();   
  25.         cfg.getFilterChain().addLast( "logger"new LoggingFilter() );   
  26.         cfg.getFilterChain().addLast( "codec"new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ))));   
  27.   
  28.         acceptor.bind( new InetSocketAddress(PORT), new TimeServerHandler(), cfg);   
  29.         System.out.println("MINA Time server started.");   
  30.     }   
  31. }  
  1. <SPAN style="FONT-FAMILY: andale mono, times"><SPAN style="FONT-SIZE: medium">import java.io.IOException;  
  2. import java.net.InetSocketAddress;  
  3. import java.nio.charset.Charset;  
  4.   
  5. import org.apache.mina.common.ByteBuffer;  
  6. import org.apache.mina.common.IoAcceptor;  
  7. import org.apache.mina.common.<SPAN class=hilite1><SPAN style="BACKGROUND-COLOR: #ffff00">SimpleByteBufferAllocator</SPAN></SPAN>;   
  8. import org.apache.mina.transport.socket.nio.SocketAcceptor;  
  9. import org.apache.mina.filter.LoggingFilter;  
  10. import org.apache.mina.filter.codec.ProtocolCodecFilter;  
  11. import org.apache.mina.filter.codec.textline.TextLineCodecFactory;  
  12. import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;  
  13.   
  14. public class MinaTimeServer {  
  15.   
  16.     private static final int PORT = 9123;  
  17.   
  18.     public static void main(String[] args) throws IOException {  
  19.         ByteBuffer.setUseDirectBuffers(false);  
  20.         ByteBuffer.setAllocator(new <SPAN class=hilite1><SPAN style="BACKGROUND-COLOR: #ffff00">SimpleByteBufferAllocator</SPAN></SPAN>());  
  21.   
  22.         IoAcceptor acceptor = new SocketAcceptor();  
  23.   
  24.         SocketAcceptorConfig cfg = new SocketAcceptorConfig();  
  25.         cfg.getFilterChain().addLast( "logger"new LoggingFilter() );  
  26.         cfg.getFilterChain().addLast( "codec"new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ))));  
  27.   
  28.         acceptor.bind( new InetSocketAddress(PORT), new TimeServerHandler(), cfg);  
  29.         System.out.println("MINA Time server started.");  
  30.     }  
  31. }  
  32.   
  33. </SPAN></SPAN>  
<span style="FONT-FAMILY: andale mono, times"><span style="font-size:14px;">import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.common.ByteBuffer;
import org.apache.mina.common.IoAcceptor;
import org.apache.mina.common.<span class="hilite1"><span style="BACKGROUND-COLOR: #ffff00">SimpleByteBufferAllocator</span></span>; 
import org.apache.mina.transport.socket.nio.SocketAcceptor;
import org.apache.mina.filter.LoggingFilter;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;

public class MinaTimeServer {

    private static final int PORT = 9123;

    public static void main(String[] args) throws IOException {
        ByteBuffer.setUseDirectBuffers(false);
        ByteBuffer.setAllocator(new <span class="hilite1"><span style="BACKGROUND-COLOR: #ffff00">SimpleByteBufferAllocator</span></span>());

        IoAcceptor acceptor = new SocketAcceptor();

        SocketAcceptorConfig cfg = new SocketAcceptorConfig();
        cfg.getFilterChain().addLast( "logger", new LoggingFilter() );
        cfg.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ))));

        acceptor.bind( new InetSocketAddress(PORT), new TimeServerHandler(), cfg);
        System.out.println("MINA Time server started.");
    }
}

</span></span>

 

 

这里定义了一个整型的端口变量,呼叫SocketAcceptor.bind(SocketAddress,IoHandler,cfg),第一个参数是要监听的网址,是本地的9123端口

 

第二个参数传的是实现IoHandler接口的类,是服务于所有的客户端请求的。在这里,将会扩展IoHandlerAdapter类,这类遵循"适配器设计模式"的。

 

第三个参数是配置对象,用于配置日志和编码过滤器。每一个信息都会通过在IoAcceptor中定义的过滤器链的所有过滤器。在这风景点,将会将信息通过日志和编码过滤器。日志过滤器用SL4J库记录信息,而编码过滤器则反编码所有收到的信息,并且将所有TextLineCodecFactory发送的信息进行编码。

 

   下面就是TimeServerHandler类的代码: 

  

Java代码 复制代码
  1. import java.util.Date;   
  2. import org.apache.mina.common.IoHandlerAdapter;   
  3. import org.apache.mina.common.IoSession;   
  4.   
  5. public class TimeServerHandler extends IoHandlerAdapter {   
  6.   
  7.     public void exceptionCaught(IoSession session, Throwable t) throws Exception {   
  8.       t.printStackTrace();   
  9.       session.close();   
  10.     }   
  11.   
  12.     public void messageReceived(IoSession session, Object msg) throws Exception {   
  13.       String str = msg.toString();   
  14.       if( str.trim().equalsIgnoreCase("quit") ) {   
  15.         session.close();   
  16.         return;   
  17.       }   
  18.       Date date = new Date();   
  19.       session.write( date.toString() );   
  20.       System.out.println("Message written...");   
  21.     }   
  22.   
  23.     public void sessionCreated(IoSession session) throws Exception {   
  24.       System.out.println("Session created...");   
  25.       if( session.getTransportType() == TransportType.SOCKET )   
  26.         ((SocketSessionConfig) session.getConfig() ).setReceiveBufferSize( 2048 );   
  27.       session.setIdleTime( IdleStatus.BOTH_IDLE, 10 );   
  28.     }   
  29. }  
  1. <SPAN style="FONT-FAMILY: andale mono, times"><SPAN style="FONT-SIZE: medium">import java.util.Date;  
  2. import org.apache.mina.common.IoHandlerAdapter;  
  3. import org.apache.mina.common.IoSession;  
  4.   
  5. public class TimeServerHandler extends IoHandlerAdapter {  
  6.   
  7.     public void exceptionCaught(IoSession session, Throwable t) throws Exception {  
  8.       t.printStackTrace();  
  9.       session.close();  
  10.     }  
  11.   
  12.     public void messageReceived(IoSession session, Object msg) throws Exception {  
  13.       String str = msg.toString();  
  14.       if( str.trim().equalsIgnoreCase("quit") ) {  
  15.         session.close();  
  16.         return;  
  17.       }  
  18.       Date date = new Date();  
  19.       session.write( date.toString() );  
  20.       System.out.println("Message written...");  
  21.     }  
  22.   
  23.     public void sessionCreated(IoSession session) throws Exception {  
  24.       System.out.println("Session created...");  
  25.       if( session.getTransportType() == TransportType.SOCKET )  
  26.         ((SocketSessionConfig) session.getConfig() ).setReceiveBufferSize( 2048 );  
  27.       session.setIdleTime( IdleStatus.BOTH_IDLE, 10 );  
  28.     }  
  29. }  
  30.   
  31. </SPAN></SPAN>  
<span style="font-family:andale mono, times;"><span style="font-size:14px;">import java.util.Date;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;

public class TimeServerHandler extends IoHandlerAdapter {

    public void exceptionCaught(IoSession session, Throwable t) throws Exception {
      t.printStackTrace();
      session.close();
    }

    public void messageReceived(IoSession session, Object msg) throws Exception {
      String str = msg.toString();
      if( str.trim().equalsIgnoreCase("quit") ) {
        session.close();
        return;
      }
      Date date = new Date();
      session.write( date.toString() );
      System.out.println("Message written...");
    }

    public void sessionCreated(IoSession session) throws Exception {
      System.out.println("Session created...");
      if( session.getTransportType() == TransportType.SOCKET )
        ((SocketSessionConfig) session.getConfig() ).setReceiveBufferSize( 2048 );
      session.setIdleTime( IdleStatus.BOTH_IDLE, 10 );
    }
}

</span></span>

 

这里用于管理信息,覆盖了exceptionCaught,messageReceived 和 sessionCreated 方法,如前所示,该类扩展了IoHandlerAdapter。

 

exceptionCaught 方法将会打印错误并且关闭对话,对于大多数的情况来讲,这是标准的处理方法,除非能从异常中恢复过来。

 

messageReceived 方法将收到从客户端发来的数据,并且写回当前时间。如果收到了"quit",对话将被关闭。该方法将当前时间发往客户端,依赖于你使用的协议编码,发送至方法的对象(第二个参数)会有不同,发送到session.write(Object)方法的对象类同。如果你没有指定协议编码,则一般会收到ByteBuffer对象,而发送的也要是ByteBuffer对象。

 

sessionCreated 方法用于对话初始化,在这里,先打印一条信息,然后判断对话的类型,再设置缓冲大小,这里设置的是2048个字节。空闲时间设置为10秒,如果覆盖了sessionIdle方法,则该方法每10秒被呼叫一次。

 

3    测试

 

到这里,编译程序。如果成功,那么启动程序,登陆telnet访问程序,如下所示:

 

客户端内容:
user@myhost:~> telnet 127.0.0.1 9123
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
hello
Mon Apr 09 23:42:55 EDT 2007
quit
Connection closed by foreign host.


user@myhost:~>

 

服务端内容:
MINA Time server started.
Session created...
Message written...

 

4 参考文档

    Apache MINA Quick Start Guide http://mina.apache.org/documentation.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值