001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.context;
018
019/**
020 * A common interface defining methods for start/stop lifecycle control.
021 * The typical use case for this is to control asynchronous processing.
022 * <b>NOTE: This interface does not imply specific auto-startup semantics.
023 * Consider implementing {@link SmartLifecycle} for that purpose.</b>
024 *
025 * <p>Can be implemented by both components (typically a Spring bean defined in a
026 * Spring context) and containers  (typically a Spring {@link ApplicationContext}
027 * itself). Containers will propagate start/stop signals to all components that
028 * apply within each container, e.g. for a stop/restart scenario at runtime.
029 *
030 * <p>Can be used for direct invocations or for management operations via JMX.
031 * In the latter case, the {@link org.springframework.jmx.export.MBeanExporter}
032 * will typically be defined with an
033 * {@link org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler},
034 * restricting the visibility of activity-controlled components to the Lifecycle
035 * interface.
036 *
037 * <p>Note that the present {@code Lifecycle} interface is only supported on
038 * <b>top-level singleton beans</b>. On any other component, the {@code Lifecycle}
039 * interface will remain undetected and hence ignored. Also, note that the extended
040 * {@link SmartLifecycle} interface provides sophisticated integration with the
041 * application context's startup and shutdown phases.
042 *
043 * @author Juergen Hoeller
044 * @since 2.0
045 * @see SmartLifecycle
046 * @see ConfigurableApplicationContext
047 * @see org.springframework.jms.listener.AbstractMessageListenerContainer
048 * @see org.springframework.scheduling.quartz.SchedulerFactoryBean
049 */
050public interface Lifecycle {
051
052        /**
053         * Start this component.
054         * <p>Should not throw an exception if the component is already running.
055         * <p>In the case of a container, this will propagate the start signal to all
056         * components that apply.
057         * @see SmartLifecycle#isAutoStartup()
058         */
059        void start();
060
061        /**
062         * Stop this component, typically in a synchronous fashion, such that the component is
063         * fully stopped upon return of this method. Consider implementing {@link SmartLifecycle}
064         * and its {@code stop(Runnable)} variant when asynchronous stop behavior is necessary.
065         * <p>Note that this stop notification is not guaranteed to come before destruction:
066         * On regular shutdown, {@code Lifecycle} beans will first receive a stop notification
067         * before the general destruction callbacks are being propagated; however, on hot
068         * refresh during a context's lifetime or on aborted refresh attempts, a given bean's
069         * destroy method will be called without any consideration of stop signals upfront.
070         * <p>Should not throw an exception if the component is not running (not started yet).
071         * <p>In the case of a container, this will propagate the stop signal to all components
072         * that apply.
073         * @see SmartLifecycle#stop(Runnable)
074         * @see org.springframework.beans.factory.DisposableBean#destroy()
075         */
076        void stop();
077
078        /**
079         * Check whether this component is currently running.
080         * <p>In the case of a container, this will return {@code true} only if <i>all</i>
081         * components that apply are currently running.
082         * @return whether the component is currently running
083         */
084        boolean isRunning();
085
086}