import java.nio.channels.ClosedChannelException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* @author Olivier Lamy
private int timeout = 0;
-
@Override
public Lock readFileLock( File file )
throws FileLockException, FileLockTimeoutException
}
}
- Lock current = lockFiles.get( file );
-
try
{
-
+ Lock current = lockFiles.get( file );
if ( current != null )
{
log.debug( "write lock file exist continue wait" );
continue;
}
- lock = new Lock( file, true );
- createNewFileQuietly( file );
- lock.openLock( true, timeout > 0 );
+ lock = new Lock(file, true);
+ createNewFileQuietly(file);
+ lock.openLock(true, timeout > 0);
acquired = true;
}
catch ( FileNotFoundException e )
}
catch ( IOException e )
{
+ if (lock!=null && lock.isValid()) {
+ try {
+ lock.close();
+ } catch (IOException ex) {
+ // Ignore
+ }
+ }
throw new FileLockException( e.getMessage(), e );
}
- catch ( IllegalStateException e )
+ catch ( Throwable e )
{
+ if (lock!=null && lock.isValid()) {
+ try {
+ lock.close();
+ } catch (IOException ex) {
+ // Ignore
+ } finally {
+ lock = null;
+ }
+ }
log.debug( "openLock {}:{}", e.getClass(), e.getMessage() );
}
+
}
Lock current = lockFiles.putIfAbsent( file, lock );
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
+import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.concurrent.atomic.AtomicInteger;
}
+ // Files.copy is not atomic so have to try several times in
+ // a multithreaded test
+ private void copyFile(Path source, Path destination) {
+ int attempts=10;
+ boolean finished = false;
+ while(!finished && attempts-->0) {
+ try {
+ Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING,
+ StandardCopyOption.COPY_ATTRIBUTES);
+ finished=true;
+ } catch (IOException ex) {
+ //
+ }
+ }
+ }
+
public void thread1()
throws FileLockException, FileLockTimeoutException, IOException
{
try
{
lock.getFile().delete();
- Files.copy( largeJar.toPath(), lock.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING,
- StandardCopyOption.COPY_ATTRIBUTES );
+ copyFile( largeJar.toPath(), lock.getFile().toPath());
}
finally
{
try
{
lock.getFile().delete();
- Files.copy( largeJar.toPath(), lock.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING,
- StandardCopyOption.COPY_ATTRIBUTES );
+ copyFile( largeJar.toPath(), lock.getFile().toPath());
}
finally
{
try
{
lock.getFile().delete();
- Files.copy( largeJar.toPath(), lock.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING,
- StandardCopyOption.COPY_ATTRIBUTES );
+ copyFile( largeJar.toPath(), lock.getFile().toPath());
}
finally
{
try
{
lock.getFile().delete();
- Files.copy( largeJar.toPath(), lock.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING,
- StandardCopyOption.COPY_ATTRIBUTES );
+ copyFile( largeJar.toPath(), lock.getFile().toPath());
}
finally
{
try
{
lock.getFile().delete();
- Files.copy( largeJar.toPath(), lock.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING,
- StandardCopyOption.COPY_ATTRIBUTES );
+ copyFile( largeJar.toPath(), lock.getFile().toPath());
}
finally
{
try
{
lock.getFile().delete();
- Files.copy( largeJar.toPath(), lock.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING,
- StandardCopyOption.COPY_ATTRIBUTES );
+ copyFile( largeJar.toPath(), lock.getFile().toPath());
}
finally
{
{
ConcurrentFileWrite concurrentFileWrite = new ConcurrentFileWrite( fileLockManager );
//concurrentFileWrite.setTrace( true );
- TestFramework.runOnce( concurrentFileWrite );
+ TestFramework.runManyTimes( concurrentFileWrite, 10);
logger.info( "success: {}", concurrentFileWrite.success );
- Assert.assertEquals( 10, concurrentFileWrite.success.intValue() );
+ Assert.assertEquals( 100, concurrentFileWrite.success.intValue() );
}