你的位置:首页 > Java教程

[Java教程]mina2.0 spring


Apache MINA是一个网络应用程序框架,它可以帮助用户开发的高性能、高扩展性的网络应用程序。它提供了一个抽象的事件驱动的异步API在不同传输如TCP/IP和UDP/IP通过java NIO。

Apache MINA通常被称为:

NIO框架库,
客户端服务器框架库,或
一个网络socket库

code案例:

test.properties 配置文件

1 #mina2.02 mina.server.serverLocationAddress = 127.0.0.1:9998

 

applicationContext.

 1 <??> 2 <beans ="http://www.springframework.org/schema/beans" 3     ="http://www.w3.org/2001/ 4     xsi:schemaLocation="http://www.springframework.org/schema/beans 5     http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 6     "> 7  8   <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 9     <property name="locations">10       <value>classpath:propertiesConfig/test.properties</value>11     </property>12   </bean>13   <!-- 定义数据处理Bean -->14   <bean id="serverHandler" class="com.maven.project.web.mina2.ServerHandler" />15   16   <!-- executorFilter多线程处理 -->17   <bean id="executorFilter" class="org.apache.mina.filter.executor.ExecutorFilter" />18   19   <bean id="mdcInjectionFilter" class="org.apache.mina.filter.logging.MdcInjectionFilter">20     <constructor-arg value="remoteAddress" />21   </bean>22   23   <!-- 字符编 码过滤器 -->24   <bean id="codecFilter" class="org.apache.mina.filter.codec.ProtocolCodecFilter">25     <constructor-arg>26     <!-- <bean />-->27     <!-- 处理对象流时候用ObjectSerializationCodecFactory -->28     <!-- <bean /> -->29       <bean class="com.maven.project.web.mina2.ServerCodeFactory" />30     </constructor-arg>31   </bean>32   33   <!-- 日志过滤器 -->34   <bean id="loggingFilter" class="org.apache.mina.filter.logging.LoggingFilter" />35   36   <!-- 过滤器链 -->37   <bean id="filterChainBuilder" class="org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder">38     <property name="filters">39       <map>40         <entry key="executor" value-ref="executorFilter" />41         <entry key="mdcInjectionFilter" value-ref="mdcInjectionFilter" />42         <entry key="codecFilter" value-ref="codecFilter" />43         <entry key="loggingFilter" value-ref="loggingFilter" />44       </map>45     </property>46   </bean>47   48   <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">49     <property name="customEditors">50       <map>51        <entry key="java.net.SocketAddress" value="org.apache.mina.integration.beans.InetSocketAddressEditor"/>52       </map>53     </property>54   </bean>55   56   <!-- session config 通过工厂方法注入 -->57   <bean id="sessionConfig" factory-bean="ioAcceptor" factory-method="getSessionConfig" >58     <property name="bothIdleTime" value="180"/><!-- 配置session 空闲时间,单位 秒 -->59   </bean>60   61   <bean id="ioAcceptor" class="org.apache.mina.transport.socket.nio.NioSocketAcceptor" init-method="bind" destroy-method="unbind">62     <property name="defaultLocalAddress" value="${mina.server.serverLocationAddress}" />63     <property name="handler" ref="serverHandler" />64     <property name="filterChainBuilder" ref="filterChainBuilder" />65     <property name="reuseAddress" value="true" />66   </bean>67 </beans>

 

maven pom.

 1   <properties> 2     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 3     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 4     <mina-version>2.0.13</mina-version> 5     <mina-groupId>org.apache.mina</mina-groupId> 6   </properties> 7 <!-- mina --> 8     <dependency> 9       <groupId>${mina-groupId}</groupId>10       <artifactId>mina-core</artifactId>11       <version>${mina-version}</version>12     </dependency>13     <dependency>14       <groupId>${mina-groupId}</groupId>15       <artifactId>mina-integration-beans</artifactId>16       <version>${mina-version}</version>17     </dependency>

 

handler 业务处理类 

 1 package com.maven.project.web.mina2; 2  3 import org.apache.mina.core.service.IoHandlerAdapter; 4 import org.apache.mina.core.session.IdleStatus; 5 import org.apache.mina.core.session.IoSession; 6  7 public class ServerHandler extends IoHandlerAdapter { 8  9   /**当一个新客户端连接后触发此方法*/10   @Override 11   public void sessionCreated(IoSession session) throws Exception {12     System.out.println("===============sessionCreated=============");13   }14 15   /**当连接后打开时触发此方法,一般此方法与 sessionCreated 会被同时触发*/16   @Override17   public void sessionOpened(IoSession session) throws Exception {18     System.out.println("===============sessionOpened=============");19   }20 21   /**当连接被关闭时触发,例如客户端程序意外退出等等*/22   @Override23   public void sessionClosed(IoSession session) throws Exception {24     System.out.println("===============sessionClosed=============");25     if(null != session){26       session.closeOnFlush();27     }28   }29 30   /**当连接空闲时触发此方法*/31   @Override32   public void sessionIdle(IoSession session, IdleStatus status) throws Exception {33     System.out.println("===============sessionIdle=============");34     this.sessionClosed(session);35   }36 37   /**当接口中其他方法抛出异常未被捕获时触发此方法*/38   @Override39   public void exceptionCaught(IoSession session, Throwable cause) throws Exception {40     System.out.println("===============exceptionCaught=============");41     this.sessionClosed(session);42   }43 44   /**当接收到客户端的请求信息后触发此方法*/45   @Override46   public void messageReceived(IoSession session, Object message) throws Exception {47     System.out.println("===============messageReceived============="+message);48   }49 50   /**当信息已经传送给客户端后触发此方法*/51   @Override52   public void messageSent(IoSession session, Object message) throws Exception {53     System.out.println("===============messageSent=============");54   }55 56   @Override57   public void inputClosed(IoSession session) throws Exception {58     System.out.println("===============inputClosed=============");59     this.sessionClosed(session);60   }61 62 }

  

encode 类

 1 package com.maven.project.web.mina2; 2  3 import java.nio.charset.Charset; 4  5 import org.apache.mina.core.buffer.IoBuffer; 6 import org.apache.mina.core.session.IoSession; 7 import org.apache.mina.filter.codec.ProtocolEncoder; 8 import org.apache.mina.filter.codec.ProtocolEncoderOutput; 9 10 public class ServerEnCoder implements ProtocolEncoder {11 12   private Charset charset = null;13 14   public ServerEnCoder(Charset charset) {15     this.charset = charset;16   }17 18   @Override19   public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {20     IoBuffer buf = IoBuffer.allocate(message.toString().getBytes().length).setAutoExpand(true);21     buf.putString(message.toString(), charset.newEncoder());22     buf.flip();23     out.write(buf);24     return;25   }26 27   @Override28   public void dispose(IoSession session) throws Exception { }29 30 }

 

decode 类

 1 package com.maven.project.web.mina2; 2  3 import java.nio.charset.Charset; 4  5 import org.apache.mina.core.buffer.IoBuffer; 6 import org.apache.mina.core.session.IoSession; 7 import org.apache.mina.filter.codec.CumulativeProtocolDecoder; 8 import org.apache.mina.filter.codec.ProtocolDecoderOutput; 9 10 public class ServerDecoder extends CumulativeProtocolDecoder {11 12   private static final int HANDLENGTH = 4;13 14   private Charset charset = null;15 16   public ServerDecoder(Charset charset) {17     this.charset = charset;18   }19 20   @Override21   protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {22     if (in.remaining() < HANDLENGTH) {23       return false;24     }25     byte[] sizeBytes = new byte[HANDLENGTH];26     in.mark();// 标记当前位置,以便reset27     in.get(sizeBytes, 0, HANDLENGTH);// 读取4字节28     int size = Integer.parseInt(new String(sizeBytes, this.charset));29     in.reset();30     if (size > in.remaining()) {// 如果消息内容不够,则重置,相当于不读取size31       return false;// 父类接收新数据,以拼凑成完整数据32     } else {33       byte[] bytes = new byte[size];34       in.get(bytes, 0, size);35       out.write(new String(bytes, this.charset));36       if (in.remaining() > 0) {// 如果读取内容后还粘了包,就让父类再重读 一次,进行下一次解析37         return true;38       }39     }40     return false;// 处理成功,让父类进行接收下个包41   }42 }

 

编码过滤工厂

 1 package com.maven.project.web.mina2; 2  3 import java.nio.charset.Charset; 4  5 import org.apache.mina.core.session.IoSession; 6 import org.apache.mina.filter.codec.ProtocolCodecFactory; 7 import org.apache.mina.filter.codec.ProtocolDecoder; 8 import org.apache.mina.filter.codec.ProtocolEncoder; 9 10 public class ServerCodeFactory implements ProtocolCodecFactory {11 12   private ServerEnCoder encoder;13   14   private ServerDecoder decoder;15   16   public ServerCodeFactory(){17     this(Charset.defaultCharset().name());18   }19   20   public ServerCodeFactory(String charsetName){21     encoder = new ServerEnCoder(Charset.forName(charsetName));22     decoder = new ServerDecoder(Charset.forName(charsetName));23   }24   25   @Override26   public ProtocolEncoder getEncoder(IoSession session) throws Exception {27     return encoder;28   }29 30   @Override31   public ProtocolDecoder getDecoder(IoSession session) throws Exception {32     return decoder;33   }34 35 }