Created
September 20, 2013 06:57
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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