Confused about Netty NIO
Iain McGinniss
iainmcgin at gmail.com
Thu Oct 15 04:34:54 EDT 2009
Typically you would have two (or three) thread pool executors:
- One for the channel bosses (the server channel acceptors)
- One for the accepted channels (maybe the same as the first)
- One for your task processing, after the request has been decoded and
understood
The general idea, from what I have found in practice of using Netty so far,
is that the threads given to Netty should only be used by quick,
non-blocking code within the upstream and downstream handlers of the
pipeline. Typically at the top of the upstream I have a handler which looks
at the resultant request object (for instance, an HTTPRequest object or some
other object representing a command produced by a channel handler further
down the stack) and then dispatches this request to some other subsystem. I
do this by wrapping the request in a Runnable which does the dispatch and
pass this Runnable to an Executor.
Alternatively, if I have established some kind of contract between the
dispatcher and the receiving subsystem that the request should be processed
quickly, I just make the method call on the subsystem and let it decide
whether it should defer execution to a separate pool or not. The first
option is typically safer as it does not allow the programmer of the
subsystem to accidentally stall my thread within Netty, which often has dire
consequences.
Hope this helps.
Iain McGinniss
www.onedrum.com
On Wed, Oct 14, 2009 at 4:30 PM, phi6 <phidinh6 at gmail.com> wrote:
>
> I understand that I should use NIO, not for performance specifically but
> for
> scalability, as traditional threaded model uses up a lot of resources due
> to
> context switching.
>
> So I am using NioServerSocketChannelFactory to implement my server.
>
> So I understand, instead of thread per connection like blocking-IO, all
> reading and writing is done on one thread asynchronously. My question is,
> how does Netty (or NIO in general for that matter) handle logic that takes
> a
> long time to complete?
>
> For example, in my pipeline I have a message received handler which does
> some tasks based on the message it receives from clients. What if a client
> wants the server to do SomeLongTask() which takes about 10 seconds. If it's
> all on one thread, how does it manage a second connection that wants to do
> SomeOtherTask() before SomeLongTask() is completed? In the threaded model
> they would run concurrently... but with NIO there is a single thread
> handling all my client messages... does SomeOtherTask() get queued up until
> SomeLongTask() is completed?
>
> I'm probably thinking about this completely the wrong way, please advise.
>
> Many thanks
> --
> View this message in context:
> http://n2.nabble.com/Confused-about-Netty-NIO-tp3823513p3823513.html
> Sent from the Netty User Group mailing list archive at Nabble.com.
> _______________________________________________
> netty-users mailing list
> netty-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/netty-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/netty-users/attachments/20091015/01f94ead/attachment.html
More information about the netty-users
mailing list