This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch camel-2.25.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-2.25.x by this push: new 986b928 CAMEL-15532: Stream Cache file not deleted (#4227) 986b928 is described below commit 986b92838f9993161a1134fc03c18b8231cae9ea Author: forsthofer <forstho...@users.noreply.github.com> AuthorDate: Tue Sep 15 07:35:21 2020 +0200 CAMEL-15532: Stream Cache file not deleted (#4227) If you have a route with a Multicast with parallel processing and a timeout and a sub-route in the multicast which is creating a StreamCache after the timeout then the created stream cache file is never deleted from the file system. Co-authored-by: Franz Forsthofer <franz.forstho...@sap.com> --- .../converter/stream/FileInputStreamCache.java | 4 +- .../MulticastParallelTimeoutStreamCachingTest.java | 109 +++++++++++++++++++++ 2 files changed, 112 insertions(+), 1 deletion(-) diff --git a/camel-core/src/main/java/org/apache/camel/converter/stream/FileInputStreamCache.java b/camel-core/src/main/java/org/apache/camel/converter/stream/FileInputStreamCache.java index 8e38495..74eacfe 100644 --- a/camel-core/src/main/java/org/apache/camel/converter/stream/FileInputStreamCache.java +++ b/camel-core/src/main/java/org/apache/camel/converter/stream/FileInputStreamCache.java @@ -219,11 +219,13 @@ public final class FileInputStreamCache extends InputStream implements StreamCac } }; UnitOfWork streamCacheUnitOfWork = exchange.getProperty(Exchange.STREAM_CACHE_UNIT_OF_WORK, UnitOfWork.class); - if (streamCacheUnitOfWork != null) { + if (streamCacheUnitOfWork != null && streamCacheUnitOfWork.getRouteContext() !=null) { // The stream cache must sometimes not be closed when the exchange is deleted. This is for example the // case in the splitter and multi-cast case with AggregationStrategy where the result of the sub-routes // are aggregated later in the main route. Here, the cached streams of the sub-routes must be closed with // the Unit of Work of the main route. + //streamCacheUnitOfWork.getRouteContext() !=null indicates that the UnitOfWork was not yet stopped: + // This can happen when a timeout is set on the multi-cast and in the sub-branch thread a stream cache is created after the timeout. streamCacheUnitOfWork.addSynchronization(onCompletion); } else { // add on completion so we can cleanup after the exchange is done such as deleting temporary files diff --git a/camel-core/src/test/java/org/apache/camel/processor/MulticastParallelTimeoutStreamCachingTest.java b/camel-core/src/test/java/org/apache/camel/processor/MulticastParallelTimeoutStreamCachingTest.java new file mode 100644 index 0000000..747775a --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/processor/MulticastParallelTimeoutStreamCachingTest.java @@ -0,0 +1,109 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.processor; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FilterInputStream; +import java.nio.charset.StandardCharsets; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.Before; +import org.junit.Test; + +public class MulticastParallelTimeoutStreamCachingTest extends ContextTestSupport { + + private static final String TARGET_MULTICAST_PARALLEL_TIMEOUT_STREAM_CACHING_TEST_CACHE = "target/MulticastParallelTimeoutStreamCachingTestCache"; + private static final String bodyString = "message body"; + private static final byte[] BODY = bodyString.getBytes(StandardCharsets.UTF_8); + + protected Endpoint startEndpoint; + protected MockEndpoint x; + + public static void deleteDirectory(File file) { + if (file.isDirectory()) { + File[] files = file.listFiles(); + for (File child : files) { + deleteDirectory(child); + } + } + + file.delete(); + } + + @Test + public void testSendingAMessageUsingMulticastConvertsToReReadable() throws Exception { + x.expectedBodiesReceived(bodyString); + + template.sendBody("direct:a", "testMessage"); + assertMockEndpointsSatisfied(); + + File f = new File(TARGET_MULTICAST_PARALLEL_TIMEOUT_STREAM_CACHING_TEST_CACHE); + assertTrue(f.isDirectory()); + Thread.sleep(500l); // deletion happens asynchron + File[] files = f.listFiles(); + assertEquals(0, files.length); + } + + @Before + @Override + public void setUp() throws Exception { + super.setUp(); + + deleteDirectory(new File(TARGET_MULTICAST_PARALLEL_TIMEOUT_STREAM_CACHING_TEST_CACHE)); + x = getMockEndpoint("mock:x"); + } + + protected RouteBuilder createRouteBuilder() { + final Processor processor1 = new Processor() { + public void process(Exchange exchange) { + try { + // sleep for one second so that the stream cache is built after the main exchange has finished due to timeout on the multicast + Thread.sleep(1000l); + } catch (InterruptedException e) { + throw new IllegalStateException("Unexpected exception", e); + } + Message in = exchange.getIn(); + // use FilterInputStream to trigger streamcaching + in.setBody(new FilterInputStream(new ByteArrayInputStream(BODY)) { + + }); + } + }; + + return new RouteBuilder() { + public void configure() { + // enable stream caching + context.getStreamCachingStrategy().setSpoolDirectory(TARGET_MULTICAST_PARALLEL_TIMEOUT_STREAM_CACHING_TEST_CACHE); + context.getStreamCachingStrategy().setEnabled(true); + context.getStreamCachingStrategy().setRemoveSpoolDirectoryWhenStopping(false); + context.getStreamCachingStrategy().setSpoolThreshold(1l); + context.setStreamCaching(true); + + from("direct:a").multicast().timeout(500l).parallelProcessing().to("direct:x"); + + from("direct:x").process(processor1).to("mock:x"); + } + }; + } +}