How to integrate a blocking task without using netty event loop?

1,661 views
Skip to first unread message

knalli...@googlemail.com

unread,
May 22, 2014, 5:37:16 PM5/22/14
to ne...@googlegroups.com
Hi,

first of all: I'm still very unfamiliar with Netty, and I'm sorry if I really missed the correct solution. I've looked into the repo's examples, but it seems there no such one?

My goal: trying to integrate an unavoidable blocking task (let's say a JSch or JDBC connection pool) into a Netty nio flow.

As far as I know yet, a blocking task is basically no problem (it will work). However, it blocks at least one worker being part of the event loop worker group.
Using Netty 4.0 (latest 4.0.19), I've extended the telnet server example and providing it at https://github.com/knalli/netty-with-bio-task

I've replaced the echo message reply from the TelnetServerHandler with 

ChannelFuture future = new BlockingCall(ctx).call();
future.addListener(ChannelFutureListener.CLOSE);

using BlockingCall as a simple Callable with a Thread.sleep (shortend)

class BlockingCall implements Callable<ChannelFuture> {

private final ChannelHandlerContext ctx;

public BlockingCall(ChannelHandlerContext ctx) {
this.ctx = ctx;
}

@Override
public ChannelFuture call() throws Exception {
try { Thread.sleep(3000); } catch (InterruptedException e) {
return ctx.write("Task finished at " + new Date());
}
}

With that configuration, the channel works fine. A simple echo "123" | nc $localhost $port will wait 3 seconds to reply.

If I try to extract the task into an own executor within my ChannelInitializer

// constructor
executorGroup = new DefaultEventExecutorGroup(10);

// initChannel
pipeline.addLast(executorGroup, "handler", serverHandler);

the channel will be closed right after leaving channelRead0. I can see with debugging/logging that the task will be processed correctly incl. receiving events in the handler.. but the channel is already closed (my echo/nc command from above returns immediately).

Am I running into a bug, or did i misunderstood how move a blocking task out of netty worker pool?


Thank you very much!

luke....@gmail.com

unread,
Aug 20, 2014, 8:38:22 AM8/20/14
to ne...@googlegroups.com, knalli...@googlemail.com
Did you figure this out? I'm trying to accomplish the same thing.

Norman Maurer

unread,
Aug 20, 2014, 1:58:37 PM8/20/14
to ne...@googlegroups.com, luke....@gmail.com, knalli...@googlemail.com
Just use a EventExecutorGroup when adding your ChannelHandler that does blocking stuff to the ChannelPipeline.


-- 
Norman Maurer
Principal Software Engineer
Red Hat
--

---
You received this message because you are subscribed to the Google Groups "Netty discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netty+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

knalli...@googlemail.com

unread,
Aug 21, 2014, 2:58:18 PM8/21/14
to ne...@googlegroups.com, knalli...@googlemail.com, luke....@gmail.com
In this case, the command `nc` was not doing what I thought it'd do. Basically it blocks not after all.

Basically, just like my demo code shows: use a dedicated executor group and it's working.

Luke Hutchison

unread,
Aug 21, 2014, 3:43:26 PM8/21/14
to knalli...@googlemail.com, ne...@googlegroups.com
Thanks Norman and knallisworld for the replies.

It turns out EventExecutor is gone in Netty 5.0, but this pointed me in the right direction, and (for the record, in case anybody stumbles into this thread) I just created a new NioEventLoopGroup and used that in place of the EventExecutor in the New and Noteworthy example for Netty 4.0. All seems to be working well now.

Reply all
Reply to author
Forward
0 new messages