溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

mina sping 整合

發(fā)布時(shí)間:2020-06-05 22:17:11 來(lái)源:網(wǎng)絡(luò) 閱讀:1212 作者:乾坤刀 欄目:移動(dòng)開(kāi)發(fā)

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問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI