Reputation: 11
I have a netty client which does basically the following:
After connecting to the server via TCP, the program calls the below snippet. The first time goes well but the second has the following symptom - it loops forever. the channel is connected. The channelFuture.isDone() == false. The channelFuture.isSuccess()==false and channelFuture.getCause()== null. There are no exceptions that I can see or any info that helps me understand what the problem is. Any ideas how I can get more information to debug this problem?
ChannelFuture channelFuture = null;
while (channelFuture == null || !channelFuture.isSuccess()) {
try {
channelFuture = channel.write(content);
channelFuture.await(1000);
} catch (InterruptedException ex) {
Logger.getGlobal().log(Level.SEVERE, "Interrupted ", ex);
}
}
Upvotes: 1
Views: 1591
Reputation: 7232
I think that you have a problem with how you are handling the ChannelFuture.
In your posted code, if any iteration of the loop results in a failed write, then this failure and cause will be lost when the subsequent iteration of the loop starts (after 1000ms) since channelFuture is being reassigned. Furthermore, if it takes longer than 1000ms for a write to complete then you will be adding a queued write in the next iteration and falling further and further behind each iteration since the channelFuture is continuously replaced and the writes never complete in the 1000ms before you check channelFuture.isSuccess().
With your posted code, what you can do is read the return value of channelFuture.await(1000)
to see if the write succeeded or failed instead of continuing blindly and discarding the channelFuture.
However, ideally you should not use ChannelFuture.await()
at all and instead use ChannelFuture.addListener()
to add a listener that will be executed when the write completes. This is the correct way to use Netty and work with asynchronous code.
Upvotes: 4