ShutdownHook implements Runnable { void shutdown(long endTime, int fromStage, int toStage) { for (int i = fromStage; i <= toStage; i++) { ShutdownStage stage = stages[i]; if (stage == null) continue; logger.info("shutdown stage: {}", i); for (Shutdown shutdown : stage.shutdowns) { try { long timeoutInMs = endTime - System.currentTimeMillis(); if (timeoutInMs < 1000) timeoutInMs = 1000; shutdown.execute(timeoutInMs); } catch (Throwable e) { logger.warn(errorCode("FAILED_TO_STOP"), "failed to shutdown, method={}", shutdown, e); } } } } ShutdownHook(LogManager logManager); void add(int stage, Shutdown shutdown); @Override void run(); static final int STAGE_0; static final int STAGE_1; static final int STAGE_2; static final int STAGE_3; static final int STAGE_4; static final int STAGE_5; static final int STAGE_7; static final int STAGE_8; static final int STAGE_9; }
@Test void shutdown() throws Exception { var shutdown = mock(ShutdownHook.Shutdown.class); shutdownHook.add(ShutdownHook.STAGE_7, shutdown); shutdownHook.shutdown(System.currentTimeMillis(), ShutdownHook.STAGE_7, ShutdownHook.STAGE_9); verify(shutdown).execute(1000); }