Search in sources :

Example 11 with AggregationStrategy

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

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

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

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

the class AggregatorConcurrencyTest method createRouteBuilder.

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

        public void configure() throws Exception {
            from(uri).aggregate(constant(true), new AggregationStrategy() {

                public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
                    Exchange answer = oldExchange != null ? oldExchange : newExchange;
                    COUNTER.getAndIncrement();
                    Integer newIndex = newExchange.getIn().getHeader("index", Integer.class);
                    int total = SUM.addAndGet(newIndex);
                    answer.getIn().setHeader("total", total);
                    LOG.debug("Index: " + newIndex + ". Total so far: " + total);
                    return answer;
                }
            }).completionTimeout(60000).completionPredicate(property(Exchange.AGGREGATED_SIZE).isEqualTo(100)).to("direct:foo");
            from("direct:foo").setBody().header("total").to("mock:result");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RouteBuilder(org.apache.camel.builder.RouteBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

Example 15 with AggregationStrategy

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

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