Class DefaultShutdownCallbackRegistry
- java.lang.Object
-
- org.apache.logging.log4j.core.util.DefaultShutdownCallbackRegistry
-
- All Implemented Interfaces:
java.lang.Runnable,LifeCycle,LifeCycle2,ShutdownCallbackRegistry
public class DefaultShutdownCallbackRegistry extends java.lang.Object implements ShutdownCallbackRegistry, LifeCycle2, java.lang.Runnable
ShutdownRegistrationStrategy that simply usesRuntime.addShutdownHook(Thread). If no strategy is specified, this one is used for shutdown hook registration.- Since:
- 2.1
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classDefaultShutdownCallbackRegistry.RegisteredCancellable-
Nested classes/interfaces inherited from interface org.apache.logging.log4j.core.LifeCycle
LifeCycle.State
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Collection<java.lang.ref.Reference<Cancellable>>hooksprotected static LoggerLOGGERStatus logger.private java.lang.ref.Reference<java.lang.Thread>shutdownHookRefprivate java.util.concurrent.atomic.AtomicReference<LifeCycle.State>stateprivate java.util.concurrent.ThreadFactorythreadFactory-
Fields inherited from interface org.apache.logging.log4j.core.util.ShutdownCallbackRegistry
SHUTDOWN_CALLBACK_REGISTRY, SHUTDOWN_HOOK_ENABLED, SHUTDOWN_HOOK_MARKER
-
-
Constructor Summary
Constructors Modifier Constructor Description DefaultShutdownCallbackRegistry()Constructs a DefaultShutdownRegistrationStrategy.protectedDefaultShutdownCallbackRegistry(java.util.concurrent.ThreadFactory threadFactory)Constructs a DefaultShutdownRegistrationStrategy using the givenThreadFactory.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CancellableaddShutdownCallback(java.lang.Runnable callback)Adds a Runnable shutdown callback to this class.private voidaddShutdownHook(java.lang.Thread thread)LifeCycle.StategetState()Gets the life-cycle state.voidinitialize()booleanisStarted()Indicates if this can accept shutdown hooks.booleanisStopped()private voidremoveShutdownHook()voidrun()Executes the registered shutdown callbacks.voidstart()Registers the shutdown thread only if this is initialized.voidstop()booleanstop(long timeout, java.util.concurrent.TimeUnit timeUnit)Cancels the shutdown thread only if this is started.
-
-
-
Field Detail
-
LOGGER
protected static final Logger LOGGER
Status logger.
-
state
private final java.util.concurrent.atomic.AtomicReference<LifeCycle.State> state
-
threadFactory
private final java.util.concurrent.ThreadFactory threadFactory
-
hooks
private final java.util.Collection<java.lang.ref.Reference<Cancellable>> hooks
-
shutdownHookRef
private java.lang.ref.Reference<java.lang.Thread> shutdownHookRef
-
-
Constructor Detail
-
DefaultShutdownCallbackRegistry
public DefaultShutdownCallbackRegistry()
Constructs a DefaultShutdownRegistrationStrategy.
-
DefaultShutdownCallbackRegistry
protected DefaultShutdownCallbackRegistry(java.util.concurrent.ThreadFactory threadFactory)
Constructs a DefaultShutdownRegistrationStrategy using the givenThreadFactory.- Parameters:
threadFactory- the ThreadFactory to use to create aRuntimeshutdown hook thread
-
-
Method Detail
-
run
public void run()
Executes the registered shutdown callbacks.- Specified by:
runin interfacejava.lang.Runnable
-
addShutdownCallback
public Cancellable addShutdownCallback(java.lang.Runnable callback)
Description copied from interface:ShutdownCallbackRegistryAdds a Runnable shutdown callback to this class. Note: The returnedCancellablemust be retained on heap by caller to avoid premature garbage-collection of the registered callback (and to ensure the callback runs on shutdown).- Specified by:
addShutdownCallbackin interfaceShutdownCallbackRegistry- Parameters:
callback- the shutdown callback to be executed upon shutdown.- Returns:
- a Cancellable wrapper of the provided callback or
nullif the shutdown hook is disabled and cannot be added.
-
initialize
public void initialize()
- Specified by:
initializein interfaceLifeCycle
-
start
public void start()
Registers the shutdown thread only if this is initialized.
-
addShutdownHook
private void addShutdownHook(java.lang.Thread thread)
-
stop
public boolean stop(long timeout, java.util.concurrent.TimeUnit timeUnit)Cancels the shutdown thread only if this is started.- Specified by:
stopin interfaceLifeCycle2- Parameters:
timeout- the maximum time to waittimeUnit- the time unit of the timeout argument- Returns:
- true if the receiver was stopped cleanly and normally, false otherwise.
-
removeShutdownHook
private void removeShutdownHook()
-
getState
public LifeCycle.State getState()
Description copied from interface:LifeCycleGets the life-cycle state.
-
isStarted
public boolean isStarted()
Indicates if this can accept shutdown hooks.
-
-