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 */
017package org.apache.camel.spi;
018
019import java.util.Collection;
020import java.util.concurrent.TimeUnit;
021
022import org.apache.camel.CamelContextAware;
023import org.apache.camel.Route;
024import org.apache.camel.ServiceStatus;
025import org.apache.camel.StaticService;
026
027/**
028 * Controller for managing the lifecycle of all the {@link Route}'s.
029 */
030public interface RouteController extends CamelContextAware, StaticService {
031
032    /**
033     * Return the list of routes controlled by this controller.
034     *
035     * @return the list of controlled routes
036     */
037    Collection<Route> getControlledRoutes();
038
039    /**
040     * Starts all the routes which currently is not started.
041     *
042     * @throws Exception is thrown if a route could not be started for whatever reason
043     */
044    void startAllRoutes() throws Exception;
045
046    /**
047     * Indicates whether current thread is starting route(s).
048     * <p/>
049     * This can be useful to know by {@link LifecycleStrategy} or the likes, in case
050     * they need to react differently.
051     *
052     * @return <tt>true</tt> if current thread is starting route(s), or <tt>false</tt> if not.
053     */
054    boolean isStartingRoutes();
055
056    /**
057     * Returns the current status of the given route
058     *
059     * @param routeId the route id
060     * @return the status for the route
061     */
062    ServiceStatus getRouteStatus(String routeId);
063
064    /**
065     * Starts the given route if it has been previously stopped
066     *
067     * @param routeId the route id
068     * @throws Exception is thrown if the route could not be started for whatever reason
069     */
070    void startRoute(String routeId) throws Exception;
071
072    /**
073     * Stops the given route using {@link org.apache.camel.spi.ShutdownStrategy}.
074     *
075     * @param routeId the route id
076     * @throws Exception is thrown if the route could not be stopped for whatever reason
077     * @see #suspendRoute(String)
078     */
079    void stopRoute(String routeId) throws Exception;
080
081    /**
082     * Stops the given route using {@link org.apache.camel.spi.ShutdownStrategy} with a specified timeout.
083     *
084     * @param routeId the route id
085     * @param timeout  timeout
086     * @param timeUnit the unit to use
087     * @throws Exception is thrown if the route could not be stopped for whatever reason
088     * @see #suspendRoute(String, long, java.util.concurrent.TimeUnit)
089     */
090    void stopRoute(String routeId, long timeout, TimeUnit timeUnit) throws Exception;
091
092    /**
093     * Stops the given route using {@link org.apache.camel.spi.ShutdownStrategy} with a specified timeout
094     * and optional abortAfterTimeout mode.
095     *
096     * @param routeId the route id
097     * @param timeout  timeout
098     * @param timeUnit the unit to use
099     * @param abortAfterTimeout should abort shutdown after timeout
100     * @return <tt>true</tt> if the route is stopped before the timeout
101     * @throws Exception is thrown if the route could not be stopped for whatever reason
102     * @see #suspendRoute(String, long, java.util.concurrent.TimeUnit)
103     */
104    boolean stopRoute(String routeId, long timeout, TimeUnit timeUnit, boolean abortAfterTimeout) throws Exception;
105
106    /**
107     * Suspends the given route using {@link org.apache.camel.spi.ShutdownStrategy}.
108     * <p/>
109     * Suspending a route is more gently than stopping, as the route consumers will be suspended (if they support)
110     * otherwise the consumers will be stopped.
111     * <p/>
112     * By suspending the route services will be kept running (if possible) and therefore its faster to resume the route.
113     * <p/>
114     * If the route does <b>not</b> support suspension the route will be stopped instead
115     *
116     * @param routeId the route id
117     * @throws Exception is thrown if the route could not be suspended for whatever reason
118     */
119    void suspendRoute(String routeId) throws Exception;
120
121    /**
122     * Suspends the given route using {@link org.apache.camel.spi.ShutdownStrategy} with a specified timeout.
123     * <p/>
124     * Suspending a route is more gently than stopping, as the route consumers will be suspended (if they support)
125     * otherwise the consumers will be stopped.
126     * <p/>
127     * By suspending the route services will be kept running (if possible) and therefore its faster to resume the route.
128     * <p/>
129     * If the route does <b>not</b> support suspension the route will be stopped instead
130     *
131     * @param routeId  the route id
132     * @param timeout  timeout
133     * @param timeUnit the unit to use
134     * @throws Exception is thrown if the route could not be suspended for whatever reason
135     */
136    void suspendRoute(String routeId, long timeout, TimeUnit timeUnit) throws Exception;
137
138    /**
139     * Resumes the given route if it has been previously suspended
140     * <p/>
141     * If the route does <b>not</b> support suspension the route will be started instead
142     *
143     * @param routeId the route id
144     * @throws Exception is thrown if the route could not be resumed for whatever reason
145     */
146    void resumeRoute(String routeId) throws Exception;
147
148    /**
149     * Access the underlying concrete RouteController implementation.
150     *
151     * @param clazz the proprietary class or interface of the underlying concrete RouteController.
152     * @return an instance of the underlying concrete RouteController as the required type.
153     */
154    default <T extends RouteController> T unwrap(Class<T> clazz) {
155        if (RouteController.class.isAssignableFrom(clazz)) {
156            return clazz.cast(this);
157        }
158
159        throw new IllegalArgumentException(
160            "Unable to unwrap this RouteController type (" + getClass() + ") to the required type (" + clazz + ")"
161        );
162    }
163}