Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Mina2

2,082 views

Published on

Apache MINA framework version 2.x

Published in: Technology, Education
  • Be the first to comment

Mina2

  1. 1. Apache MINA2 Author: Quốc Minh Đức Since: 2008 July
  2. 2. Outline Introduction Architecture Core components Synopsis Demo2
  3. 3. 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 protocol3 * : new features since 2.0
  4. 4. 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 objects4
  5. 5. Outline Introduction Architecture Core components Synopsis Demo5
  6. 6. Architecture – what does it look like? Perform actual I/O -------- Filter events & requests - A connection --------------- <Your protocol logic> ----6
  7. 7. Architecture – basic components IoAcceptor, IoConnector Base of Chain of IoFilters: LoggingFilter, SslFilter, ProtocolCodecFilter, ExecutorFilter, ... Connection instance: IoSession Logic processing: IoHandler, IoBuffer, ByteBuffer, ...7
  8. 8. Outline Introduction Architecture Core components Synopsis Demo8
  9. 9. Core components – IoSession Abstract a underlying transports 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. 10. Core components – IoService IoAcceptor (extends IoService) is for the server side MINA classes implement IoAcceptor (act as server) :  NioSocketAcceptor , NioDatagramAcceptor  VmPipeAcceptor10  AprAcceptor* , SerialAcceptor*
  11. 11. Core components – IoService (cont.) IoConnector (extends IoService) is for the client side MINA classes implement IoConnector (act as client) :  NioSocketConnector , NioDatagramConnector  VmPipeConnector11  AprConnector* , SerialConnector*
  12. 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 shrinkage12
  13. 13. Core components – IoHandler IoHandler implementations is for the logic processing You can also extend IoHandlerAdapter (base class)13
  14. 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-deployable14
  15. 15. Core components – IoFilterChain Filter all events (read/write/idle... ) , per connection Fire qualified events to next Filter / Handler15
  16. 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 codec16  Reusable components to build a custom-codec
  17. 17. Core components – ProtocolCodecFilter What does ProtocolCodecFilter looks like ?17
  18. 18. Core components – ProtocolCodecFilter Rewrite EchoHandler code using ProtocolCodecFilter + TextLineCodecFactory (bytes to Java Strings)18
  19. 19. Core components – ExecutorFilter ThreadModel – for thread pooling in MINA 1.x - has been removed in MINA 2.x . Replacement: explicit ExecutorFilter19
  20. 20. Outline Introduction Architecture Core components Synopsis Demo20
  21. 21. 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
  22. 22. 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
  23. 23. Outline Introduction Architecture Core components Synopsis Demo23
  24. 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. 25. Q&A Thank you :-)25
  26. 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. 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 generate27
  28. 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 format28

×