mina-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Trustin Lee" <trus...@gmail.com>
Subject Re: mina 0.9.4 - io/business logic decoupling with threads
Date Mon, 04 Sep 2006 02:23:49 GMT
Hi Horace,

On 9/1/06, Horace Pinker <flipcode2002@yahoo.com> wrote:
>
> i'm using mina 0.9.4.


We will release 0.9.5 soon, I think you'd better upgrade soon because
0.9.5contains a *lot* of fixes.

this is how i set up the acceptor:
>
> Acceptor = new SocketAcceptor();
> Acceptor.bind(new InetSocketAddress(Port), new NET_SessionHandler(this));


Looks good.

i read the docs and found some info regarding ThreadModel, however this info
> seems to be outdated for 0.9.4.


I am sorry about that.  Please let us know any JavaDoc which is outdated.
We need to fix it.

is by default iologic and businesslogic decoupled and in separate
> threadpools in mina 0.9.4?


We need to clarify things a little bit here.  Basically there are three
parties when you program a network application:

* I/O code - MINA transport implementation (e.g. SocketAcceptor)
* Protocol encoder and decoder - ProtocolCodecFilter in MINA
* Business logic - IoHandler in MINA

By default, a thread pool is inserted between the I/O code and the protocol
codec.  You can also insert another thread pool between the codec and the
business logic, but you have to insert it manually.

To insert the second thread pool:

SocketAcceptorConfig cfg = new SocketAcceptorConfig();

// Set the thread model to 'manual'
cfg.setThreadModel(ThreadModel.MANUAL);

DefaultIoFilterChainBuilder chain = cfg.getFilterChain();

// Add the first thread pool
ThreadPoolFilter firstPool = new ThreadPoolFilter();
firstPool.getThreadPool().setMaximumPoolSize(32); // max # of threads
chain.addLast("firstpool", firstPool);

// Add the codec
chain.addLast("codec", new ProtocolCodecFilter(...));

// Add the second thread pool
ThreadPoolFilter secondPool = new ThreadPoolFilter();
...
chain.addLast("secondpool", secondPool);

Otherwise, you could create your own ThreadModel implementation that inserts
thread pools as you want:

public class MyThreadModel implements ThreadModel {

    public void buildChain(IoSession session) {
        IoFilterChain chain = session.getFilterChain();
        chain.addFirst("pool", new ThreadPoolFilter(...));
        // insert the second thread pool next to the codec.
        chain.addAfter("codec", "pool-2", new ThreadPoolFilter(...));
    }
}

I think the second example is much cleaner and separates the responsibility
of classes properly.  Its weakness is that you have to assume the name of
the codec filter.

HTH,
Trustin
-- 
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP key fingerprints:
* E167 E6AF E73A CBCE EE41  4A29 544D DE48 FE95 4E7E
* B693 628E 6047 4F8F CFA4  455E 1C62 A7DC 0255 ECA6

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message