Package org.kiwiproject.beta.base
Interface CatchingRunnable2
-
- All Superinterfaces:
Runnable
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@Beta @FunctionalInterface public interface CatchingRunnable2 extends Runnable
Copied and modified from kiwi'sCatchingRunnable. The changes include:- The ability to provide a name, which is used when reporting unexpected exceptions
- Factory methods to easily create an instance with or without a name and exception handler
Original documentation from kiwi:
Extension of
Runnablethat never lets exceptions escape. Useful for things like scheduled executions usingScheduledExecutorServicewhere an intermittent error should not cause the executor to suppress future executions (which is the default behavior).
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default voidhandleExceptionSafely(Exception exception)Handle an exception thrown byrunSafely().default Optional<String>name()Returns a name, if available, which can be used to differentiate instances.static CatchingRunnable2of(Runnable runnable)Create a new instance wrapping the given Runnable.static CatchingRunnable2of(Runnable runnable, Consumer<Exception> exceptionHandler)Create a new instance wrapping the given Runnable.static CatchingRunnable2of(String name, Runnable runnable)Create a new instance with the given name that wraps the given Runnable.static CatchingRunnable2of(String name, Runnable runnable, Consumer<Exception> exceptionHandler)Create a new named instance wrapping the given Runnable.default voidrun()WrapsrunSafely()in a try/catch.voidrunSafely()The logic that could throw aRuntimeException.
-
-
-
Method Detail
-
of
static CatchingRunnable2 of(Runnable runnable)
Create a new instance wrapping the given Runnable.
-
of
static CatchingRunnable2 of(Runnable runnable, Consumer<Exception> exceptionHandler)
Create a new instance wrapping the given Runnable. Uses the given Consumer to handle unexpected exceptions thrown by the Runnable.
-
of
static CatchingRunnable2 of(String name, Runnable runnable)
Create a new instance with the given name that wraps the given Runnable.
-
of
static CatchingRunnable2 of(String name, Runnable runnable, Consumer<Exception> exceptionHandler)
Create a new named instance wrapping the given Runnable. Uses the given Consumer to handle unexpected exceptions thrown by the Runnable.
-
name
default Optional<String> name()
Returns a name, if available, which can be used to differentiate instances. The name is used when logging exceptions thrown by the Runnable or by the exception handler defined inhandleExceptionSafely(java.lang.Exception).For example, if an application uses multiple scheduled tasks to perform background actions and wraps those tasks with
CatchingRunnable2, then providing a name can provide context when logging exceptions.- Returns:
- an Optional with the name of this instance
-
run
default void run()
WrapsrunSafely()in a try/catch. Logs exceptions and will callhandleExceptionSafely(Exception)to permit handling of any thrown exceptions.
-
handleExceptionSafely
default void handleExceptionSafely(Exception exception)
Handle an exception thrown byrunSafely().- Parameters:
exception- theExceptionto handle
-
runSafely
void runSafely()
The logic that could throw aRuntimeException.
-
-