I have the following class
public class MaintanceTools {
public static final ScheduledThreadPoolExecutor THREADSUPERVISER;
private static final int ALLOWEDIDLESECONDS = 1*20;
static {
THREADSUPERVISER = new ScheduledThreadPoolExecutor(10);
}
public static void launchThreadsSupervising() {
THREADSUPERVISER.scheduleWithFixedDelay(() -> {
System.out.println("maintance launched " + Instant.now());
ACTIVECONNECTIONS.forEach((connection) -> {
try {
if ( !connection.isDataConnected() &&
(connection.getLastActionInstant()
.until(Instant.now(), SECONDS) > ALLOWEDIDLESECONDS)) {
connection.closeFTPConnection();
ACTIVECONNECTIONS.remove(connection);
}
} catch (Throwable e) { }
});
System.out.println("maintance finished " + Instant.now());
}, 0, 20, TimeUnit.SECONDS);
}
}
Which iterates over all FTP connections (cause I write FTP server), checks if connection not transmitting any data and idle for some time and closes the connection if so. The problem is task never runs after some exceptions thrown in the interrupting thread. I know that it's written in docs If any execution of the task encounters an exception, subsequent executions are suppressed. Otherwise, the task will only terminate via cancellation or termination of the executor. And I have the exception, but it is caught and do not go outside throwing function. This function throws AsynchronousCloseException because it hangs on channel.read(readBuffer); and when connection is closed, exception thrown and caught.
The question is how to make THREADSUPERVISER work regardless any thrown and handled exceptions.
Debug output:
- maintance launched 2017-08-30T14:03:05.504Z // launched and finished as expected
- maintance finished 2017-08-30T14:03:05.566Z
- output: FTPConnection id: 176 220 Service ready.
- ……
- output: FTPConnection id: 190 226 File stored 135 bytes.
- closing data socket: FTP connection 190, /0:0:0:0:0:0:0:1:1409
- maintance launched 2017-08-30T14:03:25.581Z // launched and finished as expected
- maintance finished 2017-08-30T14:03:25.581Z
- async exception error reading. // got exception
- maintance launched 2017-08-30T14:03:45.596Z // launched, but not finished and never run again
- output: FTPConnection id: 176 221 Timeout exceeded, closing control and data connection.
- closing data socket: FTP connection 176, /0:0:0:0:0:0:0:1:1407
As turns out, the problem was in
I had ConcurrentModifyingException. Solution in http://code.nomad-labs.com/2011/12/09/mother-fk-the-scheduledexecutorservice/ worked perfectly