BrooklynShutdownHooks { public static void invokeStopOnShutdown(Entity entity) { if (!(entity instanceof Startable)) { log.warn("Not adding entity {} for stop-on-shutdown as not an instance of {}", entity, Startable.class.getSimpleName()); return; } try { semaphore.acquire(); if (isShutDown.get()) { semaphore.release(); try { log.warn("Call to invokeStopOnShutdown for "+entity+" while system already shutting down; invoking stop now and throwing exception"); Entities.destroy(entity); throw new IllegalStateException("Call to invokeStopOnShutdown for "+entity+" while system already shutting down"); } catch (Exception e) { throw new IllegalStateException("Call to invokeStopOnShutdown for "+entity+" while system already shutting down, had error: "+e, e); } } try { entitiesToStopOnShutdown.add(entity); } finally { semaphore.release(); } addShutdownHookIfNotAlready(); } catch (Exception e) { throw Exceptions.propagate(e); } }  static void setShutdownTimeout(Duration val); static void invokeStopOnShutdown(Entity entity); static void resetShutdownFlag(); static void invokeStopAppsOnShutdown(ManagementContext managementContext); static void invokeTerminateOnShutdown(ManagementContext managementContext);  }
@Test public void testInvokeStopEntityOnShutdown() throws Exception { BrooklynShutdownHooks.invokeStopOnShutdown(entity); BrooklynShutdownHooks.BrooklynShutdownHookJob job = BrooklynShutdownHookJob.newInstanceForTesting(); job.run(); assertTrue(entity.getCallHistory().contains("stop")); }