Apache MINA2




  Author: Quốc Minh Đức
  Since: 2008 July
Outline
 Introduction

 Architecture

 Core components

 Synopsis

 Demo

2
Introduction – what is Apache MINA?
 A Java open-source network application framework.

 Abstract API                                   Multi-purpose
    
        Event-driven , built on Java New I/O
    
        Asynchronous , non-blocking
                                                 Infrastructure for
        Unit-testable
                                                 Network
    




 Implementations
                                                 Applications
    
        Socket (TCP) & Datagram (UDP)
    
        In-VM pipes* , Apache Protable Runtime (APR)*
    
        Serial Port (RS232)* - RxTx.org
    
        User-defined protocol
3                                * : new features since 2.0
Introduction – MINA2 pros
 Reusable and Maintainable
    • Networking engine – MINA I/O service
    • Protocol codec – MINA codec framework
    • Your business logic
 Extensible                                  http://mina.apache.org/
    • Runtime modification of application behaviour using 'filter'
 Manageable
    • Introspection of connections and services via JMX API
 Unit-testable
    • Abstract API , mock objects

4
Outline
 Introduction

 Architecture

 Core components

 Synopsis

 Demo

5
Architecture – what does it look like?

 Perform actual I/O --------

 Filter events & requests -



 A connection ---------------

 <Your protocol logic> ----
6
Architecture – basic components

         IoAcceptor, IoConnector
    Base of

 Chain of IoFilters: LoggingFilter,
 SslFilter, ProtocolCodecFilter,
 ExecutorFilter, ...

   Connection instance:   IoSession
   Logic processing: IoHandler,
    IoBuffer, ByteBuffer, ...
7
Outline
 Introduction

 Architecture

 Core components

 Synopsis

 Demo

8
Core components – IoSession
 Abstract a underlying transport's connection away
    (Socket, Channel, ... )

 Provides asynchronous operation to I/O service
    
        Write, close ...
    
        Asynchronous , non-blocking
    
        Returns IoFuture (WriteFuture, CloseFuture... )
    
        (you can add more IoFutureListener for notification )

 Provides I/O statistics
    
        Read bytes, written bytes, last I/O time...
9
Core components – IoService
 IoAcceptor (extends IoService) is for the server side




 MINA classes implement IoAcceptor (act as server) :
     
         NioSocketAcceptor , NioDatagramAcceptor
     
         VmPipeAcceptor
10   
         AprAcceptor* , SerialAcceptor*
Core components – IoService (cont.)
 IoConnector (extends IoService) is for the client side




 MINA classes implement IoConnector (act as client) :
     
         NioSocketConnector , NioDatagramConnector
     
         VmPipeConnector
11   
         AprConnector* , SerialConnector*
Core components – IoBuffer
 Replacement for NIO ByteBuffer
     
         Provides all methods in ByteBuffer (get, put, flip, remaining... )
     
         Provides easy wrap/unwrap methods
     
         More extensible

 Rich (low-level) manipulation methods on text & binary
     
         Unsigned value, enum, String, Java Object...

 More control over allocation mechanism

 On-demand automatic expansion and shrinkage

12
Core components – IoHandler
 IoHandler implementations is for the logic processing




 You can also extend IoHandlerAdapter (base class)
13
Core components – IoFilter
 Concerns seperated to cross-cutting ones to handle
     
         Logging
     
         Thread-safe/Overload prevention
     
         Remote Peer blacklisting (firewall)
     
         Encoder/Decoder for transport protocol
     
         Statistics/Management
     
         Compression
     
         SSL/TLS
     
         More to come – whatever you want to intercept !

 Reuseable, extensible, hot-deployable
14
Core components – IoFilterChain
 Filter all events (read/write/idle... ) , per connection




 Fire qualified events to next Filter / Handler
15
Core components – ProtocolCodecFilter
 Why Protocol ?
     
         Packets order/assembly control
     
         Message position control

 Why ProtocolCodecFilter ?
     
          Bad idea to implement a protocol with only ByteBuffer (Packet
         fragmentation and assembly, complex logic)
     
         Codecs are often reusable

 MINA ProtocolCodecFilter provides :
     
         Text line codec
     
         Object stream codec
16   
         Reusable components to build a custom-codec
Core components – ProtocolCodecFilter
 What does ProtocolCodecFilter looks like ?




17
Core components – ProtocolCodecFilter
 Rewrite EchoHandler code using ProtocolCodecFilter
     + TextLineCodecFactory (bytes to Java Strings)




18
Core components – ExecutorFilter
 ThreadModel – for thread pooling in MINA 1.x - has been
     removed in MINA 2.x . Replacement: explicit ExecutorFilter




19
Outline
 Introduction

 Architecture

 Core components

 Synopsis

 Demo

20
Synopsis – Apache MINA2
 A network (client/server) application framework based on NIO

 MINA is designed for network applications:
     
         Stable
     
         Scalable
     
         Extensible
     
         Manageable, unit-testable
     
         Ease-of-use, elegant

 MINA supports various network protocols (simple,
     complex), data (text, binary) , and evolving standards.
21
Synopsis – recommended application pattern
    basic IoService with IoAcceptor (server) or IoConnector (client)

    Add ExecutorFilter explicitly to chain for multi-threading
     
         Unless you need really low latency

    Use ProtocolCodecFilter
     
         Convert the transport protocol into a Java representation

    Put application logic into an IoHandler

    Store state in the IoSession

    Combine all using minimum Java      (5 or above)
22
Outline
 Introduction

 Architecture

 Core components

 Synopsis

 Demo

23
Demo
 TCP (PalindromeServer)

 UDP (NetworkTimeServer)

 ProtocolCodecFilter extension

( minimum libraries: mina-core-2.xxx.jar,
 slf4j-api-1.yyy.jar, slf4j-simple-1.yyy.jar )


24
Q&A




     Thank you :-)


25
Bonus
 Apache Directory ( http://directory.apache.org )

 SubEtha SMTP ( http://subethasmtp.tigris.org )

 Apache Qpid - AMQP ( http://cwiki.apache.org/qpid )

 OpenFire - XMPP ( http://jivesoftware.com/products )

 Red5 - RTMP ( http://osflash.org/red5 )

 And more... (may be you, next time ! )

26
Bonus
      Define ImageCodecFilter protocol:
       ►Request Message:
              4 byte               4 byte             4 byte
               width               height             numchar

        • width: the width of the requested image
        • height: the height of the requested image
        • numchar: the number of chars to generate




27
Bonus
      Define ImageCodecFilter protocol:
       ►Response Message:
           4 byte         n byte         4 byte         m byte
         Length1 (n)      image1      Length2 (m)       image2


        • length1: the number of bytes used by image1
        • image1: an image in PNG format
        • length2: the number of bytes used by image2
        • image2: an image in PNG format


28

MINA2 (Apache Netty)

  • 1.
    Apache MINA2 Author: Quốc Minh Đức Since: 2008 July
  • 2.
    Outline  Introduction  Architecture Core components  Synopsis  Demo 2
  • 3.
    Introduction – whatis Apache MINA?  A Java open-source network application framework.  Abstract API Multi-purpose  Event-driven , built on Java New I/O  Asynchronous , non-blocking Infrastructure for Unit-testable Network   Implementations Applications  Socket (TCP) & Datagram (UDP)  In-VM pipes* , Apache Protable Runtime (APR)*  Serial Port (RS232)* - RxTx.org  User-defined protocol 3 * : new features since 2.0
  • 4.
    Introduction – MINA2pros  Reusable and Maintainable • Networking engine – MINA I/O service • Protocol codec – MINA codec framework • Your business logic  Extensible http://mina.apache.org/ • Runtime modification of application behaviour using 'filter'  Manageable • Introspection of connections and services via JMX API  Unit-testable • Abstract API , mock objects 4
  • 5.
    Outline  Introduction  Architecture Core components  Synopsis  Demo 5
  • 6.
    Architecture – whatdoes it look like?  Perform actual I/O --------  Filter events & requests -  A connection ---------------  <Your protocol logic> ---- 6
  • 7.
    Architecture – basiccomponents  IoAcceptor, IoConnector Base of  Chain of IoFilters: LoggingFilter, SslFilter, ProtocolCodecFilter, ExecutorFilter, ...  Connection instance: IoSession  Logic processing: IoHandler, IoBuffer, ByteBuffer, ... 7
  • 8.
    Outline  Introduction  Architecture Core components  Synopsis  Demo 8
  • 9.
    Core components –IoSession  Abstract a underlying transport's connection away (Socket, Channel, ... )  Provides asynchronous operation to I/O service  Write, close ...  Asynchronous , non-blocking  Returns IoFuture (WriteFuture, CloseFuture... )  (you can add more IoFutureListener for notification )  Provides I/O statistics  Read bytes, written bytes, last I/O time... 9
  • 10.
    Core components –IoService  IoAcceptor (extends IoService) is for the server side  MINA classes implement IoAcceptor (act as server) :  NioSocketAcceptor , NioDatagramAcceptor  VmPipeAcceptor 10  AprAcceptor* , SerialAcceptor*
  • 11.
    Core components –IoService (cont.)  IoConnector (extends IoService) is for the client side  MINA classes implement IoConnector (act as client) :  NioSocketConnector , NioDatagramConnector  VmPipeConnector 11  AprConnector* , SerialConnector*
  • 12.
    Core components –IoBuffer  Replacement for NIO ByteBuffer  Provides all methods in ByteBuffer (get, put, flip, remaining... )  Provides easy wrap/unwrap methods  More extensible  Rich (low-level) manipulation methods on text & binary  Unsigned value, enum, String, Java Object...  More control over allocation mechanism  On-demand automatic expansion and shrinkage 12
  • 13.
    Core components –IoHandler  IoHandler implementations is for the logic processing  You can also extend IoHandlerAdapter (base class) 13
  • 14.
    Core components –IoFilter  Concerns seperated to cross-cutting ones to handle  Logging  Thread-safe/Overload prevention  Remote Peer blacklisting (firewall)  Encoder/Decoder for transport protocol  Statistics/Management  Compression  SSL/TLS  More to come – whatever you want to intercept !  Reuseable, extensible, hot-deployable 14
  • 15.
    Core components –IoFilterChain  Filter all events (read/write/idle... ) , per connection  Fire qualified events to next Filter / Handler 15
  • 16.
    Core components –ProtocolCodecFilter  Why Protocol ?  Packets order/assembly control  Message position control  Why ProtocolCodecFilter ?  Bad idea to implement a protocol with only ByteBuffer (Packet fragmentation and assembly, complex logic)  Codecs are often reusable  MINA ProtocolCodecFilter provides :  Text line codec  Object stream codec 16  Reusable components to build a custom-codec
  • 17.
    Core components –ProtocolCodecFilter  What does ProtocolCodecFilter looks like ? 17
  • 18.
    Core components –ProtocolCodecFilter  Rewrite EchoHandler code using ProtocolCodecFilter + TextLineCodecFactory (bytes to Java Strings) 18
  • 19.
    Core components –ExecutorFilter  ThreadModel – for thread pooling in MINA 1.x - has been removed in MINA 2.x . Replacement: explicit ExecutorFilter 19
  • 20.
    Outline  Introduction  Architecture Core components  Synopsis  Demo 20
  • 21.
    Synopsis – ApacheMINA2  A network (client/server) application framework based on NIO  MINA is designed for network applications:  Stable  Scalable  Extensible  Manageable, unit-testable  Ease-of-use, elegant  MINA supports various network protocols (simple, complex), data (text, binary) , and evolving standards. 21
  • 22.
    Synopsis – recommendedapplication pattern  basic IoService with IoAcceptor (server) or IoConnector (client)  Add ExecutorFilter explicitly to chain for multi-threading  Unless you need really low latency  Use ProtocolCodecFilter  Convert the transport protocol into a Java representation  Put application logic into an IoHandler  Store state in the IoSession  Combine all using minimum Java (5 or above) 22
  • 23.
    Outline  Introduction  Architecture Core components  Synopsis  Demo 23
  • 24.
    Demo  TCP (PalindromeServer) UDP (NetworkTimeServer)  ProtocolCodecFilter extension ( minimum libraries: mina-core-2.xxx.jar, slf4j-api-1.yyy.jar, slf4j-simple-1.yyy.jar ) 24
  • 25.
    Q&A Thank you :-) 25
  • 26.
    Bonus  Apache Directory( http://directory.apache.org )  SubEtha SMTP ( http://subethasmtp.tigris.org )  Apache Qpid - AMQP ( http://cwiki.apache.org/qpid )  OpenFire - XMPP ( http://jivesoftware.com/products )  Red5 - RTMP ( http://osflash.org/red5 )  And more... (may be you, next time ! ) 26
  • 27.
    Bonus  Define ImageCodecFilter protocol: ►Request Message: 4 byte 4 byte 4 byte width height numchar • width: the width of the requested image • height: the height of the requested image • numchar: the number of chars to generate 27
  • 28.
    Bonus  Define ImageCodecFilter protocol: ►Response Message: 4 byte n byte 4 byte m byte Length1 (n) image1 Length2 (m) image2 • length1: the number of bytes used by image1 • image1: an image in PNG format • length2: the number of bytes used by image2 • image2: an image in PNG format 28