Author: ningjiang
Date: Thu Nov 8 08:22:38 2012
New Revision: 1406948
URL: http://svn.apache.org/viewvc?rev=1406948&view=rev
Log:
CAMEL-5776 Fixed the issue that .camelLock is deleted by another camel instance
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategyReadLockFailedTest.java
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java?rev=1406948&r1=1406947&r2=1406948&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java Thu Nov
8 08:22:38 2012
@@ -120,6 +120,7 @@ public interface Exchange {
String FILE_PARENT = "CamelFileParent";
String FILE_LAST_MODIFIED = "CamelFileLastModified";
String FILTER_MATCHED = "CamelFilterMatched";
+ String FILE_LOCK_FILE_GOT = "CamelFileLockFileGot";
String GROUPED_EXCHANGE = "CamelGroupedExchange";
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java?rev=1406948&r1=1406947&r2=1406948&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java
Thu Nov 8 08:22:38 2012
@@ -52,6 +52,7 @@ public class MarkerFileExclusiveReadLock
// create a plain file as marker filer for locking (do not use
FileLock)
File lock = new File(lockFileName);
boolean acquired = lock.createNewFile();
+ exchange.setProperty(Exchange.FILE_LOCK_FILE_GOT, acquired);
return acquired;
}
@@ -60,11 +61,14 @@ public class MarkerFileExclusiveReadLock
GenericFile<File> file, Exchange
exchange) throws Exception {
String lockFileName = getLockFileName(file);
File lock = new File(lockFileName);
-
- LOG.trace("Unlocking file: {}", lockFileName);
-
- boolean deleted = FileUtil.deleteFile(lock);
- LOG.trace("Lock file: {} was deleted: {}", lockFileName, deleted);
+ // only release the file if camel get the lock before
+ if (exchange.getProperty(Exchange.FILE_LOCK_FILE_GOT, false,
Boolean.class)) {
+ LOG.trace("Unlocking file: {}", lockFileName);
+ boolean deleted = FileUtil.deleteFile(lock);
+ LOG.trace("Lock file: {} was deleted: {}", lockFileName, deleted);
+ } else {
+ LOG.trace("Don't try to delete the Lock file: {} as camel doesn't
get to lock before.", lockFileName);
+ }
}
public void setTimeout(long timeout) {
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategyReadLockFailedTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategyReadLockFailedTest.java?rev=1406948&view=auto
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategyReadLockFailedTest.java
(added)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategyReadLockFailedTest.java
Thu Nov 8 08:22:38 2012
@@ -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.component.file.strategy;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Tests the MarkerFileExclusiveReadLockStrategy in a multi-threaded scenario.
+ */
+public class MarkerFileExclusiveReadLockStrategyReadLockFailedTest extends
ContextTestSupport {
+
+ private static final transient Logger LOG =
LoggerFactory.getLogger(MarkerFileExclusiveReadLockStrategyReadLockFailedTest.class);
+
+ @Override
+ protected void setUp() throws Exception {
+ deleteDirectory("target/readlock/");
+ createDirectory("target/readlock/in");
+ super.setUp();
+ }
+
+ public void testReadLockFailed() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMessageCount(1);
+ mock.expectedFileExists("target/readlock/out/file1.dat");
+
+ writeFiles();
+
+
+ assertMockEndpointsSatisfied();
+
+ String content = context.getTypeConverter().convertTo(String.class,
new File("target/readlock/out/file1.dat").getAbsoluteFile());
+ String[] lines = content.split(LS);
+ for (int i = 0; i < 20; i++) {
+ assertEquals("Line " + i, lines[i]);
+ }
+
+ // wait for a while for camel to clean up the file
+ Thread.sleep(500);
+
+ assertFileDoesNotExists("target/readlock/in/file1.dat.camelLock");
+ assertFileExists("target/readlock/in/file2.dat.camelLock");
+
+ assertFileDoesNotExists("target/readlock/in/file1.dat");
+ assertFileExists("target/readlock/in/file2.dat");
+
+ }
+
+ private void writeFiles() throws Exception {
+ LOG.debug("Writing files...");
+ // create a camelLock file first
+ File lock = new File("target/readlock/in/file2.dat.camelLock");
+ lock.createNewFile();
+
+ FileOutputStream fos = new
FileOutputStream("target/readlock/in/file1.dat");
+ FileOutputStream fos2 = new
FileOutputStream("target/readlock/in/file2.dat");
+ for (int i = 0; i < 20; i++) {
+ fos.write(("Line " + i + LS).getBytes());
+ fos2.write(("Line " + i + LS).getBytes());
+ LOG.debug("Writing line " + i);
+ }
+
+ fos.flush();
+ fos.close();
+ fos2.flush();
+ fos2.close();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("file:target/readlock/in?readLock=markerFile")
+ .to("file:target/readlock/out", "mock:result");
+ }
+ };
+ }
+
+
+ private static void assertFileDoesNotExists(String filename) {
+ File file = new File(filename).getAbsoluteFile();
+ assertFalse("File " + filename + " should not exist, it should have
been deleted after being processed", file.exists());
+ }
+
+}