Search in sources :

Example 1 with AggregationStrategy

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

the class MulticastProcessor method getAggregationStrategy.

protected AggregationStrategy getAggregationStrategy(Exchange exchange) {
    AggregationStrategy answer = null;
    // prefer to use per Exchange aggregation strategy over a global strategy
    if (exchange != null) {
        Map<?, ?> property = exchange.getProperty(Exchange.AGGREGATION_STRATEGY, Map.class);
        Map<Object, AggregationStrategy> map = CastUtils.cast(property);
        if (map != null) {
            answer = map.get(this);
        }
    }
    if (answer == null) {
        // fallback to global strategy
        answer = getAggregationStrategy();
    }
    return answer;
}
Also used : DelegateAggregationStrategy(org.apache.camel.processor.aggregate.DelegateAggregationStrategy) TimeoutAwareAggregationStrategy(org.apache.camel.processor.aggregate.TimeoutAwareAggregationStrategy) CompletionAwareAggregationStrategy(org.apache.camel.processor.aggregate.CompletionAwareAggregationStrategy) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

Example 2 with AggregationStrategy

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

the class SplitWithCustomAggregationStrategyTest method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            from("direct:start").setBody().simple("<search><key>foo-${id}</key><key>bar-${id}</key><key>baz-${id}</key></search>").to("direct:splitInOut").to("mock:result");
            from("direct:splitInOut").setHeader("com.example.id").simple("${id}").split(xpath("/search/key"), 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;
                }
            }).parallelProcessing().streaming().to("direct:processLine").end().transform().simple("<results>${in.body}</results>");
            from("direct:processLine").to("log:line").transform().simple("<index>${in.header.CamelSplitIndex}</index>${in.body}");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) RouteBuilder(org.apache.camel.builder.RouteBuilder) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

Example 3 with AggregationStrategy

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

the class EnricherRouteNumberOfProcessorTest method testOneProcesssor.

public void testOneProcesssor() throws Exception {
    failed = false;
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").enrich("direct:enrich", 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;
                }
            }).to("mock:foo").end().to("mock:result");
            from("direct:enrich").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertFalse("Should not have out", failed);
                    String s = exchange.getIn().getBody(String.class);
                    exchange.getIn().setBody("Hi " + s);
                }
            });
        }
    });
    context.start();
    MockEndpoint result = getMockEndpoint("mock:result");
    result.expectedMessageCount(1);
    getMockEndpoint("mock:foo").expectedBodiesReceived("Hi Claus");
    getMockEndpoint("mock:foo").expectedHeaderReceived("id", 1);
    template.requestBodyAndHeader("direct:start", "Claus", "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 4 with AggregationStrategy

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

the class EnricherRouteNumberOfProcessorTest method testThreeProcesssors.

public void testThreeProcesssors() throws Exception {
    failed = false;
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").enrich("direct:enrich", 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;
                }
            }).to("mock:foo").end().to("mock:result");
            from("direct:enrich").pipeline("log:a", "log:b").to("log:foo").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertFalse("Should not have out", failed);
                    String s = exchange.getIn().getBody(String.class);
                    exchange.getIn().setBody("Hi " + s);
                }
            });
        }
    });
    context.start();
    MockEndpoint result = getMockEndpoint("mock:result");
    result.expectedMessageCount(1);
    getMockEndpoint("mock:foo").expectedBodiesReceived("Hi Claus");
    getMockEndpoint("mock:foo").expectedHeaderReceived("id", 1);
    template.requestBodyAndHeader("direct:start", "Claus", "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 5 with AggregationStrategy

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

the class SplitRouteNumberOfProcessorTest method testThreeProcessors.

public void testThreeProcessors() 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;
                }
            }).pipeline("log:a", "log:b").to("log:foo").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);
                }
            }).to("mock:split").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)

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