stoneechogx 2019-11-06
Apache MINA 是一个用于简化开发构建高性能、高可扩展的网络应用框架。通过JAVA NIO在各种传输协议(如:TCP/IP、UDP/IP)上提供抽象的事件驱动异步API
Apache MINA可以称为:NIO框架库、服务端客户端框架库、一个网络套接字库
为各种传输协议提供统一API
提供类似servlet filter的过滤链路支持
可定制化线程模型
开箱即用的 SSL · TLS · StartTLS
(Apache MINA在应用中的角色)
从图中可以看出,只需要关注与MINA的交互而复杂的网络层处理交由MINA来完成
(总体架构)
采用三层架构:
所以创建一个基于Apache MINA的应用只需要
(服务端架构)
(服务端流程示意)
(客户端架构)
public static void main(String[] args) throws IOException { //IoService final IoAcceptor acceptor = new NioSocketAcceptor(); //IoFilter acceptor.getFilterChain().addLast("logger",new LoggingFilter()); acceptor.getFilterChain().addLast("codec",new ProtocolCodecFilter( //ProtocolCodecFactory new TextLineCodecFactory(Charset.forName("UTF-8")))); //IoHandler acceptor.setHandler(new TimeServerHandler()); //IoService acceptor.getSessionConfig().setReadBufferSize(2048); acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE,10); acceptor.bind(new InetSocketAddress(PORT)); LOGGER.info("服务端启动成功,端口:{}",PORT); }
public static void main(String[] args) throws InterruptedException { //IoConnector final NioSocketConnector connector = new NioSocketConnector(); connector.setConnectTimeoutMillis(3*1000); // connector.getSessionConfig().setUseReadOperation(true); //IoFilter connector.getFilterChain().addLast("codec", new ProtocolCodecFilter( // ProtocolCodecFactory new ObjectSerializationCodecFactory())); connector.getFilterChain().addLast("logger",new LoggingFilter()); //IoHandler connector.setHandler(new TcpClientHandler()); IoSession session = null; for (;;){ try { //IoFuture final ConnectFuture connectFuture = connector.connect(new InetSocketAddress(HOST, PORT)); connectFuture.awaitUninterruptibly(); session = connectFuture.getSession(); break; }catch (RuntimeIoException e){ System.err.println("failed connected"); e.printStackTrace(); Thread.sleep(5000); } session.getCloseFuture().awaitUninterruptibly(); //IoProcessor connector.dispose(); } }
Apache MINA 官网
附件具标注各个类方法的具体实现,请注意查看
引用请注明出处