liguangqiang1101
liguangqiang1101
2017-10-25 07:03

Apache-mina设置分包符不生效,必须以enter键输入作为分包符

  • protocolcodecfilter
  • mina

package com.changhongit.loving.config;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.filterchain.IoFilter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.executor.ExecutorFilter;
import org.apache.mina.filter.keepalive.KeepAliveFilter;
import org.apache.mina.filter.keepalive.KeepAliveMessageFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.filter.logging.MdcInjectionFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.changhongit.loving.filter.KeepAliveMessageFactoryImpl;
import com.changhongit.loving.handler.ServiceMainHandler;

/**

  • mina config
  • Created by ligq on 2017-09-06.
    */
    @Configuration
    @ConfigurationProperties(prefix = "mina")
    public class MinaSocketConfig {

    /**

    • 监听端口号 */ private int port;

    @Bean(initMethod = "bind", destroyMethod = "unbind")
    public NioSocketAcceptor nioSocketAcceptor(
    ServiceMainHandler serviceMainHandler,
    DefaultIoFilterChainBuilder defaultIoFilterChainBuilder) {

    NioSocketAcceptor nioSocketAcceptor = new NioSocketAcceptor();
    nioSocketAcceptor.setDefaultLocalAddress(new InetSocketAddress(port));
    nioSocketAcceptor.setReuseAddress(true);
    nioSocketAcceptor.setFilterChainBuilder(defaultIoFilterChainBuilder);
    

    //设置缓存大小
    nioSocketAcceptor.getSessionConfig().setReadBufferSize(2048);
    KeepAliveMessageFactory kamf=new KeepAliveMessageFactoryImpl();
    KeepAliveFilter kaf = new KeepAliveFilter(kamf, IdleStatus.BOTH_IDLE);
    kaf.setForwardEvent(true);
    kaf.setRequestInterval(60*3); //本服务器为被定型心跳 即需要每3分钟接受一个心跳请求 否则该连接进入空闲状态 并且发出idled方法回调
    nioSocketAcceptor.getFilterChain().addLast("heart", kaf);

    //读写通道60秒内无操作进入空闲状态
    nioSocketAcceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 60*3);
    //绑定逻辑处理器
    nioSocketAcceptor.setHandler(serviceMainHandler); 
    return nioSocketAcceptor;
    

    }

    @Bean
    public DefaultIoFilterChainBuilder defaultIoFilterChainBuilder(
    ExecutorFilter executorFilter,
    MdcInjectionFilter mdcInjectionFilter,
    ProtocolCodecFilter protocolCodecFilter,
    LoggingFilter loggingFilter) {

    DefaultIoFilterChainBuilder defaultIoFilterChainBuilder = new DefaultIoFilterChainBuilder();
    Map<String, IoFilter> filters = new LinkedHashMap<>();
    filters.put("executor", executorFilter);
    filters.put("mdcInjectionFilter", mdcInjectionFilter);
    filters.put("codecFilter", protocolCodecFilter);
    filters.put("loggingFilter", loggingFilter);
    defaultIoFilterChainBuilder.setFilters(filters);
    
    return defaultIoFilterChainBuilder;
    

    }

    @Bean
    public ExecutorFilter executorFilter() {
    return new ExecutorFilter();
    }

    @Bean
    public MdcInjectionFilter mdcInjectionFilter() {
    return new MdcInjectionFilter(
    MdcInjectionFilter.MdcKey.remoteAddress);
    }

    @Bean
    public TextLineCodecFactory textLineCodecFactory(){
    TextLineCodecFactory minaCodeFactory = new TextLineCodecFactory(
    Charset.forName("UTF-8"),

    LineDelimiter.CRLF.getValue(),

    LineDelimiter.CRLF.getValue());
    minaCodeFactory.setDecoderMaxLineLength(1024*1024);//Allow to receive up to 1M of data
    minaCodeFactory.setEncoderMaxLineLength(1024*1024);
    return minaCodeFactory;
    }

    @Bean
    public ProtocolCodecFilter protocolCodecFilter(
    TextLineCodecFactory minaCodeFactory) {

    return new ProtocolCodecFilter(minaCodeFactory);
    

    }

    @Bean
    public LoggingFilter loggingFilter() {
    return new LoggingFilter();
    }

    public int getPort() {
    return port;
    }

    public void setPort(int port) {
    this.port = port;
    }
    }

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

0条回答