mina2

28
Apache MINA2 Apache MINA2 Author: Author: Quốc Minh Đức Quốc Minh Đức Since: Since: 2008 July 2008 July

Upload: ducquocvn

Post on 17-Jan-2015

1.460 views

Category:

Technology


0 download

DESCRIPTION

Apache MINA framework version 2.x

TRANSCRIPT

Page 1: Mina2

Apache MINA2 Apache MINA2

Author:Author: Quốc Minh Đức Quốc Minh ĐứcSince:Since: 2008 July 2008 July

Page 2: Mina2

2

OutlineOutline

IntroductionIntroduction

Architecture Architecture

Core components Core components

Synopsis Synopsis

Demo Demo

Page 3: Mina2

3

Introduction Introduction – what is Apache MINA?– what is Apache MINA?

A Java open-source A Java open-source network applicationnetwork application framework. framework.

Abstract API Abstract API Event-driven , built on Java New I/O Event-driven , built on Java New I/O Asynchronous , non-blocking Asynchronous , non-blocking Unit-testable Unit-testable

Implementations Implementations Socket (TCP) & Datagram (UDP) Socket (TCP) & Datagram (UDP) In-VM pipes* , Apache Protable Runtime (APR)* In-VM pipes* , Apache Protable Runtime (APR)* Serial Port (RS232)* - RxTx.org Serial Port (RS232)* - RxTx.org User-defined protocol User-defined protocol

* : new features since 2.0

Multi-purpose Infrastructure forNetwork Applications

Page 4: Mina2

4

Introduction Introduction – MINA2 pros– MINA2 pros

Reusable and Maintainable Reusable and Maintainable • Networking engine – MINA I/O service Networking engine – MINA I/O service • Protocol codec – MINA codec framework Protocol codec – MINA codec framework • Your business logic Your business logic

Extensible Extensible • Runtime modification of application behaviour using 'filter'Runtime modification of application behaviour using 'filter'

Manageable Manageable • Introspection of connections and services via JMX APIIntrospection of connections and services via JMX API

Unit-testable Unit-testable • Abstract API , mock objectsAbstract API , mock objects

http://mina.apache.org/

Page 5: Mina2

5

OutlineOutline

Introduction Introduction

ArchitectureArchitecture

Core components Core components

Synopsis Synopsis

Demo Demo

Page 6: Mina2

6

Architecture Architecture – what does it look like?– what does it look like?

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

Filter events & requests Filter events & requests --

A connection A connection ------------------------------

<Your protocol logic><Your protocol logic> --------

Page 7: Mina2

7

Architecture Architecture – basic components– basic components

Base ofBase of IoAcceptor, IoConnectorIoAcceptor, IoConnector Chain of IoFilters:Chain of IoFilters: LoggingFilter, LoggingFilter,

SslFilter, ProtocolCodecFilter, SslFilter, ProtocolCodecFilter, ExecutorFilter, ...ExecutorFilter, ...

Connection instance: Connection instance: IoSessionIoSession Logic processing: Logic processing: IoHandler,IoHandler,

IoBuffer, ByteBuffer, ... IoBuffer, ByteBuffer, ...

Page 8: Mina2

8

OutlineOutline

Introduction Introduction

Architecture Architecture

Core componentsCore components

Synopsis Synopsis

Demo Demo

Page 9: Mina2

9

Core components Core components – IoSession– IoSession

Abstract a underlying transport's Abstract a underlying transport's connectionconnection away away

((SocketSocket, , ChannelChannel, ... ), ... )

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

Provides I/O statistics Provides I/O statistics Read bytes, written bytes, last I/O time... Read bytes, written bytes, last I/O time...

Page 10: Mina2

10

Core components Core components – IoService– IoService

IoAcceptorIoAcceptor (extends (extends IoServiceIoService) is for the server side) is for the server side

MINA classes implement MINA classes implement IoAcceptorIoAcceptor (act as server) : (act as server) : NioSocketAcceptor , NioDatagramAcceptorNioSocketAcceptor , NioDatagramAcceptor VmPipeAcceptorVmPipeAcceptor AprAcceptor* , SerialAcceptor*AprAcceptor* , SerialAcceptor*

Page 11: Mina2

11

Core components Core components – IoService (cont.)– IoService (cont.)

IoConnectorIoConnector (extends (extends IoServiceIoService) is for the client side) is for the client side

MINA classes implement MINA classes implement IoConnectorIoConnector (act as client) : (act as client) : NioSocketConnector , NioDatagramConnectorNioSocketConnector , NioDatagramConnector VmPipeConnectorVmPipeConnector AprConnector* , SerialConnector*AprConnector* , SerialConnector*

Page 12: Mina2

12

Core components Core components – IoBuffer– IoBuffer

Replacement for NIO Replacement for NIO ByteBufferByteBuffer Provides all methods in ByteBuffer (Provides all methods in ByteBuffer (getget, , putput, , flipflip, , remainingremaining... ) ... ) Provides easy Provides easy wrapwrap//unwrapunwrap methods methods More extensible More extensible

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

More control over allocation mechanism More control over allocation mechanism

On-demand automatic expansion and shrinkage On-demand automatic expansion and shrinkage

Page 13: Mina2

13

Core components Core components – IoHandler– IoHandler

IoHandlerIoHandler implementations is for the logic processing implementations is for the logic processing

You can also extend You can also extend IoHandlerAdapterIoHandlerAdapter (base class) (base class)

Page 14: Mina2

14

Core components Core components – IoFilter – IoFilter

Concerns seperated to cross-cutting ones to handleConcerns seperated to cross-cutting ones to handle Logging Logging Thread-safe/Overload prevention Thread-safe/Overload prevention Remote Peer blacklisting (firewall)Remote Peer blacklisting (firewall) Encoder/Decoder for transport protocolEncoder/Decoder for transport protocol Statistics/ManagementStatistics/Management Compression Compression SSL/TLS SSL/TLS More to come – whatever you want to intercept ! More to come – whatever you want to intercept !

Reuseable, extensible, hot-deployable Reuseable, extensible, hot-deployable

Page 15: Mina2

15

Core components Core components – IoFilterChain– IoFilterChain

Filter all events (read/write/idle... ) , per connection Filter all events (read/write/idle... ) , per connection

Fire qualified events to next Filter / Handler Fire qualified events to next Filter / Handler

Page 16: Mina2

16

Core components Core components – ProtocolCodecFilter – ProtocolCodecFilter

Why Protocol ? Why Protocol ? Packets order/assemblyPackets order/assembly control control Message position control Message position control

Why ProtocolCodecFilter ?Why ProtocolCodecFilter ? Bad idea to implement a protocol with only Bad idea to implement a protocol with only ByteBufferByteBuffer (Packet (Packet

fragmentation and assembly, complex logic) fragmentation and assembly, complex logic) Codecs are often reusable Codecs are often reusable

MINA ProtocolCodecFilter provides : MINA ProtocolCodecFilter provides : Text line codecText line codec Object stream codecObject stream codec Reusable components to build a custom-codecReusable components to build a custom-codec

Page 17: Mina2

17

Core components Core components – ProtocolCodecFilter– ProtocolCodecFilter

What does ProtocolCodecFilter looks like ? What does ProtocolCodecFilter looks like ?

Page 18: Mina2

18

Core components Core components – ProtocolCodecFilter– ProtocolCodecFilter

Rewrite EchoHandler code using ProtocolCodecFilter Rewrite EchoHandler code using ProtocolCodecFilter

+ TextLineCodecFactory + TextLineCodecFactory (bytes to Java (bytes to Java StringStrings)s)

Page 19: Mina2

19

Core components Core components – ExecutorFilter– ExecutorFilter

ThreadModelThreadModel – for thread pooling in MINA 1.x -– for thread pooling in MINA 1.x - has been has been

removed in MINA 2.xremoved in MINA 2.x . Replacement: explicit . Replacement: explicit ExecutorFilterExecutorFilter

Page 20: Mina2

20

OutlineOutline

Introduction Introduction

Architecture Architecture

Core components Core components

SynopsisSynopsis

Demo Demo

Page 21: Mina2

21

Synopsis Synopsis – Apache MINA2– Apache MINA2

A networkA network (client/server) (client/server) application frameworkapplication framework based on NIO based on NIO

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

MINA supports various network protocols (simple, MINA supports various network protocols (simple,

complex), data (text, binary) , and evolving standards. complex), data (text, binary) , and evolving standards.

Page 22: Mina2

22

Synopsis Synopsis – recommended application pattern– recommended application pattern

basic basic IoServiceIoService with with IoAcceptorIoAcceptor (server) or (server) or IoConnectorIoConnector (client) (client)

Add Add ExecutorFilterExecutorFilter explicitly to chain for multi-threading explicitly to chain for multi-threading Unless you need really low latency Unless you need really low latency

Use Use ProtocolCodecFilterProtocolCodecFilter Convert the transport protocol into a Java representation Convert the transport protocol into a Java representation

Put application logic into an Put application logic into an IoHandlerIoHandler

Store state in the Store state in the IoSessionIoSession

Combine all using minimum Combine all using minimum Java (5 or above) Java (5 or above)

Page 23: Mina2

23

OutlineOutline

Introduction Introduction

Architecture Architecture

Core components Core components

Synopsis Synopsis

DemoDemo

Page 24: Mina2

24

DemoDemo

TCPTCP (PalindromeServer) (PalindromeServer)

UDPUDP (NetworkTimeServer) (NetworkTimeServer)

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

Page 25: Mina2

25

Thank you :-) Thank you :-)

Q & A

Page 26: Mina2

26

BonusBonus

Apache Directory ( Apache Directory ( http://directory.apache.orghttp://directory.apache.org ) )

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

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

OpenFireOpenFire - XMPP - XMPP ( ( http://jivesoftware.com/productshttp://jivesoftware.com/products ) )

Red5Red5 - RTMP - RTMP ( ( http://osflash.org/red5http://osflash.org/red5 ) )

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

Page 27: Mina2

27

BonusBonus

Define ImageCodecFilter protocol:Define ImageCodecFilter protocol:►Request Message:

4 byte 4 byte 4 bytewidth height numchar

• width: the width of the requested image

• height: the height of the requested image

• numchar: the number of chars to generate

Page 28: Mina2

28

BonusBonus

Define ImageCodecFilter protocol:Define ImageCodecFilter protocol:►Response Message:

4 byte n byte 4 byte m byteLength1 (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