Search in sources :

Example 6 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 {
    LOG.debug("Running ScheduledJob: jobExecutionContext={}", jobExecutionContext);
    SchedulerContext schedulerContext = getSchedulerContext(jobExecutionContext);
    ScheduledJobState state = (ScheduledJobState) schedulerContext.get(jobExecutionContext.getJobDetail().getKey().toString());
    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().getKey(), e);
        }
    }
}
Also used : 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)

Example 7 with RoutePolicy

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

the class SpringScheduledRoutePolicyTest method startRouteWithPolicy.

@SuppressWarnings("unchecked")
private CamelContext startRouteWithPolicy(String policyBeanName) throws Exception {
    CamelContext context = new DefaultCamelContext();
    List<RouteDefinition> routes = (List<RouteDefinition>) applicationContext.getBean("testRouteContext");
    RoutePolicy policy = applicationContext.getBean(policyBeanName, RoutePolicy.class);
    assertTrue(getTestType() == TestType.SIMPLE ? policy instanceof SimpleScheduledRoutePolicy : policy instanceof CronScheduledRoutePolicy);
    routes.get(0).routePolicy(policy);
    ((ModelCamelContext) context).addRouteDefinitions(routes);
    context.start();
    return context;
}
Also used : CamelContext(org.apache.camel.CamelContext) ModelCamelContext(org.apache.camel.model.ModelCamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) RouteDefinition(org.apache.camel.model.RouteDefinition) List(java.util.List) RoutePolicy(org.apache.camel.spi.RoutePolicy) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ModelCamelContext(org.apache.camel.model.ModelCamelContext)

Example 8 with RoutePolicy

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

the class SpringScheduledRoutePolicyTest method startRouteWithPolicy.

@SuppressWarnings("unchecked")
private CamelContext startRouteWithPolicy(String policyBeanName) throws Exception {
    CamelContext context = new DefaultCamelContext();
    List<RouteDefinition> routes = (List<RouteDefinition>) applicationContext.getBean("testRouteContext");
    RoutePolicy policy = applicationContext.getBean(policyBeanName, RoutePolicy.class);
    assertTrue(getTestType() == TestType.SIMPLE ? policy instanceof SimpleScheduledRoutePolicy : policy instanceof CronScheduledRoutePolicy);
    routes.get(0).routePolicy(policy);
    ((ModelCamelContext) context).addRouteDefinitions(routes);
    context.start();
    return context;
}
Also used : CamelContext(org.apache.camel.CamelContext) ModelCamelContext(org.apache.camel.model.ModelCamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) RouteDefinition(org.apache.camel.model.RouteDefinition) List(java.util.List) RoutePolicy(org.apache.camel.spi.RoutePolicy) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ModelCamelContext(org.apache.camel.model.ModelCamelContext)

Example 9 with RoutePolicy

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

the class ManagedRoute method getRoutePolicyList.

public String getRoutePolicyList() {
    List<RoutePolicy> policyList = route.getRouteContext().getRoutePolicyList();
    if (policyList == null || policyList.isEmpty()) {
        // return an empty string to have it displayed nicely in JMX consoles
        return "";
    }
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < policyList.size(); i++) {
        RoutePolicy policy = policyList.get(i);
        sb.append(policy.getClass().getSimpleName());
        sb.append("(").append(ObjectHelper.getIdentityHashCode(policy)).append(")");
        if (i < policyList.size() - 1) {
            sb.append(", ");
        }
    }
    return sb.toString();
}
Also used : RoutePolicy(org.apache.camel.spi.RoutePolicy)

Example 10 with RoutePolicy

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

the class DefaultRouteContext method commit.

public void commit() {
    // now lets turn all of the event driven consumer processors into a single route
    if (!eventDrivenProcessors.isEmpty()) {
        Processor target = Pipeline.newInstance(getCamelContext(), eventDrivenProcessors);
        // force creating the route id so its known ahead of the route is started
        String routeId = route.idOrCreate(getCamelContext().getNodeIdFactory());
        // and wrap it in a unit of work so the UoW is on the top, so the entire route will be in the same UoW
        CamelInternalProcessor internal = new CamelInternalProcessor(target);
        internal.addAdvice(new CamelInternalProcessor.UnitOfWorkProcessorAdvice(this));
        // and then optionally add route policy processor if a custom policy is set
        List<RoutePolicy> routePolicyList = getRoutePolicyList();
        if (routePolicyList != null && !routePolicyList.isEmpty()) {
            for (RoutePolicy policy : routePolicyList) {
                // this ensures Camel can control the lifecycle of the policy
                if (!camelContext.hasService(policy)) {
                    try {
                        camelContext.addService(policy);
                    } catch (Exception e) {
                        throw ObjectHelper.wrapRuntimeCamelException(e);
                    }
                }
            }
            internal.addAdvice(new CamelInternalProcessor.RoutePolicyAdvice(routePolicyList));
        }
        // wrap in route inflight processor to track number of inflight exchanges for the route
        internal.addAdvice(new CamelInternalProcessor.RouteInflightRepositoryAdvice(camelContext.getInflightRepository(), routeId));
        // wrap in JMX instrumentation processor that is used for performance stats
        internal.addAdvice(new CamelInternalProcessor.InstrumentationAdvice("route"));
        // wrap in route lifecycle
        internal.addAdvice(new CamelInternalProcessor.RouteLifecycleAdvice());
        // wrap in REST binding
        if (route.getRestBindingDefinition() != null) {
            try {
                internal.addAdvice(route.getRestBindingDefinition().createRestBindingAdvice(this));
            } catch (Exception e) {
                throw ObjectHelper.wrapRuntimeCamelException(e);
            }
        }
        // wrap in contract
        if (route.getInputType() != null || route.getOutputType() != null) {
            Contract contract = new Contract();
            if (route.getInputType() != null) {
                contract.setInputType(route.getInputType().getUrn());
                contract.setValidateInput(route.getInputType().isValidate());
            }
            if (route.getOutputType() != null) {
                contract.setOutputType(route.getOutputType().getUrn());
                contract.setValidateOutput(route.getOutputType().isValidate());
            }
            internal.addAdvice(new ContractAdvice(contract));
        }
        // and create the route that wraps the UoW
        Route edcr = new EventDrivenConsumerRoute(this, getEndpoint(), internal);
        edcr.getProperties().put(Route.ID_PROPERTY, routeId);
        edcr.getProperties().put(Route.PARENT_PROPERTY, Integer.toHexString(route.hashCode()));
        edcr.getProperties().put(Route.DESCRIPTION_PROPERTY, route.getDescriptionText());
        if (route.getGroup() != null) {
            edcr.getProperties().put(Route.GROUP_PROPERTY, route.getGroup());
        }
        String rest = "false";
        if (route.isRest() != null && route.isRest()) {
            rest = "true";
        }
        edcr.getProperties().put(Route.REST_PROPERTY, rest);
        // after the route is created then set the route on the policy processor so we get hold of it
        CamelInternalProcessor.RoutePolicyAdvice task = internal.getAdvice(CamelInternalProcessor.RoutePolicyAdvice.class);
        if (task != null) {
            task.setRoute(edcr);
        }
        CamelInternalProcessor.RouteLifecycleAdvice task2 = internal.getAdvice(CamelInternalProcessor.RouteLifecycleAdvice.class);
        if (task2 != null) {
            task2.setRoute(edcr);
        }
        // invoke init on route policy
        if (routePolicyList != null && !routePolicyList.isEmpty()) {
            for (RoutePolicy policy : routePolicyList) {
                policy.onInit(edcr);
            }
        }
        routes.add(edcr);
    }
}
Also used : CamelInternalProcessor(org.apache.camel.processor.CamelInternalProcessor) Processor(org.apache.camel.Processor) RoutePolicy(org.apache.camel.spi.RoutePolicy) NoSuchEndpointException(org.apache.camel.NoSuchEndpointException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) CamelInternalProcessor(org.apache.camel.processor.CamelInternalProcessor) ContractAdvice(org.apache.camel.processor.ContractAdvice) Contract(org.apache.camel.spi.Contract) Route(org.apache.camel.Route) ShutdownRoute(org.apache.camel.ShutdownRoute)

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