use of org.apache.camel.StreamCache in project camel by apache.
the class MulticastProcessor method createProcessorExchangePairs.
protected Iterable<ProcessorExchangePair> createProcessorExchangePairs(Exchange exchange) throws Exception {
List<ProcessorExchangePair> result = new ArrayList<ProcessorExchangePair>(processors.size());
StreamCache streamCache = null;
if (isParallelProcessing() && exchange.getIn().getBody() instanceof StreamCache) {
// in parallel processing case, the stream must be copied, therefore get the stream
streamCache = (StreamCache) exchange.getIn().getBody();
}
int index = 0;
for (Processor processor : processors) {
// copy exchange, and do not share the unit of work
Exchange copy = ExchangeHelper.createCorrelatedCopy(exchange, false);
if (streamCache != null) {
if (index > 0) {
// copy it otherwise parallel processing is not possible,
// because streams can only be read once
StreamCache copiedStreamCache = streamCache.copy(copy);
if (copiedStreamCache != null) {
copy.getIn().setBody(copiedStreamCache);
}
}
}
// if it is not already set.
if (copy.getProperty(Exchange.STREAM_CACHE_UNIT_OF_WORK) == null) {
copy.setProperty(Exchange.STREAM_CACHE_UNIT_OF_WORK, exchange.getUnitOfWork());
}
// if we share unit of work, we need to prepare the child exchange
if (isShareUnitOfWork()) {
prepareSharedUnitOfWork(copy, exchange);
}
// and add the pair
RouteContext routeContext = exchange.getUnitOfWork() != null ? exchange.getUnitOfWork().getRouteContext() : null;
result.add(createProcessorExchangePair(index++, processor, copy, routeContext));
}
if (exchange.getException() != null) {
// before returning the answer;
throw exchange.getException();
}
return result;
}
use of org.apache.camel.StreamCache in project camel by apache.
the class StreamCachingInterceptorTest method testConvertStreamSourceWithRouteBuilderStreamCaching.
public void testConvertStreamSourceWithRouteBuilderStreamCaching() throws Exception {
a.expectedMessageCount(1);
StreamSource message = new StreamSource(new StringReader(MESSAGE));
template.sendBody("direct:a", message);
assertMockEndpointsSatisfied();
assertTrue(a.assertExchangeReceived(0).getIn().getBody() instanceof StreamCache);
}
use of org.apache.camel.StreamCache in project camel by apache.
the class NoStreamCachingTest method testMixed.
public void testMixed() throws Exception {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:a").to("mock:a");
from("direct:b").streamCaching().to("mock:b");
}
});
context.start();
a.expectedMessageCount(1);
b.expectedMessageCount(1);
InputStream message = new ByteArrayInputStream(MESSAGE.getBytes());
template.sendBody("direct:a", message);
InputStream message2 = new ByteArrayInputStream(MESSAGE.getBytes());
template.sendBody("direct:b", message2);
assertMockEndpointsSatisfied();
assertTrue(a.assertExchangeReceived(0).getIn().getBody() instanceof ByteArrayInputStream);
assertEquals(a.assertExchangeReceived(0).getIn().getBody(String.class), MESSAGE);
assertTrue(b.assertExchangeReceived(0).getIn().getBody() instanceof StreamCache);
assertEquals(b.assertExchangeReceived(0).getIn().getBody(String.class), MESSAGE);
}
Aggregations