use of org.apache.camel.processor.aggregate.AggregationStrategy in project camel by apache.
the class MulticastProcessor method getAggregationStrategy.
protected AggregationStrategy getAggregationStrategy(Exchange exchange) {
AggregationStrategy answer = null;
// prefer to use per Exchange aggregation strategy over a global strategy
if (exchange != null) {
Map<?, ?> property = exchange.getProperty(Exchange.AGGREGATION_STRATEGY, Map.class);
Map<Object, AggregationStrategy> map = CastUtils.cast(property);
if (map != null) {
answer = map.get(this);
}
}
if (answer == null) {
// fallback to global strategy
answer = getAggregationStrategy();
}
return answer;
}
use of org.apache.camel.processor.aggregate.AggregationStrategy in project camel by apache.
the class SplitWithCustomAggregationStrategyTest method createRouteBuilder.
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").setBody().simple("<search><key>foo-${id}</key><key>bar-${id}</key><key>baz-${id}</key></search>").to("direct:splitInOut").to("mock:result");
from("direct:splitInOut").setHeader("com.example.id").simple("${id}").split(xpath("/search/key"), new AggregationStrategy() {
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;
}
}).parallelProcessing().streaming().to("direct:processLine").end().transform().simple("<results>${in.body}</results>");
from("direct:processLine").to("log:line").transform().simple("<index>${in.header.CamelSplitIndex}</index>${in.body}");
}
};
}
use of org.apache.camel.processor.aggregate.AggregationStrategy in project camel by apache.
the class EnricherRouteNumberOfProcessorTest method testOneProcesssor.
public void testOneProcesssor() throws Exception {
failed = false;
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").enrich("direct:enrich", new AggregationStrategy() {
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
if (oldExchange == null) {
return newExchange;
}
// should always be in
String body = newExchange.getIn().getBody(String.class);
assertNotNull(body);
return newExchange;
}
}).to("mock:foo").end().to("mock:result");
from("direct:enrich").process(new Processor() {
public void process(Exchange exchange) throws Exception {
assertFalse("Should not have out", failed);
String s = exchange.getIn().getBody(String.class);
exchange.getIn().setBody("Hi " + s);
}
});
}
});
context.start();
MockEndpoint result = getMockEndpoint("mock:result");
result.expectedMessageCount(1);
getMockEndpoint("mock:foo").expectedBodiesReceived("Hi Claus");
getMockEndpoint("mock:foo").expectedHeaderReceived("id", 1);
template.requestBodyAndHeader("direct:start", "Claus", "id", 1);
assertMockEndpointsSatisfied();
}
use of org.apache.camel.processor.aggregate.AggregationStrategy in project camel by apache.
the class EnricherRouteNumberOfProcessorTest method testThreeProcesssors.
public void testThreeProcesssors() throws Exception {
failed = false;
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").enrich("direct:enrich", new AggregationStrategy() {
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
if (oldExchange == null) {
return newExchange;
}
// should always be in
String body = newExchange.getIn().getBody(String.class);
assertNotNull(body);
return newExchange;
}
}).to("mock:foo").end().to("mock:result");
from("direct:enrich").pipeline("log:a", "log:b").to("log:foo").process(new Processor() {
public void process(Exchange exchange) throws Exception {
assertFalse("Should not have out", failed);
String s = exchange.getIn().getBody(String.class);
exchange.getIn().setBody("Hi " + s);
}
});
}
});
context.start();
MockEndpoint result = getMockEndpoint("mock:result");
result.expectedMessageCount(1);
getMockEndpoint("mock:foo").expectedBodiesReceived("Hi Claus");
getMockEndpoint("mock:foo").expectedHeaderReceived("id", 1);
template.requestBodyAndHeader("direct:start", "Claus", "id", 1);
assertMockEndpointsSatisfied();
}
use of org.apache.camel.processor.aggregate.AggregationStrategy in project camel by apache.
the class SplitRouteNumberOfProcessorTest method testThreeProcessors.
public void testThreeProcessors() throws Exception {
failed.set(false);
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").split(body().tokenize(","), new AggregationStrategy() {
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
if (oldExchange == null) {
return newExchange;
}
// should always be in
String body = newExchange.getIn().getBody(String.class);
assertNotNull(body);
return newExchange;
}
}).pipeline("log:a", "log:b").to("log:foo").process(new Processor() {
public void process(Exchange exchange) throws Exception {
assertFalse("Should not have out", failed.get());
String s = exchange.getIn().getBody(String.class);
exchange.getIn().setBody("Hi " + s);
context.createProducerTemplate().send("mock:foo", exchange);
}
}).to("mock:split").end().to("mock:result");
}
});
context.start();
MockEndpoint result = getMockEndpoint("mock:result");
result.expectedMessageCount(1);
getMockEndpoint("mock:foo").expectedBodiesReceived("Hi Claus", "Hi Willem");
template.requestBodyAndHeader("direct:start", "Claus,Willem", "id", 1);
assertMockEndpointsSatisfied();
}
Aggregations