Search in sources :

Example 1 with SynchronousExecutorService

use of org.apache.camel.util.concurrent.SynchronousExecutorService 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)

Aggregations

ExecutorService (java.util.concurrent.ExecutorService)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 Expression (org.apache.camel.Expression)1 Predicate (org.apache.camel.Predicate)1 Processor (org.apache.camel.Processor)1 CamelInternalProcessor (org.apache.camel.processor.CamelInternalProcessor)1 AggregateController (org.apache.camel.processor.aggregate.AggregateController)1 AggregateProcessor (org.apache.camel.processor.aggregate.AggregateProcessor)1 AggregationStrategy (org.apache.camel.processor.aggregate.AggregationStrategy)1 GroupedExchangeAggregationStrategy (org.apache.camel.processor.aggregate.GroupedExchangeAggregationStrategy)1 AggregationRepository (org.apache.camel.spi.AggregationRepository)1 AsPredicate (org.apache.camel.spi.AsPredicate)1 SynchronousExecutorService (org.apache.camel.util.concurrent.SynchronousExecutorService)1