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 2011/01/11 08:24:12 UTC
svn commit: r1057505 - in
/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file: ./
strategy/
Author: davsclaus
Date: Tue Jan 11 07:24:11 2011
New Revision: 1057505
URL: http://svn.apache.org/viewvc?rev=1057505&view=rev
Log:
CAMEL-3520: Added readLockCheckInterval option to file/ftp consumer. Thanks to Pham Ngoc Hai for the patch.
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileExclusiveReadLockStrategy.java
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileChangedExclusiveReadLockStrategy.java
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileProcessStrategyFactory.java
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileRenameExclusiveReadLockStrategy.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/component/file/GenericFileEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java?rev=1057505&r1=1057504&r2=1057505&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java Tue Jan 11 07:24:11 2011
@@ -84,6 +84,7 @@ public abstract class GenericFileEndpoin
protected Comparator<GenericFile<T>> sorter;
protected Comparator<Exchange> sortBy;
protected String readLock = "none";
+ protected long readLockCheckInterval = 1000;
protected long readLockTimeout = 10000;
protected GenericFileExclusiveReadLockStrategy<T> exclusiveReadLockStrategy;
protected boolean keepLastModified;
@@ -107,11 +108,11 @@ public abstract class GenericFileEndpoin
public abstract Exchange createExchange(GenericFile<T> file);
public abstract String getScheme();
-
+
public abstract char getFileSeparator();
-
+
public abstract boolean isAbsolute(String name);
-
+
/**
* Return the file name that will be auto-generated for the given message if
* none is provided
@@ -296,11 +297,11 @@ public abstract class GenericFileEndpoin
public Boolean isIdempotent() {
return idempotent != null ? idempotent : false;
}
-
+
public String getCharset() {
return charset;
}
-
+
public void setCharset(String charset) {
IOConverter.validateCharset(charset);
this.charset = charset;
@@ -415,6 +416,14 @@ public abstract class GenericFileEndpoin
this.readLock = readLock;
}
+ public long getReadLockCheckInterval() {
+ return readLockCheckInterval;
+ }
+
+ public void setReadLockCheckInterval(long readLockCheckInterval) {
+ this.readLockCheckInterval = readLockCheckInterval;
+ }
+
public long getReadLockTimeout() {
return readLockTimeout;
}
@@ -530,9 +539,10 @@ public abstract class GenericFileEndpoin
message.setHeader(Exchange.FILE_NAME, name);
}
}
-
+
/**
* Set up the exchange properties with the options of the file endpoint
+ *
* @param exchange
*/
public void configureExchange(Exchange exchange) {
@@ -545,6 +555,7 @@ public abstract class GenericFileEndpoin
/**
* Strategy to configure the move or premove option based on a String input.
* <p/>
+ *
* @param expression the original string input
* @return configured string or the original if no modifications is needed
*/
@@ -597,6 +608,9 @@ public abstract class GenericFileEndpoin
if (readLock != null) {
params.put("readLock", readLock);
}
+ if (readLockCheckInterval > 0) {
+ params.put("readLockCheckInterval", readLockCheckInterval);
+ }
if (readLockTimeout > 0) {
params.put("readLockTimeout", readLockTimeout);
}
@@ -620,7 +634,7 @@ public abstract class GenericFileEndpoin
* <p/>
* This method should only be invoked if a done filename property has been set on this endpoint.
*
- * @param fileName the file name
+ * @param fileName the file name
* @return name of the associated done file name
*/
protected String createDoneFileName(String fileName) {
@@ -660,7 +674,7 @@ public abstract class GenericFileEndpoin
* <p/>
* This method should only be invoked if a done filename property has been set on this endpoint.
*
- * @param fileName the file name
+ * @param fileName the file name
* @return <tt>true</tt> if its a done file, <tt>false</tt> otherwise
*/
protected boolean isDoneFile(String fileName) {
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileExclusiveReadLockStrategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileExclusiveReadLockStrategy.java?rev=1057505&r1=1057504&r2=1057505&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileExclusiveReadLockStrategy.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileExclusiveReadLockStrategy.java Tue Jan 11 07:24:11 2011
@@ -26,10 +26,10 @@ import org.apache.camel.Exchange;
* <p/>
* Camel supports out of the box the following strategies:
* <ul>
- * <li>FileRenameExclusiveReadLockStrategy waiting until its possible to rename the file.</li>
- * <li>FileLockExclusiveReadLockStrategy acquiring a RW file lock for the duration of the processing.</li>
- * <li>MarkerFileExclusiveReadLockStrategy using a marker file for acquiring read lock.</li>
- * <li>FileChangedExclusiveReadLockStrategy using a file changed detection for acquiring read lock.</li>
+ * <li>FileRenameExclusiveReadLockStrategy waiting until its possible to rename the file.</li>
+ * <li>FileLockExclusiveReadLockStrategy acquiring a RW file lock for the duration of the processing.</li>
+ * <li>MarkerFileExclusiveReadLockStrategy using a marker file for acquiring read lock.</li>
+ * <li>FileChangedExclusiveReadLockStrategy using a file changed detection for acquiring read lock.</li>
* </ul>
*/
public interface GenericFileExclusiveReadLockStrategy<T> {
@@ -38,7 +38,7 @@ public interface GenericFileExclusiveRea
* Allows custom logic to be run on startup preparing the strategy, such as removing old lock files etc.
*
* @param operations generic file operations
- * @param endpoint the endpoint
+ * @param endpoint the endpoint
* @throws Exception can be thrown in case of errors
*/
void prepareOnStartup(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint) throws Exception;
@@ -75,4 +75,18 @@ public interface GenericFileExclusiveRea
*/
void setTimeout(long timeout);
+
+ /**
+ * Sets the check interval period.
+ * <p/>
+ * The check interval is used for sleeping between attempts to acquire read lock.
+ * Setting a high value allows to cater for <i>slow writes</i> in case the producer
+ * of the file is slow.
+ * <p/>
+ * The default period is 1000 millis.
+ *
+ * @param checkInterval interval in millis
+ */
+ void setCheckInterval(long checkInterval);
+
}
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileChangedExclusiveReadLockStrategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileChangedExclusiveReadLockStrategy.java?rev=1057505&r1=1057504&r2=1057505&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileChangedExclusiveReadLockStrategy.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileChangedExclusiveReadLockStrategy.java Tue Jan 11 07:24:11 2011
@@ -34,6 +34,7 @@ import org.apache.commons.logging.LogFac
public class FileChangedExclusiveReadLockStrategy extends MarkerFileExclusiveReadLockStrategy {
private static final transient Log LOG = LogFactory.getLog(FileChangedExclusiveReadLockStrategy.class);
private long timeout;
+ private long checkInterval = 1000;
@Override
public void prepareOnStartup(GenericFileOperations<File> operations, GenericFileEndpoint<File> endpoint) {
@@ -111,10 +112,10 @@ public class FileChangedExclusiveReadLoc
private boolean sleep() {
if (LOG.isTraceEnabled()) {
- LOG.trace("Exclusive read lock not granted. Sleeping for 1000 millis.");
+ LOG.trace("Exclusive read lock not granted. Sleeping for " + checkInterval + " millis.");
}
try {
- Thread.sleep(1000);
+ Thread.sleep(checkInterval);
return false;
} catch (InterruptedException e) {
if (LOG.isDebugEnabled()) {
@@ -132,4 +133,12 @@ public class FileChangedExclusiveReadLoc
this.timeout = timeout;
}
+ public long getCheckInterval() {
+ return checkInterval;
+ }
+
+ public void setCheckInterval(long checkInterval) {
+ this.checkInterval = checkInterval;
+ }
+
}
\ No newline at end of file
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java?rev=1057505&r1=1057504&r2=1057505&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java Tue Jan 11 07:24:11 2011
@@ -41,6 +41,7 @@ import org.apache.commons.logging.LogFac
public class FileLockExclusiveReadLockStrategy implements GenericFileExclusiveReadLockStrategy<File> {
private static final transient Log LOG = LogFactory.getLog(FileLockExclusiveReadLockStrategy.class);
private long timeout;
+ private long checkInterval = 1000;
private FileLock lock;
private String lockFileName;
@@ -128,10 +129,10 @@ public class FileLockExclusiveReadLockSt
private boolean sleep() {
if (LOG.isTraceEnabled()) {
- LOG.trace("Exclusive read lock not granted. Sleeping for 1000 millis.");
+ LOG.trace("Exclusive read lock not granted. Sleeping for " + checkInterval + " millis.");
}
try {
- Thread.sleep(1000);
+ Thread.sleep(checkInterval);
return false;
} catch (InterruptedException e) {
if (LOG.isDebugEnabled()) {
@@ -145,16 +146,12 @@ public class FileLockExclusiveReadLockSt
return timeout;
}
- /**
- * Sets an optional timeout period.
- * <p/>
- * If the readlock could not be granted within the time period then the wait is stopped and the
- * acquireReadLock returns <tt>false</tt>.
- *
- * @param timeout period in millis
- */
public void setTimeout(long timeout) {
this.timeout = timeout;
}
+ public void setCheckInterval(long checkInterval) {
+ this.checkInterval = checkInterval;
+ }
+
}
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileProcessStrategyFactory.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileProcessStrategyFactory.java?rev=1057505&r1=1057504&r2=1057505&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileProcessStrategyFactory.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileProcessStrategyFactory.java Tue Jan 11 07:24:11 2011
@@ -115,6 +115,10 @@ public final class FileProcessStrategyFa
if (timeout != null) {
readLockStrategy.setTimeout(timeout);
}
+ Long checkInterval = (Long) params.get("readLockCheckInterval");
+ if (checkInterval != null) {
+ readLockStrategy.setCheckInterval(checkInterval);
+ }
return readLockStrategy;
} else if ("rename".equals(readLock)) {
GenericFileExclusiveReadLockStrategy<File> readLockStrategy = new GenericFileRenameExclusiveReadLockStrategy<File>();
@@ -122,6 +126,10 @@ public final class FileProcessStrategyFa
if (timeout != null) {
readLockStrategy.setTimeout(timeout);
}
+ Long checkInterval = (Long) params.get("readLockCheckInterval");
+ if (checkInterval != null) {
+ readLockStrategy.setCheckInterval(checkInterval);
+ }
return readLockStrategy;
} else if ("changed".equals(readLock)) {
GenericFileExclusiveReadLockStrategy readLockStrategy = new FileChangedExclusiveReadLockStrategy();
@@ -129,6 +137,10 @@ public final class FileProcessStrategyFa
if (timeout != null) {
readLockStrategy.setTimeout(timeout);
}
+ Long checkInterval = (Long) params.get("readLockCheckInterval");
+ if (checkInterval != null) {
+ readLockStrategy.setCheckInterval(checkInterval);
+ }
return readLockStrategy;
} else if ("markerFile".equals(readLock)) {
return new MarkerFileExclusiveReadLockStrategy();
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileRenameExclusiveReadLockStrategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileRenameExclusiveReadLockStrategy.java?rev=1057505&r1=1057504&r2=1057505&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileRenameExclusiveReadLockStrategy.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileRenameExclusiveReadLockStrategy.java Tue Jan 11 07:24:11 2011
@@ -33,6 +33,7 @@ import org.apache.commons.logging.LogFac
public class GenericFileRenameExclusiveReadLockStrategy<T> implements GenericFileExclusiveReadLockStrategy<T> {
private static final transient Log LOG = LogFactory.getLog(GenericFileRenameExclusiveReadLockStrategy.class);
private long timeout;
+ private long checkInterval;
public void prepareOnStartup(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint) throws Exception {
// noop
@@ -55,7 +56,7 @@ public class GenericFileRenameExclusiveR
boolean exclusive = false;
while (!exclusive) {
- // timeout check
+ // timeout check
if (timeout > 0) {
long delta = watch.taken();
if (delta > timeout) {
@@ -64,8 +65,8 @@ public class GenericFileRenameExclusiveR
return false;
}
}
-
- exclusive = operations.renameFile(file.getAbsoluteFilePath(), newFile.getAbsoluteFilePath());
+
+ exclusive = operations.renameFile(file.getAbsoluteFilePath(), newFile.getAbsoluteFilePath());
if (exclusive) {
if (LOG.isTraceEnabled()) {
LOG.trace("Acquired exclusive read lock to file: " + file);
@@ -89,14 +90,14 @@ public class GenericFileRenameExclusiveR
// noop
}
- private boolean sleep() {
+ private boolean sleep() {
if (LOG.isTraceEnabled()) {
- LOG.trace("Exclusive read lock not granted. Sleeping for 1000 millis.");
+ LOG.trace("Exclusive read lock not granted. Sleeping for " + checkInterval + " millis.");
}
try {
- Thread.sleep(1000);
+ Thread.sleep(checkInterval);
return false;
- } catch (InterruptedException e) {
+ } catch (InterruptedException e) {
LOG.debug("Sleep interrupted while waiting for exclusive read lock, so breaking out");
return true;
}
@@ -109,4 +110,8 @@ public class GenericFileRenameExclusiveR
public void setTimeout(long timeout) {
this.timeout = timeout;
}
+
+ public void setCheckInterval(long checkInterval) {
+ this.checkInterval = checkInterval;
+ }
}
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=1057505&r1=1057504&r2=1057505&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 Tue Jan 11 07:24:11 2011
@@ -84,6 +84,10 @@ public class MarkerFileExclusiveReadLock
// noop
}
+ public void setCheckInterval(long checkInterval) {
+ // noop
+ }
+
private static void deleteLockFiles(File dir, boolean recursive) {
File[] files = dir.listFiles();
if (files == null || files.length == 0) {