You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2013/11/14 12:50:37 UTC
[3/4] git commit: CAMEL-6964: Fixed file consumer on rollback to
delete the done file if moveFailed is in use. Thanks to Christoph Giera for
patch.
CAMEL-6964: Fixed file consumer on rollback to delete the done file if moveFailed is in use. Thanks to Christoph Giera for patch.
Conflicts:
camel-core/src/main/java/org/apache/camel/component/file/GenericFileOnCompletion.java
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/5b537d5f
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/5b537d5f
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/5b537d5f
Branch: refs/heads/camel-2.11.x
Commit: 5b537d5f54039f77ccb2cc36d13f95e49d1368f9
Parents: e99e6c5
Author: Claus Ibsen <da...@apache.org>
Authored: Thu Nov 14 12:01:48 2013 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Nov 14 12:46:56 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/5b537d5f/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 9b0701b..60e84b6 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
@@ -119,27 +119,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(e);
- }
- }
- }
+ handleDoneFile(exchange);
try {
log.trace("Commit file strategy: {} for file: {}", processStrategy, file);
@@ -162,6 +142,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) {
@@ -169,6 +156,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(e);
+ }
+ }
+ }
+ }
+
protected void handleException(Throwable t) {
Throwable newt = (t == null) ? new IllegalArgumentException("Handling [null] exception") : t;
getExceptionHandler().handleException(newt);
http://git-wip-us.apache.org/repos/asf/camel/blob/5b537d5f/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/5b537d5f/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");
+ }
+ }
+ });
+ }
+ };
+ }
+
+}