Search in sources :

Example 26 with AggregationStrategy

use of org.apache.camel.processor.aggregate.AggregationStrategy in project camel by apache.

the class MulticastParallelStreamingTimeoutTest method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            from("direct:start").multicast(new AggregationStrategy() {

                public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
                    if (oldExchange == null) {
                        return newExchange;
                    }
                    String body = oldExchange.getIn().getBody(String.class);
                    oldExchange.getIn().setBody(body + newExchange.getIn().getBody(String.class));
                    return oldExchange;
                }
            }).parallelProcessing().streaming().timeout(2000).to("direct:a", "direct:b", "direct:c").end().to("mock:result");
            from("direct:a").delay(3000).setBody(constant("A"));
            from("direct:b").delay(500).setBody(constant("B"));
            from("direct:c").setBody(constant("C"));
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) RouteBuilder(org.apache.camel.builder.RouteBuilder) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

Example 27 with AggregationStrategy

use of org.apache.camel.processor.aggregate.AggregationStrategy 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 28 with AggregationStrategy

use of org.apache.camel.processor.aggregate.AggregationStrategy in project camel by apache.

the class AggregateDefinition method createAggregationStrategy.

private AggregationStrategy createAggregationStrategy(RouteContext routeContext) {
    AggregationStrategy strategy = getAggregationStrategy();
    if (strategy == null && strategyRef != null) {
        Object aggStrategy = routeContext.lookup(strategyRef, Object.class);
        if (aggStrategy instanceof AggregationStrategy) {
            strategy = (AggregationStrategy) aggStrategy;
        } else if (aggStrategy != null) {
            AggregationStrategyBeanAdapter adapter = new AggregationStrategyBeanAdapter(aggStrategy, getAggregationStrategyMethodName());
            if (getStrategyMethodAllowNull() != null) {
                adapter.setAllowNullNewExchange(getStrategyMethodAllowNull());
                adapter.setAllowNullOldExchange(getStrategyMethodAllowNull());
            }
            strategy = adapter;
        } else {
            throw new IllegalArgumentException("Cannot find AggregationStrategy in Registry with name: " + strategyRef);
        }
    }
    if (groupExchanges != null && groupExchanges) {
        if (strategy != null || strategyRef != null) {
            throw new IllegalArgumentException("Options groupExchanges and AggregationStrategy cannot be enabled at the same time");
        }
        if (eagerCheckCompletion != null && !eagerCheckCompletion) {
            throw new IllegalArgumentException("Option eagerCheckCompletion cannot be false when groupExchanges has been enabled");
        }
        // set eager check to enabled by default when using grouped exchanges
        setEagerCheckCompletion(true);
        // if grouped exchange is enabled then use special strategy for that
        strategy = new GroupedExchangeAggregationStrategy();
    }
    if (strategy == null) {
        throw new IllegalArgumentException("AggregationStrategy or AggregationStrategyRef must be set on " + this);
    }
    if (strategy instanceof CamelContextAware) {
        ((CamelContextAware) strategy).setCamelContext(routeContext.getCamelContext());
    }
    return strategy;
}
Also used : CamelContextAware(org.apache.camel.CamelContextAware) GroupedExchangeAggregationStrategy(org.apache.camel.processor.aggregate.GroupedExchangeAggregationStrategy) GroupedExchangeAggregationStrategy(org.apache.camel.processor.aggregate.GroupedExchangeAggregationStrategy) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy) AggregationStrategyBeanAdapter(org.apache.camel.processor.aggregate.AggregationStrategyBeanAdapter)

Example 29 with AggregationStrategy

use of org.apache.camel.processor.aggregate.AggregationStrategy in project camel by apache.

the class EnrichDefinition method createProcessor.

@Override
public Processor createProcessor(RouteContext routeContext) throws Exception {
    Expression exp = getExpression().createExpression(routeContext);
    boolean isShareUnitOfWork = getShareUnitOfWork() != null && getShareUnitOfWork();
    boolean isIgnoreInvalidEndpoint = getIgnoreInvalidEndpoint() != null && getIgnoreInvalidEndpoint();
    Enricher enricher = new Enricher(exp);
    enricher.setShareUnitOfWork(isShareUnitOfWork);
    enricher.setIgnoreInvalidEndpoint(isIgnoreInvalidEndpoint);
    AggregationStrategy strategy = createAggregationStrategy(routeContext);
    if (strategy != null) {
        enricher.setAggregationStrategy(strategy);
    }
    if (aggregateOnException != null) {
        enricher.setAggregateOnException(aggregateOnException);
    }
    return enricher;
}
Also used : Expression(org.apache.camel.Expression) Enricher(org.apache.camel.processor.Enricher) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

Example 30 with AggregationStrategy

use of org.apache.camel.processor.aggregate.AggregationStrategy in project camel by apache.

the class PollEnrichDefinition method createProcessor.

@Override
public Processor createProcessor(RouteContext routeContext) throws Exception {
    // if no timeout then we should block, and there use a negative timeout
    long time = timeout != null ? timeout : -1;
    boolean isIgnoreInvalidEndpoint = getIgnoreInvalidEndpoint() != null && getIgnoreInvalidEndpoint();
    Expression exp = getExpression().createExpression(routeContext);
    PollEnricher enricher = new PollEnricher(exp, time);
    AggregationStrategy strategy = createAggregationStrategy(routeContext);
    if (strategy == null) {
        enricher.setDefaultAggregationStrategy();
    } else {
        enricher.setAggregationStrategy(strategy);
    }
    if (getAggregateOnException() != null) {
        enricher.setAggregateOnException(getAggregateOnException());
    }
    if (getCacheSize() != null) {
        enricher.setCacheSize(getCacheSize());
    }
    enricher.setIgnoreInvalidEndpoint(isIgnoreInvalidEndpoint);
    return enricher;
}
Also used : Expression(org.apache.camel.Expression) PollEnricher(org.apache.camel.processor.PollEnricher) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

Aggregations

AggregationStrategy (org.apache.camel.processor.aggregate.AggregationStrategy)46 Exchange (org.apache.camel.Exchange)38 Processor (org.apache.camel.Processor)26 RouteBuilder (org.apache.camel.builder.RouteBuilder)23 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)19 Expression (org.apache.camel.Expression)18 AggregateProcessor (org.apache.camel.processor.aggregate.AggregateProcessor)15 DefaultExchange (org.apache.camel.impl.DefaultExchange)14 BodyInAggregatingStrategy (org.apache.camel.processor.BodyInAggregatingStrategy)14 SendProcessor (org.apache.camel.processor.SendProcessor)14 Predicate (org.apache.camel.Predicate)6 UseLatestAggregationStrategy (org.apache.camel.processor.aggregate.UseLatestAggregationStrategy)3 ExecutorService (java.util.concurrent.ExecutorService)2 CompletionAwareAggregationStrategy (org.apache.camel.processor.aggregate.CompletionAwareAggregationStrategy)2 DelegateAggregationStrategy (org.apache.camel.processor.aggregate.DelegateAggregationStrategy)2 GroupedExchangeAggregationStrategy (org.apache.camel.processor.aggregate.GroupedExchangeAggregationStrategy)2 ShareUnitOfWorkAggregationStrategy (org.apache.camel.processor.aggregate.ShareUnitOfWorkAggregationStrategy)2 TimeoutAwareAggregationStrategy (org.apache.camel.processor.aggregate.TimeoutAwareAggregationStrategy)2 Closeable (java.io.Closeable)1 ArrayList (java.util.ArrayList)1