Apache MINA is a simplified development for building high-performance, highly scalable network application framework. In various transport protocol JAVA NIO: providing an abstract (such as TCP / IP, UDP / IP) asynchronous event-driven API
Apache MINA can be called: NIO framework libraries, client server framework library, a network socket library
characteristic
-
Provide a unified API for a variety of transport protocols
-
Provide support for a similar link filter servlet filter of
-
Customizable threading model
-
Out of the box SSL · TLS · StartTLS
- Overload protection and flow control transmission
- Ease of integration (such as: Integration with the Spring)
- It may be a smooth transition to Netty
- ...
(Apache MINA role in the application)
As can be seen from the figure, only concerned with the complex interaction MINA network layer to complete the process referred MINA
Apache MINA framework
(Overall architecture)
A three-tier:
- I / O Service - performs the actual I / O processing
- I / O Filter Chain - byte filter / converted to the required data structures, and vice versa
- I / O Handler - Local application layer logic
So create applications based on Apache MINA need only
- Creating an I / O service - to choose the right custom or Acceptor
- Create a Filter Chain - switching request response
- Creating an I / O Handler - handle different message
(Server architecture)
(Process a schematic side service)
( Client Architecture)
case analysis
Server
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); }
Client
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(); } }
The main class diagram
annex
Illustrates a specific analysis
reference
Explanation
Accessories with tagging each class method implementation, see Note
Quoted, please indicate the source