Reputation: 23
I have a project that uses Netty
, and there're some problems:
If the connection
is idle for a while, whether the connection
will close itself or not? if it closes by itself, how can I set the close time?
There's 5 thread,and they send 100 data. They use the same channel e.g
/** release connect*/
public void closeConnect(ChannelFuture writeFuture){
if(writeFuture != null){
writeFuture.awaitUninterruptibly();
}
future.getChannel().close();
future.getChannel().getCloseFuture().awaitUninterruptibly();
client.releaseExternalResources();
}
//write data
ChannelFuture future = Channels.write(data);
closeConnect(future);
The above code will result in a closeChannelexception
. My question is: how can I avoid the exception?
Also, when I use ReadTimeoutHandler
at client, and set timeout
= 5s, and I make threads sleep for 6s, then ReadTimeoutException
occurs. When i invoke e.getChannel().close()
, it also creates a closeChannelException
. How can i handle the exception myself or close the connection without exception?
Upvotes: 1
Views: 3734
Reputation: 973
Try this way:
/** release connect*/
public void closeConnect(ChannelFuture writeFuture){
if(writeFuture != null){
writeFuture.awaitUninterruptibly();
}
future.getChannel().getCloseFuture().awaitUninterruptibly();
future.getChannel().close().awaitUninterruptibly();
client.releaseExternalResources();
}
//write data
ChannelFuture future = Channels.write(data);
closeConnect(future);
Try to close channelFuture first then channel itself.
Upvotes: 0
Reputation: 9260
You could catch ReadTimeoutException
if the API throws it and then call your closeConnect(...)
.
Seems to me though, that the channel is already closed; so either just ignore CloseChannelException
or handle it if it affects the program state.
Upvotes: 1