Search in sources :

Example 1 with BodyInAggregatingStrategy

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

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

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

the class AggregateShutdownThreadPoolTest method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            myPool = context.getExecutorServiceManager().newDefaultThreadPool(this, "myPool");
            from("direct:foo").routeId("foo").aggregate(header("id"), new BodyInAggregatingStrategy()).completionSize(3).to("mock:aggregated");
            from("direct:bar").routeId("bar").aggregate(header("id"), new BodyInAggregatingStrategy()).executorService(myPool).completionSize(3).to("mock:aggregated");
        }
    };
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) BodyInAggregatingStrategy(org.apache.camel.processor.BodyInAggregatingStrategy)

Example 4 with BodyInAggregatingStrategy

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

the class AggregateThreadPoolProfileTest method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            // create and register thread pool profile
            ThreadPoolProfile profile = new ThreadPoolProfile("myProfile");
            profile.setPoolSize(2);
            profile.setMaxPoolSize(8);
            profile.setRejectedPolicy(ThreadPoolRejectedPolicy.Abort);
            context.getExecutorServiceManager().registerThreadPoolProfile(profile);
            from("direct:start").aggregate(header("id"), new BodyInAggregatingStrategy()).completionSize(3).executorServiceRef("myProfile").to("log:foo").to("mock:aggregated");
        }
    };
}
Also used : ThreadPoolProfile(org.apache.camel.spi.ThreadPoolProfile) RouteBuilder(org.apache.camel.builder.RouteBuilder) BodyInAggregatingStrategy(org.apache.camel.processor.BodyInAggregatingStrategy)

Example 5 with BodyInAggregatingStrategy

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

the class AggregateIgnoreInvalidCorrelationKeysTest method testAggregateNotIgnoreInvalidCorrelationKeys.

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

        @Override
        public void configure() throws Exception {
            from("direct:start").aggregate(header("id"), new BodyInAggregatingStrategy()).completionSize(2).to("mock:result");
        }
    });
    context.start();
    getMockEndpoint("mock:result").expectedBodiesReceived("A+C");
    template.sendBodyAndHeader("direct:start", "A", "id", 1);
    try {
        template.sendBodyAndHeader("direct:start", "B", "id", null);
        fail("Should throw an exception");
    } catch (CamelExecutionException e) {
        CamelExchangeException cause = assertIsInstanceOf(CamelExchangeException.class, e.getCause());
        assertTrue(cause.getMessage().startsWith("Invalid correlation key"));
    }
    template.sendBodyAndHeader("direct:start", "C", "id", 1);
    assertMockEndpointsSatisfied();
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) CamelExchangeException(org.apache.camel.CamelExchangeException) RouteBuilder(org.apache.camel.builder.RouteBuilder) BodyInAggregatingStrategy(org.apache.camel.processor.BodyInAggregatingStrategy) CamelExecutionException(org.apache.camel.CamelExecutionException) CamelExchangeException(org.apache.camel.CamelExchangeException)

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