Search in sources :

Example 16 with AggregationStrategy

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

the class AlbertoAggregatorTest method createRouteBuilder.

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

        AggregationStrategy surnameAggregator = new AggregationStrategy() {

            @SuppressWarnings("unchecked")
            public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
                debugIn("Surname Aggregator", oldExchange, newExchange);
                Exchange answer = newExchange;
                if (oldExchange != null) {
                    List<String> brothers = oldExchange.getIn().getBody(List.class);
                    brothers.add(newExchange.getIn().getBody(String.class));
                    answer = oldExchange;
                } else {
                    List<String> brothers = new ArrayList<String>();
                    brothers.add(newExchange.getIn().getBody(String.class));
                    newExchange.getIn().setBody(brothers);
                }
                debugOut("Surname Aggregator", answer);
                return answer;
            }
        };

        @SuppressWarnings("unchecked")
        AggregationStrategy brothersAggregator = new AggregationStrategy() {

            public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
                debugIn("Brothers Aggregator", oldExchange, newExchange);
                Exchange answer = newExchange;
                if (oldExchange != null) {
                    Map<String, List<?>> brothers = oldExchange.getIn().getBody(Map.class);
                    brothers.put(newExchange.getIn().getHeader(SURNAME_HEADER, String.class), newExchange.getIn().getBody(List.class));
                    answer = oldExchange;
                } else {
                    Map<String, List<?>> brothers = new HashMap<String, List<?>>();
                    brothers.put(newExchange.getIn().getHeader(SURNAME_HEADER, String.class), newExchange.getIn().getBody(List.class));
                    newExchange.getIn().setBody(brothers);
                }
                debugOut("Brothers Aggregator", answer);
                return answer;
            }
        };

        private void debugIn(String stringId, Exchange oldExchange, Exchange newExchange) {
            if (oldExchange != null) {
                log.debug(stringId + " old headers in: " + oldExchange.getIn().getHeaders());
                log.debug(stringId + " old body in: " + oldExchange.getIn().getBody());
            }
            log.debug(stringId + " new headers in: " + newExchange.getIn().getHeaders());
            log.debug(stringId + " new body in: " + newExchange.getIn().getBody());
        }

        private void debugOut(String stringId, Exchange exchange) {
            log.debug(stringId + " old headers out: " + exchange.getIn().getHeaders());
            log.debug(stringId + " old body out: " + exchange.getIn().getBody());
        }

        @Override
        public void configure() throws Exception {
            from("direct:start").split(bodyAs(String.class).tokenize(",")).process(// header
            new Processor() {

                public void process(Exchange exchange) throws Exception {
                    String[] parts = exchange.getIn().getBody(String.class).split(" ");
                    exchange.getIn().setBody(parts[0]);
                    exchange.getIn().setHeader(SURNAME_HEADER, parts[1]);
                }
            }).to("direct:joinSurnames");
            from("direct:joinSurnames").aggregate(header(SURNAME_HEADER), surnameAggregator).completionTimeout(2000L).setHeader(TYPE_HEADER, constant(BROTHERS_TYPE)).to("direct:joinBrothers");
            // Join all brothers lists and remove surname and type headers
            AggregateDefinition agg = from("direct:joinBrothers").aggregate(header(TYPE_HEADER), brothersAggregator);
            agg.setCompletionTimeout(2000L);
            agg.removeHeader(SURNAME_HEADER).removeHeader(TYPE_HEADER).to("mock:result");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) List(java.util.List) ArrayList(java.util.ArrayList) AggregateDefinition(org.apache.camel.model.AggregateDefinition) Map(java.util.Map) HashMap(java.util.HashMap) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

Example 17 with AggregationStrategy

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

the class SplitWithEndTest method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            context.setTracing(true);
            MySplitBean bean = new MySplitBean();
            from("direct:start").to("mock:start").split(body().tokenize(","), new AggregationStrategy() {

                public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
                    if (oldExchange == null) {
                        return newExchange;
                    }
                    String body = oldExchange.getIn().getBody(String.class);
                    String newBody = newExchange.getIn().getBody(String.class);
                    newExchange.getIn().setBody(body + "@" + newBody);
                    return newExchange;
                }
            }).bean(bean, "hi").to("mock:split").to("log:foo").end().transform(body().prepend("last ")).to("mock:last");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) RouteBuilder(org.apache.camel.builder.RouteBuilder) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

Example 18 with AggregationStrategy

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

the class MulticastDefinition method createCompositeProcessor.

protected Processor createCompositeProcessor(RouteContext routeContext, List<Processor> list) throws Exception {
    final AggregationStrategy strategy = createAggregationStrategy(routeContext);
    boolean isParallelProcessing = getParallelProcessing() != null && getParallelProcessing();
    boolean isShareUnitOfWork = getShareUnitOfWork() != null && getShareUnitOfWork();
    boolean isStreaming = getStreaming() != null && getStreaming();
    boolean isStopOnException = getStopOnException() != null && getStopOnException();
    boolean isParallelAggregate = getParallelAggregate() != null && getParallelAggregate();
    boolean isStopOnAggregateException = getStopOnAggregateException() != null && getStopOnAggregateException();
    boolean shutdownThreadPool = ProcessorDefinitionHelper.willCreateNewThreadPool(routeContext, this, isParallelProcessing);
    ExecutorService threadPool = ProcessorDefinitionHelper.getConfiguredExecutorService(routeContext, "Multicast", this, isParallelProcessing);
    long timeout = getTimeout() != null ? getTimeout() : 0;
    if (timeout > 0 && !isParallelProcessing) {
        throw new IllegalArgumentException("Timeout is used but ParallelProcessing has not been enabled.");
    }
    if (onPrepareRef != null) {
        onPrepare = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), onPrepareRef, Processor.class);
    }
    MulticastProcessor answer = new MulticastProcessor(routeContext.getCamelContext(), list, strategy, isParallelProcessing, threadPool, shutdownThreadPool, isStreaming, isStopOnException, timeout, onPrepare, isShareUnitOfWork, isParallelAggregate, isStopOnAggregateException);
    return answer;
}
Also used : Processor(org.apache.camel.Processor) MulticastProcessor(org.apache.camel.processor.MulticastProcessor) ExecutorService(java.util.concurrent.ExecutorService) MulticastProcessor(org.apache.camel.processor.MulticastProcessor) UseLatestAggregationStrategy(org.apache.camel.processor.aggregate.UseLatestAggregationStrategy) ShareUnitOfWorkAggregationStrategy(org.apache.camel.processor.aggregate.ShareUnitOfWorkAggregationStrategy) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

Example 19 with AggregationStrategy

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

the class MulticastParallelStreamingTwoTimeoutTest method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            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().streaming().timeout(2000).to("direct:a", "direct:b", "direct:c").end().to("mock:result");
            from("direct:a").delay(3000).setBody(constant("A"));
            from("direct:b").setBody(constant("B"));
            from("direct:c").delay(4000).setBody(constant("C"));
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) RouteBuilder(org.apache.camel.builder.RouteBuilder) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

Example 20 with AggregationStrategy

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

the class MulticastParallelStressTest method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            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().to("direct:a", "direct:b", "direct:c", "direct:d").end().to("mock:result");
            from("direct:a").delay(20).setBody(body().append("A"));
            from("direct:b").setBody(body().append("B"));
            from("direct:c").delay(50).setBody(body().append("C"));
            from("direct:d").delay(10).setBody(body().append("D"));
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) RouteBuilder(org.apache.camel.builder.RouteBuilder) 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