CAMEL-6964: Fixed file consumer on rollback to delete the done file if moveFailed is in use. Thanks to Christoph Giera for patch.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/aaec6826 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/aaec6826 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/aaec6826 Branch: refs/heads/camel-2.12.x Commit: aaec682679499af21387a32ade90282006679301 Parents: fee86db Author: Claus Ibsen <[email protected]> Authored: Thu Nov 14 12:01:48 2013 +0100 Committer: Claus Ibsen <[email protected]> Committed: Thu Nov 14 12:45:05 2013 +0100 ---------------------------------------------------------------------- .../component/file/GenericFileOnCompletion.java | 53 +++++++------ ...FilerConsumerMoveFailedDoneFileNameTest.java | 67 +++++++++++++++++ .../FilerConsumerRetryDoneFileNameTest.java | 79 ++++++++++++++++++++ 3 files changed, 178 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/aaec6826/camel-core/src/main/java/org/apache/camel/component/file/GenericFileOnCompletion.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/file/GenericFileOnCompletion.java b/camel-core/src/main/java/org/apache/camel/component/file/GenericFileOnCompletion.java index 5bedc00..db16a66 100644 --- a/camel-core/src/main/java/org/apache/camel/component/file/GenericFileOnCompletion.java +++ b/camel-core/src/main/java/org/apache/camel/component/file/GenericFileOnCompletion.java @@ -117,27 +117,7 @@ public class GenericFileOnCompletion<T> implements Synchronization { } } - // must be last in batch to delete the done file name - // delete done file if used (and not noop=true) - boolean complete = exchange.getProperty(Exchange.BATCH_COMPLETE, false, Boolean.class); - if (endpoint.getDoneFileName() != null && !endpoint.isNoop()) { - // done file must be in same path as the original input file - String doneFileName = endpoint.createDoneFileName(absoluteFileName); - ObjectHelper.notEmpty(doneFileName, "doneFileName", endpoint); - // we should delete the dynamic done file - if (endpoint.getDoneFileName().indexOf("{file:name") > 0 || complete) { - try { - // delete done file - boolean deleted = operations.deleteFile(doneFileName); - log.trace("Done file: {} was deleted: {}", doneFileName, deleted); - if (!deleted) { - log.warn("Done file: " + doneFileName + " could not be deleted"); - } - } catch (Exception e) { - handleException("Error deleting done file: " + doneFileName, exchange, e); - } - } - } + handleDoneFile(exchange); try { log.trace("Commit file strategy: {} for file: {}", processStrategy, file); @@ -160,6 +140,13 @@ public class GenericFileOnCompletion<T> implements Synchronization { if (log.isWarnEnabled()) { log.warn("Rollback file strategy: " + processStrategy + " for file: " + file); } + + // only delete done file if moveFailed option is enabled, as otherwise on rollback, + // we should leave the done file so we can retry + if (endpoint.getMoveFailed() != null) { + handleDoneFile(exchange); + } + try { processStrategy.rollback(operations, endpoint, exchange, file); } catch (Exception e) { @@ -167,6 +154,30 @@ public class GenericFileOnCompletion<T> implements Synchronization { } } + protected void handleDoneFile(Exchange exchange) { + // must be last in batch to delete the done file name + // delete done file if used (and not noop=true) + boolean complete = exchange.getProperty(Exchange.BATCH_COMPLETE, false, Boolean.class); + if (endpoint.getDoneFileName() != null && !endpoint.isNoop()) { + // done file must be in same path as the original input file + String doneFileName = endpoint.createDoneFileName(absoluteFileName); + ObjectHelper.notEmpty(doneFileName, "doneFileName", endpoint); + // we should delete the dynamic done file + if (endpoint.getDoneFileName().indexOf("{file:name") > 0 || complete) { + try { + // delete done file + boolean deleted = operations.deleteFile(doneFileName); + log.trace("Done file: {} was deleted: {}", doneFileName, deleted); + if (!deleted) { + log.warn("Done file: " + doneFileName + " could not be deleted"); + } + } catch (Exception e) { + handleException("Error deleting done file: " + doneFileName, exchange, e); + } + } + } + } + protected void handleException(String message, Exchange exchange, Throwable t) { Throwable newt = (t == null) ? new IllegalArgumentException("Handling [null] exception") : t; getExceptionHandler().handleException(message, exchange, newt); http://git-wip-us.apache.org/repos/asf/camel/blob/aaec6826/camel-core/src/test/java/org/apache/camel/component/file/FilerConsumerMoveFailedDoneFileNameTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FilerConsumerMoveFailedDoneFileNameTest.java b/camel-core/src/test/java/org/apache/camel/component/file/FilerConsumerMoveFailedDoneFileNameTest.java new file mode 100644 index 0000000..35a9241 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/file/FilerConsumerMoveFailedDoneFileNameTest.java @@ -0,0 +1,67 @@ +/** + * 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.component.file; + +import java.io.File; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; + +/** + * Unit test done files with moveFailed option + */ +public class FilerConsumerMoveFailedDoneFileNameTest extends ContextTestSupport { + + @Override + protected void setUp() throws Exception { + deleteDirectory("target/done"); + super.setUp(); + } + + public void testDoneFile() throws Exception { + getMockEndpoint("mock:input").expectedMessageCount(1); + + template.sendBodyAndHeader("file:target/done", "Hello World", Exchange.FILE_NAME, "hello.txt"); + template.sendBodyAndHeader("file:target/done", "", Exchange.FILE_NAME, "done"); + + assertMockEndpointsSatisfied(); + + oneExchangeDone.matchesMockWaitTime(); + + // done file should be deleted now + File file = new File("target/done/done"); + assertFalse("Done file should be deleted: " + file, file.exists()); + + // as well the original file should be moved to failed + file = new File("target/done/failed/hello.txt"); + assertTrue("Original file should be moved: " + file, file.exists()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("file:target/done?doneFileName=done&initialDelay=0&delay=10&moveFailed=failed") + .to("mock:input") + .throwException(new IllegalArgumentException("Forced")); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/aaec6826/camel-core/src/test/java/org/apache/camel/component/file/FilerConsumerRetryDoneFileNameTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FilerConsumerRetryDoneFileNameTest.java b/camel-core/src/test/java/org/apache/camel/component/file/FilerConsumerRetryDoneFileNameTest.java new file mode 100644 index 0000000..f92821e --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/file/FilerConsumerRetryDoneFileNameTest.java @@ -0,0 +1,79 @@ +/** + * 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.component.file; + +import java.io.File; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; + +/** + * Unit test done files with retry + */ +public class FilerConsumerRetryDoneFileNameTest extends ContextTestSupport { + + @Override + protected void setUp() throws Exception { + deleteDirectory("target/done"); + super.setUp(); + } + + public void testDoneFile() throws Exception { + getMockEndpoint("mock:input").expectedMessageCount(2); + getMockEndpoint("mock:input").expectedFileExists("target/done/.camel/hello.txt"); + + template.sendBodyAndHeader("file:target/done", "Hello World", Exchange.FILE_NAME, "hello.txt"); + template.sendBodyAndHeader("file:target/done", "", Exchange.FILE_NAME, "done"); + + assertMockEndpointsSatisfied(); + + // done file should be deleted now + File file = new File("target/done/done"); + assertFalse("Done file should be deleted: " + file, file.exists()); + + // as well the original file should be moved to .camel + file = new File("target/done/.camel/hello.txt"); + assertTrue("Original file should be moved: " + file, file.exists()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("file:target/done?doneFileName=done&initialDelay=0&delay=10") + .to("mock:input") + .process(new Processor() { + int index = 0; + @Override + public void process(Exchange exchange) throws Exception { + if (index++ == 0) { + // done file should still exists + File file = new File("target/done/done"); + assertTrue("Done file should exists: " + file, file.exists()); + + throw new IllegalArgumentException("Forced"); + } + } + }); + } + }; + } + +}
