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