温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

mina sping 整合

发布时间:2020-06-05 22:17:11 来源:网络 阅读:1219 作者:乾坤刀 栏目:移动开发

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/ch27-spring-integration/ch27-spring-integration.html

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI