Apache Mina development manual

Source: Internet
Author: User

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

 
 
  1. Package ch. chiqms. server;

  2. Import java. io. IOException;
  3. Import java.net. InetSocketAddress;
  4. Import java. nio. charset. Charset;

  5. Import org. apache. mina. core. service. IoAcceptor;
  6. Import org. apache. mina. core. session. IdleStatus;
  7. Import org. apache. mina. filter. codec. ProtocolCodecFilter;
  8. Import org. apache. mina. filter. codec. textline. TextLineCodecFactory;
  9. Import org. apache. mina. filter. logging. LoggingFilter;
  10. Import org. apache. mina. transport. socket. nio. NioSocketAcceptor;

  11. Public class MinaTimeServer {
  12. Private static final int PORT = 9123;
  13. Public static void main (String [] args ){
  14. // Listener connection object
  15. IoAcceptor acceptor = new NioSocketAcceptor ();

  16. // Configure the filter
  17. // The logger filter outputs all information, such as newly created sessions, message receipt, message sending, and session Closure.
  18. // The codec filter converts the data specified by the binary active protocol to the message object. Here, it processes text-based messages.
  19. Acceptor. getFilterChain (). addLast ("logger", new LoggingFilter ());
  20. Acceptor. getFilterChain (). addLast ("codec", new ProtocolCodecFilter (
  21. New TextLineCodecFactory (Charset. forName ("UTF-8 "))));

  22. //
  23. Acceptor. setHandler (new TimeServerHandler ());

  24. // Set the size of the input buffer and the IDLE of the session.
  25. Acceptor. getSessionConfig (). setReadBufferSize (2048 );
  26. Acceptor. getSessionConfig (). setIdleTime (IdleStatus. BOTH_IDLE, 10 );

  27. Try {
  28. Acceptor. bind (new InetSocketAddress (PORT ));
  29. } Catch (IOException e ){
  30. E. printStackTrace ();
  31. }
  32. }
  33. }

4. Compile the Handler of the Time Service

  
  
  1. package ch.chiqms.server;

  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;

  4. import org.apache.mina.core.service.IoHandlerAdapter;
  5. import org.apache.mina.core.session.IdleStatus;
  6. import org.apache.mina.core.session.IoSession;

  7. public class TimeServerHandler extends IoHandlerAdapter {

  8. @Override
  9. public void exceptionCaught(IoSession session, Throwable cause)
  10. throws Exception {
  11. cause.printStackTrace();
  12. }

  13. @Override
  14. public void messageReceived(IoSession session, Object message) throws Exception {
  15. String str = message.toString();
  16. if(str.trim().equalsIgnoreCase("quit")){
  17. session.close(true);
  18. return;
  19. }
  20. Calendar time = Calendar.getInstance();
  21. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  22. session.write(df.format(time.getTime()));
  23. System.out.println("Time Message written...");
  24. }

  25. @Override
  26. public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
  27. System.out.println("IDLE "+session.getIdleCount(status));
  28. }

  29. }

5. Run MinaTimeServer.
Enter telnet 127.0.0.1 9123 in the command line.


The server output can also be seen:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.