Java 网络密接
我有一个项目使用了Java 网络密接,java,netty,Java,Netty,我有一个项目使用了Netty,但存在一些问题: 如果连接闲置一段时间,则连接是否会自动关闭?如果自动关闭,我如何设置关闭时间 有5个线程,它们发送100个数据。他们使用相同的频道 /** release connect*/ public void closeConnect(ChannelFuture writeFuture){ if(writeFuture != null){ writeFuture.awaitUninterruptibly(); }
Netty
,但存在一些问题:
如果连接
闲置一段时间,则连接
是否会自动关闭?如果自动关闭,我如何设置关闭时间
有5个线程,它们发送100个数据。他们使用相同的频道
/** 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);
上述代码将导致closeChannelexception
。我的问题是:如何避免例外
另外,当我在客户端使用
ReadTimeoutHandler
并将timeout
=5s时,我使线程睡眠6s,然后发生ReadTimeoutException
。当我调用e.getChannel().close()
时,它还会创建一个closeChannelException
。如何自己处理异常或无异常地关闭连接?如果API抛出它,您可以捕获ReadTimeoutException
,然后调用您的closeConnect(…)
但在我看来,这条通道已经关闭;因此,要么忽略
CloseChannelException
,要么在它影响程序状态时处理它。如果API抛出它,您可以捕获ReadTimeoutException
,然后调用您的closeConnect(…)
但在我看来,这条通道已经关闭;因此,只要忽略CloseChannelException
,或者在它影响程序状态时处理它。尝试以下方法:
/**释放连接*/
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);
尝试先关闭channel Future,然后关闭channel本身。尝试以下方法:
/**释放连接*/
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);
首先尝试关闭channelFuture,然后关闭通道本身。我无法捕获ReadTimeoutException,它由netty在exceptionCaught方法中捕获,此方法在channelUpstream中调用,如果我调用closeConnect(…),它也会创建closeChannelException。我也想忽略异常,但我的老板不能。我无法捕获ReadTimeoutException,它由netty在exceptionCaught方法中捕获,并且此方法在channelUpstream中调用,如果我调用closeConnect(…),它还会创建closeChannelException。我也想忽略这个例外,但我的老板不能。