论坛首页 Java企业应用论坛

mina2 结合spring应用的小例子

浏览 6007 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-01-17  

先来看spring中的配置:

<?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-2.0.xsd">


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

 <bean id="ioAcceptor"
  class="org.apache.mina.transport.socket.nio.NioSocketAcceptor"
  init-method="bind" destroy-method="unbind">

  <property name="defaultLocalAddress" value=":1235" />


  <property name="handler" ref="SampleHandler" />
  <property name="filterChainBuilder" ref="filterChainBuilder" />
  <property name="reuseAddress" value="true" />

 </bean>
 <bean id="executorFilter"
  class="org.apache.mina.filter.executor.ExecutorFilter" />
 <bean id="mdcInjectionFilter"
  class="org.apache.mina.filter.logging.MdcInjectionFilter">
  <constructor-arg value="remoteAddress" />
 </bean>
 <bean id="codecFilter"
  class="org.apache.mina.filter.codec.ProtocolCodecFilter">
  <constructor-arg>
  <!-- 
   <bean
    class="org.apache.mina.filter.codec.textline.TextLineCodecFactory" />
    -->
    <bean class="cn.org.handler.MyCodeFactory"></bean>//自定义的 字符编码类
  </constructor-arg>
 </bean>
 <bean id="loggingFilter"
  class="org.apache.mina.filter.logging.LoggingFilter" />
 <bean id="SampleHandler" class="cn.org.handler.HandlerTwo" />
 <!--boss server  -->
 <bean id="bossSampleHandler" class="cn.org.handler.HandlerOne" />

 <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="codecFilter" value-ref="codecFilter" />
    <entry key="loggingFilter" value-ref="loggingFilter" />
   </map>
  </property>
 </bean>


</beans>

 

然后再看 :SampleHandler:

很简单的:

package cn.org.handler;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;


public class HandlerTwo extends IoHandlerAdapter {

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

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

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

}

测试的://启动 可以通过telnet 端口号来,也可以写个客户端的测试类

 

public class Test {

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

 }

}

 

客户端的测试类:


public class ClintTest {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // 创建客户端连接器.
  NioSocketConnector connector = new NioSocketConnector();
  connector.getFilterChain().addLast( "logger", new LoggingFilter() );
  connector.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "GBK" )))); //设置编码过滤器
  connector.setConnectTimeout(1);
  connector.setHandler(new HandlerOne());//设置事件处理器
  ConnectFuture cf = connector.connect(
  new InetSocketAddress("127.0.0.1", 1235));//建立连接
  cf.awaitUninterruptibly();//等待连接创建完成
  cf.getSession().write("知识");//发送消息
  //cf.getSession().write("quit");//发送消息
  cf.getSession().close();
  cf.getSession().getCloseFuture().awaitUninterruptibly();//等待连接断开
  connector.dispose();

 }

}

还有自定义的 编码类:

package cn.org.handler;

import java.nio.charset.Charset;

import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineDecoder;
import org.apache.mina.filter.codec.textline.TextLineEncoder;

public  class MyCodeFactory implements ProtocolCodecFactory {

     private final TextLineEncoder encoder;
     private final TextLineDecoder decoder;
     /*final static char endchar = 0x1a;*/
     final static char endchar = 0x0d;
     public MyCodeFactory() {
         this(Charset.forName("gb2312"));
     }
     public MyCodeFactory(Charset charset) {
       encoder = new TextLineEncoder(charset, LineDelimiter.UNIX);  
          decoder = new TextLineDecoder(charset, LineDelimiter.AUTO);  
          }

     @Override
  public ProtocolDecoder getDecoder(IoSession session) throws Exception {
   // TODO Auto-generated method stub
   return decoder;
  }
  @Override
  public ProtocolEncoder getEncoder(IoSession session) throws Exception {
   // TODO Auto-generated method stub
   return encoder;
  }
  public int getEncoderMaxLineLength() {
         return encoder.getMaxLineLength();
     }
     public void setEncoderMaxLineLength(int maxLineLength) {
         encoder.setMaxLineLength(maxLineLength);
     }
     public int getDecoderMaxLineLength() {
         return decoder.getMaxLineLength();
     }
     public void setDecoderMaxLineLength(int maxLineLength) {
         decoder.setMaxLineLength(maxLineLength);
     }

}

 

这样简单的例子就OK了

 

  jar 包 :spring.jar  mina2.0的jar包    slf4j-nop.1.5.2.jar、

 

 

 

 

 

 

   发表时间:2010-03-12  
请问多线程怎么配置的
配置之后 Handler 里面的sessionClosed方法中
加入 System.out.println(session.getRemoteAddress().toString()+" closed");是不是肯定会报错
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics