Search in sources :

Example 21 with AggregationStrategy

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

the class MulticastParallelTest 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").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) RouteBuilder(org.apache.camel.builder.RouteBuilder) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

Example 22 with AggregationStrategy

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

the class JettyHandle404Test method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            // setup the jetty component with the customx error handler
            JettyHttpComponent jettyComponent = (JettyHttpComponent) context.getComponent("jetty");
            jettyComponent.setErrorHandler(new MyErrorHandler());
            // 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(HttpOperationFailedException.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
                    HttpOperationFailedException cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, HttpOperationFailedException.class);
                    exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, cause.getStatusCode());
                    exchange.getOut().setBody(cause.getResponseBody());
                }
            }).end();
            // this is our jetty server where we simulate the 404
            from("jetty://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) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

Example 23 with AggregationStrategy

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

the class JettySimplifiedHandle404Test method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            // disable error handling
            errorHandler(noErrorHandler());
            // START SNIPPET: e1
            // We set throwExceptionOnFailure to false to let Camel return any response from the remove HTTP server without thrown
            // HttpOperationFailedException in case of failures.
            // This allows us to handle all responses in the aggregation strategy where we can check the HTTP response code
            // and decide what to do. As this is based on an unit test we assert the code is 404
            from("direct:start").enrich("http://localhost:{{port}}/myserver?throwExceptionOnFailure=false&user=Camel", 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");
            // this is our jetty server where we simulate the 404
            from("jetty://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);
                }
            });
        // END SNIPPET: e1
        }
    };
}
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 24 with AggregationStrategy

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

the class AggregratedJmsRouteTest method createRouteBuilder.

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

        public void configure() throws Exception {
            from(timeOutEndpointUri).to("jms:queue:test.b");
            from("jms:queue:test.b").aggregate(header("cheese"), new AggregationStrategy() {

                public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        LOG.error("aggregration delay sleep inturrepted", e);
                        fail("aggregration delay sleep inturrepted");
                    }
                    return newExchange;
                }
            }).completionTimeout(2000L).to("mock:result");
            from(multicastEndpointUri).to("jms:queue:point1", "jms:queue:point2", "jms:queue:point3");
            from("jms:queue:point1").process(new MyProcessor()).to("jms:queue:reply");
            from("jms:queue:point2").process(new MyProcessor()).to("jms:queue:reply");
            from("jms:queue:point3").process(new MyProcessor()).to("jms:queue:reply");
            from("jms:queue:reply").aggregate(header("cheese"), new UseLatestAggregationStrategy()).completionSize(3).to("mock:reply");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) RouteBuilder(org.apache.camel.builder.RouteBuilder) UseLatestAggregationStrategy(org.apache.camel.processor.aggregate.UseLatestAggregationStrategy) UseLatestAggregationStrategy(org.apache.camel.processor.aggregate.UseLatestAggregationStrategy) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

Example 25 with AggregationStrategy

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

the class Splitter method process.

@Override
public boolean process(Exchange exchange, final AsyncCallback callback) {
    final AggregationStrategy strategy = getAggregationStrategy();
    // to ensure it supports async routing
    if (strategy == null) {
        AggregationStrategy original = new UseOriginalAggregationStrategy(exchange, true);
        if (isShareUnitOfWork()) {
            original = new ShareUnitOfWorkAggregationStrategy(original);
        }
        setAggregationStrategyOnExchange(exchange, original);
    }
    return super.process(exchange, callback);
}
Also used : UseOriginalAggregationStrategy(org.apache.camel.processor.aggregate.UseOriginalAggregationStrategy) ShareUnitOfWorkAggregationStrategy(org.apache.camel.processor.aggregate.ShareUnitOfWorkAggregationStrategy) UseOriginalAggregationStrategy(org.apache.camel.processor.aggregate.UseOriginalAggregationStrategy) ShareUnitOfWorkAggregationStrategy(org.apache.camel.processor.aggregate.ShareUnitOfWorkAggregationStrategy) 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