首页 > 代码库 > mina sping 整合

mina sping 整合

xml代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
                         http://www.springframework.org/schema/beans/spring-beans.xsd
                         "
       >
   <!-- The IoHandler implementation -->
   <bean id="timeServerHandler" class="com.spring.mina.TimeServerHandler" />

   <!--filter-->
   <bean id="loggingFilter" class="org.apache.mina.filter.logging.LoggingFilter" />
   <bean id="protocolCodecFilter" class="org.apache.mina.filter.codec.ProtocolCodecFilter" >
       <constructor-arg>
          <bean class="org.apache.mina.filter.codec.textline.TextLineCodecFactory">
          </bean>
       </constructor-arg>
   </bean>
   <bean id="executorFilter" class="org.apache.mina.filter.executor.ExecutorFilter">
       <constructor-arg index="0" value="http://www.mamicode.com/10" />
       <constructor-arg index="1" value="http://www.mamicode.com/20"/>
   </bean>
   <bean id="mdcInjectionFilter" class="org.apache.mina.filter.logging.MdcInjectionFilter">
       <constructor-arg value="http://www.mamicode.com/remoteAddress"/>
   </bean>

   <!-- The filter chain. -->
   <bean id="filterChainBuilder" class="org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder">
       <property name="filters">
           <map>
               <entry key="executor" value-ref="executorFilter"/>
               <entry key="mdcInjectionFilter" value-ref="mdcInjectionFilter"/>
               <entry key="loggingFilter" value-ref="loggingFilter"/>
               <entry key="codecFilter" value-ref="protocolCodecFilter"/>
           </map>
       </property>
   </bean>

   <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
       <property name="customEditors">
           <map>
               <entry key="java.net.SocketAddress" value="http://www.mamicode.com/org.apache.mina.integration.beans.InetSocketAddressEditor">
               </entry>
           </map>
       </property>
   </bean>

   <!-- session config -->
   <bean id="sessionConfig" factory-bean="ioAcceptor" factory-method="getSessionConfig" >
       <property name="bothIdleTime" value="http://www.mamicode.com/10"/>
       <property name="receiveBufferSize" value="http://www.mamicode.com/1024"/>
       <property name="sendBufferSize" value="http://www.mamicode.com/1024"/>
   </bean>

   <!-- The IoAcceptor which binds to port 9123-->
   <bean id="ioAcceptor" class="org.apache.mina.transport.socket.nio.NioSocketAcceptor" init-method="bind" destroy-method="unbind">
       <property name="defaultLocalAddress" value="http://www.mamicode.com/:9123" />
       <property name="handler" ref="timeServerHandler" />
       <property name="filterChainBuilder" ref="filterChainBuilder" />
       <property name="reuseAddress"  value="http://www.mamicode.com/true"/>
       <property name="backlog" value="http://www.mamicode.com/100"/>
   </bean>

</beans>
    
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:" +status.toString() +";count:"+ 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);
    }
}
public class Test {
    public static void main(String[] args)
    {
        new ClassPathXmlApplicationContext("spring-config.xml");
    }
}

参考文档:

http://www.tuicool.com/articles/JRzmqi

http://mina.apache.org/mina-project/userguide/ch17-spring-integration/ch17-spring-integration.html

mina sping 整合