Search in sources :

Example 41 with Expression

use of org.apache.camel.Expression in project camel by apache.

the class RecipientListDefinition method createProcessor.

@Override
public Processor createProcessor(RouteContext routeContext) throws Exception {
    final Expression expression = getExpression().createExpression(routeContext);
    boolean isParallelProcessing = getParallelProcessing() != null && getParallelProcessing();
    boolean isStreaming = getStreaming() != null && getStreaming();
    boolean isParallelAggregate = getParallelAggregate() != null && getParallelAggregate();
    boolean isShareUnitOfWork = getShareUnitOfWork() != null && getShareUnitOfWork();
    boolean isStopOnException = getStopOnException() != null && getStopOnException();
    boolean isIgnoreInvalidEndpoints = getIgnoreInvalidEndpoints() != null && getIgnoreInvalidEndpoints();
    boolean isStopOnAggregateException = getStopOnAggregateException() != null && getStopOnAggregateException();
    RecipientList answer;
    if (delimiter != null) {
        answer = new RecipientList(routeContext.getCamelContext(), expression, delimiter);
    } else {
        answer = new RecipientList(routeContext.getCamelContext(), expression);
    }
    answer.setAggregationStrategy(createAggregationStrategy(routeContext));
    answer.setParallelProcessing(isParallelProcessing);
    answer.setParallelAggregate(isParallelAggregate);
    answer.setStreaming(isStreaming);
    answer.setShareUnitOfWork(isShareUnitOfWork);
    answer.setStopOnException(isStopOnException);
    answer.setIgnoreInvalidEndpoints(isIgnoreInvalidEndpoints);
    answer.setStopOnAggregateException(isStopOnAggregateException);
    if (getCacheSize() != null) {
        answer.setCacheSize(getCacheSize());
    }
    if (onPrepareRef != null) {
        onPrepare = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), onPrepareRef, Processor.class);
    }
    if (onPrepare != null) {
        answer.setOnPrepare(onPrepare);
    }
    if (getTimeout() != null) {
        answer.setTimeout(getTimeout());
    }
    boolean shutdownThreadPool = ProcessorDefinitionHelper.willCreateNewThreadPool(routeContext, this, isParallelProcessing);
    ExecutorService threadPool = ProcessorDefinitionHelper.getConfiguredExecutorService(routeContext, "RecipientList", this, isParallelProcessing);
    answer.setExecutorService(threadPool);
    answer.setShutdownExecutorService(shutdownThreadPool);
    long timeout = getTimeout() != null ? getTimeout() : 0;
    if (timeout > 0 && !isParallelProcessing) {
        throw new IllegalArgumentException("Timeout is used but ParallelProcessing has not been enabled.");
    }
    // create a pipeline with two processors
    // the first is the eval processor which evaluates the expression to use
    // the second is the recipient list
    List<Processor> pipe = new ArrayList<Processor>(2);
    // the eval processor must be wrapped in error handler, so in case there was an
    // error during evaluation, the error handler can deal with it
    // the recipient list is not in error handler, as its has its own special error handling
    // when sending to the recipients individually
    Processor evalProcessor = new EvaluateExpressionProcessor(expression);
    evalProcessor = super.wrapInErrorHandler(routeContext, evalProcessor);
    pipe.add(evalProcessor);
    pipe.add(answer);
    // (threads definition does this as well)
    return new Pipeline(routeContext.getCamelContext(), pipe) {

        @Override
        public String toString() {
            return "RecipientList[" + expression + "]";
        }
    };
}
Also used : EvaluateExpressionProcessor(org.apache.camel.processor.EvaluateExpressionProcessor) Processor(org.apache.camel.Processor) EvaluateExpressionProcessor(org.apache.camel.processor.EvaluateExpressionProcessor) Expression(org.apache.camel.Expression) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) RecipientList(org.apache.camel.processor.RecipientList) Pipeline(org.apache.camel.processor.Pipeline)

Example 42 with Expression

use of org.apache.camel.Expression 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 43 with Expression

use of org.apache.camel.Expression in project camel by apache.

the class DynamicRouterDefinition method createProcessor.

@Override
public Processor createProcessor(RouteContext routeContext) throws Exception {
    Expression expression = getExpression().createExpression(routeContext);
    String delimiter = getUriDelimiter() != null ? getUriDelimiter() : DEFAULT_DELIMITER;
    DynamicRouter dynamicRouter = new DynamicRouter(routeContext.getCamelContext(), expression, delimiter);
    if (getIgnoreInvalidEndpoints() != null) {
        dynamicRouter.setIgnoreInvalidEndpoints(getIgnoreInvalidEndpoints());
    }
    if (getCacheSize() != null) {
        dynamicRouter.setCacheSize(getCacheSize());
    }
    return dynamicRouter;
}
Also used : Expression(org.apache.camel.Expression) DynamicRouter(org.apache.camel.processor.DynamicRouter)

Example 44 with Expression

use of org.apache.camel.Expression in project camel by apache.

the class PredicateBuilderConcurrentTest method testPredicateBuilderConcurrent.

public void testPredicateBuilderConcurrent() throws Exception {
    context.start();
    List<Future<Boolean>> futures = new ArrayList<Future<Boolean>>();
    ExecutorService pool = Executors.newFixedThreadPool(10);
    for (int i = 0; i < 1000; i++) {
        final Integer num = i;
        Future<Boolean> future = pool.submit(new Callable<Boolean>() {

            public Boolean call() throws Exception {
                Expression left = ExpressionBuilder.headerExpression("foo");
                Expression right;
                if (num % 2 == 0) {
                    right = ExpressionBuilder.constantExpression("ABC");
                } else {
                    right = ExpressionBuilder.constantExpression("DEF");
                }
                Predicate predicate = PredicateBuilder.isEqualTo(left, right);
                Exchange exchange = new DefaultExchange(context);
                exchange.getIn().setBody("Hello World");
                exchange.getIn().setHeader("foo", "ABC");
                return predicate.matches(exchange);
            }
        });
        futures.add(future);
    }
    for (int i = 0; i < 1000; i++) {
        Boolean result = futures.get(i).get(10, TimeUnit.SECONDS);
        if (i % 2 == 0) {
            assertEquals("Should be true for #" + i, true, result.booleanValue());
        } else {
            assertEquals("Should be false for #" + i, false, result.booleanValue());
        }
    }
    pool.shutdownNow();
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) ArrayList(java.util.ArrayList) Predicate(org.apache.camel.Predicate) DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) Expression(org.apache.camel.Expression) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future)

Example 45 with Expression

use of org.apache.camel.Expression in project camel by apache.

the class ExpressionBuilderTest method testRegexTokenize.

public void testRegexTokenize() throws Exception {
    Expression expression = regexTokenizeExpression(headerExpression("location"), ",");
    List<String> expected = new ArrayList<String>(Arrays.asList(new String[] { "Islington", "London", "UK" }));
    assertExpression(expression, exchange, expected);
    Predicate predicate = contains(regexTokenizeExpression(headerExpression("location"), ","), constantExpression("London"));
    assertPredicate(predicate, exchange, true);
    predicate = contains(regexTokenizeExpression(headerExpression("location"), ","), constantExpression("Manchester"));
    assertPredicate(predicate, exchange, false);
}
Also used : Expression(org.apache.camel.Expression) ArrayList(java.util.ArrayList) Predicate(org.apache.camel.Predicate)

Aggregations

Expression (org.apache.camel.Expression)183 Exchange (org.apache.camel.Exchange)44 Processor (org.apache.camel.Processor)24 Predicate (org.apache.camel.Predicate)22 DefaultExchange (org.apache.camel.impl.DefaultExchange)21 AggregationStrategy (org.apache.camel.processor.aggregate.AggregationStrategy)18 ArrayList (java.util.ArrayList)15 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)15 AggregateProcessor (org.apache.camel.processor.aggregate.AggregateProcessor)15 Language (org.apache.camel.spi.Language)15 BodyInAggregatingStrategy (org.apache.camel.processor.BodyInAggregatingStrategy)14 SendProcessor (org.apache.camel.processor.SendProcessor)14 Test (org.junit.Test)8 SimpleParserException (org.apache.camel.language.simple.types.SimpleParserException)7 File (java.io.File)4 ExecutorService (java.util.concurrent.ExecutorService)4 SimpleIllegalSyntaxException (org.apache.camel.language.simple.types.SimpleIllegalSyntaxException)4 HashMap (java.util.HashMap)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 CamelExchangeException (org.apache.camel.CamelExchangeException)3