码迷,mamicode.com
首页 > 其他好文 > 详细

mina sping 整合

时间:2017-04-29 09:41:21      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:mina spring

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 整合

标签:mina spring

原文地址:http://881206524.blog.51cto.com/10315134/1920643

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!