1

我有一个使用 的项目,Netty但存在一些问题:

如果connection闲置一段时间,是否connection会自行关闭?如果它自己关闭,我该如何设置关闭时间?

有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. 如何自己处理异常或无异常关闭连接?

4

2 回答 2

1

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.

于 2012-08-17T10:17:32.960 回答
0

试试这个方法:

/** 释放连接*/

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,然后再关闭自己的频道。

于 2013-02-08T16:08:21.057 回答