001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.spi;
018    
019    import java.util.List;
020    import java.util.concurrent.TimeUnit;
021    
022    import org.apache.camel.CamelContext;
023    import org.apache.camel.Service;
024    
025    /**
026     * Pluggable shutdown strategy executed during shutdown of routes.
027     * <p/>
028     * Shutting down routes in a reliable and graceful manner is not a trivial task. Therefore Camel provides a pluggable
029     * strategy allowing 3rd party to use their own strategy if needed.
030     * <p/>
031     * The key problem is to stop the input consumers for the routes such that no new messages is coming into Camel.
032     * But at the same time still keep the routes running so the existing in flight exchanges can still be run to
033     * completion. On top of that there are some in memory components (such as SEDA) which may have pending messages
034     * on its in memory queue which we want to run to completion as well, otherwise they will get lost.
035     * <p/>
036     * Camel provides a default strategy which supports all that that can be used as inspiration for your own strategy.
037     *
038     * @version $Revision: 893963 $
039     * @see org.apache.camel.spi.ShutdownAware
040     */
041    public interface ShutdownStrategy extends Service {
042    
043        /**
044         * Shutdown the routes
045         *
046         * @param context   the camel context
047         * @param routes the routes, ordered by the order they was started
048         * @throws Exception is thrown if error shutting down the consumers, however its preferred to avoid this
049         */
050        void shutdown(CamelContext context, List<RouteStartupOrder> routes) throws Exception;
051    
052        /**
053         * Set an timeout to wait for the shutdown to complete.
054         * <p/>
055         * Setting a value of 0 or negative will disable timeout and wait until complete
056         * (potential blocking forever)
057         *
058         * @param timeout timeout in millis
059         */
060        void setTimeout(long timeout);
061    
062        /**
063         * Gets the timeout.
064         * <p/>
065         * Use 0 or a negative value to disable timeout
066         *
067         * @return the timeout
068         */
069        long getTimeout();
070    
071        /**
072         * Set the time unit to use
073         *
074         * @param timeUnit the unit to use
075         */
076        void setTimeUnit(TimeUnit timeUnit);
077    
078        /**
079         * Gets the time unit used
080         *
081         * @return the time unit
082         */
083        TimeUnit getTimeUnit();
084    
085        /**
086         * Sets whether to force shutdown of all consumers when a timeout occurred and thus
087         * not all consumers was shutdown within that period.
088         * <p/>
089         * You should have good reasons to set this option to <tt>false</tt> as it means that the routes
090         * keep running and is halted abruptly when {@link CamelContext} has been shutdown.
091         *
092         * @param shutdownNowOnTimeout <tt>true</tt> to force shutdown, <tt>false</tt> to leave them running
093         */
094        void setShutdownNowOnTimeout(boolean shutdownNowOnTimeout);
095    
096        /**
097         * whether to force shutdown of all consumers when a timeout occurred.
098         *
099         * @return force shutdown or not
100         */
101        boolean isShutdownNowOnTimeout();
102    }