}
}
private static void setUpPingForChannel(final Channel channel, int interval) {
final AtomicBoolean isInClosed = new AtomicBoolean(false);
final PingThread t = new PingThread(channel, interval * 60 * 1000) {
protected void onDead(Throwable cause) {
try {
if (isInClosed.get()) {
LOGGER.log(FINE,"Ping failed after the channel is already partially closed",cause);
} else {
LOGGER.log(INFO,"Ping failed. Terminating the channel.",cause);
channel.close(cause);
}
} catch (IOException e) {
LOGGER.log(SEVERE,"Failed to terminate the channel: ",e);
}
}
protected void onDead() {
onDead(null);
}
};
channel.addListener(new Channel.Listener() {
@Override
public void onClosed(Channel channel, IOException cause) {
LOGGER.fine("Terminating ping thread for " + channel);
isInClosed.set(true);
t.interrupt(); // make sure the ping thread is terminated
}
});
t.start();
LOGGER.fine("Ping thread started for " + channel + " with a " + interval + " minute interval");
}