mina
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mina相关的知识,希望对你有一定的参考价值。
1、MINA是什么
MINA是apache的开源项目,是一个网络通信层封装框架。融合了多种通信协议,如:TCP/IP、UDP/IP等,采用NIO的非阻塞通信。
2、MINA的简单应用
2.1 下载相关jar包,注意日志的对应版本。
mina-core-2.0.7.jar
mina-example-2.0.7.jar
slf4j-api-1.6.6.jar
slf4j-log4j12-1.6.6.jar
log4j-1.2.17.jar
2.2 服务端代码
IoAcceptor acceptor = new NiosocketAcceptor(); DefaultIoFilterChainBuilder filterChain = acceptor.getFilterChain(); filterChain.addLast("logger", new LoggingFilter()); filterChain.addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName("UTF-8")))); acceptor.getSessionConfig().setReadBufferSize(2048); acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 3); acceptor.setHandler(new TimeServerHandler()); acceptor.bind(new InetSocketAddress(PORT) ); public class TimeServerHandler extends IoHandlerAdapter { public TimeServerHandler() { super(); } @Override public void sessionCreated(IoSession session) throws Exception { super.sessionCreated(session); System.err.println("session is created !"); } @Override public void sessionOpened(IoSession session) throws Exception { super.sessionOpened(session); System.err.println("session is open !"); } @Override public void sessionClosed(IoSession session) throws Exception { super.sessionClosed(session); System.err.println("session is closed !"); } @Override public void sessionIdle(IoSession session, IdleStatus status) throws Exception { super.sessionIdle(session, status); System.err.println("IDLE " + session.getIdleCount( status )); } @Override public void exceptionCaught(IoSession session, Throwable cause) throws Exception { super.exceptionCaught(session, cause); } @Override public void messageReceived(IoSession session, Object message) throws Exception { // super.messageReceived(session, message); String str = message.toString(); System.err.println("receive message:"+str); if( str.trim().equalsIgnoreCase("quit") ) { session.closeOnFlush(); return; } Date date = new Date(); session.write(date.toString()); System.out.println("Message written..."); } @Override public void messageSent(IoSession session, Object message) throws Exception { System.err.println("ready send message!"+message.toString()); super.messageSent(session, message); } @Override public void inputClosed(IoSession session) throws Exception { super.inputClosed(session); } }
2.3 客户端代码
NioSocketConnector connector = NioSocketConnector(); connector.setConnectTimeoutMillis(); DefaultIoFilterChainBuilder filterChain = connector.getFilterChain(); filterChain.addLast(, LoggingFilter()); filterChain.addLast(, ProtocolCodecFilter(TextLineCodecFactory(Charset.()))); connector.setHandler(TimeClientHandler()); IoSession session; (;;) { { ConnectFuture future = connector.connect(InetSocketAddress(, )); future.awaitUninterruptibly(); session = future.getSession(); ; } (RuntimeIoException e) { System..println(); e.printStackTrace(); Thread.(); } }
3、MINA的重要类解读
3.1 IoService的子类。
session的管理器,如:create、open及close;
filter chain的管理,如:logger、codec;
IoHandler的管理,跟实际业务相关。
sessionconfig的设置,如:readBufferSize、writeBufferSize、IdleTime
bind绑定地址及端口;
它同时也是服务器端IoAcceptor和客户端IoConnector的基础接口;
3.2 IoAcceptor 服务端接收客户端面的连接请求。
主要实现类:NioSocketAcceptor 、NioDatagramAcceptor 、AprSocketAcceptor 、VmPipeSocketAcceptor
3.3 IoConnector 客户端连接服务端的连接器。
主要实现类:NioSocketConnector 、NioDatagramConnector 、AprSocketConnector 、SerialConnector 等
3.3 session
是mina的核心,每次客户端连接服务器,都会在服务端和客户端分别创建一个session,一直到断开连接。
session的状态:
session状态判断方法:
isActive()
isClosing()
isConnected()
3.4 IoFilter 过滤器
1.内置的一些过滤器
如:ProtocolCodecFilter、CompressionFilter、ExecutorFilter、KeepAliveFilter、LoggingFilter、ProxyFilter、SslFilter
2.自定义过滤器
需要继承IoFilterAdapter
3.5 IoHandler 业务处理器
一般是继承IoHandlerAdapter来实现相关业务;
主要的方法有:
sessionCreated
sessionOpened
sessionClosed
sessionIdle
exceptionCaught
messageReceived
messageSent
4、MINA与spring整合
<bean id="trapHandler" class="com.ashishpaliwal.udp.mina.server.ServerHandler">
<bean id="snmpCodecFilter" class="org.apache.mina.filter.codec.ProtocolCodecFilter">
<constructor-arg>
<bean class="com.ashishpaliwal.udp.mina.snmp.SNMPCodecFactory" />
</constructor-arg>
</bean>
<bean id="loggingFilter" class="org.apache.mina.filter.logging.LoggingFilter" />
<bean id="filterChainBuilder" class="org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder">
<property name="filters">
<map>
<entry key="loggingFilter" value-ref="loggingFilter"/>
<entry key="codecFilter" value-ref="snmpCodecFilter"/>
</map>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.net.SocketAddress">
<bean class="org.apache.mina.integration.beans.InetSocketAddressEditor" />
</entry>
</map>
</property>
</bean>
<bean id="ioAcceptor" class="org.apache.mina.transport.socket.nio.NioDatagramAcceptor" init-method="bind" destroy-method="unbind">
<property name="defaultLocalAddress" value=":161" />
<property name="handler" ref="trapHandler" />
<property name="filterChainBuilder" ref="filterChainBuilder" />
</bean>
参考文档:http://mina.apache.org/mina-project/index.html
以上是关于mina的主要内容,如果未能解决你的问题,请参考以下文章