Search in sources :

Example 36 with StreamCache

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;
}
Also used : AtomicExchange(org.apache.camel.util.concurrent.AtomicExchange) Exchange(org.apache.camel.Exchange) AsyncProcessor(org.apache.camel.AsyncProcessor) Processor(org.apache.camel.Processor) StreamCache(org.apache.camel.StreamCache) ArrayList(java.util.ArrayList) Endpoint(org.apache.camel.Endpoint) RouteContext(org.apache.camel.spi.RouteContext)

Example 37 with StreamCache

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);
}
Also used : StreamCache(org.apache.camel.StreamCache) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader)

Example 38 with 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);
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamCache(org.apache.camel.StreamCache) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Aggregations

StreamCache (org.apache.camel.StreamCache)38 InputStream (java.io.InputStream)14 IOException (java.io.IOException)10 File (java.io.File)7 Exchange (org.apache.camel.Exchange)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ArrayList (java.util.ArrayList)5 StreamSource (javax.xml.transform.stream.StreamSource)5 KeyValueAnnotation (com.github.kristofa.brave.KeyValueAnnotation)4 StringReader (java.io.StringReader)4 OutputStream (java.io.OutputStream)3 SAXSource (javax.xml.transform.sax.SAXSource)3 Message (org.apache.camel.Message)3 SalesforceException (org.apache.camel.component.salesforce.api.SalesforceException)3 BatchInfo (org.apache.camel.component.salesforce.api.dto.bulk.BatchInfo)3 BulkApiClient (org.apache.camel.component.salesforce.internal.client.BulkApiClient)3 DefaultBulkApiClient (org.apache.camel.component.salesforce.internal.client.DefaultBulkApiClient)3 DefaultExchange (org.apache.camel.impl.DefaultExchange)3 Writer (java.io.Writer)2 Source (javax.xml.transform.Source)2