I've written a Java class that implements a "lock file" to prevent a period job from running more than once concurrently. It's based upon java.nio.channels.FileChannel.tryLock
and works quite well.
My class allows client code to supply a timeout value indicating how long it's willing to wait for the lock file to become available, and if a timeout occurs, I'm throwing an IOException
. This also works quite well.
But I'm wondering if there is a better exception type to be using, since IOException
is fairly generic. Client code catching an IOException
wouldn't be able to know if the problem was due to e.g. the timeout itself or some other issue with the filesystem, etc. (unless that other issue throws a subclass of IOException
of course).
Briefly leafing through the Java API, I see some candidates but I don't really like any of them for various reasons:
java.util.concurrent.TimeoutException
(this isn't really a "concurrent" usage)java.nio.channels.FileLockInterruptionException
(docs indicate a very specific reason for this exception; doesn't match my case)java.nio.channels.InterruptedByTimeoutException
(similar reasons to above)
Any suggestions?
I'd prefer something that is available back to Java 7 if possible.
EDIT
Obviously, a custom exception class is possible, but I was wondering if there was something in the standard API that would be appropriate.