Apache Mina development manual
Apache Mina development manual
I. Introduction
Apache Mina is a network application framework that simplifies the development of high-performance and highly scalable network applications. Mina provides an abstract event-driven asynchronous API that uses Java NIO to implement various transmission protocols, such as TCP/IP and UDP/IP.
Apache Mina is often used:
1) NIO Framework library
2) Client/Server Communication Framework library
3) network Socket communication library
Apache Mina also has many sub-projects:
1) Asyncweb
HTTP server built on the Apache Mina asynchronous framework
2) FtpServer
An FTP server
3) SSHd
A Java library that supports the SSHH Protocol
4) Vysper
One XMPP Server
Ii. Download Apache Mina
Download the latest Mina v2.0.8
See: http://mina.apache.org/mina-project/downloads.html
Iii. Use Mina to develop the Time Server
It is actually based on the official example and slightly modified, because it is found that the official example is too old and even contains the deprecated method.
1. Prerequisites
Apache Mina 2.0.8 Core
JDK 7
SLF4J + LOGBACK
2. Project dependency package
Mina-core-2.0.8.jar
Slf4j-api-1.6.6.jar
3. Write a Mina-based Time server
- Package ch. chiqms. server;
- 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. session. IdleStatus;
- 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;
- Public class MinaTimeServer {
- Private static final int PORT = 9123;
- Public static void main (String [] args ){
- // Listener connection object
- IoAcceptor acceptor = new NioSocketAcceptor ();
- // Configure the filter
- // The logger filter outputs all information, such as newly created sessions, message receipt, message sending, and session Closure.
- // The codec filter converts the data specified by the binary active protocol to the message object. Here, it processes text-based messages.
- Acceptor. getFilterChain (). addLast ("logger", new LoggingFilter ());
- Acceptor. getFilterChain (). addLast ("codec", new ProtocolCodecFilter (
- New TextLineCodecFactory (Charset. forName ("UTF-8 "))));
- //
- Acceptor. setHandler (new TimeServerHandler ());
- // Set the size of the input buffer and the IDLE of the session.
- Acceptor. getSessionConfig (). setReadBufferSize (2048 );
- Acceptor. getSessionConfig (). setIdleTime (IdleStatus. BOTH_IDLE, 10 );
- Try {
- Acceptor. bind (new InetSocketAddress (PORT ));
- } Catch (IOException e ){
- E. printStackTrace ();
- }
- }
- }
4. Compile the Handler of the Time Service
- package ch.chiqms.server;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import org.apache.mina.core.service.IoHandlerAdapter;
- import org.apache.mina.core.session.IdleStatus;
- import org.apache.mina.core.session.IoSession;
- public class TimeServerHandler extends IoHandlerAdapter {
- @Override
- public void exceptionCaught(IoSession session, Throwable cause)
- throws Exception {
- cause.printStackTrace();
- }
- @Override
- public void messageReceived(IoSession session, Object message) throws Exception {
- String str = message.toString();
- if(str.trim().equalsIgnoreCase("quit")){
- session.close(true);
- return;
- }
- Calendar time = Calendar.getInstance();
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- session.write(df.format(time.getTime()));
- System.out.println("Time Message written...");
- }
- @Override
- public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
- System.out.println("IDLE "+session.getIdleCount(status));
- }
- }
5. Run MinaTimeServer.
Enter telnet 127.0.0.1 9123 in the command line.
The server output can also be seen: