Search in sources :

Example 6 with GenericFile

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;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) FileLock(java.nio.channels.FileLock) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File) StopWatch(org.apache.camel.util.StopWatch)

Example 7 with GenericFile

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;
}
Also used : GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File) Date(java.util.Date) StopWatch(org.apache.camel.util.StopWatch)

Example 8 with GenericFile

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;
}
Also used : GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File)

Example 9 with GenericFile

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;
}
Also used : GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File)

Example 10 with GenericFile

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;
}
Also used : GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File)

Aggregations

GenericFile (org.apache.camel.component.file.GenericFile)34 File (java.io.File)25 IOException (java.io.IOException)11 InputStream (java.io.InputStream)11 Exchange (org.apache.camel.Exchange)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 GenericFileOperationFailedException (org.apache.camel.component.file.GenericFileOperationFailedException)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 CamelExchangeException (org.apache.camel.CamelExchangeException)4 Message (org.apache.camel.Message)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileOutputStream (java.io.FileOutputStream)3 OutputStream (java.io.OutputStream)3 Serializable (java.io.Serializable)3 FallbackConverter (org.apache.camel.FallbackConverter)3 TypeConverter (org.apache.camel.TypeConverter)3 WrappedFile (org.apache.camel.WrappedFile)3 FileEndpoint (org.apache.camel.component.file.FileEndpoint)3 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)3 Test (org.junit.Test)3