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.
Copied and modified from kiwi's
CatchingRunnable. 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 Runnable that never lets exceptions escape. Useful for things like scheduled executions
using ScheduledExecutorService where an intermittent error should not cause the
executor to suppress future executions (which is the default behavior).
-
Method Summary
Modifier and TypeMethodDescriptiondefault voidhandleExceptionSafely(Exception exception) Handle an exception thrown byrunSafely().name()Returns a name, if available, which can be used to differentiate instances.static CatchingRunnable2Create a new instance wrapping the given Runnable.static CatchingRunnable2Create a new instance wrapping the given Runnable.static CatchingRunnable2Create a new instance with the given name that wraps the given Runnable.static CatchingRunnable2Create a new named-instance wrapping the given Runnable.default voidrun()WrapsrunSafely()in a try/catch.voidThe logic that could throw aRuntimeException.
-
Method Details
-
of
Create a new instance wrapping the given Runnable. -
of
Create a new instance wrapping the given Runnable. Uses the given Consumer to handle unexpected exceptions thrown by the Runnable. -
of
Create a new instance with the given name that wraps the given Runnable. -
of
Create a new named-instance wrapping the given Runnable. Uses the given Consumer to handle unexpected exceptions thrown by the Runnable. -
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
Handle an exception thrown byrunSafely().- Parameters:
exception- theExceptionto handle
-
runSafely
void runSafely()The logic that could throw aRuntimeException.
-