I have a ScheduledExecutorService
, which invokes a Runnable periodically via scheduleWithFixedDelay()
(could have used scheduleAtFixedRate()
instead).
Am now considering what to do if an error occurs. If it's something that can't easily be recovered from(*) I'd like the option of stopping all further invocations but not sure of the best way of doing this.
Apparently checked exceptions can't be thrown from a Runnable so would appreciate any guidance on how to choose from the following:
scheduledFuture.cancel(false);
...or...
scheduledFuture.cancel(true);
...or...
scheduledExecutorService.shutdown();
...or...
scheduledExecutorService.shutdownNow();
...or...
Throw a custom RuntimeException myself?
...or...
Something else?
(*) Would like to know the general case but in case anyone's interested, the checked exception I'm currently looking at is a ParserConfigurationException
thrown from DocumentBuilderFactory.newDocumentBuilder()
. If this is thrown, it indicates a serious problem so I'd basically like the scheduling to completely stop rather than potentially repeating the error every time.