use of org.apache.camel.processor.aggregate.AggregationStrategy in project camel by apache.
the class MulticastThreadPoolProfileTest method createRouteBuilder.
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
// register thread pool profile
ThreadPoolProfile profile = new ThreadPoolProfileBuilder("myProfile").poolSize(5).maxPoolSize(10).maxQueueSize(20).build();
context.getExecutorServiceManager().registerThreadPoolProfile(profile);
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().executorServiceRef("myProfile").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 SplitRouteNumberOfProcessorTest method testOneProcessor.
public void testOneProcessor() 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;
}
}).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);
}
}).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();
}
use of org.apache.camel.processor.aggregate.AggregationStrategy in project camel by apache.
the class AggregateProcessorTest method testAggregateUseBatchSizeFromConsumer.
public void testAggregateUseBatchSizeFromConsumer() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedBodiesReceived("A+B", "C+D+E");
mock.expectedPropertyReceived(Exchange.AGGREGATED_COMPLETED_BY, "consumer");
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.setCompletionSize(100);
ap.setCompletionFromBatchConsumer(true);
ap.start();
Exchange e1 = new DefaultExchange(context);
e1.getIn().setBody("A");
e1.getIn().setHeader("id", 123);
e1.setProperty(Exchange.BATCH_INDEX, 0);
e1.setProperty(Exchange.BATCH_SIZE, 2);
e1.setProperty(Exchange.BATCH_COMPLETE, false);
Exchange e2 = new DefaultExchange(context);
e2.getIn().setBody("B");
e2.getIn().setHeader("id", 123);
e2.setProperty(Exchange.BATCH_INDEX, 1);
e2.setProperty(Exchange.BATCH_SIZE, 2);
e2.setProperty(Exchange.BATCH_COMPLETE, true);
Exchange e3 = new DefaultExchange(context);
e3.getIn().setBody("C");
e3.getIn().setHeader("id", 123);
e3.setProperty(Exchange.BATCH_INDEX, 0);
e3.setProperty(Exchange.BATCH_SIZE, 3);
e3.setProperty(Exchange.BATCH_COMPLETE, false);
Exchange e4 = new DefaultExchange(context);
e4.getIn().setBody("D");
e4.getIn().setHeader("id", 123);
e4.setProperty(Exchange.BATCH_INDEX, 1);
e4.setProperty(Exchange.BATCH_SIZE, 3);
e4.setProperty(Exchange.BATCH_COMPLETE, false);
Exchange e5 = new DefaultExchange(context);
e5.getIn().setBody("E");
e5.getIn().setHeader("id", 123);
e5.setProperty(Exchange.BATCH_INDEX, 2);
e5.setProperty(Exchange.BATCH_SIZE, 3);
e5.setProperty(Exchange.BATCH_COMPLETE, true);
ap.process(e1);
ap.process(e2);
ap.process(e3);
ap.process(e4);
ap.process(e5);
assertMockEndpointsSatisfied();
ap.stop();
}
use of org.apache.camel.processor.aggregate.AggregationStrategy in project camel by apache.
the class AggregateProcessorTest method doTestAggregateLogFailedExchange.
private void doTestAggregateLogFailedExchange(ExceptionHandler handler) throws Exception {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedBodiesReceived("A+END");
Processor done = new Processor() {
public void process(Exchange exchange) throws Exception {
if (exchange.getIn().getBody(String.class).contains("Kaboom")) {
throw new IllegalArgumentException("Damn");
}
// else send it further along
SendProcessor send = new SendProcessor(context.getEndpoint("mock:result"));
send.start();
send.process(exchange);
}
};
Expression corr = header("id");
AggregationStrategy as = new BodyInAggregatingStrategy();
AggregateProcessor ap = new AggregateProcessor(context, done, corr, as, executorService, true);
ap.setEagerCheckCompletion(true);
ap.setCompletionPredicate(body().isEqualTo("END"));
if (handler != null) {
ap.setExceptionHandler(handler);
}
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", 456);
Exchange e3 = new DefaultExchange(context);
e3.getIn().setBody("Kaboom");
e3.getIn().setHeader("id", 456);
Exchange e4 = new DefaultExchange(context);
e4.getIn().setBody("END");
e4.getIn().setHeader("id", 456);
Exchange e5 = new DefaultExchange(context);
e5.getIn().setBody("END");
e5.getIn().setHeader("id", 123);
ap.process(e1);
ap.process(e2);
ap.process(e3);
ap.process(e4);
ap.process(e5);
assertMockEndpointsSatisfied();
ap.stop();
}
use of org.apache.camel.processor.aggregate.AggregationStrategy in project camel by apache.
the class AggregateProcessorTest method testAggregateIgnoreInvalidCorrelationKey.
public void testAggregateIgnoreInvalidCorrelationKey() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedBodiesReceived("A+C+END");
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.setIgnoreInvalidCorrelationKeys(true);
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");
Exchange e3 = new DefaultExchange(context);
e3.getIn().setBody("C");
e3.getIn().setHeader("id", 123);
Exchange e4 = new DefaultExchange(context);
e4.getIn().setBody("END");
e4.getIn().setHeader("id", 123);
ap.process(e1);
ap.process(e2);
ap.process(e3);
ap.process(e4);
assertMockEndpointsSatisfied();
ap.stop();
}
Aggregations