LifecycleManager extends AbstractLifecycle { public Lifecycle addShutdownTask(Runnable shutdownTask) { Lifecycle lifecycle = new Lifecycle() { @Override public void startup() {} @Override public void shutdown() { shutdownTask.run(); } }; addLifecycle(lifecycle); return lifecycle; } LifecycleManager();  LifecycleManager(ShutdownOrder shutdownOrder); void addLifecycle(Lifecycle lifecycle); void removeLifecycle(Lifecycle lifecycle); Lifecycle addStartupTask(Runnable startupTask); Lifecycle addShutdownTask(Runnable shutdownTask);  }
@Test public void testShutdownOrderLinear() { LifecycleManager manager = new LifecycleManager(); AtomicInteger i = new AtomicInteger(0); manager.addShutdownTask(() -> assertEquals(i.getAndIncrement(), 0)); manager.addShutdownTask(() -> assertEquals(i.getAndIncrement(), 1)); manager.addShutdownTask(() -> assertEquals(i.getAndIncrement(), 2)); manager.startup(); manager.shutdown(); }
@Test public void testShutdownOrderInverse() { LifecycleManager manager = new LifecycleManager(LifecycleManager.ShutdownOrder.INVERSE); AtomicInteger i = new AtomicInteger(0); manager.addShutdownTask(() -> assertEquals(i.getAndIncrement(), 2)); manager.addShutdownTask(() -> assertEquals(i.getAndIncrement(), 1)); manager.addShutdownTask(() -> assertEquals(i.getAndIncrement(), 0)); manager.startup(); manager.shutdown(); }