Search in sources :

Example 6 with CamelInternalProcessor

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

the class AggregateDefinition method createAggregator.

protected AggregateProcessor createAggregator(RouteContext routeContext) throws Exception {
    Processor childProcessor = this.createChildProcessor(routeContext, true);
    // wrap the aggregate route in a unit of work processor
    CamelInternalProcessor internal = new CamelInternalProcessor(childProcessor);
    internal.addAdvice(new CamelInternalProcessor.UnitOfWorkProcessorAdvice(routeContext));
    Expression correlation = getExpression().createExpression(routeContext);
    AggregationStrategy strategy = createAggregationStrategy(routeContext);
    boolean parallel = getParallelProcessing() != null && getParallelProcessing();
    boolean shutdownThreadPool = ProcessorDefinitionHelper.willCreateNewThreadPool(routeContext, this, parallel);
    ExecutorService threadPool = ProcessorDefinitionHelper.getConfiguredExecutorService(routeContext, "Aggregator", this, parallel);
    if (threadPool == null && !parallel) {
        // executor service is mandatory for the Aggregator
        // we do not run in parallel mode, but use a synchronous executor, so we run in current thread
        threadPool = new SynchronousExecutorService();
        shutdownThreadPool = true;
    }
    AggregateProcessor answer = new AggregateProcessor(routeContext.getCamelContext(), internal, correlation, strategy, threadPool, shutdownThreadPool);
    AggregationRepository repository = createAggregationRepository(routeContext);
    if (repository != null) {
        answer.setAggregationRepository(repository);
    }
    if (getAggregateController() == null && getAggregateControllerRef() != null) {
        setAggregateController(routeContext.mandatoryLookup(getAggregateControllerRef(), AggregateController.class));
    }
    // this EIP supports using a shared timeout checker thread pool or fallback to create a new thread pool
    boolean shutdownTimeoutThreadPool = false;
    ScheduledExecutorService timeoutThreadPool = timeoutCheckerExecutorService;
    if (timeoutThreadPool == null && timeoutCheckerExecutorServiceRef != null) {
        // lookup existing thread pool
        timeoutThreadPool = routeContext.getCamelContext().getRegistry().lookupByNameAndType(timeoutCheckerExecutorServiceRef, ScheduledExecutorService.class);
        if (timeoutThreadPool == null) {
            // then create a thread pool assuming the ref is a thread pool profile id
            timeoutThreadPool = routeContext.getCamelContext().getExecutorServiceManager().newScheduledThreadPool(this, AggregateProcessor.AGGREGATE_TIMEOUT_CHECKER, timeoutCheckerExecutorServiceRef);
            if (timeoutThreadPool == null) {
                throw new IllegalArgumentException("ExecutorServiceRef " + timeoutCheckerExecutorServiceRef + " not found in registry or as a thread pool profile.");
            }
            shutdownTimeoutThreadPool = true;
        }
    }
    answer.setTimeoutCheckerExecutorService(timeoutThreadPool);
    answer.setShutdownTimeoutCheckerExecutorService(shutdownTimeoutThreadPool);
    // set other options
    answer.setParallelProcessing(parallel);
    if (getOptimisticLocking() != null) {
        answer.setOptimisticLocking(getOptimisticLocking());
    }
    if (getCompletionPredicate() != null) {
        Predicate predicate = getCompletionPredicate().createPredicate(routeContext);
        answer.setCompletionPredicate(predicate);
    } else if (strategy instanceof Predicate) {
        // if aggregation strategy implements predicate and was not configured then use as fallback
        log.debug("Using AggregationStrategy as completion predicate: {}", strategy);
        answer.setCompletionPredicate((Predicate) strategy);
    }
    if (getCompletionTimeoutExpression() != null) {
        Expression expression = getCompletionTimeoutExpression().createExpression(routeContext);
        answer.setCompletionTimeoutExpression(expression);
    }
    if (getCompletionTimeout() != null) {
        answer.setCompletionTimeout(getCompletionTimeout());
    }
    if (getCompletionInterval() != null) {
        answer.setCompletionInterval(getCompletionInterval());
    }
    if (getCompletionSizeExpression() != null) {
        Expression expression = getCompletionSizeExpression().createExpression(routeContext);
        answer.setCompletionSizeExpression(expression);
    }
    if (getCompletionSize() != null) {
        answer.setCompletionSize(getCompletionSize());
    }
    if (getCompletionFromBatchConsumer() != null) {
        answer.setCompletionFromBatchConsumer(getCompletionFromBatchConsumer());
    }
    if (getEagerCheckCompletion() != null) {
        answer.setEagerCheckCompletion(getEagerCheckCompletion());
    }
    if (getIgnoreInvalidCorrelationKeys() != null) {
        answer.setIgnoreInvalidCorrelationKeys(getIgnoreInvalidCorrelationKeys());
    }
    if (getCloseCorrelationKeyOnCompletion() != null) {
        answer.setCloseCorrelationKeyOnCompletion(getCloseCorrelationKeyOnCompletion());
    }
    if (getDiscardOnCompletionTimeout() != null) {
        answer.setDiscardOnCompletionTimeout(getDiscardOnCompletionTimeout());
    }
    if (getForceCompletionOnStop() != null) {
        answer.setForceCompletionOnStop(getForceCompletionOnStop());
    }
    if (getCompleteAllOnStop() != null) {
        answer.setCompleteAllOnStop(getCompleteAllOnStop());
    }
    if (optimisticLockRetryPolicy == null) {
        if (getOptimisticLockRetryPolicyDefinition() != null) {
            answer.setOptimisticLockRetryPolicy(getOptimisticLockRetryPolicyDefinition().createOptimisticLockRetryPolicy());
        }
    } else {
        answer.setOptimisticLockRetryPolicy(optimisticLockRetryPolicy);
    }
    if (getAggregateController() != null) {
        answer.setAggregateController(getAggregateController());
    }
    return answer;
}
Also used : SynchronousExecutorService(org.apache.camel.util.concurrent.SynchronousExecutorService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CamelInternalProcessor(org.apache.camel.processor.CamelInternalProcessor) Processor(org.apache.camel.Processor) AggregateProcessor(org.apache.camel.processor.aggregate.AggregateProcessor) AggregateController(org.apache.camel.processor.aggregate.AggregateController) AggregateProcessor(org.apache.camel.processor.aggregate.AggregateProcessor) AsPredicate(org.apache.camel.spi.AsPredicate) Predicate(org.apache.camel.Predicate) CamelInternalProcessor(org.apache.camel.processor.CamelInternalProcessor) Expression(org.apache.camel.Expression) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) SynchronousExecutorService(org.apache.camel.util.concurrent.SynchronousExecutorService) AggregationRepository(org.apache.camel.spi.AggregationRepository) GroupedExchangeAggregationStrategy(org.apache.camel.processor.aggregate.GroupedExchangeAggregationStrategy) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

Example 7 with CamelInternalProcessor

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

the class ResequenceDefinition method createStreamResequencer.

/**
     * Creates a {@link StreamResequencer} instance applying the given <code>config</code>.
     * 
     * @param routeContext route context.
     * @param config stream resequencer configuration.
     * @return the configured stream resequencer.
     * @throws Exception can be thrwon
     */
protected StreamResequencer createStreamResequencer(RouteContext routeContext, StreamResequencerConfig config) throws Exception {
    Processor processor = this.createChildProcessor(routeContext, true);
    Expression expression = getExpression().createExpression(routeContext);
    CamelInternalProcessor internal = new CamelInternalProcessor(processor);
    internal.addAdvice(new CamelInternalProcessor.UnitOfWorkProcessorAdvice(routeContext));
    ObjectHelper.notNull(config, "config", this);
    ObjectHelper.notNull(expression, "expression", this);
    ExpressionResultComparator comparator;
    if (config.getComparatorRef() != null) {
        comparator = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), config.getComparatorRef(), ExpressionResultComparator.class);
    } else {
        comparator = config.getComparator();
    }
    comparator.setExpression(expression);
    StreamResequencer resequencer = new StreamResequencer(routeContext.getCamelContext(), internal, comparator, expression);
    resequencer.setTimeout(config.getTimeout());
    resequencer.setCapacity(config.getCapacity());
    resequencer.setRejectOld(config.getRejectOld());
    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) ExpressionResultComparator(org.apache.camel.processor.resequencer.ExpressionResultComparator) StreamResequencer(org.apache.camel.processor.StreamResequencer)

Example 8 with CamelInternalProcessor

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

the class OnCompletionDefinition method createProcessor.

@Override
public Processor createProcessor(RouteContext routeContext) throws Exception {
    // and therefore is in a better position to decide among context/route scoped OnCompletion at runtime
    if (routeScoped == null) {
        routeScoped = super.getParent() != null;
    }
    boolean isOnCompleteOnly = getOnCompleteOnly() != null && getOnCompleteOnly();
    boolean isOnFailureOnly = getOnFailureOnly() != null && getOnFailureOnly();
    boolean isParallelProcessing = getParallelProcessing() != null && getParallelProcessing();
    boolean original = getUseOriginalMessagePolicy() != null && getUseOriginalMessagePolicy();
    if (isOnCompleteOnly && isOnFailureOnly) {
        throw new IllegalArgumentException("Both onCompleteOnly and onFailureOnly cannot be true. Only one of them can be true. On node: " + this);
    }
    if (original) {
        // ensure allow original is turned on
        routeContext.setAllowUseOriginalMessage(true);
    }
    String routeId = routeContext.getRoute().idOrCreate(routeContext.getCamelContext().getNodeIdFactory());
    Processor childProcessor = this.createChildProcessor(routeContext, true);
    // wrap the on completion route in a unit of work processor
    CamelInternalProcessor internal = new CamelInternalProcessor(childProcessor);
    internal.addAdvice(new CamelInternalProcessor.UnitOfWorkProcessorAdvice(routeContext));
    onCompletions.put(routeId, internal);
    Predicate when = null;
    if (onWhen != null) {
        when = onWhen.getExpression().createPredicate(routeContext);
    }
    boolean shutdownThreadPool = ProcessorDefinitionHelper.willCreateNewThreadPool(routeContext, this, isParallelProcessing);
    ExecutorService threadPool = ProcessorDefinitionHelper.getConfiguredExecutorService(routeContext, "OnCompletion", this, isParallelProcessing);
    // should be after consumer by default
    boolean afterConsumer = mode == null || mode == OnCompletionMode.AfterConsumer;
    OnCompletionProcessor answer = new OnCompletionProcessor(routeContext.getCamelContext(), internal, threadPool, shutdownThreadPool, isOnCompleteOnly, isOnFailureOnly, when, original, afterConsumer);
    return answer;
}
Also used : CamelInternalProcessor(org.apache.camel.processor.CamelInternalProcessor) CamelInternalProcessor(org.apache.camel.processor.CamelInternalProcessor) Processor(org.apache.camel.Processor) OnCompletionProcessor(org.apache.camel.processor.OnCompletionProcessor) OnCompletionProcessor(org.apache.camel.processor.OnCompletionProcessor) ExecutorService(java.util.concurrent.ExecutorService) AsPredicate(org.apache.camel.spi.AsPredicate) Predicate(org.apache.camel.Predicate)

Example 9 with CamelInternalProcessor

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

the class CamelPostProcessorHelper method createConsumerProcessor.

/**
     * Create a processor which invokes the given method when an incoming
     * message exchange is received
     */
protected Processor createConsumerProcessor(final Object pojo, final Method method, final Endpoint endpoint) {
    BeanInfo info = new BeanInfo(getCamelContext(), method);
    BeanProcessor answer = new BeanProcessor(pojo, info);
    // must ensure the consumer is being executed in an unit of work so synchronization callbacks etc is invoked
    CamelInternalProcessor internal = new CamelInternalProcessor(answer);
    internal.addAdvice(new CamelInternalProcessor.UnitOfWorkProcessorAdvice(null));
    return internal;
}
Also used : CamelInternalProcessor(org.apache.camel.processor.CamelInternalProcessor) BeanInfo(org.apache.camel.component.bean.BeanInfo) BeanProcessor(org.apache.camel.component.bean.BeanProcessor)

Example 10 with CamelInternalProcessor

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

the class WireTapDefinition method createProcessor.

@Override
public Processor createProcessor(RouteContext routeContext) throws Exception {
    // executor service is mandatory for wire tap
    boolean shutdownThreadPool = ProcessorDefinitionHelper.willCreateNewThreadPool(routeContext, this, true);
    ExecutorService threadPool = ProcessorDefinitionHelper.getConfiguredExecutorService(routeContext, "WireTap", this, true);
    // must use InOnly for WireTap
    setPattern(ExchangePattern.InOnly);
    // create the send dynamic producer to send to the wire tapped endpoint
    SendDynamicProcessor dynamicTo = (SendDynamicProcessor) super.createProcessor(routeContext);
    // create error handler we need to use for processing the wire tapped
    Processor target = wrapInErrorHandler(routeContext, dynamicTo);
    // and wrap in unit of work
    CamelInternalProcessor internal = new CamelInternalProcessor(target);
    internal.addAdvice(new CamelInternalProcessor.UnitOfWorkProcessorAdvice(routeContext));
    // is true bt default
    boolean isCopy = getCopy() == null || getCopy();
    WireTapProcessor answer = new WireTapProcessor(dynamicTo, internal, getPattern(), threadPool, shutdownThreadPool);
    answer.setCopy(isCopy);
    if (newExchangeProcessorRef != null) {
        newExchangeProcessor = routeContext.mandatoryLookup(newExchangeProcessorRef, Processor.class);
    }
    if (newExchangeProcessor != null) {
        answer.addNewExchangeProcessor(newExchangeProcessor);
    }
    if (newExchangeExpression != null) {
        answer.setNewExchangeExpression(newExchangeExpression.createExpression(routeContext));
    }
    if (headers != null && !headers.isEmpty()) {
        for (SetHeaderDefinition header : headers) {
            Processor processor = createProcessor(routeContext, header);
            answer.addNewExchangeProcessor(processor);
        }
    }
    if (onPrepareRef != null) {
        onPrepare = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), onPrepareRef, Processor.class);
    }
    if (onPrepare != null) {
        answer.setOnPrepare(onPrepare);
    }
    return answer;
}
Also used : SendDynamicProcessor(org.apache.camel.processor.SendDynamicProcessor) CamelInternalProcessor(org.apache.camel.processor.CamelInternalProcessor) WireTapProcessor(org.apache.camel.processor.WireTapProcessor) CamelInternalProcessor(org.apache.camel.processor.CamelInternalProcessor) Processor(org.apache.camel.Processor) SendDynamicProcessor(org.apache.camel.processor.SendDynamicProcessor) ExecutorService(java.util.concurrent.ExecutorService) WireTapProcessor(org.apache.camel.processor.WireTapProcessor)

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