diff --git a/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java b/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java index e229b5f3104c1..37f6897529beb 100644 --- a/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java +++ b/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java @@ -63,9 +63,12 @@ public boolean acquireExclusiveReadLock(GenericFileOperations operations, LOG.trace("Waiting for exclusive read lock to file: {}", target); + FileChannel channel = null; + RandomAccessFile randomAccessFile = null; try { + randomAccessFile = new RandomAccessFile(target, "rw"); // try to acquire rw lock on the file before we can consume it - FileChannel channel = new RandomAccessFile(target, "rw").getChannel(); + channel = randomAccessFile.getChannel(); boolean exclusive = false; StopWatch watch = new StopWatch(); @@ -113,6 +116,24 @@ public boolean acquireExclusiveReadLock(GenericFileOperations operations, // we were interrupted while sleeping, we are likely being shutdown so return false return false; } + } finally { + // Close channel + try { + if (channel != null) { + channel.close(); + } + } catch (IOException ioe) { + LOG.warn("Unable to close channel for file " + file.getAbsoluteFilePath() + "!", ioe); + } + + // Close file + try { + if (randomAccessFile != null) { + randomAccessFile.close(); + } + } catch (IOException ioe) { + LOG.warn("Unable to close file " + file.getAbsoluteFilePath() + "!", ioe); + } } return true;