Wednesday, 25 November 2009

Preventing multiple instances of an application from running.

This is a common question, with a very common answer - create a machine wide semaphore. The most common recommendation in Java is to use a socket. However this answer has a flaw. In a multi-user environment you do not want a machine-wide, you want a user-wide semaphore. I have seen some very complex solutions to this issue but there is a very simple answer. Use a file lock.
The common reason against using files is that they are not automatically cleaned when the application is forcible terminated (for example hitting the big red button). This is true if you use the existence of a file to determine if a previous instance is running, but there is a better option than this. Use the FileLock for a file in the users home directory. FileLocks are automatically cleaned by the OS should the application die without unlocking and can be placed in a location that will be local to the user allowing this solution to work on multi-user environment such as Windows Terminal Server.


import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.util.logging.Level;
import java.util.logging.Logger;

/** Prevents two instances of the application from starting at the same time.
 *
 * @author Mlk
 *
 */
public final class InstanceChecker {
    /** A singleton instance. */
    public static final InstanceChecker INSTANCE = new InstanceChecker();
    /** The file channel to be locked. */
    private FileChannel channel;
    /** The lock on the file. */
    private FileLock lock;
    /** The file to be locked. */
    private final File file = new File(new File(System.getProperty("user.home")), "/APPLICATION_NAME.lock");
    /** Logger. */
    private Logger log = Logger.getLogger(getClass().getName());

   
   
    /** ctor. */
    private InstanceChecker() {    }

    /** Is this the only instance of this application currently executing.
     *
     * @return should the application be allowed to start up.
     */
    public boolean onlyInstance() {
       
        try {
            channel = new RandomAccessFile(file, "rw").getChannel();
           
            try {
                lock = channel.tryLock();
            } catch (OverlappingFileLockException e) {
                return false;
            }
            if (lock == null) {
                return false;
            }
           
            Runtime.getRuntime().addShutdownHook(new Thread() {
                public void run() {
                    close();
                }
            });
        } catch (IOException e) {
            log.log(Level.WARNING, "Failed to lock file: " + file, e);
            return false;
        }
        return true;
    }

    /** Unlocks the file. */
    private void close() {
        try {
            if (lock != null) {
                lock.release();
            }
            if (channel != null) {
                channel.close();
            }
           
            file.delete();
        } catch (final IOException e) {
            System.err.println("Failed to unlock file!");
        }
    }
}

And you use it:
public static void main(String argv...) {
     if( !InstanceChecker.INSTANCE.onlyInstance()) {
     // Die!
  }
}

It currently lacks the inter application communication, but I think that would be easily added by also having a "ping" file that would be touched during the die section of code.

0 comments: