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"));
}
};
}
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);
}
});
}
};
}
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
}
};
}
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");
}
};
}
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);
}
Aggregations