CAMEL-7055: Fixed stream caching to clean temp file when UoW done. Thanks to Franz Forsthofer for the patch.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/15e1077d Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/15e1077d Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/15e1077d Branch: refs/heads/master Commit: 15e1077d2e11385b7cd763c3fc40e4b4282b21ea Parents: dc08192 Author: Claus Ibsen <[email protected]> Authored: Sat Dec 14 11:09:02 2013 +0100 Committer: Claus Ibsen <[email protected]> Committed: Sat Dec 14 11:09:02 2013 +0100 ---------------------------------------------------------------------- .../converter/stream/CachedOutputStream.java | 42 +++++++++++--------- .../processor/DataFormatStreamingTest.java | 31 +++++++++++++++ 2 files changed, 54 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/15e1077d/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java b/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java index c5f3b49..0e3540c 100644 --- a/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java +++ b/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java @@ -71,31 +71,36 @@ public class CachedOutputStream extends OutputStream { this(exchange, true); } - public CachedOutputStream(Exchange exchange, boolean closedOnCompletion) { + public CachedOutputStream(Exchange exchange, final boolean closedOnCompletion) { this.strategy = exchange.getContext().getStreamCachingStrategy(); currentStream = new CachedByteArrayOutputStream(strategy.getBufferSize()); - if (closedOnCompletion) { - // add on completion so we can cleanup after the exchange is done such as deleting temporary files - exchange.addOnCompletion(new SynchronizationAdapter() { - @Override - public void onDone(Exchange exchange) { - try { - if (fileInputStreamCache != null) { - fileInputStreamCache.close(); - } + // add on completion so we can cleanup after the exchange is done such as deleting temporary files + exchange.addOnCompletion(new SynchronizationAdapter() { + @Override + public void onDone(Exchange exchange) { + try { + if (fileInputStreamCache != null) { + fileInputStreamCache.close(); + } + if (closedOnCompletion) { close(); - } catch (Exception e) { - LOG.warn("Error deleting temporary cache file: " + tempFile, e); } + } catch (Exception e) { + LOG.warn("Error closing streams. This exception will be ignored.", e); } - - @Override - public String toString() { - return "OnCompletion[CachedOutputStream]"; + try { + cleanUpTempFile(); + } catch (Exception e) { + LOG.warn("Error deleting temporary cache file: " + tempFile + ". This exception will be ignored.", e); } - }); - } + } + + @Override + public String toString() { + return "OnCompletion[CachedOutputStream]"; + } + }); } public void flush() throws IOException { @@ -104,7 +109,6 @@ public class CachedOutputStream extends OutputStream { public void close() throws IOException { currentStream.close(); - cleanUpTempFile(); } public boolean equals(Object obj) { http://git-wip-us.apache.org/repos/asf/camel/blob/15e1077d/camel-core/src/test/java/org/apache/camel/processor/DataFormatStreamingTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/processor/DataFormatStreamingTest.java b/camel-core/src/test/java/org/apache/camel/processor/DataFormatStreamingTest.java new file mode 100644 index 0000000..d1a0e0e --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/processor/DataFormatStreamingTest.java @@ -0,0 +1,31 @@ +/** + * 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 org.apache.camel.CamelContext; + +public class DataFormatStreamingTest extends DataFormatTest { + + @Override + public CamelContext createCamelContext() throws Exception { + CamelContext cc = super.createCamelContext(); + cc.setStreamCaching(Boolean.TRUE); + cc.getStreamCachingStrategy().setSpoolThreshold(1L); + return cc; + } + +}
