Search in sources :

Example 41 with AggregationStrategy

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

the class MulticastProcessor method doDone.

/**
     * Common work which must be done when we are done multicasting.
     * <p/>
     * This logic applies for both running synchronous and asynchronous as there are multiple exist points
     * when using the asynchronous routing engine. And therefore we want the logic in one method instead
     * of being scattered.
     *
     * @param original     the original exchange
     * @param subExchange  the current sub exchange, can be <tt>null</tt> for the synchronous part
     * @param pairs        the pairs with the exchanges to process
     * @param callback     the callback
     * @param doneSync     the <tt>doneSync</tt> parameter to call on callback
     * @param forceExhaust whether or not error handling is exhausted
     */
protected void doDone(Exchange original, Exchange subExchange, final Iterable<ProcessorExchangePair> pairs, AsyncCallback callback, boolean doneSync, boolean forceExhaust) {
    // we are done so close the pairs iterator
    if (pairs != null && pairs instanceof Closeable) {
        IOHelper.close((Closeable) pairs, "pairs", LOG);
    }
    AggregationStrategy strategy = getAggregationStrategy(subExchange);
    if (strategy instanceof DelegateAggregationStrategy) {
        strategy = ((DelegateAggregationStrategy) strategy).getDelegate();
    }
    // invoke the on completion callback
    if (strategy instanceof CompletionAwareAggregationStrategy) {
        ((CompletionAwareAggregationStrategy) strategy).onCompletion(subExchange);
    }
    // cleanup any per exchange aggregation strategy
    removeAggregationStrategyFromExchange(original);
    // we need to know if there was an exception, and if the stopOnException option was enabled
    // also we would need to know if any error handler has attempted redelivery and exhausted
    boolean stoppedOnException = false;
    boolean exception = false;
    boolean exhaust = forceExhaust || subExchange != null && (subExchange.getException() != null || ExchangeHelper.isRedeliveryExhausted(subExchange));
    if (original.getException() != null || subExchange != null && subExchange.getException() != null) {
        // there was an exception and we stopped
        stoppedOnException = isStopOnException();
        exception = true;
    }
    // must copy results at this point
    if (subExchange != null) {
        if (stoppedOnException) {
            // if we stopped due an exception then only propagate the exception
            original.setException(subExchange.getException());
        } else {
            // copy the current result to original so it will contain this result of this eip
            ExchangeHelper.copyResults(original, subExchange);
        }
    }
    // handled has been in use, then the exhaust would be false (if not forced)
    if (exception) {
        // multicast uses error handling on its output processors and they have tried to redeliver
        // so we shall signal back to the other error handlers that we are exhausted and they should not
        // also try to redeliver as we will then do that twice
        original.setProperty(Exchange.REDELIVERY_EXHAUSTED, exhaust);
    }
    callback.done(doneSync);
}
Also used : Closeable(java.io.Closeable) DelegateAggregationStrategy(org.apache.camel.processor.aggregate.DelegateAggregationStrategy) CompletionAwareAggregationStrategy(org.apache.camel.processor.aggregate.CompletionAwareAggregationStrategy) 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 42 with AggregationStrategy

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

the class CassandraAggregationSerializedHeadersTest method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            AggregationStrategy aggregationStrategy = new AggregationStrategy() {

                @Override
                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;
                }
            };
            from("direct:input").aggregate(header("aggregationId"), aggregationStrategy).completionSize(3).completionTimeout(3000L).aggregationRepository(aggregationRepository).to("mock:output");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) RouteBuilder(org.apache.camel.builder.RouteBuilder) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

Example 43 with AggregationStrategy

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

the class CassandraAggregationTest method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            AggregationStrategy aggregationStrategy = new AggregationStrategy() {

                @Override
                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;
                }
            };
            from("direct:input").aggregate(header("aggregationId"), aggregationStrategy).completionSize(3).completionTimeout(3000L).aggregationRepository(aggregationRepository).to("mock:output");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) RouteBuilder(org.apache.camel.builder.RouteBuilder) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

Example 44 with AggregationStrategy

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

the class NettyHttpHandle404Test method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            // disable error handling
            errorHandler(noErrorHandler());
            from("direct:start").enrich("direct:tohttp", new AggregationStrategy() {

                public Exchange aggregate(Exchange original, Exchange resource) {
                    // get the response code
                    Integer code = resource.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);
                    assertEquals(404, code.intValue());
                    return resource;
                }
            }).to("mock:result");
            // use this sub route as indirection to handle the HttpOperationFailedException
            // and set the data back as data on the exchange to not cause the exception to be thrown
            from("direct:tohttp").doTry().to(getProducerUrl()).doCatch(NettyHttpOperationFailedException.class).process(new Processor() {

                public void process(Exchange exchange) {
                    // copy the caused exception values to the exchange as we want the response in the regular exchange
                    // instead as an exception that will get thrown and thus the route breaks
                    NettyHttpOperationFailedException cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, NettyHttpOperationFailedException.class);
                    exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, cause.getStatusCode());
                    exchange.getOut().setBody(cause.getContentAsString());
                }
            }).end();
            // this is our jetty server where we simulate the 404
            from("netty4-http:http://localhost:{{port}}/myserver").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    exchange.getOut().setBody("Page not found");
                    exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 404);
                }
            });
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

Example 45 with AggregationStrategy

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

the class NettyHttpHandle404Test method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            // disable error handling
            errorHandler(noErrorHandler());
            from("direct:start").enrich("direct:tohttp", new AggregationStrategy() {

                public Exchange aggregate(Exchange original, Exchange resource) {
                    // get the response code
                    Integer code = resource.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);
                    assertEquals(404, code.intValue());
                    return resource;
                }
            }).to("mock:result");
            // use this sub route as indirection to handle the HttpOperationFailedException
            // and set the data back as data on the exchange to not cause the exception to be thrown
            from("direct:tohttp").doTry().to(getProducerUrl()).doCatch(NettyHttpOperationFailedException.class).process(new Processor() {

                public void process(Exchange exchange) {
                    // copy the caused exception values to the exchange as we want the response in the regular exchange
                    // instead as an exception that will get thrown and thus the route breaks
                    NettyHttpOperationFailedException cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, NettyHttpOperationFailedException.class);
                    exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, cause.getStatusCode());
                    exchange.getOut().setBody(cause.getResponse().getContent().toString(Charset.defaultCharset()));
                }
            }).end();
            // this is our jetty server where we simulate the 404
            from("netty-http:http://localhost:{{port}}/myserver").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    exchange.getOut().setBody("Page not found");
                    exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 404);
                }
            });
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) 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