All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dxee.dject.extend.ShutdownHookModule Maven / Gradle / Ivy

Go to download

A collection of guice extensions, help to improve the developer experience of guice.

There is a newer version: 1.5.1
Show newest version
package com.github.dxee.dject.extend;

import javax.inject.Inject;
import javax.inject.Singleton;

import com.github.dxee.dject.lifecycle.LifecycleShutdown;
import com.google.inject.AbstractModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * When installed ShutdownHookModule will link a JVM shutdown hook to
 * LifecycleManager so that calling System.exit() will shutdown 
 * it down.
 * 
 * 
 * {@code
 *    Dject.builder().withModule(new ShutdownHookModule());
 * }
 * 
*/ public final class ShutdownHookModule extends AbstractModule { private static final Logger LOGGER = LoggerFactory.getLogger(ShutdownHookModule.class); @Singleton public static class SystemShutdownHook extends Thread { @Inject public SystemShutdownHook(final LifecycleShutdown shutdown) { Runtime.getRuntime().addShutdownHook(new Thread(() -> { try { LOGGER.info("Runtime Lifecycle shutdown hook begain running"); shutdown.shutdown(); } catch (Throwable t) { LOGGER.info("Runtime Lifecycle shutdown hook result in error ", t); throw t; } LOGGER.info("Runtime Lifecycle shutdown hook finished running"); })); } } @Override protected void configure() { bind(SystemShutdownHook.class).asEagerSingleton(); } @Override public boolean equals(Object obj) { return getClass().equals(obj.getClass()); } @Override public int hashCode() { return getClass().hashCode(); } @Override public String toString() { return "ShutdownHookModule[]"; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy