Search in sources :

Example 6 with AggregationStrategy

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

the class AggregateProcessorTimeoutCompletionRestartTest method testAggregateProcessorTimeoutExpressionRestart.

public void testAggregateProcessorTimeoutExpressionRestart() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("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", 2000);
    Exchange e2 = new DefaultExchange(context);
    e2.getIn().setBody("B");
    e2.getIn().setHeader("id", 123);
    e2.getIn().setHeader("myTimeout", 2000);
    ap.process(e1);
    ap.process(e2);
    // 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(1, 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 7 with AggregationStrategy

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

the class AggregateProcessorTimeoutCompletionRestartTest method testAggregateProcessorTimeoutRestart.

public void testAggregateProcessorTimeoutRestart() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("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.setCompletionTimeout(2000);
    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);
    ap.process(e1);
    ap.process(e2);
    // 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(1, 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 8 with AggregationStrategy

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

the class AggregateLostGroupIssueTest method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            from("timer://foo?period=100&delay=1000").startupOrder(2).process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    exchange.getOut().setBody(messageIndex++);
                    exchange.getOut().setHeader("aggregateGroup", "group1");
                }
            }).to("direct:aggregator");
            from("direct:aggregator").startupOrder(1).aggregate(header("aggregateGroup"), new AggregationStrategy() {

                public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
                    if (oldExchange == null) {
                        return newExchange;
                    }
                    String oldBody = oldExchange.getIn().getBody(String.class);
                    String newBody = newExchange.getIn().getBody(String.class);
                    oldExchange.getIn().setBody(oldBody + "," + newBody);
                    return oldExchange;
                }
            }).completionSize(10).completionTimeout(2000L).to("log:aggregated").to("mock:result");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

Example 9 with AggregationStrategy

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

the class AggregateProcessorTest method testAggregateForceCompletion.

public void testAggregateForceCompletion() throws Exception {
    // camel context must be started
    context.start();
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceivedInAnyOrder("B+END", "A+END");
    mock.expectedPropertyReceived(Exchange.AGGREGATED_COMPLETED_BY, "force");
    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(10);
    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", 456);
    Exchange e3 = new DefaultExchange(context);
    e3.getIn().setBody("END");
    e3.getIn().setHeader("id", 123);
    Exchange e4 = new DefaultExchange(context);
    e4.getIn().setBody("END");
    e4.getIn().setHeader("id", 456);
    ap.process(e1);
    ap.process(e2);
    ap.process(e3);
    ap.process(e4);
    assertEquals("should not have completed yet", 0, mock.getExchanges().size());
    ap.forceCompletionOfAllGroups();
    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 10 with AggregationStrategy

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

the class AggregateProcessorTest method testAggregateProcessorCompletionPredicateEager.

public void testAggregateProcessorCompletionPredicateEager() 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().isEqualTo("END");
    AggregateProcessor ap = new AggregateProcessor(context, done, corr, as, executorService, true);
    ap.setCompletionPredicate(complete);
    ap.setEagerCheckCompletion(true);
    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)

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