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 2008/12/19 11:40:17 UTC

svn commit: r727997 - in /activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file: ExclusiveReadLockStrategy.java strategy/FileLockExclusiveReadLockStrategy.java strategy/FileRenameExclusiveReadLockStrategy.java

Author: davsclaus
Date: Fri Dec 19 02:40:17 2008
New Revision: 727997

URL: http://svn.apache.org/viewvc?rev=727997&view=rev
Log:
CAMEL-1195: reworked to not throw ioexception. file lock now catch ioexception as antivirus or MS office throws IOE when locking files.

Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/ExclusiveReadLockStrategy.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileRenameExclusiveReadLockStrategy.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/ExclusiveReadLockStrategy.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/ExclusiveReadLockStrategy.java?rev=727997&r1=727996&r2=727997&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/ExclusiveReadLockStrategy.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/ExclusiveReadLockStrategy.java Fri Dec 19 02:40:17 2008
@@ -17,7 +17,6 @@
 package org.apache.camel.component.file;
 
 import java.io.File;
-import java.io.IOException;
 
 /**
  * Strategy for acquiring exclusive read locks for files to be consumed.
@@ -37,9 +36,9 @@
      * Acquires exclusive read lock to the file.
      *
      * @param file the file
-     * @return true if read lock was acquired
-     * @throws IOException can be thrown
+     * @return <tt>true</tt> if read lock was acquired.
+     *                       If <tt>false</tt> Camel will skip the file and try it on the next poll
      */
-    boolean acquireExclusiveReadLock(File file) throws IOException;
+    boolean acquireExclusiveReadLock(File file);
 
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java?rev=727997&r1=727996&r2=727997&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java Fri Dec 19 02:40:17 2008
@@ -22,6 +22,7 @@
 import java.nio.channels.FileChannel;
 import java.nio.channels.FileLock;
 
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.component.file.ExclusiveReadLockStrategy;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.commons.logging.Log;
@@ -35,18 +36,20 @@
 public class FileLockExclusiveReadLockStrategy implements ExclusiveReadLockStrategy {
     private static final transient Log LOG = LogFactory.getLog(FileLockExclusiveReadLockStrategy.class);
     private long timeout;
-
-    public boolean acquireExclusiveReadLock(File file) throws IOException {
+    
+    public boolean acquireExclusiveReadLock(File file) {
         if (LOG.isTraceEnabled()) {
             LOG.trace("Waiting for exclusive read lock to file: " + file);
         }
 
-        // try to acquire rw lock on the file before we can consume it
-        FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
-
-        long start = System.currentTimeMillis();
-        boolean exclusive = false;
+        FileChannel channel = null;
         try {
+            // try to acquire rw lock on the file before we can consume it
+            channel = new RandomAccessFile(file, "rw").getChannel();
+    
+            long start = System.currentTimeMillis();
+            boolean exclusive = false;
+
             while (!exclusive) {
                 // timeout check
                 if (timeout > 0) {
@@ -68,14 +71,20 @@
                     lock.release();
                     exclusive = true;
                 } else {
-                    LOG.trace("Exclusive read lock not granted. Sleeping for 1000 millis.");
-                    try {
-                        Thread.sleep(1000);
-                    } catch (InterruptedException e) {
-                        // ignore
-                    }
+                    sleep();
                 }
             }
+        } catch (IOException e) {
+            // must handle IOException as some apps on Windows etc. will still somehow hold a lock to a file
+            // such as AntiVirus or MS Office that has special locks for it's supported files
+            if (timeout == 0) {
+                // if not using timeout, then we cant retry, so rethrow
+                throw new RuntimeCamelException(e);
+            }
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Can not acquire read lock. Will try again.", e);
+            }
+            sleep();
         } finally {
             // must close channel
             ObjectHelper.close(channel, "while acquiring exclusive read lock for file: " + file, LOG);
@@ -83,6 +92,15 @@
 
         return true;
     }
+    
+    private void sleep() {
+        LOG.trace("Exclusive read lock not granted. Sleeping for 1000 millis.");
+        try {
+            Thread.sleep(1000);
+        } catch (InterruptedException e) {
+            // ignore
+        }
+    }
 
     public long getTimeout() {
         return timeout;

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileRenameExclusiveReadLockStrategy.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileRenameExclusiveReadLockStrategy.java?rev=727997&r1=727996&r2=727997&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileRenameExclusiveReadLockStrategy.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileRenameExclusiveReadLockStrategy.java Fri Dec 19 02:40:17 2008
@@ -17,8 +17,8 @@
 package org.apache.camel.component.file.strategy;
 
 import java.io.File;
-import java.io.IOException;
 
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.component.file.ExclusiveReadLockStrategy;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -32,7 +32,7 @@
     private static final transient Log LOG = LogFactory.getLog(FileRenameExclusiveReadLockStrategy.class);
     private long timeout;
 
-    public boolean acquireExclusiveReadLock(File file) throws IOException {
+    public boolean acquireExclusiveReadLock(File file) {
         if (LOG.isTraceEnabled()) {
             LOG.trace("Waiting for exclusive read lock to file: " + file);
         }
@@ -64,18 +64,22 @@
                 // rename it back so we can read it
                 tempFile.renameTo(file);
             } else {
-                LOG.trace("Exclusive read lock not granted. Sleeping for 1000 millis.");
-                try {
-                    Thread.sleep(1000);
-                } catch (InterruptedException e) {
-                    // ignore
-                }
+                sleep();
             }
         }
-
+    
         return true;
     }
 
+    private void sleep() {
+        LOG.trace("Exclusive read lock not granted. Sleeping for 1000 millis.");
+        try {
+            Thread.sleep(1000);
+        } catch (InterruptedException e) {
+            // ignore
+        }
+    }
+
     public long getTimeout() {
         return timeout;
     }