use of org.apache.camel.processor.BodyInAggregatingStrategy in project camel by apache.
the class AggregateClosedCorrelationKeyTest method testAggregateClosedCorrelationKeyCache.
public void testAggregateClosedCorrelationKeyCache() throws Exception {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").aggregate(header("id"), new BodyInAggregatingStrategy()).completionSize(2).closeCorrelationKeyOnCompletion(2).to("mock:result");
}
});
context.start();
getMockEndpoint("mock:result").expectedBodiesReceived("A+B", "C+D", "E+F");
template.sendBodyAndHeader("direct:start", "A", "id", 1);
template.sendBodyAndHeader("direct:start", "B", "id", 1);
template.sendBodyAndHeader("direct:start", "C", "id", 2);
template.sendBodyAndHeader("direct:start", "D", "id", 2);
template.sendBodyAndHeader("direct:start", "E", "id", 3);
template.sendBodyAndHeader("direct:start", "F", "id", 3);
// 2 of them should now be closed
int closed = 0;
// should NOT be closed because only 2 and 3 is remembered as they are the two last used
try {
template.sendBodyAndHeader("direct:start", "G", "id", 1);
} catch (CamelExecutionException e) {
closed++;
ClosedCorrelationKeyException cause = assertIsInstanceOf(ClosedCorrelationKeyException.class, e.getCause());
assertEquals("1", cause.getCorrelationKey());
assertTrue(cause.getMessage().startsWith("The correlation key [1] has been closed."));
}
// should be closed
try {
template.sendBodyAndHeader("direct:start", "H", "id", 2);
} catch (CamelExecutionException e) {
closed++;
ClosedCorrelationKeyException cause = assertIsInstanceOf(ClosedCorrelationKeyException.class, e.getCause());
assertEquals("2", cause.getCorrelationKey());
assertTrue(cause.getMessage().startsWith("The correlation key [2] has been closed."));
}
// should be closed
try {
template.sendBodyAndHeader("direct:start", "I", "id", 3);
} catch (CamelExecutionException e) {
closed++;
ClosedCorrelationKeyException cause = assertIsInstanceOf(ClosedCorrelationKeyException.class, e.getCause());
assertEquals("3", cause.getCorrelationKey());
assertTrue(cause.getMessage().startsWith("The correlation key [3] has been closed."));
}
assertMockEndpointsSatisfied();
assertEquals("There should be 2 closed", 2, closed);
}
use of org.apache.camel.processor.BodyInAggregatingStrategy in project camel by apache.
the class AggregateParallelProcessingTest method testAggregateParallelProcessing.
public void testAggregateParallelProcessing() throws Exception {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").aggregate(header("id"), new BodyInAggregatingStrategy()).eagerCheckCompletion().completionPredicate(body().isEqualTo("END")).parallelProcessing().to("log:result", "mock:result");
}
});
context.start();
getMockEndpoint("mock:result").expectedBodiesReceivedInAnyOrder("A+Donkey+END", "B+Camel+END");
template.sendBodyAndHeader("direct:start", "A", "id", 1);
template.sendBodyAndHeader("direct:start", "Donkey", "id", 1);
template.sendBodyAndHeader("direct:start", "END", "id", 1);
template.sendBodyAndHeader("direct:start", "B", "id", 2);
template.sendBodyAndHeader("direct:start", "Camel", "id", 2);
template.sendBodyAndHeader("direct:start", "END", "id", 2);
assertMockEndpointsSatisfied();
}
use of org.apache.camel.processor.BodyInAggregatingStrategy in project camel by apache.
the class AggregateParallelProcessingTest method testAggregateNotParallelProcessing.
public void testAggregateNotParallelProcessing() throws Exception {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").aggregate(header("id"), new BodyInAggregatingStrategy()).eagerCheckCompletion().completionPredicate(body().isEqualTo("END")).to("log:result", "mock:result");
}
});
context.start();
getMockEndpoint("mock:result").expectedBodiesReceived("A+Donkey+END", "B+Camel+END");
template.sendBodyAndHeader("direct:start", "A", "id", 1);
template.sendBodyAndHeader("direct:start", "Donkey", "id", 1);
template.sendBodyAndHeader("direct:start", "END", "id", 1);
template.sendBodyAndHeader("direct:start", "B", "id", 2);
template.sendBodyAndHeader("direct:start", "Camel", "id", 2);
template.sendBodyAndHeader("direct:start", "END", "id", 2);
assertMockEndpointsSatisfied();
}
use of org.apache.camel.processor.BodyInAggregatingStrategy 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.BodyInAggregatingStrategy 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();
}
Aggregations