You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Mike Grundvig <mi...@electrotank.com> on 2006/09/10 03:14:27 UTC

Dumb Questions about MINA Implementation

Hi all; I'm having a hell of a time grasping MINA. I don't know why, but for 
some reason I just can't seem to get a handle on how to properly implement 
anything with it. I can get it working just fine but because it has many 
solutions to a single problem I always second-guess my design decisions.

Currently, I'm working with a protocol that delimits every message with a 
character. After some checking, I determined that I could simply use the 
TextLineEncoder/Decoder to process my messages if I passed in the right 
delimiter. I then created my own IoHandler and in the sessionCreate method, 
I instantiate the encoder/decoder and a ProtocolCodecFilter and just add 
them to the filter chain as the last entry. The code is basically this 
(stripped down for brevity in email):

public void sessionCreated(IoSession ioSession) throws Exception {
    ProtocolEncoder delimitedEncoder = new TextLineEncoder(defaultCharset, 
terminator);
    ProtocolDecoder delimitedDecoder = new TextLineDecoder(defaultCharset, 
terminator);
    ProtocolCodecFilter codecFilter = new 
ProtocolCodecFilter(delimitedEncoder, delimitedDecoder);
    ioSession.getFilterChain().addLast("protocol", codecFilter);
}

This all works fine, but it seems horribly inefficient to create a filter, 
encoder and decoder for each and every connection established. Particularly 
when the ioSession provides the concept of attributes that can be used to 
store stateful data. Is my implementation actually correct? It seems a 
better approach (from a performance standpoint) would be to create stateless 
versions of those objects and tie them to the IOAcceptor itself unless I'm 
missing something (which seems likely at this point).

I've seen examples that add filters to either the ioSession OR the 
ioAcceptor. What's the difference? Adding to the acceptor adds them to all 
new sessions, is that correct?

Thanks in advance for any advice/help. I appreciate it!

Michael 



Re: Dumb Questions about MINA Implementation

Posted by Mike Grundvig <mi...@electrotank.com>.
Thanks Niklas! That makes sense. I was using the SumUp example as a 
foundation for my code and it simply creates the needed objects in the 
sessionCreated method. This is where I started going astray. Thanks again!

Michael

----- Original Message ----- 
From: "Niklas Therning" <ni...@trillian.se>
To: <mi...@directory.apache.org>
Sent: Sunday, September 10, 2006 1:16 AM
Subject: Re: Dumb Questions about MINA Implementation


> Mike Grundvig wrote:
>> ...
>>
>> public void sessionCreated(IoSession ioSession) throws Exception {
>>    ProtocolEncoder delimitedEncoder = new
>> TextLineEncoder(defaultCharset, terminator);
>>    ProtocolDecoder delimitedDecoder = new
>> TextLineDecoder(defaultCharset, terminator);
>>    ProtocolCodecFilter codecFilter = new
>> ProtocolCodecFilter(delimitedEncoder, delimitedDecoder);
>>    ioSession.getFilterChain().addLast("protocol", codecFilter);
>> }
>>
>> This all works fine, but it seems horribly inefficient to create a
>> filter, encoder and decoder for each and every connection established.
>> Particularly when the ioSession provides the concept of attributes
>> that can be used to store stateful data. Is my implementation actually
>> correct? It seems a better approach (from a performance standpoint)
>> would be to create stateless versions of those objects and tie them to
>> the IOAcceptor itself unless I'm missing something (which seems likely
>> at this point).
>>
>> I've seen examples that add filters to either the ioSession OR the
>> ioAcceptor. What's the difference? Adding to the acceptor adds them to
>> all new sessions, is that correct?
> Yes, you're correct. Adding filters to the IoAcceptor means that they
> will be added to all session created from that IoAcceptor. I'd say that
> I would either add the ProtocolCodecFilter to the IoAcceptor or use a
> single ProtocolCodecFilter instance in my IoHandler like this:
>
> public MyIoHandler() {
>   ProtocolEncoder delimitedEncoder = new
> TextLineEncoder(defaultCharset, terminator);
>   ProtocolDecoder delimitedDecoder = new
> TextLineDecoder(defaultCharset, terminator);
>   this.codecFilter = new ProtocolCodecFilter(delimitedEncoder,
> delimitedDecoder);
> }
>
> public void sessionCreated(IoSession ioSession) throws Exception {
>   ioSession.getFilterChain().addLast("protocol", codecFilter);
> }
>
> Of course, for this to work TextLineEncoder, TextLineDecoder and
> ProtocolCodecFilter must be stateless. Luckily they are.
>
> HTH
>
> -- 
> Niklas Therning
> www.spamdrain.net
>
> 



Re: Dumb Questions about MINA Implementation

Posted by Niklas Therning <ni...@trillian.se>.
Mike Grundvig wrote:
> ...
>
> public void sessionCreated(IoSession ioSession) throws Exception {
>    ProtocolEncoder delimitedEncoder = new
> TextLineEncoder(defaultCharset, terminator);
>    ProtocolDecoder delimitedDecoder = new
> TextLineDecoder(defaultCharset, terminator);
>    ProtocolCodecFilter codecFilter = new
> ProtocolCodecFilter(delimitedEncoder, delimitedDecoder);
>    ioSession.getFilterChain().addLast("protocol", codecFilter);
> }
>
> This all works fine, but it seems horribly inefficient to create a
> filter, encoder and decoder for each and every connection established.
> Particularly when the ioSession provides the concept of attributes
> that can be used to store stateful data. Is my implementation actually
> correct? It seems a better approach (from a performance standpoint)
> would be to create stateless versions of those objects and tie them to
> the IOAcceptor itself unless I'm missing something (which seems likely
> at this point).
>
> I've seen examples that add filters to either the ioSession OR the
> ioAcceptor. What's the difference? Adding to the acceptor adds them to
> all new sessions, is that correct?
Yes, you're correct. Adding filters to the IoAcceptor means that they
will be added to all session created from that IoAcceptor. I'd say that
I would either add the ProtocolCodecFilter to the IoAcceptor or use a
single ProtocolCodecFilter instance in my IoHandler like this:

public MyIoHandler() {
   ProtocolEncoder delimitedEncoder = new
TextLineEncoder(defaultCharset, terminator);
   ProtocolDecoder delimitedDecoder = new
TextLineDecoder(defaultCharset, terminator);
   this.codecFilter = new ProtocolCodecFilter(delimitedEncoder,
delimitedDecoder);
}

public void sessionCreated(IoSession ioSession) throws Exception {
   ioSession.getFilterChain().addLast("protocol", codecFilter);
}

Of course, for this to work TextLineEncoder, TextLineDecoder and
ProtocolCodecFilter must be stateless. Luckily they are.

HTH

-- 
Niklas Therning
www.spamdrain.net