Search in sources :

Example 36 with AggregationStrategy

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

the class AggregateProcessorTest method testAggregateBadCorrelationKey.

public void testAggregateBadCorrelationKey() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("A+C+END");
    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.start();
    Exchange e1 = new DefaultExchange(context);
    e1.getIn().setBody("A");
    e1.getIn().setHeader("id", 123);
    Exchange e2 = new DefaultExchange(context);
    e2.getIn().setBody("B");
    Exchange e3 = new DefaultExchange(context);
    e3.getIn().setBody("C");
    e3.getIn().setHeader("id", 123);
    Exchange e4 = new DefaultExchange(context);
    e4.getIn().setBody("END");
    e4.getIn().setHeader("id", 123);
    ap.process(e1);
    ap.process(e2);
    Exception e = e2.getException();
    assertNotNull(e);
    assertTrue(e.getMessage().startsWith("Invalid correlation key."));
    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 37 with AggregationStrategy

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

the class AggregateProcessorTest method testAggregateCloseCorrelationKeyOnCompletion.

public void testAggregateCloseCorrelationKeyOnCompletion() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("A+B+END");
    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.setCloseCorrelationKeyOnCompletion(1000);
    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("C");
    e4.getIn().setHeader("id", 123);
    ap.process(e1);
    ap.process(e2);
    ap.process(e3);
    ap.process(e4);
    Exception e = e4.getException();
    assertNotNull(e);
    assertTrue(e.getMessage().startsWith("The correlation key [123] has been closed."));
    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 38 with AggregationStrategy

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

the class AggregateProcessorTest method doTestAggregateProcessorCompletionAggregatedSize.

private void doTestAggregateProcessorCompletionAggregatedSize(boolean eager) throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("A+B+C");
    mock.expectedPropertyReceived(Exchange.AGGREGATED_COMPLETED_BY, "size");
    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.setCompletionSize(3);
    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);
    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) AggregateProcessor(org.apache.camel.processor.aggregate.AggregateProcessor)

Example 39 with AggregationStrategy

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

the class AggregateProcessorTimeoutCompletionRestartTest method testAggregateProcessorTwoTimeoutExpressionRestart.

public void testAggregateProcessorTwoTimeoutExpressionRestart() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("C+D", "A+B");
    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);
    // start with a high timeout so no completes before we stop
    ap.setCompletionTimeoutExpression(header("myTimeout"));
    ap.start();
    Exchange e1 = new DefaultExchange(context);
    e1.getIn().setBody("A");
    e1.getIn().setHeader("id", 123);
    e1.getIn().setHeader("myTimeout", 3000);
    Exchange e2 = new DefaultExchange(context);
    e2.getIn().setBody("B");
    e2.getIn().setHeader("id", 123);
    e2.getIn().setHeader("myTimeout", 3000);
    Exchange e3 = new DefaultExchange(context);
    e3.getIn().setBody("C");
    e3.getIn().setHeader("id", 456);
    e3.getIn().setHeader("myTimeout", 2000);
    Exchange e4 = new DefaultExchange(context);
    e4.getIn().setBody("D");
    e4.getIn().setHeader("id", 456);
    e4.getIn().setHeader("myTimeout", 2000);
    ap.process(e1);
    ap.process(e2);
    ap.process(e3);
    ap.process(e4);
    // shutdown before the 2 sec timeout occurs
    // however we use stop instead of shutdown as shutdown will clear the in memory aggregation repository,
    ap.stop();
    // should be no completed
    assertEquals(0, mock.getReceivedCounter());
    // start aggregator again
    ap.start();
    // the aggregator should restore the timeout condition and trigger timeout
    assertMockEndpointsSatisfied();
    assertEquals(2, mock.getReceivedCounter());
    ap.shutdown();
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Processor(org.apache.camel.Processor) SendProcessor(org.apache.camel.processor.SendProcessor) 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 40 with AggregationStrategy

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

the class FlexibleAggregationStrategiesTest method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            from("direct:start1").aggregate(AggregationStrategies.flexible(String.class).accumulateInCollection(ArrayList.class).pick(simple("${body}")).condition(simple("${body} contains 'AGGREGATE'"))).header("id").completionSize(5).to("mock:result1");
            from("direct:start2").aggregate(AggregationStrategies.flexible(String.class).accumulateInCollection(HashSet.class).pick(simple("${header.input}")).condition(simple("${header.input} contains 'AGGREGATE'")).storeInProperty("AggregationResult")).constant(true).completionSize(5).to("mock:result2");
            from("direct:start3").aggregate(AggregationStrategies.flexible(String.class).storeInHeader("AggregationResult")).constant(true).completionSize(3).to("mock:result3");
            from("direct:start4").aggregate(AggregationStrategies.flexible().accumulateInCollection(ArrayList.class)).constant(true).completionSize(3).to("mock:result4");
            from("direct:start5").aggregate(AggregationStrategies.flexible(Integer.class).accumulateInCollection(ArrayList.class)).constant(true).completionSize(3).to("mock:result5");
            from("direct:start6").aggregate(AggregationStrategies.flexible(Integer.class).ignoreInvalidCasts().storeNulls().accumulateInCollection(ArrayList.class)).constant(true).completionSize(3).to("mock:result6");
            AggregationStrategy timeoutCompletionStrategy = AggregationStrategies.flexible(String.class).condition(simple("${body} contains 'AGGREGATE'")).timeoutAware(new TimeoutAwareMixin() {

                @Override
                public void timeout(Exchange exchange, int index, int total, long timeout) {
                    exchange.setProperty("Timeout", true);
                    timeoutLatch.countDown();
                }
            }).completionAware(new CompletionAwareMixin() {

                @Override
                public void onCompletion(Exchange exchange) {
                    completionLatch.countDown();
                }
            });
            from("direct:start.timeoutAndCompletionAware").aggregate(timeoutCompletionStrategy).constant(true).completionTimeout(500).completionSize(2).to("mock:result.timeoutAndCompletionAware");
            from("direct:start.xpath1").aggregate(AggregationStrategies.flexible(Node.class).pick(xpath("//result[1]").nodeResult()).accumulateInCollection(ArrayList.class)).constant(true).completionSize(3).to("mock:result.xpath1");
            from("direct:linkedlist").log(LoggingLevel.INFO, "Before the first split the body is ${body} and has class ${body.getClass()}").split(body(), AggregationStrategies.flexible().pick(body()).accumulateInCollection(LinkedList.class)).log(LoggingLevel.INFO, "During the first split the body is ${body} and has class ${body.getClass()}").end().log(LoggingLevel.INFO, "Before the second split the body is ${body} and has class ${body.getClass()}").split(body(), AggregationStrategies.flexible().pick(body()).accumulateInCollection(LinkedList.class)).log(LoggingLevel.INFO, "During the second split the body is ${body} and has class ${body.getClass()}").end().log(LoggingLevel.INFO, "After the second split the body is ${body} and has class ${body.getClass()}");
            from("direct:hashset").log(LoggingLevel.INFO, "Before the first split the body is ${body} and has class ${body.getClass()}").split(body(), AggregationStrategies.flexible().pick(body()).accumulateInCollection(HashSet.class)).log(LoggingLevel.INFO, "During the first split the body is ${body} and has class ${body.getClass()}").end().log(LoggingLevel.INFO, "Before the second split the body is ${body} and has class ${body.getClass()}").split(body(), AggregationStrategies.flexible().pick(body()).accumulateInCollection(HashSet.class)).log(LoggingLevel.INFO, "During the second split the body is ${body} and has class ${body.getClass()}").end().log(LoggingLevel.INFO, "After the second split the body is ${body} and has class ${body.getClass()}");
        }
    };
}
Also used : TimeoutAwareMixin(org.apache.camel.util.toolbox.FlexibleAggregationStrategy.TimeoutAwareMixin) Exchange(org.apache.camel.Exchange) RouteBuilder(org.apache.camel.builder.RouteBuilder) CompletionAwareMixin(org.apache.camel.util.toolbox.FlexibleAggregationStrategy.CompletionAwareMixin) HashSet(java.util.HashSet) 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