use of org.apache.camel.processor.aggregate.AggregationStrategy in project camel by apache.
the class AggregateProcessorTest method doTestAggregateProcessorCompletionTimeout.
private void doTestAggregateProcessorCompletionTimeout(boolean eager) throws Exception {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedBodiesReceived("A+B+C");
mock.expectedPropertyReceived(Exchange.AGGREGATED_COMPLETED_BY, "timeout");
Processor done = new SendProcessor(context.getEndpoint("mock:result"));
Expression corr = header("id");
AggregationStrategy as = new BodyInAggregatingStrategy();
AggregateProcessor ap = new AggregateProcessor(context, done, corr, as, executorService, true);
ap.setCompletionTimeout(3000);
ap.setEagerCheckCompletion(eager);
ap.start();
Exchange e1 = new DefaultExchange(context);
e1.getIn().setBody("A");
e1.getIn().setHeader("id", 123);
Exchange e2 = new DefaultExchange(context);
e2.getIn().setBody("B");
e2.getIn().setHeader("id", 123);
Exchange e3 = new DefaultExchange(context);
e3.getIn().setBody("C");
e3.getIn().setHeader("id", 123);
Exchange e4 = new DefaultExchange(context);
e4.getIn().setBody("D");
e4.getIn().setHeader("id", 123);
ap.process(e1);
Thread.sleep(250);
ap.process(e2);
Thread.sleep(500);
ap.process(e3);
Thread.sleep(5000);
ap.process(e4);
assertMockEndpointsSatisfied();
ap.stop();
}
use of org.apache.camel.processor.aggregate.AggregationStrategy in project camel by apache.
the class AggregateProcessorTest method testAggregateProcessorCompletionPredicate.
public void testAggregateProcessorCompletionPredicate() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedBodiesReceived("A+B+END");
mock.expectedPropertyReceived(Exchange.AGGREGATED_COMPLETED_BY, "predicate");
Processor done = new SendProcessor(context.getEndpoint("mock:result"));
Expression corr = header("id");
AggregationStrategy as = new BodyInAggregatingStrategy();
Predicate complete = body().contains("END");
AggregateProcessor ap = new AggregateProcessor(context, done, corr, as, executorService, true);
ap.setCompletionPredicate(complete);
ap.setEagerCheckCompletion(false);
ap.start();
Exchange e1 = new DefaultExchange(context);
e1.getIn().setBody("A");
e1.getIn().setHeader("id", 123);
Exchange e2 = new DefaultExchange(context);
e2.getIn().setBody("B");
e2.getIn().setHeader("id", 123);
Exchange e3 = new DefaultExchange(context);
e3.getIn().setBody("END");
e3.getIn().setHeader("id", 123);
Exchange e4 = new DefaultExchange(context);
e4.getIn().setBody("D");
e4.getIn().setHeader("id", 123);
ap.process(e1);
ap.process(e2);
ap.process(e3);
ap.process(e4);
assertMockEndpointsSatisfied();
ap.stop();
}
use of org.apache.camel.processor.aggregate.AggregationStrategy in project camel by apache.
the class AggregateProcessorTest method testAggregateCompletionInterval.
public void testAggregateCompletionInterval() throws Exception {
// camel context must be started
context.start();
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedBodiesReceived("A+B+C", "D");
mock.expectedPropertyReceived(Exchange.AGGREGATED_COMPLETED_BY, "interval");
Processor done = new SendProcessor(context.getEndpoint("mock:result"));
Expression corr = header("id");
AggregationStrategy as = new BodyInAggregatingStrategy();
AggregateProcessor ap = new AggregateProcessor(context, done, corr, as, executorService, true);
ap.setCompletionInterval(3000);
ap.start();
Exchange e1 = new DefaultExchange(context);
e1.getIn().setBody("A");
e1.getIn().setHeader("id", 123);
Exchange e2 = new DefaultExchange(context);
e2.getIn().setBody("B");
e2.getIn().setHeader("id", 123);
Exchange e3 = new DefaultExchange(context);
e3.getIn().setBody("C");
e3.getIn().setHeader("id", 123);
Exchange e4 = new DefaultExchange(context);
e4.getIn().setBody("D");
e4.getIn().setHeader("id", 123);
ap.process(e1);
ap.process(e2);
ap.process(e3);
Thread.sleep(5000);
ap.process(e4);
assertMockEndpointsSatisfied();
ap.stop();
}
use of org.apache.camel.processor.aggregate.AggregationStrategy in project camel by apache.
the class AggregatorConcurrencyTest method createRouteBuilder.
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
public void configure() throws Exception {
from(uri).aggregate(constant(true), new AggregationStrategy() {
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
Exchange answer = oldExchange != null ? oldExchange : newExchange;
COUNTER.getAndIncrement();
Integer newIndex = newExchange.getIn().getHeader("index", Integer.class);
int total = SUM.addAndGet(newIndex);
answer.getIn().setHeader("total", total);
LOG.debug("Index: " + newIndex + ". Total so far: " + total);
return answer;
}
}).completionTimeout(60000).completionPredicate(property(Exchange.AGGREGATED_SIZE).isEqualTo(100)).to("direct:foo");
from("direct:foo").setBody().header("total").to("mock:result");
}
};
}
use of org.apache.camel.processor.aggregate.AggregationStrategy in project camel by apache.
the class AggregatorExceptionInPredicateTest method createRouteBuilder.
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
onException(IllegalArgumentException.class).handled(true).to("mock:handled");
from("direct:start").aggregate(header("id")).completionTimeout(500).aggregationStrategy(new AggregationStrategy() {
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
Object body = newExchange.getIn().getBody();
if ("Damn".equals(body)) {
throw new IllegalArgumentException();
}
return newExchange;
}
}).to("mock:result");
from("direct:predicate").aggregate(new Expression() {
public <T> T evaluate(Exchange exchange, Class<T> type) {
if (exchange.getIn().getBody().equals("Damn")) {
throw new IllegalArgumentException();
}
return ExpressionBuilder.headerExpression("id").evaluate(exchange, type);
}
}, new UseLatestAggregationStrategy()).completionTimeout(500).to("mock:result");
}
};
}
Aggregations