acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ))));
TextLineCodecFactory textLine = new TextLineCodecFactory(Charset.forName("UTF-8"), LineDelimiter.UNIX.getValue(), "EOF"); textLine.setDecoderMaxLineLength(Integer.MAX_VALUE); textLine.setEncoderMaxLineLength(Integer.MAX_VALUE); ProtocolCodecFilter codecFilter = new ProtocolCodecFilter(textLine); this.acceptor.getFilterChain().addLast( "codec", codecFilter);
public void start() { IoBuffer.setUseDirectBuffer(false); IoBuffer.setAllocator(new SimpleBufferAllocator()); acceptor = new NioSocketAcceptor(); // cfg.getFilterChain().addLast("mimemessage1", new MimeMessageIOFilter() ); acceptor.getFilterChain().addLast("logger", new LoggingFilter()); acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("US-ASCII")))); acceptor.getFilterChain().addLast("stream", new StreamWriteFilter() ); acceptor.setHandler( new PopIOHandlerAdapter(resourceFactory, filters) ); try { //cfg.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8")))); acceptor.bind(new InetSocketAddress(popPort)); } catch (IOException ex) { throw new RuntimeException("Couldnt bind to port: " + popPort, ex); } }
/** * <p>startServer</p> * * @throws java.lang.Exception if any. */ public void startServer() throws Exception { m_acceptor = new NioSocketAcceptor(); m_acceptor.getFilterChain().addLast("logger", new LoggingFilter()); m_acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(StandardCharsets.UTF_8))); m_acceptor.setHandler(getServerHandler()); m_acceptor.getSessionConfig().setReadBufferSize(getBufferSize()); m_acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, getIdleTime()); ((NioSocketAcceptor) m_acceptor).setReuseAddress(true); m_acceptor.bind(new InetSocketAddress(getPort())); }
private PushManager() { connector=new NioSocketConnector(); connector.setConnectTimeoutMillis(Params.CONNECT_TIMEOUT); //为接收器设置管理服务 connector.setHandler(new ClientSessionHandler()); //设置过滤器(使用Mina提供的文本换行符编解码器) connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"), LineDelimiter.WINDOWS.getValue(),LineDelimiter.WINDOWS.getValue()))); //读写通道5秒内无操作进入空闲状态 connector.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, Params.REQUEST_TIMEOUT); //设置读取数据的缓冲区大小 connector.getSessionConfig().setReadBufferSize(2048); //设置心跳 KeepAliveMessageFactory heartBeatFactory = new ClientKeepAliveMessageFactoryImp(); KeepAliveRequestTimeoutHandler heartBeatHandler = new ClientKeepAliveMessageTimeoutFactoryImp(); KeepAliveFilter heartBeat = new KeepAliveFilter(heartBeatFactory, IdleStatus.BOTH_IDLE, heartBeatHandler); //是否回发 heartBeat.setForwardEvent(true); //心跳发送频率 heartBeat.setRequestInterval(Params.REQUEST_INTERVAL); connector.getSessionConfig().setKeepAlive(true); connector.getFilterChain().addLast("keepalive", heartBeat); }