Search in sources :

Example 1 with RoutePolicy

use of org.apache.camel.spi.RoutePolicy in project camel by apache.

the class RouteService method doShutdown.

@Override
protected void doShutdown() throws Exception {
    for (Route route : routes) {
        try (MDCHelper mdcHelper = new MDCHelper(route.getId())) {
            LOG.debug("Shutting down services on route: {}", route.getId());
            // gather list of services to stop as we need to start child services as well
            Set<Service> services = gatherChildServices(route, true);
            // shutdown services
            stopChildService(route, services, true);
            // shutdown the route itself
            ServiceHelper.stopAndShutdownServices(route);
            // endpoints should only be stopped when Camel is shutting down
            // see more details in the warmUp method
            ServiceHelper.stopAndShutdownServices(route.getEndpoint());
            // invoke callbacks on route policy
            if (route.getRouteContext().getRoutePolicyList() != null) {
                for (RoutePolicy routePolicy : route.getRouteContext().getRoutePolicyList()) {
                    routePolicy.onRemove(route);
                }
            }
            // fire event
            EventHelper.notifyRouteRemoved(camelContext, route);
        }
    }
    // need to call onRoutesRemove when the CamelContext is shutting down or Route is shutdown
    for (LifecycleStrategy strategy : camelContext.getLifecycleStrategies()) {
        strategy.onRoutesRemove(routes);
    }
    // remove the routes from the inflight registry
    for (Route route : routes) {
        camelContext.getInflightRepository().removeRoute(route.getId());
    }
    // remove the routes from the collections
    camelContext.removeRouteCollection(routes);
    // clear inputs on shutdown
    inputs.clear();
    warmUpDone.set(false);
    endpointDone.set(false);
}
Also used : LifecycleStrategy(org.apache.camel.spi.LifecycleStrategy) Service(org.apache.camel.Service) RoutePolicy(org.apache.camel.spi.RoutePolicy) Route(org.apache.camel.Route)

Example 2 with RoutePolicy

use of org.apache.camel.spi.RoutePolicy in project camel by apache.

the class RouteService method doStop.

protected void doStop() throws Exception {
    // if we are stopping CamelContext then we are shutting down
    boolean isShutdownCamelContext = camelContext.isStopping();
    if (isShutdownCamelContext || isRemovingRoutes()) {
        // need to call onRoutesRemove when the CamelContext is shutting down or Route is shutdown
        for (LifecycleStrategy strategy : camelContext.getLifecycleStrategies()) {
            strategy.onRoutesRemove(routes);
        }
    }
    for (Route route : routes) {
        try (MDCHelper mdcHelper = new MDCHelper(route.getId())) {
            LOG.debug("Stopping services on route: {}", route.getId());
            // gather list of services to stop as we need to start child services as well
            Set<Service> services = gatherChildServices(route, true);
            // stop services
            stopChildService(route, services, isShutdownCamelContext);
            // stop the route itself
            if (isShutdownCamelContext) {
                ServiceHelper.stopAndShutdownServices(route);
            } else {
                ServiceHelper.stopServices(route);
            }
            // invoke callbacks on route policy
            if (route.getRouteContext().getRoutePolicyList() != null) {
                for (RoutePolicy routePolicy : route.getRouteContext().getRoutePolicyList()) {
                    routePolicy.onStop(route);
                }
            }
            // fire event
            EventHelper.notifyRouteStopped(camelContext, route);
        }
    }
    if (isRemovingRoutes()) {
        camelContext.removeRouteCollection(routes);
    }
    // need to warm up again
    warmUpDone.set(false);
}
Also used : LifecycleStrategy(org.apache.camel.spi.LifecycleStrategy) Service(org.apache.camel.Service) RoutePolicy(org.apache.camel.spi.RoutePolicy) Route(org.apache.camel.Route)

Example 3 with RoutePolicy

use of org.apache.camel.spi.RoutePolicy in project camel by apache.

the class RouteDefinition method addRoutes.

// Implementation methods
// -------------------------------------------------------------------------
protected RouteContext addRoutes(CamelContext camelContext, Collection<Route> routes, FromDefinition fromType) throws Exception {
    RouteContext routeContext = new DefaultRouteContext(camelContext, this, fromType, routes);
    // configure tracing
    if (trace != null) {
        Boolean isTrace = CamelContextHelper.parseBoolean(camelContext, getTrace());
        if (isTrace != null) {
            routeContext.setTracing(isTrace);
            if (isTrace) {
                log.debug("Tracing is enabled on route: {}", getId());
            // tracing is added in the DefaultChannel so we can enable it on the fly
            }
        }
    }
    // configure message history
    if (messageHistory != null) {
        Boolean isMessageHistory = CamelContextHelper.parseBoolean(camelContext, getMessageHistory());
        if (isMessageHistory != null) {
            routeContext.setMessageHistory(isMessageHistory);
            if (isMessageHistory) {
                log.debug("Message history is enabled on route: {}", getId());
            }
        }
    }
    // configure stream caching
    if (streamCache != null) {
        Boolean isStreamCache = CamelContextHelper.parseBoolean(camelContext, getStreamCache());
        if (isStreamCache != null) {
            routeContext.setStreamCaching(isStreamCache);
            if (isStreamCache) {
                log.debug("StreamCaching is enabled on route: {}", getId());
            }
        }
    }
    // configure handle fault
    if (handleFault != null) {
        Boolean isHandleFault = CamelContextHelper.parseBoolean(camelContext, getHandleFault());
        if (isHandleFault != null) {
            routeContext.setHandleFault(isHandleFault);
            if (isHandleFault) {
                log.debug("HandleFault is enabled on route: {}", getId());
                // only add a new handle fault if not already a global configured on camel context
                if (HandleFault.getHandleFault(camelContext) == null) {
                    addInterceptStrategy(new HandleFault());
                }
            }
        }
    }
    // configure delayer
    if (delayer != null) {
        Long delayer = CamelContextHelper.parseLong(camelContext, getDelayer());
        if (delayer != null) {
            routeContext.setDelayer(delayer);
            if (delayer > 0) {
                log.debug("Delayer is enabled with: {} ms. on route: {}", delayer, getId());
            } else {
                log.debug("Delayer is disabled on route: {}", getId());
            }
        }
    }
    // configure route policy
    if (routePolicies != null && !routePolicies.isEmpty()) {
        for (RoutePolicy policy : routePolicies) {
            log.debug("RoutePolicy is enabled: {} on route: {}", policy, getId());
            routeContext.getRoutePolicyList().add(policy);
        }
    }
    if (routePolicyRef != null) {
        StringTokenizer policyTokens = new StringTokenizer(routePolicyRef, ",");
        while (policyTokens.hasMoreTokens()) {
            String ref = policyTokens.nextToken().trim();
            RoutePolicy policy = CamelContextHelper.mandatoryLookup(camelContext, ref, RoutePolicy.class);
            log.debug("RoutePolicy is enabled: {} on route: {}", policy, getId());
            routeContext.getRoutePolicyList().add(policy);
        }
    }
    if (camelContext.getRoutePolicyFactories() != null) {
        for (RoutePolicyFactory factory : camelContext.getRoutePolicyFactories()) {
            RoutePolicy policy = factory.createRoutePolicy(camelContext, getId(), this);
            if (policy != null) {
                log.debug("RoutePolicy is enabled: {} on route: {}", policy, getId());
                routeContext.getRoutePolicyList().add(policy);
            }
        }
    }
    // configure auto startup
    Boolean isAutoStartup = CamelContextHelper.parseBoolean(camelContext, getAutoStartup());
    if (isAutoStartup != null) {
        log.debug("Using AutoStartup {} on route: {}", isAutoStartup, getId());
        routeContext.setAutoStartup(isAutoStartup);
    }
    // configure shutdown
    if (shutdownRoute != null) {
        log.debug("Using ShutdownRoute {} on route: {}", getShutdownRoute(), getId());
        routeContext.setShutdownRoute(getShutdownRoute());
    }
    if (shutdownRunningTask != null) {
        log.debug("Using ShutdownRunningTask {} on route: {}", getShutdownRunningTask(), getId());
        routeContext.setShutdownRunningTask(getShutdownRunningTask());
    }
    // should inherit the intercept strategies we have defined
    routeContext.setInterceptStrategies(this.getInterceptStrategies());
    // force endpoint resolution
    routeContext.getEndpoint();
    for (LifecycleStrategy strategy : camelContext.getLifecycleStrategies()) {
        strategy.onRouteContextCreate(routeContext);
    }
    // validate route has output processors
    if (!ProcessorDefinitionHelper.hasOutputs(outputs, true)) {
        RouteDefinition route = routeContext.getRoute();
        String at = fromType.toString();
        Exception cause = new IllegalArgumentException("Route " + route.getId() + " has no output processors." + " You need to add outputs to the route such as to(\"log:foo\").");
        throw new FailedToCreateRouteException(route.getId(), route.toString(), at, cause);
    }
    List<ProcessorDefinition<?>> list = new ArrayList<ProcessorDefinition<?>>(outputs);
    for (ProcessorDefinition<?> output : list) {
        try {
            output.addRoutes(routeContext, routes);
        } catch (Exception e) {
            RouteDefinition route = routeContext.getRoute();
            throw new FailedToCreateRouteException(route.getId(), route.toString(), output.toString(), e);
        }
    }
    routeContext.commit();
    return routeContext;
}
Also used : DefaultRouteContext(org.apache.camel.impl.DefaultRouteContext) ArrayList(java.util.ArrayList) RoutePolicy(org.apache.camel.spi.RoutePolicy) NoSuchEndpointException(org.apache.camel.NoSuchEndpointException) FailedToCreateRouteException(org.apache.camel.FailedToCreateRouteException) DefaultRouteContext(org.apache.camel.impl.DefaultRouteContext) RouteContext(org.apache.camel.spi.RouteContext) FailedToCreateRouteException(org.apache.camel.FailedToCreateRouteException) StringTokenizer(java.util.StringTokenizer) LifecycleStrategy(org.apache.camel.spi.LifecycleStrategy) RoutePolicyFactory(org.apache.camel.spi.RoutePolicyFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HandleFault(org.apache.camel.processor.interceptor.HandleFault)

Example 4 with RoutePolicy

use of org.apache.camel.spi.RoutePolicy in project camel by apache.

the class FlipRoutePolicyTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            // create the flip route policy
            RoutePolicy policy = new FlipRoutePolicy("foo", "bar");
            // use the flip route policy in the foo route
            from("timer://foo").routeId("foo").routePolicy(policy).setBody().constant("Foo message").to("log:foo").to("mock:foo");
            // use the flip route policy in the bar route and do NOT start
            // this route on startup
            from("timer://bar").routeId("bar").routePolicy(policy).noAutoStartup().setBody().constant("Bar message").to("log:bar").to("mock:bar");
        }
    };
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) RoutePolicy(org.apache.camel.spi.RoutePolicy)

Example 5 with RoutePolicy

use of org.apache.camel.spi.RoutePolicy in project camel by apache.

the class ScheduledJob method execute.

public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
    SchedulerContext schedulerContext;
    try {
        schedulerContext = jobExecutionContext.getScheduler().getContext();
    } catch (SchedulerException e) {
        throw new JobExecutionException("Failed to obtain scheduler context for job " + jobExecutionContext.getJobDetail().getName());
    }
    ScheduledJobState state = (ScheduledJobState) schedulerContext.get(jobExecutionContext.getJobDetail().getName());
    Action storedAction = state.getAction();
    Route storedRoute = state.getRoute();
    List<RoutePolicy> policyList = storedRoute.getRouteContext().getRoutePolicyList();
    for (RoutePolicy policy : policyList) {
        try {
            if (policy instanceof ScheduledRoutePolicy) {
                ((ScheduledRoutePolicy) policy).onJobExecute(storedAction, storedRoute);
            }
        } catch (Exception e) {
            throw new JobExecutionException("Failed to execute Scheduled Job for route " + storedRoute.getId() + " with trigger name: " + jobExecutionContext.getTrigger().getFullName(), e);
        }
    }
}
Also used : SchedulerException(org.quartz.SchedulerException) JobExecutionException(org.quartz.JobExecutionException) SchedulerContext(org.quartz.SchedulerContext) RoutePolicy(org.apache.camel.spi.RoutePolicy) Route(org.apache.camel.Route) JobExecutionException(org.quartz.JobExecutionException) SchedulerException(org.quartz.SchedulerException)

Aggregations

RoutePolicy (org.apache.camel.spi.RoutePolicy)11 Route (org.apache.camel.Route)6 LifecycleStrategy (org.apache.camel.spi.LifecycleStrategy)3 List (java.util.List)2 CamelContext (org.apache.camel.CamelContext)2 NoSuchEndpointException (org.apache.camel.NoSuchEndpointException)2 Service (org.apache.camel.Service)2 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)2 ModelCamelContext (org.apache.camel.model.ModelCamelContext)2 RouteDefinition (org.apache.camel.model.RouteDefinition)2 JobExecutionException (org.quartz.JobExecutionException)2 SchedulerContext (org.quartz.SchedulerContext)2 SchedulerException (org.quartz.SchedulerException)2 ArrayList (java.util.ArrayList)1 StringTokenizer (java.util.StringTokenizer)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 FailedToCreateRouteException (org.apache.camel.FailedToCreateRouteException)1 Processor (org.apache.camel.Processor)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1 ShutdownRoute (org.apache.camel.ShutdownRoute)1