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
Apache Mina whitelist Implementation Method
Apache MINA practice
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;
Importorg. apache. mina. core. service. IoAcceptor;
Importorg. apache. mina. core. session. IdleStatus;
Importorg. apache. mina. filter. codec. ProtocolCodecFilter;
Importorg. apache. mina. filter. codec. textline. TextLineCodecFactory;
Importorg. apache. mina. filter. logging. LoggingFilter;
Import org. apache. mina. transport. socket. nio. NioSocketAcceptor;
Public class MinaTimeServer {
Privatestatic final int PORT = 9123;
Publicstatic void main (String [] args ){
// Listener connection object
Ioacceptpolicceptor = 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 (
NewTextLineCodecFactory (Charset. forName ("UTF-8 "))));
//
Acceptor. setHandler (newTimeServerHandler ());
// 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 (newInetSocketAddress (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;
Importorg. apache. mina. core. session. IdleStatus;
Importorg. apache. mina. core. session. IoSession;
Public class TimeServerHandler extendsIoHandlerAdapter {
@ Override
Publicvoid exceptionCaught (IoSession session, Throwable cause)
ThrowsException {
Cause. printStackTrace ();
}
@ Override
Publicvoid messageReceived (IoSession session, Object message) throws Exception {
Stringstr = message. toString ();
If (str. trim (). inclusignorecase ("quit ")){
Session. close (true );
Return;
}
Calendartime = Calendar. getInstance ();
SimpleDateFormatdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");
Session. write (df. format (time. getTime ()));
System. out. println ("TimeMessage written ...");
}
@ Override
Publicvoid 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:
SLF4J: Failed to load class "org. slf4j. impl. StaticLoggerBinder ".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: Seehttp: // www.slf4j.org/codes.html?staticloggerbinder for further details.
Time Message written...
Time Message written...
Time Message written...
This article permanently updates the link address: