Search in sources :

Example 1 with ErrorHandlerBuilderSupport

use of org.apache.camel.builder.ErrorHandlerBuilderSupport in project camel by apache.

the class DefaultCamelContext method removeRoute.

public synchronized boolean removeRoute(String routeId) throws Exception {
    // remove the route from ErrorHandlerBuilder if possible
    if (getErrorHandlerBuilder() instanceof ErrorHandlerBuilderSupport) {
        ErrorHandlerBuilderSupport builder = (ErrorHandlerBuilderSupport) getErrorHandlerBuilder();
        builder.removeOnExceptionList(routeId);
    }
    // gather a map of all the endpoints in use by the routes, so we can known if a given endpoints is in use
    // by one or more routes, when we remove the route
    Map<String, Set<Endpoint>> endpointsInUse = new HashMap<String, Set<Endpoint>>();
    for (Map.Entry<String, RouteService> entry : routeServices.entrySet()) {
        endpointsInUse.put(entry.getKey(), entry.getValue().gatherEndpoints());
    }
    RouteService routeService = routeServices.get(routeId);
    if (routeService != null) {
        if (getRouteStatus(routeId).isStopped()) {
            routeService.setRemovingRoutes(true);
            shutdownRouteService(routeService);
            removeRouteDefinition(routeId);
            routeServices.remove(routeId);
            // remove route from startup order as well, as it was removed
            Iterator<RouteStartupOrder> it = routeStartupOrder.iterator();
            while (it.hasNext()) {
                RouteStartupOrder order = it.next();
                if (order.getRoute().getId().equals(routeId)) {
                    it.remove();
                }
            }
            // from the route which we have removed, then remove all its private endpoints
            // (eg the endpoints which are not in use by other routes)
            Set<Endpoint> toRemove = new LinkedHashSet<Endpoint>();
            for (Endpoint endpoint : endpointsInUse.get(routeId)) {
                // how many times is the endpoint in use
                int count = 0;
                for (Set<Endpoint> endpoints : endpointsInUse.values()) {
                    if (endpoints.contains(endpoint)) {
                        count++;
                    }
                }
                // notice we will count ourselves so if there is only 1 then its safe to remove
                if (count <= 1) {
                    toRemove.add(endpoint);
                }
            }
            for (Endpoint endpoint : toRemove) {
                log.debug("Removing: {} which was only in use by route: {}", endpoint, routeId);
                removeEndpoint(endpoint);
            }
            return true;
        } else {
            return false;
        }
    }
    return false;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ErrorHandlerBuilderSupport(org.apache.camel.builder.ErrorHandlerBuilderSupport) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) RouteStartupOrder(org.apache.camel.spi.RouteStartupOrder) Endpoint(org.apache.camel.Endpoint) Endpoint(org.apache.camel.Endpoint) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Aggregations

HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Endpoint (org.apache.camel.Endpoint)1 ErrorHandlerBuilderSupport (org.apache.camel.builder.ErrorHandlerBuilderSupport)1 RouteStartupOrder (org.apache.camel.spi.RouteStartupOrder)1