Spring MVC + Mina 整合

项目中用到mvc以及mina ,对于高性能框架mina第一次使用,在整合过程出现了一些小问题,不过都解决了。

gradle 是pom管理工具

Spring 版本

'org.springframework:spring-webmvc:4.0.6.RELEASE',
'org.springframework:spring-tx:4.0.6.RELEASE',
'org.springframework:spring-jdbc:4.0.6.RELEASE',
'org.springframework:spring-test:4.0.6.RELEASE',

Mina依赖引入

'org.apache.mina:mina-core:2.0.7',
'org.apache.mina:mina-integration-spring:1.1.7',
'org.apache.mina:mina-integration-beans:2.0.7'

这三个包必须的

spring-mina.xml配置

<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>
<bean id="socketHandler" class="com.xxx.mina.SocketHandler"/>
<!-- 创建服务器 -->
<bean id="ioAcceptor" class="org.apache.mina.transport.socket.nio.NioSocketAcceptor" init-method="bind"
      destroy-method="unbind">
    <property name="defaultLocalAddress" value=":8888"/>
    <property name="handler" ref="socketHandler"/>
    <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="com.xxx.mina.SocketCodeFactory"></bean>
    </constructor-arg>
</bean>
<bean id="loggingFilter" class="org.apache.mina.filter.logging.LoggingFilter"/>
<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>

SocketHandler -Server端Socket服务处理者

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class SocketHandler extends IoHandlerAdapter {
    private final Set<IoSession> sessions = Collections.synchronizedSet(new HashSet<IoSession>());
    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {
        // super.messageReceived(session, message);
        System.out.println("received message :" + message);
    }
    @Override
    public void sessionClosed(IoSession session) throws Exception {
        super.sessionClosed(session);
    }
    @Override
    public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
        super.sessionIdle(session, status);
    }
}

SocketCodeFactory-Socket服务编码工厂,编码过滤器使用

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 SocketCodeFactory implements ProtocolCodecFactory {
    private final TextLineEncoder encoder;
    private final TextLineDecoder decoder;
    /*final static char endchar = 0x1a;*/
    final static char endchar = 0x0d;
    public SocketCodeFactory() {
        this(Charset.forName("gb2312"));
    }
    public SocketCodeFactory(Charset charset) {
        encoder = new TextLineEncoder(charset, LineDelimiter.UNIX);
        decoder = new TextLineDecoder(charset, LineDelimiter.AUTO);
    }
    public ProtocolDecoder getDecoder(IoSession session) throws Exception {
        return decoder;
    }
    public ProtocolEncoder getEncoder(IoSession session) throws Exception {
        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);
    }
}


你可能感兴趣的:(springMVC,gradle,Mina)