Skip to content

Instantly share code, notes, and snippets.

@holyjak
Created September 20, 2013 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holyjak/6634159 to your computer and use it in GitHub Desktop.
Save holyjak/6634159 to your computer and use it in GitHub Desktop.
Wonders of Code - Simplicity vs. Robustness In Lock File Handling (http://wp.me/p3ui6n-1k) - the (overly) simple design
public class SimpleSingletonBatchJob {
private static boolean getLock() {
File file = new File(LOCK_DIRECTORY+File.separatorChar+Configuration.getGroupPrefix());
try {
return file.createNewFile();
} catch (IOException e) {
return false;
}
}
private static void releaseLock() {
File file = new File(LOCK_DIRECTORY+File.separatorChar+Configuration.getGroupPrefix());
file.delete();
}
public static void exit(int nr) {
releaseLock();
System.exit(nr);
}
public static void main(String[] args) throws IOException {
...
if (! getLock()) { // #1 try to create lock
System.out.println("Already running");
return;
}
... // do the job (may throw exceptions)
releaseLock(); // #2 release lock when done
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment