DANCE WITH JAVA

开发出高质量的系统

常用链接

统计

积分与排名

好友之家

最新评论

apache mina (异步连接框架)介绍

一、介绍
mina(Multipurpose Infrastructure for Network Applications) 是apache的顶级项目之一,用于构造异步连接的各种网络应用
二、最简单的使用实例
package aicu.mina;
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.SimpleByteBufferAllocator;
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.SocketAcceptor;
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 {
        
//设置buffer
        ByteBuffer.setUseDirectBuffers(false);
        ByteBuffer.setAllocator(
new SimpleByteBufferAllocator());
        
//定义acceptor
        IoAcceptor acceptor = new SocketAcceptor();
        
//定义config
        SocketAcceptorConfig cfg = new SocketAcceptorConfig();
        
//设置config,加入filter
        cfg.getSessionConfig().setReuseAddress( true );
        cfg.getFilterChain().addLast( 
"logger"new LoggingFilter() );
        cfg.getFilterChain().addLast( 
"codec"new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ))));
        
//加入port handler cfg
        acceptor.bind( new InetSocketAddress(PORT), new TimeServerHandler(), cfg);
        System.out.println(
"MINA Time server started.");
    }

}

对应的handler ,handler的作用是在对应的网事件的处理代码
package aicu.mina;
import java.util.Date;

import org.apache.mina.common.IdleStatus;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
import org.apache.mina.common.TransportType;
import org.apache.mina.transport.socket.nio.SocketSessionConfig;

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();
        
//如果是quit就关闭session退出
        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 );
    }

}
三、使用方法:
编译上边两个类(需要加入mina的jar文件),然后运行terminalServer
开始->运行->cmd进入控制台
telnet 127.0.0.1 9123
输入hello
得到当前日期
输入quit关闭session。

posted on 2007-07-09 11:26 dreamstone 阅读(6374) 评论(6)  编辑  收藏 所属分类: 利器其它开源框架

评论

# re: apache mina (异步连接框架)介绍 2007-07-09 12:54 BeanSoft

NIO, 好啊, 支持!  回复  更多评论   

# re: apache mina (异步连接框架)介绍 2007-07-09 13:15 永恒

介绍的简单了点,能不能给个详细的介绍呢,呵呵  回复  更多评论   

# re: apache mina (异步连接框架)介绍 2007-07-09 14:09 flybean

Apache MINA (Multipurpose Infrastructure for Network Applications) is a network application framework which helps users develop high performance and high scalability network applications easily.

哪里看出来是异步?  回复  更多评论   

# re: apache mina (异步连接框架)介绍 2007-07-09 15:01 BeanSoft

啊, 不会吧, 自从 JDK 1.4 后, Tomcat 就用 NIO 改写了, 其它很多厂商也是, 毕竟 NIO 效率要高很多.  回复  更多评论   

# re: apache mina (异步连接框架)介绍 2007-07-09 16:49 dreamstone

@flybean
我也是刚开始接触,所说的异步是只mina内部,使用它封装的各种类,就实现了异步。我理解这个框架的作用就是不用自己再去实现异步。利用mina的类来编写程序,在mina内部实现了异步。这也是它的作用。  回复  更多评论   

# re: apache mina (异步连接框架)介绍 2009-10-18 17:03 tablechair

如果我要同步的,mina实现不了?  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: