I / O线程中的SimpleChannelUpstreamHandler等待*()会导致死锁

时间:2012-05-30 11:22:07

标签: multithreading netty

在我的Netty SimpleChannelUpstreamHandler收到消息时,我需要启动与另一个Netty Server的连接并转发消息。但是,在启动第二个连接时,我使用:

ChannelFuture channelFuture = clientBootstrap.connect(new InetSocketAddress(host, port));
hannelFuture.awaitUninterruptibly();

导致以下错误:

java.lang.IllegalStateException: await*() in I/O thread causes a dead lock or sudden performance drop. Use addListener() instead or call await*() from a different thread.
    at org.jboss.netty.channel.DefaultChannelFuture.checkDeadLock(DefaultChannelFuture.java:314)
    at org.jboss.netty.channel.DefaultChannelFuture.awaitUninterruptibly(DefaultChannelFuture.java:226)
    at com.my.NettyClient.start(NettyClient.java:204)
  ....
  at com.my.MyChannelUpstreamHandler.messageReceived(MyChannelUpstreamHandler.java:52)

开始第二次连接的最佳方法是什么?我应该做以下事情吗?:

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {

        ExecutorService executorService = Executors.newSingleThreadExecutor();

        executorService.submit(new Runnable() {

            @Override
            public void run() {
                // Connect to another Netty Server...

                // Forward on message...
            }
        });

        executorService.shutdown();
...

在收到的每封邮件上开始一个新主题是否浪费?

1 个答案:

答案 0 :(得分:1)

签出代理示例,了解如何在不阻止的情况下执行此操作:

http://netty.io/docs/stable/xref/org/jboss/netty/example/proxy/HexDumpProxyInboundHandler.html