Apache MINA is a network application framework used to build high-performance and high-scalability applications. It provides a unified set of event-driven asynchronous APIs built on Java NIO.
To understand the MINA framework, several official articles of MINA must be viewed as follows:
* Application Architecture http://mina.apache.org/mina-based-application-architecture.html
* Server Architecture http://mina.apache.org/server-architecture.html
* Client Architecture http://mina.apache.org/client-architecture.html
The main components are as follows:
I/O Service-it is used to process I/O streams. For the implementation class of IOAcceptor on the Server side, it accepts connection requests from the Client side, the Client is the implementation class of IoConnector to establish a connection to the Server.
I/O Filter Chain-used to Filter or convert data. Both the Server and Client are implementation classes of the IoFilter interface. MINA has many built-in implementation classes of the IoFilter interface. For more information, see the official documentation.
I/O Handler-class used to process real business logic. Both the Server and Client are implementation classes of the IoHandler interface, which usually needs to be compiled by yourself.
Because both the Server and Client are based on the above three components, both the Server and Client programming have a similar code structure.
For the Server side:
1. Create an I/O service. Here, create an IOAcceptor listener port.
2. Create an I/O Filter Chain-here we will tell you to use those iofilters.
3. Create an I/O Handler-your own business logic.
For the Client:
1. Create an I/O service-create an IOConnector class to establish a connection to the Server.
2. Create an I/O Filter Chain-here we will tell you to use those iofilters.
3. Create an I/O Handler-your own business logic.
The following example shows how MINA works. Most applications are based on TCP/IP, so UDP/IP is not mentioned here.
Here I used Maven to create a simple java application. For detailed steps, see the official Maven manual. Here we will only list the maven configuration file pom. xml that I use for the convenience of the following and subsequent articles. The content of the pom. xml file is as follows:
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.google.code.garbagecan.minastudy</groupId>
- <artifactId>minastudy</artifactId>
- <packaging>jar</packaging>
- <version>1.0-SNAPSHOT</version>
- <name>minastudy</name>
- <url>http://maven.apache.org</url>
- <dependencies>
- <dependency>
- <groupId>org.apache.mina</groupId>
- <artifactId>mina-core</artifactId>
- <version>2.0.4</version>
- </dependency>
- <dependency>
- <groupId>org.apache.mina</groupId>
- <artifactId>mina-filter-compression</artifactId>
- <version>2.0.4</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>1.3.0</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>1.3.0</version>
- </dependency>
- </dependencies>
- </project>
First, let's look at the Server code.
- package com.google.code.garbagecan.minastudy.sample1;
-
- import java.io.IOException;
- import java.net.InetSocketAddress;
- import java.nio.charset.Charset;
-
- import org.apache.mina.core.service.IoAcceptor;
- import org.apache.mina.core.service.IoHandlerAdapter;
- import org.apache.mina.core.session.IdleStatus;
- import org.apache.mina.core.session.IoSession;
- import org.apache.mina.filter.codec.ProtocolCodecFilter;
- import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
- import org.apache.mina.filter.logging.LoggingFilter;
- import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
- public class MyServer {
-
- private static final Logger logger = LoggerFactory.getLogger(MyServer.class);
-
- public static void main(String[] args) {
- IoAcceptor acceptor = new NioSocketAcceptor();
-
- acceptor.getFilterChain().addLast("logger", new LoggingFilter());
- acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
-
- acceptor.setHandler(new IoHandlerAdapter() {
-
- @Override
- public void sessionCreated(IoSession session) throws Exception {
- }
-
- @Override
- public void sessionOpened(IoSession session) throws Exception {
- }
-
- @Override
- public void sessionClosed(IoSession session) throws Exception {
- }
-
- @Override
- public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
- }
-
- @Override
- public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
- logger.error(cause.getMessage(), cause);
- session.close(true);
- }
-
- @Override
- public void messageReceived(IoSession session, Object message) throws Exception {
- logger.info("Received message " + message);
- session.write(message);
- }
-
- @Override
- public void messageSent(IoSession session, Object message) throws Exception {
- logger.info("Sent message " + message);
- }
- });
-
- try {
- acceptor.bind(new InetSocketAddress(10000));
- } catch (IOException ex) {
- logger.error(ex.getMessage(), ex);
- }
- }
- }
1. First create an I/O Service. Here we use the NioSocketAcceptor class to create an IoAcceptor instance.
2. create an I/O Filter Chain. Two iofilters are used here. One is LoggingFilter used to record logs and print event messages, and the other is ProtocolCodecFilter instance used to encode data, here we actually encode the transmitted data into text.
3. do not be afraid to create I/O Handler. It seems that there are many codes. It is actually a subclass that implements the IoHandler interface. You need to implement some of these methods. Here there are many methods, however, here I only implement the messageSent, messageReceived, and predictioncaught methods.
4. Finally, bind the IoAcceptor class instance to a port for listening.