Search in sources :

Example 26 with Expression

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

the class AggregateProcessorTest method doTestAggregateProcessorCompletionTimeout.

private void doTestAggregateProcessorCompletionTimeout(boolean eager) throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("A+B+C");
    mock.expectedPropertyReceived(Exchange.AGGREGATED_COMPLETED_BY, "timeout");
    Processor done = new SendProcessor(context.getEndpoint("mock:result"));
    Expression corr = header("id");
    AggregationStrategy as = new BodyInAggregatingStrategy();
    AggregateProcessor ap = new AggregateProcessor(context, done, corr, as, executorService, true);
    ap.setCompletionTimeout(3000);
    ap.setEagerCheckCompletion(eager);
    ap.start();
    Exchange e1 = new DefaultExchange(context);
    e1.getIn().setBody("A");
    e1.getIn().setHeader("id", 123);
    Exchange e2 = new DefaultExchange(context);
    e2.getIn().setBody("B");
    e2.getIn().setHeader("id", 123);
    Exchange e3 = new DefaultExchange(context);
    e3.getIn().setBody("C");
    e3.getIn().setHeader("id", 123);
    Exchange e4 = new DefaultExchange(context);
    e4.getIn().setBody("D");
    e4.getIn().setHeader("id", 123);
    ap.process(e1);
    Thread.sleep(250);
    ap.process(e2);
    Thread.sleep(500);
    ap.process(e3);
    Thread.sleep(5000);
    ap.process(e4);
    assertMockEndpointsSatisfied();
    ap.stop();
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) SendProcessor(org.apache.camel.processor.SendProcessor) Processor(org.apache.camel.Processor) AggregateProcessor(org.apache.camel.processor.aggregate.AggregateProcessor) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Expression(org.apache.camel.Expression) BodyInAggregatingStrategy(org.apache.camel.processor.BodyInAggregatingStrategy) SendProcessor(org.apache.camel.processor.SendProcessor) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy) AggregateProcessor(org.apache.camel.processor.aggregate.AggregateProcessor)

Example 27 with Expression

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

the class AggregateProcessorTest method testAggregateProcessorCompletionPredicate.

public void testAggregateProcessorCompletionPredicate() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("A+B+END");
    mock.expectedPropertyReceived(Exchange.AGGREGATED_COMPLETED_BY, "predicate");
    Processor done = new SendProcessor(context.getEndpoint("mock:result"));
    Expression corr = header("id");
    AggregationStrategy as = new BodyInAggregatingStrategy();
    Predicate complete = body().contains("END");
    AggregateProcessor ap = new AggregateProcessor(context, done, corr, as, executorService, true);
    ap.setCompletionPredicate(complete);
    ap.setEagerCheckCompletion(false);
    ap.start();
    Exchange e1 = new DefaultExchange(context);
    e1.getIn().setBody("A");
    e1.getIn().setHeader("id", 123);
    Exchange e2 = new DefaultExchange(context);
    e2.getIn().setBody("B");
    e2.getIn().setHeader("id", 123);
    Exchange e3 = new DefaultExchange(context);
    e3.getIn().setBody("END");
    e3.getIn().setHeader("id", 123);
    Exchange e4 = new DefaultExchange(context);
    e4.getIn().setBody("D");
    e4.getIn().setHeader("id", 123);
    ap.process(e1);
    ap.process(e2);
    ap.process(e3);
    ap.process(e4);
    assertMockEndpointsSatisfied();
    ap.stop();
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) SendProcessor(org.apache.camel.processor.SendProcessor) Processor(org.apache.camel.Processor) AggregateProcessor(org.apache.camel.processor.aggregate.AggregateProcessor) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Expression(org.apache.camel.Expression) BodyInAggregatingStrategy(org.apache.camel.processor.BodyInAggregatingStrategy) SendProcessor(org.apache.camel.processor.SendProcessor) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy) Predicate(org.apache.camel.Predicate) AggregateProcessor(org.apache.camel.processor.aggregate.AggregateProcessor)

Example 28 with Expression

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

the class AggregateProcessorTest method testAggregateCompletionInterval.

public void testAggregateCompletionInterval() throws Exception {
    // camel context must be started
    context.start();
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("A+B+C", "D");
    mock.expectedPropertyReceived(Exchange.AGGREGATED_COMPLETED_BY, "interval");
    Processor done = new SendProcessor(context.getEndpoint("mock:result"));
    Expression corr = header("id");
    AggregationStrategy as = new BodyInAggregatingStrategy();
    AggregateProcessor ap = new AggregateProcessor(context, done, corr, as, executorService, true);
    ap.setCompletionInterval(3000);
    ap.start();
    Exchange e1 = new DefaultExchange(context);
    e1.getIn().setBody("A");
    e1.getIn().setHeader("id", 123);
    Exchange e2 = new DefaultExchange(context);
    e2.getIn().setBody("B");
    e2.getIn().setHeader("id", 123);
    Exchange e3 = new DefaultExchange(context);
    e3.getIn().setBody("C");
    e3.getIn().setHeader("id", 123);
    Exchange e4 = new DefaultExchange(context);
    e4.getIn().setBody("D");
    e4.getIn().setHeader("id", 123);
    ap.process(e1);
    ap.process(e2);
    ap.process(e3);
    Thread.sleep(5000);
    ap.process(e4);
    assertMockEndpointsSatisfied();
    ap.stop();
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) SendProcessor(org.apache.camel.processor.SendProcessor) Processor(org.apache.camel.Processor) AggregateProcessor(org.apache.camel.processor.aggregate.AggregateProcessor) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Expression(org.apache.camel.Expression) BodyInAggregatingStrategy(org.apache.camel.processor.BodyInAggregatingStrategy) SendProcessor(org.apache.camel.processor.SendProcessor) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy) AggregateProcessor(org.apache.camel.processor.aggregate.AggregateProcessor)

Example 29 with Expression

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

the class AggregatorExceptionInPredicateTest method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            onException(IllegalArgumentException.class).handled(true).to("mock:handled");
            from("direct:start").aggregate(header("id")).completionTimeout(500).aggregationStrategy(new AggregationStrategy() {

                public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
                    Object body = newExchange.getIn().getBody();
                    if ("Damn".equals(body)) {
                        throw new IllegalArgumentException();
                    }
                    return newExchange;
                }
            }).to("mock:result");
            from("direct:predicate").aggregate(new Expression() {

                public <T> T evaluate(Exchange exchange, Class<T> type) {
                    if (exchange.getIn().getBody().equals("Damn")) {
                        throw new IllegalArgumentException();
                    }
                    return ExpressionBuilder.headerExpression("id").evaluate(exchange, type);
                }
            }, new UseLatestAggregationStrategy()).completionTimeout(500).to("mock:result");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) RouteBuilder(org.apache.camel.builder.RouteBuilder) Expression(org.apache.camel.Expression) UseLatestAggregationStrategy(org.apache.camel.processor.aggregate.UseLatestAggregationStrategy) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy) UseLatestAggregationStrategy(org.apache.camel.processor.aggregate.UseLatestAggregationStrategy)

Example 30 with Expression

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

the class ExpressionListComparatorTest method testExpressionListComparator.

public void testExpressionListComparator() {
    List<Expression> list = new ArrayList<Expression>();
    list.add(new MyFooExpression());
    list.add(new MyBarExpression());
    ExpressionListComparator comp = new ExpressionListComparator(list);
    Exchange e1 = new DefaultExchange(context);
    Exchange e2 = new DefaultExchange(context);
    int out = comp.compare(e1, e2);
    assertEquals(0, out);
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Expression(org.apache.camel.Expression) ArrayList(java.util.ArrayList)

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