mina sping 整合

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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="10" />
       <constructor-arg index="1" value="20"/>
   </bean>
   <bean id="mdcInjectionFilter" class="org.apache.mina.filter.logging.MdcInjectionFilter">
       <constructor-arg value="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="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="10"/>
       <property name="receiveBufferSize" value="1024"/>
       <property name="sendBufferSize" value="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=":9123" />
       <property name="handler" ref="timeServerHandler" />
       <property name="filterChainBuilder" ref="filterChainBuilder" />
       <property name="reuseAddress"  value="true"/>
       <property name="backlog" value="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 整合的主要内容,如果未能解决你的问题,请参考以下文章

ssm整合总结--第二步之整合sping,sping mvc,bootstrap

ssm整合-Sping整合Mybatis框架06

ssm整合-Sping整合Mybatis框架06

ssm整合-Sping整合Mybatis框架配置事务07

ssm整合-Sping整合Mybatis框架配置事务07

sping整合redis,以及做mybatis的第三方缓存