Elexis API documentation
Version 2.1.6 as of December 11 2011

ch.rgw.io
Class LockFile

java.lang.Object
  extended by ch.rgw.io.LockFile

public class LockFile
extends java.lang.Object

A class that creates and detects lockfile to provide cooperative detection od running instances of an applicatiion (or for other purposes)

Author:
gerry

Constructor Summary
LockFile(java.io.File dir, java.lang.String basename, int maxNum, int timeoutSeconds)
          Create an instance of a LockFile class.
 
Method Summary
 boolean existsLock()
          Check if at least one lockfile with the given pattern exists
 int lock()
          create a lockfile with the parameters as given to the constructor.
 boolean updateLock(int n)
          Refresh the lock i.e. extend its validity time by the original timeout once again.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LockFile

public LockFile(java.io.File dir,
                java.lang.String basename,
                int maxNum,
                int timeoutSeconds)
Create an instance of a LockFile class. This will not yet create an actual file. A call to LockFile#lock() will create a file in the given dir and with the given name and a suffix ranging from 1 to maxNum.

Parameters:
dir - the dir where the lockfile(s) should be created
basename - the basename for the files
maxNum - maximum number of instances of this lock that may be aquired simultaneously
timeoutSeconds - after what time will a lock treaded as invalid
Method Detail

lock

public int lock()
         throws java.io.IOException
create a lockfile with the parameters as given to the constructor. There will be created at most maxNUm lockfiles. Each of them will expire after timeoutSeconds, or after the application exits. If the application does not exist normally, the lockfile might not be deleted. It will the be considered valid until the expire time is reached.

Returns:
lock number on success, 0 if there are already maxNum lockfiles and the lock could not be aquired
Throws:
java.io.IOException - id something went wrong

updateLock

public boolean updateLock(int n)
Refresh the lock i.e. extend its validity time by the original timeout once again. An application should prefer this update mechanism over a too long timeout, because in case of abnormal termination, the lock does not stay too long active.

Parameters:
n - number of the lockfile (as received by the lock() call)
Returns:
true on success

existsLock

public boolean existsLock()
                   throws java.io.IOException
Check if at least one lockfile with the given pattern exists

Returns:
true if one ore more lockfiles exist
Throws:
java.io.IOException

Elexis API documentation
Version 2.1.6 as of December 11 2011

Copyright 2005-2011 by Gerry Weirich, Elexis