1 /*
2  * Hunt - A refined core library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.util.Lifecycle;
13 
14 import core.atomic;
15 
16 
17 import hunt.logging.ConsoleLogger;
18 
19 /**
20  * A common interface defining methods for start/stop lifecycle control.
21  * The typical use case for this is to control asynchronous processing.
22  * <b>NOTE: This interface does not imply specific auto-startup semantics.
23  * Consider implementing {@link SmartLifecycle} for that purpose.</b>
24  *
25  * <p>Can be implemented by both components (typically a Spring bean defined in a
26  * Spring context) and containers  (typically a Spring {@link ApplicationContext}
27  * itself). Containers will propagate start/stop signals to all components that
28  * apply within each container, e.g. for a stop/restart scenario at runtime.
29  *
30  * <p>Can be used for direct invocations or for management operations via JMX.
31  * In the latter case, the {@link org.springframework.jmx.export.MBeanExporter}
32  * will typically be defined with an
33  * {@link org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler},
34  * restricting the visibility of activity-controlled components to the Lifecycle
35  * interface.
36  *
37  * <p>Note that the present {@code Lifecycle} interface is only supported on
38  * <b>top-level singleton beans</b>. On any other component, the {@code Lifecycle}
39  * interface will remain undetected and hence ignored. Also, note that the extended
40  * {@link SmartLifecycle} interface provides sophisticated integration with the
41  * application context's startup and shutdown phases.
42  *
43  */
44 interface Lifecycle {
45 	/**
46 	 * Start this component.
47 	 * <p>Should not throw an exception if the component is already running.
48 	 * <p>In the case of a container, this will propagate the start signal to all
49 	 * components that apply.
50 	 */    
51 	void start();
52 
53 	/**
54 	 * Stop this component, typically in a synchronous fashion, such that the component is
55 	 * fully stopped upon return of this method. Consider implementing {@link SmartLifecycle}
56 	 * and its {@code stop(Runnable)} variant when asynchronous stop behavior is necessary.
57 	 * <p>Note that this stop notification is not guaranteed to come before destruction:
58 	 * On regular shutdown, {@code Lifecycle} beans will first receive a stop notification
59 	 * before the general destruction callbacks are being propagated; however, on hot
60 	 * refresh during a context's lifetime or on aborted refresh attempts, a given bean's
61 	 * destroy method will be called without any consideration of stop signals upfront.
62 	 * <p>Should not throw an exception if the component is not running (not started yet).
63 	 * <p>In the case of a container, this will propagate the stop signal to all components
64 	 * that apply.
65 	 */
66 	void stop();
67 
68 
69 	/**
70 	 * Check whether this component is currently running.
71 	 * <p>In the case of a container, this will return {@code true} only if <i>all</i>
72 	 * components that apply are currently running.
73 	 * @return whether the component is currently running
74 	 */
75 	bool isRunning();
76 }