My First Glance @ Apache MINA - A Network Application Development Framework
Questions of programming a network application:1. Basic Connection Questions
1.1 Server Port and Transportation Protocol(TCP/UDP)
1.2 Connection Timeout Settings
1.3 Connection Idle Settings
2. Advanced Connection Questions
2.1 Application Protocol
2.2 Authentication and Connection Security
2.3 Data Compression
2.4 Exception Handling
2.5 Sending and Receiving Bufferring
3. Business Level Questions
3.1 Business Logic (Transactions, etc)
3.2 Network Statistics and Management
3.3 Asynchronous Communication Model
Note that this thread of articles are written by myself for discussion with you.
They're not the official programming documentation and don't take for granted,
for I'm not 100% sure about the correctivity.
Key @ Oct, 2009
[ 本帖最后由 key 于 22-10-2009 15:24 编辑 ]
What Apache MINA is?
A Network Application FrameworkApache MINA is a framework. As a framework, it provides features like:
[*]Abstract
[*]Flexible
[*]Develop Model
Abstract
Apache MINA abstracts the network application into layers and
components.
Abstract - Layers
From the software layers' point of view, Apache MINA divides
the network application development into three layers:
[*]1. IoService Layer
[*]2. IoFilterChain Layer
[*]3. IoHandler Layer
1. IoService Layer
This is the layer which performs actual I/O with the communication
peer. So it's the lowest layer.
2. IoFilterChain Layer
This is a lower layer which handle the transportation protocols,
including encrypting data, compression, text reformatting, etc.
You'd better regard the communication data as chunk, rather than
as a logical message.
As the name suggests, Apache MINA apply chain-of-responsibility
design pattern.
3. IoHandler Layer
IoHandler layer is the business logic implementation layer of
MINA framework. You need to implement the business logic of your
network application within this layer.
[ 本帖最后由 key 于 22-10-2009 14:32 编辑 ]
What Apache MINA is? (Cont)
Abstract - ComponentsFrom the server side's point of view, there are eight
popular Apache MINA components you need to master before
you program the application. They are:
[*]1. Buffer
[*]2. Acceptor
[*]3. Config
[*]4. Filter
[*]5. Codec
[*]6. Handler
[*]7. Session
[*]8. Message
1. Buffer
Buffer is a concept from Java NIO. Please refer to
http://java.sun.com/j2se/1.5.0/docs/api/java/nio/Buffer.html?is-external=true
for more information. To simplify, Buffer is a container which represents
the content of the communication.
Relative class/interface:
[*]org.apache.mina.common.ByteBuffer
[*]org.apache.mina.common.ByteBufferAllocator
[*]org.apache.mina.common.SimpleByteBufferAllocator
[*]org.apache.mina.common.PooledByteBufferAllocator
[*]org.apache.mina.common.ByteBufferProxy
[*]org.apache.mina.util.ByteBufferUtil
2. Acceptor
Acceptor is a server side concept, which accepts incoming connection,
communications with clients, and fires events to Handlers.
If you're an experienced network programmer, you must have know the
binding, listening and accepting concepts. If you only program with
Java ServerSocket before, you may miss the listening part.
However, ServerSocket combines all the network operation within one
class, while Apache MINA extracts binding, listening and accepting
operations into Acceptors.
IoAcceptor is the interface for Acceptors. The implementations of this interface
includes:
[*]org.apache.mina.common.support.BaseIoAcceptor
[*]org.apache.mina.transport.socket.nio.DatagramAcceptor
[*]org.apache.mina.common.support.DelegatedIoAcceptor
[*]org.apache.mina.transport.socket.nio.SocketAcceptor
[*]org.apache.mina.transport.vmpipe.VmPipeAcceptor
Note that: IoAcceptor is a sub interface of IoService, and another
sub interface of IoService is IoConnection. Please refer to Abstract - layers
for more information about IoService Layer concept.
[ 本帖最后由 key 于 22-10-2009 12:25 编辑 ] 3. Config
Apache MINA Config is not configuration file set. Please refer
to the following class diagram for a high level view.
You can take a look at the relation of Config/Filter/Codec
for further understanding of MINA Config.
[ 本帖最后由 key 于 22-10-2009 13:36 编辑 ] 4. Filter
Filter is one of the most important concept of Apache MINA.
From the previous article, you can see the relationship between
Config and Filter, Filter and Codec.
4.1 Config vs. Filter
From the layer's point of view, Filter serves at the transport
layer, in between IoService and IoHandler. In terms of
class relationship, Filter is part of the service configuration.
More specifically, Filter Chain is part of the service configuration.
To make use of a filter, you can add the following code into your
program:config.getFilterChain().addLast("filter-name", filterObject);4.2 Filter vs. Codec
See 3
4.3 Exist Filter
5. Codec
The above class diagram presents the high level hierarchy of
Apache MINA Codec. It's a typical AbstractFactory design pattern.
According to 4. Filter, you must have known the relationship between
Filter and Codec. Please take a look at the following class diagram:
5.1 Exist Codec
[*] Message Codec: org.apache.mina.filter.codec.demux: Protocol codecs that helps you to implement even more complex protocols by splitting a codec into multiple sub-codecs.
[*] Netty Codec: org.apache.mina.filter.codec.netty: Protocol codec which provides the integration with Netty2 messages.
[*] Object Codec: org.apache.mina.filter.codec.serialization: Protocol codecs which uses Java object serilization and leads to rapid protocol implementation.
[*] Text Codec: org.apache.mina.filter.codec.textline: A protocol codec for text-based protocols
That's cool! 6. Handler
Handler Components are components serve at IoHandler Layer, which is
the business layer of Apache MINA application.
Usually, people program their own Handler class for their own business
requirement. There are some exist handler classes.
6.1 IoHandler vs. IoHandlerAdapter
IoHandler is the interface for the Handler Layer or Handler Component.
It provides abstract programming entries for every single programming point.
Since we only concern about only some of these programming points usually,
to simplify the program, Apache MINA provides IoHandlerAdapter class, giving
default implementation of all points, and letting the programming to
implement the specific points they concern about. You can compare
IoHandler in Apache MINA with WindowEvent in GUI programming.
Summary
Apache MINA is a well design Java Network Application framework,with supporting components for developing a Java Network Application.
It's clear, clean, and useful. It takes me only 5 hours to learn
all the concepts and programming APIs. After that, I don't need
to program my Network Application from scratch. Even more,
I can get benefits from this framework, such as framework based
feature, modularisation, event-driven programming, flexibility,
NIO for efficiency, etc. :good :good :good :good :good :good :good
页:
[1]