Search in sources :

Example 31 with AggregationStrategy

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

the class MulticastThreadPoolProfileTest method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            // register thread pool profile
            ThreadPoolProfile profile = new ThreadPoolProfileBuilder("myProfile").poolSize(5).maxPoolSize(10).maxQueueSize(20).build();
            context.getExecutorServiceManager().registerThreadPoolProfile(profile);
            from("direct:start").multicast(new AggregationStrategy() {

                public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
                    if (oldExchange == null) {
                        return newExchange;
                    }
                    String body = oldExchange.getIn().getBody(String.class);
                    oldExchange.getIn().setBody(body + newExchange.getIn().getBody(String.class));
                    return oldExchange;
                }
            }).parallelProcessing().executorServiceRef("myProfile").to("direct:a", "direct:b").end().to("mock:result");
            from("direct:a").delay(100).setBody(constant("A"));
            from("direct:b").setBody(constant("B"));
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) ThreadPoolProfile(org.apache.camel.spi.ThreadPoolProfile) RouteBuilder(org.apache.camel.builder.RouteBuilder) ThreadPoolProfileBuilder(org.apache.camel.builder.ThreadPoolProfileBuilder) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

Example 32 with AggregationStrategy

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

the class SplitRouteNumberOfProcessorTest method testOneProcessor.

public void testOneProcessor() throws Exception {
    failed.set(false);
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").split(body().tokenize(","), new AggregationStrategy() {

                public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
                    if (oldExchange == null) {
                        return newExchange;
                    }
                    // should always be in
                    String body = newExchange.getIn().getBody(String.class);
                    assertNotNull(body);
                    return newExchange;
                }
            }).process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertFalse("Should not have out", failed.get());
                    String s = exchange.getIn().getBody(String.class);
                    exchange.getIn().setBody("Hi " + s);
                    context.createProducerTemplate().send("mock:foo", exchange);
                }
            }).end().to("mock:result");
        }
    });
    context.start();
    MockEndpoint result = getMockEndpoint("mock:result");
    result.expectedMessageCount(1);
    getMockEndpoint("mock:foo").expectedBodiesReceived("Hi Claus", "Hi Willem");
    template.requestBodyAndHeader("direct:start", "Claus,Willem", "id", 1);
    assertMockEndpointsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

Example 33 with AggregationStrategy

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

the class AggregateProcessorTest method testAggregateUseBatchSizeFromConsumer.

public void testAggregateUseBatchSizeFromConsumer() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("A+B", "C+D+E");
    mock.expectedPropertyReceived(Exchange.AGGREGATED_COMPLETED_BY, "consumer");
    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(100);
    ap.setCompletionFromBatchConsumer(true);
    ap.start();
    Exchange e1 = new DefaultExchange(context);
    e1.getIn().setBody("A");
    e1.getIn().setHeader("id", 123);
    e1.setProperty(Exchange.BATCH_INDEX, 0);
    e1.setProperty(Exchange.BATCH_SIZE, 2);
    e1.setProperty(Exchange.BATCH_COMPLETE, false);
    Exchange e2 = new DefaultExchange(context);
    e2.getIn().setBody("B");
    e2.getIn().setHeader("id", 123);
    e2.setProperty(Exchange.BATCH_INDEX, 1);
    e2.setProperty(Exchange.BATCH_SIZE, 2);
    e2.setProperty(Exchange.BATCH_COMPLETE, true);
    Exchange e3 = new DefaultExchange(context);
    e3.getIn().setBody("C");
    e3.getIn().setHeader("id", 123);
    e3.setProperty(Exchange.BATCH_INDEX, 0);
    e3.setProperty(Exchange.BATCH_SIZE, 3);
    e3.setProperty(Exchange.BATCH_COMPLETE, false);
    Exchange e4 = new DefaultExchange(context);
    e4.getIn().setBody("D");
    e4.getIn().setHeader("id", 123);
    e4.setProperty(Exchange.BATCH_INDEX, 1);
    e4.setProperty(Exchange.BATCH_SIZE, 3);
    e4.setProperty(Exchange.BATCH_COMPLETE, false);
    Exchange e5 = new DefaultExchange(context);
    e5.getIn().setBody("E");
    e5.getIn().setHeader("id", 123);
    e5.setProperty(Exchange.BATCH_INDEX, 2);
    e5.setProperty(Exchange.BATCH_SIZE, 3);
    e5.setProperty(Exchange.BATCH_COMPLETE, true);
    ap.process(e1);
    ap.process(e2);
    ap.process(e3);
    ap.process(e4);
    ap.process(e5);
    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 34 with AggregationStrategy

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

the class AggregateProcessorTest method doTestAggregateLogFailedExchange.

private void doTestAggregateLogFailedExchange(ExceptionHandler handler) throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("A+END");
    Processor done = new Processor() {

        public void process(Exchange exchange) throws Exception {
            if (exchange.getIn().getBody(String.class).contains("Kaboom")) {
                throw new IllegalArgumentException("Damn");
            }
            // else send it further along
            SendProcessor send = new SendProcessor(context.getEndpoint("mock:result"));
            send.start();
            send.process(exchange);
        }
    };
    Expression corr = header("id");
    AggregationStrategy as = new BodyInAggregatingStrategy();
    AggregateProcessor ap = new AggregateProcessor(context, done, corr, as, executorService, true);
    ap.setEagerCheckCompletion(true);
    ap.setCompletionPredicate(body().isEqualTo("END"));
    if (handler != null) {
        ap.setExceptionHandler(handler);
    }
    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("Kaboom");
    e3.getIn().setHeader("id", 456);
    Exchange e4 = new DefaultExchange(context);
    e4.getIn().setBody("END");
    e4.getIn().setHeader("id", 456);
    Exchange e5 = new DefaultExchange(context);
    e5.getIn().setBody("END");
    e5.getIn().setHeader("id", 123);
    ap.process(e1);
    ap.process(e2);
    ap.process(e3);
    ap.process(e4);
    ap.process(e5);
    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 35 with AggregationStrategy

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

the class AggregateProcessorTest method testAggregateIgnoreInvalidCorrelationKey.

public void testAggregateIgnoreInvalidCorrelationKey() 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.setIgnoreInvalidCorrelationKeys(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");
    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);
    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