mina在spring中的配置多个端口

本次练习中是监听2个端口

applicationContext-mina.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//spring//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd "> 
<beans>

 

 <bean 
  class="org.springframework.beans.factory.config.CustomEditorConfigurer"> 
  <property name="customEditors"> 
   <map> 
    <entry key="Java.net.SocketAddress"> 
     <bean 
      class="org.apache.mina.integration.spring.InetSocketAddressEditor" /> 
    </entry> 
   </map> 
  </property> 
 </bean>

 

 

<!--创建服务器-->

 <bean id="ioAcceptor" 
  class="org.apache.mina.integration.spring.IoAcceptorFactoryBean"> 
  <property name="target"> 
   <bean 
    class="org.apache.mina.transport.socket.nio.SocketAcceptor" /> 
  </property> 
  <property name="bindings"> 
    <list>//这里是个list 里面可以创建多个监听 
    <bean 
     class="org.apache.mina.integration.spring.Binding"> 
     <property name="address" value=":8888" /> //监听端口:8888 
     <property name="handler" ref="SampleHandler" />// SampleHandler:定义服务器的handler 
     <property name="serviceConfig"> 
      <bean 
       class="org.apache.mina.transport.socket.nio.SocketAcceptorConfig"> 
       <property name="filterChainBuilder "//配置filter 
        ref="filterChainBuilder" /> //指向: filterChainBuilder 
       <property name="reuseAddress" value="true" /> 
      </bean> 
     </property> 
    </bean> 
     
    <bean 
     class="org.apache.mina.integration.spring.Binding"> 
     <property name="address" value=":9999" /> //第二个监听端口:9999 
     <property name="handler" ref="bossSampleHandler" /> //监听该端口的 handler 
     <property name="serviceConfig"> 
      <bean 
       class="org.apache.mina.transport.socket.nio.SocketAcceptorConfig"> 
       <property name="filterChainBuilder" 
        ref="filterChainBuilder" /> 
       <property name="reuseAddress" value="true" /> 
      </bean> 
     </property> 
    </bean> 
     
   </list> 
  </property> 
 </bean> 
 <bean id="SampleHandler" class="cn.org.handler.HandlerTwo" /> 
 <bean id="bossSampleHandler" class="cn.org.handler.HandlerOne" />

 

 

//定义filter 
 <bean id="filterChainBuilder" 
  class="org.apache.mina.integration.spring.DefaultIoFilterChainBuilderFactoryBean"> 
  <property name="filters"> 
   <list> 
    <bean 
     class="org.apache.mina.filter.codec.ProtocolCodecFilter"> 
     <constructor-arg> 
        <bean 
       class="org.apache.mina.filter.codec.textline.TextLineCodecFactory" />  //这个是编码格式filter 
     </constructor-arg> 
    </bean> 
    <bean class="org.apache.mina.filter.LoggingFilter" / >//日志的filter 
   </list> 
  </property> 
 </bean> 
</beans>

 

 

 

 

sprng的简单配置文件就是这样:

 

然后看 handler:

 

 

SampleHandler 和 bossSampleHandler 。我写的都是一样的:

 

package cn.org.handler;

import org.apache.mina.common.IdleStatus; 
import org.apache.mina.common.IoHandlerAdapter; 
import org.apache.mina.common.IoSession;

public class HandlerOne extends IoHandlerAdapter {

 public HandlerOne() { 
  // TODO Auto-generated constructor stub 
 }

 @Override 
 public void exceptionCaught(IoSession session, Throwable cause) 
   throws Exception {

 }

 @Override 
 public void messageReceived(IoSession session, Object message) 
   throws Exception { 
  // TODO Auto-generated method stub 
  System.out.println("收到信息"); 
  System.out.println("message :"+message.toString()); 
 }

 @Override 
 public void messageSent(IoSession session, Object message) throws Exception { 
      System.out.println("小心发送"); 
 }

 @Override 
 public void sessionClosed(IoSession session) throws Exception { 
  // TODO Auto-generated method stub 
  super.sessionClosed(session); 
 }

 @Override 
 public void sessionCreated(IoSession session) throws Exception { 
  System.out.println(session.getRemoteAddress().toString() +"---create");

 }

 @Override 
 public void sessionIdle(IoSession session, IdleStatus status) 
   throws Exception { 
               System.out.println(session.getServiceAddress() +"IDS"); 
 }

 @Override 
 public void sessionOpened(IoSession session) throws Exception { 
     System.out.println("连接打开:"+session.getLocalAddress()); 
 }

}

 

 

 

 

现在测试:

 

Test.java

 

public class Test {

 /** 
  * @param args 
  */ 
 public static void main(String[] args) { 
  ClassPathXmlApplicationContext ct =    new ClassPathXmlApplicationContext("applicationContext-mina.xml");  

 }

}

 

 

控制台打印出:

 

2010-1-16 19:00:51 org.springframework.context.support.AbstractApplicationContext prepareRefresh 
信息: Refreshingorg.springframework.context.support.ClassPathXmlApplicationContext@4b4333 : display name [org.springframework.context.support.ClassPathXmlApplicationContext@4b4333]; startup date [Sat Jan 16 19:00:51 CST 2010]; root of context hierarchy 
2010-1-16 19:00:51 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
信息: Loading XML bean definitions from class path resource [applicationContext-mina.xml] 
2010-1-16 19:00:51 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory 
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@4b4333]:org.springframework.beans.factory.support.DefaultListableBeanFactory@1975b59 
2010-1-16 19:00:52 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 
信息: Pre-instantiating singletons inorg.springframework.beans.factory.support.DefaultListableBeanFactory@1975b59 : defining beans [org.springframework.beans.factory.config.CustomEditorConfigurer#0,ioAcceptor,SampleHandler,bossSampleHandler,filterChainBuilder]; root of factory hierarchy

表明服务器就已经启动了:

 

 

现在你可以用telent的方式测试。也可以写个客户端的测试类:

 

写了个客户端的测试类:Clint.java

public class Clint {

 /** 
  * @param args 
  */ 
  
 final static char end = 0x1a; 
  
 public static void main(String[] args) { 
     SocketConnector connector = new SocketConnector(); 
  SocketAddress address = new InetSocketAddress("127.0.0.1", 8888); 
  SocketConnectorConfig config = new SocketConnectorConfig(); 
  config.setConnectTimeout(10);// 秒 
  connector.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "GB2312" )))); //设置编码过滤器  
  connector.getFilterChain().addLast( "logger", new org.apache.mina.filter.LoggingFilter() );

  IoSession session =null; 
  ConnectFuture cf = connector.connect(address, new ClintHandler(), 
    config); 
  cf.join(); 
  if (cf.isConnected()) { 
   session = cf.getSession(); 
   System.out.println("连接成功"); 
   session.write("abc"); 
  }else{ 
   System.out.println("连接失败!!!"); 
  }

    } 
 }

 
 
posted on 2014-02-19 09:42  duanxz  阅读(1214)  评论(0编辑  收藏  举报