Search in sources :

Example 11 with BodyInAggregatingStrategy

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

use of org.apache.camel.processor.BodyInAggregatingStrategy in project camel by apache.

the class AggregateEagerCheckCompletionTest method testAggregateEagerCheckCompletion.

public void testAggregateEagerCheckCompletion() throws Exception {
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").aggregate(header("id"), new BodyInAggregatingStrategy()).completionPredicate(body().isEqualTo("END")).eagerCheckCompletion().to("mock:result");
        }
    });
    context.start();
    getMockEndpoint("mock:result").expectedBodiesReceived("A+B+END");
    template.sendBodyAndHeader("direct:start", "A", "id", 1);
    template.sendBodyAndHeader("direct:start", "B", "id", 1);
    template.sendBodyAndHeader("direct:start", "END", "id", 1);
    assertMockEndpointsSatisfied();
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) BodyInAggregatingStrategy(org.apache.camel.processor.BodyInAggregatingStrategy)

Example 13 with BodyInAggregatingStrategy

use of org.apache.camel.processor.BodyInAggregatingStrategy in project camel by apache.

the class AggregateForceCompletionOnStopParallelTest method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            from("direct:forceCompletionTrue").routeId("foo").aggregate(header("id"), new BodyInAggregatingStrategy()).forceCompletionOnStop().completionSize(10).parallelProcessing().delay(100).process("myCompletionProcessor");
            from("direct:forceCompletionFalse").routeId("bar").aggregate(header("id"), new BodyInAggregatingStrategy()).completionSize(10).parallelProcessing().delay(100).process("myCompletionProcessor");
        }
    };
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) BodyInAggregatingStrategy(org.apache.camel.processor.BodyInAggregatingStrategy)

Example 14 with BodyInAggregatingStrategy

use of org.apache.camel.processor.BodyInAggregatingStrategy in project camel by apache.

the class AggregateForceCompletionOnStopTest method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            from("direct:forceCompletionTrue").routeId("foo").aggregate(header("id"), new BodyInAggregatingStrategy()).forceCompletionOnStop().completionSize(10).delay(100).process("myCompletionProcessor");
            from("direct:forceCompletionFalse").routeId("bar").aggregate(header("id"), new BodyInAggregatingStrategy()).completionSize(10).delay(100).process("myCompletionProcessor");
        }
    };
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) BodyInAggregatingStrategy(org.apache.camel.processor.BodyInAggregatingStrategy)

Example 15 with BodyInAggregatingStrategy

use of org.apache.camel.processor.BodyInAggregatingStrategy in project camel by apache.

the class AggregateClosedCorrelationKeyTest method testAggregateClosedCorrelationKey.

public void testAggregateClosedCorrelationKey() throws Exception {
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").aggregate(header("id"), new BodyInAggregatingStrategy()).completionSize(2).closeCorrelationKeyOnCompletion(1000).to("mock:result");
        }
    });
    context.start();
    getMockEndpoint("mock:result").expectedBodiesReceived("A+B");
    template.sendBodyAndHeader("direct:start", "A", "id", 1);
    template.sendBodyAndHeader("direct:start", "B", "id", 1);
    // should be closed
    try {
        template.sendBodyAndHeader("direct:start", "C", "id", 1);
        fail("Should throw an exception");
    } catch (CamelExecutionException e) {
        ClosedCorrelationKeyException cause = assertIsInstanceOf(ClosedCorrelationKeyException.class, e.getCause());
        assertEquals("1", cause.getCorrelationKey());
        assertTrue(cause.getMessage().startsWith("The correlation key [1] has been closed."));
    }
    assertMockEndpointsSatisfied();
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) RouteBuilder(org.apache.camel.builder.RouteBuilder) BodyInAggregatingStrategy(org.apache.camel.processor.BodyInAggregatingStrategy) ClosedCorrelationKeyException(org.apache.camel.processor.aggregate.ClosedCorrelationKeyException) CamelExecutionException(org.apache.camel.CamelExecutionException) ClosedCorrelationKeyException(org.apache.camel.processor.aggregate.ClosedCorrelationKeyException)

Aggregations

BodyInAggregatingStrategy (org.apache.camel.processor.BodyInAggregatingStrategy)28 Exchange (org.apache.camel.Exchange)14 Expression (org.apache.camel.Expression)14 Processor (org.apache.camel.Processor)14 RouteBuilder (org.apache.camel.builder.RouteBuilder)14 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)14 DefaultExchange (org.apache.camel.impl.DefaultExchange)14 SendProcessor (org.apache.camel.processor.SendProcessor)14 AggregateProcessor (org.apache.camel.processor.aggregate.AggregateProcessor)14 AggregationStrategy (org.apache.camel.processor.aggregate.AggregationStrategy)14 Predicate (org.apache.camel.Predicate)5 CamelExecutionException (org.apache.camel.CamelExecutionException)4 CamelExchangeException (org.apache.camel.CamelExchangeException)2 ClosedCorrelationKeyException (org.apache.camel.processor.aggregate.ClosedCorrelationKeyException)2 FailedToCreateRouteException (org.apache.camel.FailedToCreateRouteException)1 OptimisticLockRetryPolicy (org.apache.camel.processor.aggregate.OptimisticLockRetryPolicy)1 ThreadPoolProfile (org.apache.camel.spi.ThreadPoolProfile)1