mina 2.0 多线程 socket通信主体代码
NioSocketAcceptor acceptor = new NioSocketAcceptor();
DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
SocketCfg socketCfg = SpringConfig.getBean(SocketCfg.class);
//启用log
if(socketCfg.isUseLog()){
MdcInjectionFilter mdcInjectionFilter = new MdcInjectionFilter();
chain.addLast("mdc", mdcInjectionFilter);
addLogger(chain);
}
// Add SSL filter if SSL is enabled.
if (socketCfg.isUseSSL()) {
addSSLSupport(chain);
}
//过滤器
chain.addLast("codec", new ProtocolCodecFilter(
new MessageCodecFactory()));
//启用多线程 线程池
chain.addLast("threadPool", new ExecutorFilter(Executors.newCachedThreadPool()));
// Bind接口实现
acceptor.setHandler(new MessageHandler());
acceptor.bind(new InetSocketAddress(socketCfg.getPort()));
System.out.println("Listening on port " + socketCfg.getPort());