use of org.apache.camel.component.file.GenericFile in project camel by apache.
the class FileLockExclusiveReadLockStrategy method acquireExclusiveReadLock.
@Override
public boolean acquireExclusiveReadLock(GenericFileOperations<File> operations, GenericFile<File> file, Exchange exchange) throws Exception {
// must call super
if (!super.acquireExclusiveReadLock(operations, file, exchange)) {
return false;
}
File target = new File(file.getAbsoluteFilePath());
LOG.trace("Waiting for exclusive read lock to file: {}", target);
FileChannel channel = null;
RandomAccessFile randomAccessFile = null;
boolean exclusive = false;
FileLock lock = null;
try {
randomAccessFile = new RandomAccessFile(target, "rw");
// try to acquire rw lock on the file before we can consume it
channel = randomAccessFile.getChannel();
StopWatch watch = new StopWatch();
while (!exclusive) {
// timeout check
if (timeout > 0) {
long delta = watch.taken();
if (delta > timeout) {
CamelLogger.log(LOG, readLockLoggingLevel, "Cannot acquire read lock within " + timeout + " millis. Will skip the file: " + target);
// we could not get the lock within the timeout period, so return false
return false;
}
}
// get the lock using either try lock or not depending on if we are using timeout or not
try {
lock = timeout > 0 ? channel.tryLock() : channel.lock();
} catch (IllegalStateException ex) {
// Also catch the OverlappingFileLockException here. Do nothing here
}
if (lock != null) {
LOG.trace("Acquired exclusive read lock: {} to file: {}", lock, target);
exclusive = true;
} else {
boolean interrupted = sleep();
if (interrupted) {
// we were interrupted while sleeping, we are likely being shutdown so return false
return false;
}
}
}
} catch (IOException e) {
// 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 return false
return false;
}
LOG.debug("Cannot acquire read lock. Will try again.", e);
boolean interrupted = sleep();
if (interrupted) {
// we were interrupted while sleeping, we are likely being shutdown so return false
return false;
}
} finally {
// close channels if we did not grab the lock
if (!exclusive) {
IOHelper.close(channel, "while acquiring exclusive read lock for file: " + target, LOG);
IOHelper.close(randomAccessFile, "while acquiring exclusive read lock for file: " + target, LOG);
// and also must release super lock
super.releaseExclusiveReadLockOnAbort(operations, file, exchange);
}
}
// store read-lock state
exchange.setProperty(asReadLockKey(file, Exchange.FILE_LOCK_EXCLUSIVE_LOCK), lock);
exchange.setProperty(asReadLockKey(file, Exchange.FILE_LOCK_RANDOM_ACCESS_FILE), randomAccessFile);
// we grabbed the lock
return true;
}
use of org.apache.camel.component.file.GenericFile in project camel by apache.
the class FileChangedExclusiveReadLockStrategy method acquireExclusiveReadLock.
@Override
public boolean acquireExclusiveReadLock(GenericFileOperations<File> operations, GenericFile<File> file, Exchange exchange) throws Exception {
// must call super
if (!super.acquireExclusiveReadLock(operations, file, exchange)) {
return false;
}
File target = new File(file.getAbsoluteFilePath());
boolean exclusive = false;
LOG.trace("Waiting for exclusive read lock to file: {}", file);
long lastModified = Long.MIN_VALUE;
long length = Long.MIN_VALUE;
StopWatch watch = new StopWatch();
long startTime = (new Date()).getTime();
while (!exclusive) {
// timeout check
if (timeout > 0) {
long delta = watch.taken();
if (delta > timeout) {
CamelLogger.log(LOG, readLockLoggingLevel, "Cannot acquire read lock within " + timeout + " millis. Will skip the file: " + file);
// we could not get the lock within the timeout period, so return false
return false;
}
}
long newLastModified = target.lastModified();
long newLength = target.length();
long newOlderThan = startTime + watch.taken() - minAge;
LOG.trace("Previous last modified: {}, new last modified: {}", lastModified, newLastModified);
LOG.trace("Previous length: {}, new length: {}", length, newLength);
LOG.trace("New older than threshold: {}", newOlderThan);
if (newLength >= minLength && ((minAge == 0 && newLastModified == lastModified && newLength == length) || (minAge != 0 && newLastModified < newOlderThan))) {
LOG.trace("Read lock acquired.");
exclusive = true;
} else {
// set new base file change information
lastModified = newLastModified;
length = newLength;
boolean interrupted = sleep();
if (interrupted) {
// we were interrupted while sleeping, we are likely being shutdown so return false
return false;
}
}
}
return exclusive;
}
use of org.apache.camel.component.file.GenericFile in project camel by apache.
the class FileIdempotentChangedRepositoryReadLockStrategy method acquireExclusiveReadLock.
@Override
public boolean acquireExclusiveReadLock(GenericFileOperations<File> operations, GenericFile<File> file, Exchange exchange) throws Exception {
// in clustered mode then another node may have processed the file so we must check here again if the file exists
File path = file.getFile();
if (!path.exists()) {
return false;
}
// check if we can begin on this file
String key = asKey(file);
boolean answer = idempotentRepository.add(key);
if (!answer) {
// another node is processing the file so skip
CamelLogger.log(LOG, readLockLoggingLevel, "Cannot acquire read lock. Will skip the file: " + file);
}
if (answer) {
// if we acquired during idempotent then check changed also
answer = changed.acquireExclusiveReadLock(operations, file, exchange);
if (!answer) {
// remove from idempontent as we did not acquire it from changed
idempotentRepository.remove(key);
}
}
return answer;
}
use of org.apache.camel.component.file.GenericFile in project camel by apache.
the class FileIdempotentRenameRepositoryReadLockStrategy method acquireExclusiveReadLock.
@Override
public boolean acquireExclusiveReadLock(GenericFileOperations<File> operations, GenericFile<File> file, Exchange exchange) throws Exception {
// in clustered mode then another node may have processed the file so we must check here again if the file exists
File path = file.getFile();
if (!path.exists()) {
return false;
}
// check if we can begin on this file
String key = asKey(file);
boolean answer = idempotentRepository.add(key);
if (!answer) {
// another node is processing the file so skip
CamelLogger.log(LOG, readLockLoggingLevel, "Cannot acquire read lock. Will skip the file: " + file);
}
if (answer) {
// if we acquired during idempotent then check rename also
answer = rename.acquireExclusiveReadLock(operations, file, exchange);
if (!answer) {
// remove from idempontent as we did not acquire it from changed
idempotentRepository.remove(key);
}
}
return answer;
}
use of org.apache.camel.component.file.GenericFile in project camel by apache.
the class FileIdempotentRepositoryReadLockStrategy method acquireExclusiveReadLock.
@Override
public boolean acquireExclusiveReadLock(GenericFileOperations<File> operations, GenericFile<File> file, Exchange exchange) throws Exception {
// in clustered mode then another node may have processed the file so we must check here again if the file exists
File path = file.getFile();
if (!path.exists()) {
return false;
}
// check if we can begin on this file
String key = asKey(file);
boolean answer = idempotentRepository.add(key);
if (!answer) {
// another node is processing the file so skip
CamelLogger.log(LOG, readLockLoggingLevel, "Cannot acquire read lock. Will skip the file: " + file);
}
return answer;
}
Aggregations