Search in sources :

Example 1 with AggregateDefinition

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

use of org.apache.camel.model.AggregateDefinition in project camel by apache.

the class TraceInterceptor method traceAggregate.

private void traceAggregate(TracedRouteNodes traced, Exchange exchange) {
    traced.addTraced(new AggregateRouteNode((AggregateDefinition) node.getParent()));
    traced.addTraced(new DefaultRouteNode(node, super.getProcessor()));
}
Also used : AggregateRouteNode(org.apache.camel.impl.AggregateRouteNode) AggregateDefinition(org.apache.camel.model.AggregateDefinition) DefaultRouteNode(org.apache.camel.impl.DefaultRouteNode)

Aggregations

AggregateDefinition (org.apache.camel.model.AggregateDefinition)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Exchange (org.apache.camel.Exchange)1 Processor (org.apache.camel.Processor)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 AggregateRouteNode (org.apache.camel.impl.AggregateRouteNode)1 DefaultRouteNode (org.apache.camel.impl.DefaultRouteNode)1 AggregationStrategy (org.apache.camel.processor.aggregate.AggregationStrategy)1