HADOOP-15366. Add a helper shutdown routine in HadoopExecutor to ensure clean shutdown. Contributed by Shashikant Banerjee.

This commit is contained in:
Mukul Kumar Singh 2018-04-07 16:29:01 +05:30
parent eb47c3de74
commit 0b345b7653
1 changed files with 33 additions and 1 deletions

View File

@ -27,7 +27,7 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
/** Factory methods for ExecutorService, ScheduledExecutorService instances.
* These executor service instances provide additional functionality (e.g
@ -91,6 +91,38 @@ public final class HadoopExecutors {
return Executors.newSingleThreadScheduledExecutor(threadFactory);
}
/**
* Helper routine to shutdown a executorService.
*
* @param executorService - executorService
* @param logger - Logger
* @param timeout - Timeout
* @param unit - TimeUnits, generally seconds.
*/
public static void shutdown(ExecutorService executorService, Logger logger,
long timeout, TimeUnit unit) {
try {
if (executorService != null) {
executorService.shutdown();
try {
if (!executorService.awaitTermination(timeout, unit)) {
executorService.shutdownNow();
}
if (!executorService.awaitTermination(timeout, unit)) {
logger.error("Unable to shutdown properly.");
}
} catch (InterruptedException e) {
logger.error("Error attempting to shutdown.", e);
executorService.shutdownNow();
}
}
} catch (Exception e) {
logger.error("Error during shutdown: ", e);
throw e;
}
}
//disable instantiation
private HadoopExecutors() { }
}