Enterprise Integration Zone is brought to you in partnership with:

He learned gwbasic at 11 and clearing browsing data at 20. He believes that Schrödinger cat is half-dead. He hates corn and PPT. Giancarlo is a DZone MVB and is not an employee of DZone and has posted 3 posts at DZone. You can read more from them at their website. View Full User Profile

The Power of Apache Mina: Building Client Auth and Async Notifications

05.03.2012
| 9399 views |
  • submit to reddit
The application being discussed has to behave as follows: performs the client authentication, accomplishes request and response operations and forwards notifications asynchronously to the client. The Mina framework fulfills these needs because it was created to be as flexible and easy-fitting as possible in an array of case scenarios. Mina is structured in several layers that briefly can be broken down into: input parsing, execution of concatenated processes, and serialization of related responses, if needed. The infrastructure takes care of I/O  quirks, while it lets you write within it your business processes and handles session lifecycles through simple callbacks that you can manage within a  few codes. It is Majestic! You will love it.

In addition, there is a need to look for something that could help me, the author, to write down an application which implements many commands, preferably in a appealing way, where each piece of service could be isolated from the rest of the application.

The demultiplexer is a device with a single input and many outputs. Its role is to select the output line according to context rules. This approach is also implemented within Apache Mina for writing decoders, handlers and encoders. Apache Mina’s demux package includes: DemuxingIoHandler, DemuxingProtocolDecoder and the DemuxingProtocolEncoder.

Apache Mina Architecture

Filters

Filters are used for several purposes: I/O logging, performance tracking, thread pooling, overload control, blacklists, and so on. In a specific case I once had to configure two filters. One for the user authentication, and the other for thread pooling. Once a user is logged in, the authentication filter removes itself from the client session filter list, while substituting one transforming the raw input into POJOs.

Decoder

The TCP server must implements a proprietary protocol. It is a simple ASCII protocol for exchanging commands. Such command lengths are undefinable. They, however, are a sequence of characters much like SMTP. Therefore, the CumulativeProtocolDecoder class is extended enabling it to gather input through the end of command. It is then left to us to parse of the bytes and create a simple Java Bean. Post the operation, the bean is transferred through the filter chain to be executed.

Handler

One of the IoHandler implementations drew my attention while I was looking for something resembling the Command Pattern. Each message coming from clients means a specific action, and I find it so tedious writing a single Handler that switches operations by the type of the incoming request. An elegant solution is provided by DemuxingIoHandler that posts the requests toward the corresponding handler. The handlers have to implement the MessageHandler<E>, the generic type defined will be the object’s class that the DemuxingIoHandler will submit to the handler, and register themselves invoking addReceivedMessageHandler(Class<E> type, MessageHandler<? super E> handler).

The asynchronous nature of Mina allows to handle a huge number of clients by an handful set of threads. A further decoupling between I/O and business logic may be done by the ExecutorFilter, which is in charge of the messages after the NioProcessor.

Encoder

The transformation component works in a reverse way compared to the decoder: it serialises the POJO response coming from the handler process, to the output stream, toward the client. Likewise the handlers, it is possible to delegate its own encoder for each response object, but why not to send the IoBuffer straightly from the handler that elaborates the request? Separation of concerns might be the answer. The handler receives a command, a java bean, then it is processed and the handler returns an object for response, a POJO. It’s up to the encoder to transform the abstract response to a concrete message through the agreed TCP protocol.

Published at DZone with permission of Giancarlo Frison, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Mike Heath replied on Thu, 2012/05/03 - 7:13pm

If you like MINA, I would highly recommend you take a look at Netty. Netty performs much better than MINA and has a cleaner API, IMO. There also seems to be much more activity in the Netty community.

I don't mean to dog on MINA. MINA's great but it's development seems to have stalled quite a bit since Trustin Lee left MINA to work on Netty.

Charles Hudak replied on Fri, 2012/05/04 - 10:29am

Concur with the above comment.

 I've written several MINA applications. The latest one I refactored to use Netty and in many ways it is much cleaner. I had some serious issues in the 2.x version of MINA that I didn't have in the 1.x version and could never get them resolved.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.