Search in sources :

Example 1 with CamelInternalProcessor

use of org.apache.camel.processor.CamelInternalProcessor in project camel by apache.

the class ProducerCache method prepareInternalProcessor.

protected CamelInternalProcessor prepareInternalProcessor(Producer producer, Processor resultProcessor) {
    // if we have a result processor then wrap in pipeline to execute both of them in sequence
    Processor target;
    if (resultProcessor != null) {
        List<Processor> processors = new ArrayList<Processor>(2);
        processors.add(producer);
        processors.add(resultProcessor);
        target = Pipeline.newInstance(getCamelContext(), processors);
    } else {
        target = producer;
    }
    // wrap in unit of work
    CamelInternalProcessor internal = new CamelInternalProcessor(target);
    internal.addAdvice(new CamelInternalProcessor.UnitOfWorkProcessorAdvice(null));
    return internal;
}
Also used : CamelInternalProcessor(org.apache.camel.processor.CamelInternalProcessor) CamelInternalProcessor(org.apache.camel.processor.CamelInternalProcessor) Processor(org.apache.camel.Processor) AsyncProcessor(org.apache.camel.AsyncProcessor) ArrayList(java.util.ArrayList)

Example 2 with CamelInternalProcessor

use of org.apache.camel.processor.CamelInternalProcessor in project camel by apache.

the class ResequenceDefinition method createBatchResequencer.

/**
     * Creates a batch {@link Resequencer} instance applying the given <code>config</code>.
     * 
     * @param routeContext route context.
     * @param config batch resequencer configuration.
     * @return the configured batch resequencer.
     * @throws Exception can be thrown
     */
@SuppressWarnings("deprecation")
protected Resequencer createBatchResequencer(RouteContext routeContext, BatchResequencerConfig config) throws Exception {
    Processor processor = this.createChildProcessor(routeContext, true);
    Expression expression = getExpression().createExpression(routeContext);
    // and wrap in unit of work
    CamelInternalProcessor internal = new CamelInternalProcessor(processor);
    internal.addAdvice(new CamelInternalProcessor.UnitOfWorkProcessorAdvice(routeContext));
    ObjectHelper.notNull(config, "config", this);
    ObjectHelper.notNull(expression, "expression", this);
    boolean isReverse = config.getReverse() != null && config.getReverse();
    boolean isAllowDuplicates = config.getAllowDuplicates() != null && config.getAllowDuplicates();
    Resequencer resequencer = new Resequencer(routeContext.getCamelContext(), internal, expression, isAllowDuplicates, isReverse);
    resequencer.setBatchSize(config.getBatchSize());
    resequencer.setBatchTimeout(config.getBatchTimeout());
    resequencer.setReverse(isReverse);
    resequencer.setAllowDuplicates(isAllowDuplicates);
    if (config.getIgnoreInvalidExchanges() != null) {
        resequencer.setIgnoreInvalidExchanges(config.getIgnoreInvalidExchanges());
    }
    return resequencer;
}
Also used : CamelInternalProcessor(org.apache.camel.processor.CamelInternalProcessor) CamelInternalProcessor(org.apache.camel.processor.CamelInternalProcessor) Processor(org.apache.camel.Processor) Expression(org.apache.camel.Expression) Resequencer(org.apache.camel.processor.Resequencer) StreamResequencer(org.apache.camel.processor.StreamResequencer)

Example 3 with CamelInternalProcessor

use of org.apache.camel.processor.CamelInternalProcessor in project camel by apache.

the class DefaultManagementLifecycleStrategy method onRoutesAdd.

public void onRoutesAdd(Collection<Route> routes) {
    for (Route route : routes) {
        // enabled, then enlist the route as a known route
        if (getCamelContext().getStatus().isStarting() || getManagementStrategy().getManagementAgent().getRegisterAlways() || getManagementStrategy().getManagementAgent().getRegisterNewRoutes()) {
            // register as known route id
            knowRouteIds.add(route.getId());
        }
        if (!shouldRegister(route, route)) {
            // avoid registering if not needed, skip to next route
            continue;
        }
        Object mr = getManagementObjectStrategy().getManagedObjectForRoute(camelContext, route);
        // skip already managed routes, for example if the route has been restarted
        if (getManagementStrategy().isManaged(mr, null)) {
            LOG.trace("The route is already managed: {}", route);
            continue;
        }
        // and set me as the counter
        if (route instanceof EventDrivenConsumerRoute) {
            EventDrivenConsumerRoute edcr = (EventDrivenConsumerRoute) route;
            Processor processor = edcr.getProcessor();
            if (processor instanceof CamelInternalProcessor && mr instanceof ManagedRoute) {
                CamelInternalProcessor internal = (CamelInternalProcessor) processor;
                ManagedRoute routeMBean = (ManagedRoute) mr;
                CamelInternalProcessor.InstrumentationAdvice task = internal.getAdvice(CamelInternalProcessor.InstrumentationAdvice.class);
                if (task != null) {
                    // we need to wrap the counter with the camel context so we get stats updated on the context as well
                    if (camelContextMBean != null) {
                        CompositePerformanceCounter wrapper = new CompositePerformanceCounter(routeMBean, camelContextMBean);
                        task.setCounter(wrapper);
                    } else {
                        task.setCounter(routeMBean);
                    }
                }
            }
        }
        try {
            manageObject(mr);
        } catch (JMException e) {
            LOG.warn("Could not register Route MBean", e);
        } catch (Exception e) {
            LOG.warn("Could not create Route MBean", e);
        }
    }
}
Also used : CamelInternalProcessor(org.apache.camel.processor.CamelInternalProcessor) CamelInternalProcessor(org.apache.camel.processor.CamelInternalProcessor) Processor(org.apache.camel.Processor) ManagedRoute(org.apache.camel.management.mbean.ManagedRoute) JMException(javax.management.JMException) EventDrivenConsumerRoute(org.apache.camel.impl.EventDrivenConsumerRoute) ManagedRoute(org.apache.camel.management.mbean.ManagedRoute) Route(org.apache.camel.Route) EventDrivenConsumerRoute(org.apache.camel.impl.EventDrivenConsumerRoute) MalformedObjectNameException(javax.management.MalformedObjectNameException) JMException(javax.management.JMException) VetoCamelContextStartException(org.apache.camel.VetoCamelContextStartException)

Example 4 with CamelInternalProcessor

use of org.apache.camel.processor.CamelInternalProcessor 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)

Example 5 with CamelInternalProcessor

use of org.apache.camel.processor.CamelInternalProcessor in project camel by apache.

the class ProducerCache method asyncDispatchExchange.

protected boolean asyncDispatchExchange(final Endpoint endpoint, Producer producer, final Processor resultProcessor, Exchange exchange, AsyncCallback callback) {
    // now lets dispatch
    LOG.debug(">>>> {} {}", endpoint, exchange);
    // set property which endpoint we send to
    exchange.setProperty(Exchange.TO_ENDPOINT, endpoint.getEndpointUri());
    // send the exchange using the processor
    try {
        if (eventNotifierEnabled) {
            callback = new EventNotifierCallback(callback, exchange, endpoint);
        }
        CamelInternalProcessor internal = prepareInternalProcessor(producer, resultProcessor);
        return internal.process(exchange, callback);
    } catch (Throwable e) {
        // ensure exceptions is caught and set on the exchange
        exchange.setException(e);
        callback.done(true);
        return true;
    }
}
Also used : CamelInternalProcessor(org.apache.camel.processor.CamelInternalProcessor)

Aggregations

CamelInternalProcessor (org.apache.camel.processor.CamelInternalProcessor)10 Processor (org.apache.camel.Processor)8 ExecutorService (java.util.concurrent.ExecutorService)3 Expression (org.apache.camel.Expression)3 Predicate (org.apache.camel.Predicate)2 Route (org.apache.camel.Route)2 StreamResequencer (org.apache.camel.processor.StreamResequencer)2 AsPredicate (org.apache.camel.spi.AsPredicate)2 ArrayList (java.util.ArrayList)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 JMException (javax.management.JMException)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 AsyncProcessor (org.apache.camel.AsyncProcessor)1 NoSuchEndpointException (org.apache.camel.NoSuchEndpointException)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1 ShutdownRoute (org.apache.camel.ShutdownRoute)1 VetoCamelContextStartException (org.apache.camel.VetoCamelContextStartException)1 BeanInfo (org.apache.camel.component.bean.BeanInfo)1 BeanProcessor (org.apache.camel.component.bean.BeanProcessor)1 EventDrivenConsumerRoute (org.apache.camel.impl.EventDrivenConsumerRoute)1