DarVar
DarVar

Reputation: 18124

ClosedChannelException after sending async message and then closing channel

Netty is throwing the following exception when i send a message asynchronously from my client and then close the channel.

java.nio.channels.ClosedChannelException
at sun.nio.ch.SocketChannelImpl.ensureWriteOpen(SocketChannelImpl.java:133)
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:324)
at org.jboss.netty.channel.socket.nio.SocketSendBufferPool$PooledSendBuffer.transferTo(SocketSendBufferPool.java:239)
at org.jboss.netty.channel.socket.nio.NioWorker.write0(NioWorker.java:469)
at org.jboss.netty.channel.socket.nio.NioWorker.writeFromTaskLoop(NioWorker.java:392)
at org.jboss.netty.channel.socket.nio.NioSocketChannel$WriteTask.run(NioSocketChannel.java:276)
at org.jboss.netty.channel.socket.nio.NioWorker.processWriteTaskQueue(NioWorker.java:268)
at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:199)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)

My send code is as follows:

Channels.write(clientChannel, messageObject);

My close code is as follows:

    ChannelGroupFuture future = ALL_CHANNELS.close();
    future.awaitUninterruptibly();
    if (null != clientBootstrap) {
        clientBootstrap.releaseExternalResources();
    }

Is there anything I need to do to flush the Channel before closing?

Upvotes: 2

Views: 3705

Answers (2)

Norman Maurer
Norman Maurer

Reputation: 211

Sorry I don't understand your problem. You now close the channel after the write is complete (like Veebs suggested) and now you complain it fails for new writes as the channel is closed ? If you need more then write operation you need to add the ChannelFutureListener.CLOSE to the last future that was returned.

Hope it helps..

Upvotes: 0

Veebs
Veebs

Reputation: 2388

I had a similar problem. The fix for me was to close on the ChannelFuture returned by the write.

ChannelFuture f = clientChannel.write(messageObject);
f.addListener(ChannelFutureListener.CLOSE);

Hope this works for you.

Upvotes: 2

Related Questions