Class Cleanup<EX extends Exception>
- Type Parameters:
EX- The checked exception type for transparent pass-through orRuntimeException. This is the one that will be forwarded when explicitly called. We cannot ensure that all clean-up task provide the very same exception type or a subclass. So all checked exceptions thrown by clean-up tasks incompatible with this type will be wrapped in aUndeclaredThrowableException.
Cleanup aims to complement the functionality of Cleaner and provide some
answers to questions that Cleaner left open. As with Cleaner the main
purpose is to release resources when the objects holding those resources aren't reachable
anymore. This is only meant as a fail-safe mechanism; resources should always be properly
released once they are no longer needed.
Cleanupallows to register multiple clean-up tasks and they can be added either at the start or the end of the queue. This enables subclasses to share the sameCleanupand decide whether their tasks need to be executed before or after those of the superclass(es). Every clean-up tasks is guaranteed to be executed even in case of exceptions. The first encountered exception is passed through, with all others being attached as suppressed.- Transparent pass-through of checked exceptions:
Cleaner.Cleanable.clean()cannot pass through checked exceptions, like the typicalIOException. They would need to be manually wrapped and maybe unwrapped again.Cleanupcan handle this transparently for one checked exception type specified during creation. This allows to use the clean-up for an explicitly called clean-up, e. g.AutoCloseable.close()with normal exception handling. - Exceptions occurring post-mortem (i. e. during implicit clean-up) are logged as they cannot be passed through to a caller.
CleanupTask must fulfil certain requirements aimed at preventing
accidental references to the monitored object. The implementation
- must be
final, - must NOT be a lambda,
- must NOT be an anonymous class,
- and must NOT have any non-static inner classes in its type hierarchy.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA builder forCleanupthat provides some additional options and generally easier usage than complex constructors.static final classA cleanup task for cleaning up a collection of objects being converted and afterwards being closed in case the conversion yields aAutoCloseable.static final classDeprecated, for removal: This API element is subject to removal in a future version. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final CleanerThe defaultCleanerused when no other is specified via theCleanup.Builder.protected static final LoggerThe defaultLoggerused when no other is specified via theCleanup.Builder. -
Method Summary
Modifier and TypeMethodDescriptionaddTask(CleanupTask<? extends Exception> task) Adds the given clean-up task to the end of the list of registered clean-up tasks.addTask(AutoCloseable... autoCloseables) Adds anCleanup.AutoCloseableCleanupTaskfor the designatedAutoCloseables to the end of the list of registered clean-up tasks.addTask(String descriptor, AutoCloseable... autoCloseables) Adds anCleanup.AutoCloseableCleanupTaskfor the designatedAutoCloseables to the end of the list of registered clean-up tasks.addTask(String descriptor, Collection<? extends AutoCloseable> autoCloseables) Adds anCleanup.AutoCloseableCleanupTaskfor the designatedAutoCloseables to the end of the list of registered clean-up tasks.addTask(String descriptor, Supplier<? extends AutoCloseable> autoCloseable) Adds anCleanup.AutoCloseableCleanupTaskfor the designatedAutoCloseables to the end of the list of registered clean-up tasks.addTask(Collection<? extends AutoCloseable> autoCloseables) Adds anCleanup.AutoCloseableCleanupTaskfor the designatedAutoCloseables to the end of the list of registered clean-up tasks.addTask(Supplier<? extends AutoCloseable> autoCloseable) Adds anCleanup.AutoCloseableCleanupTaskfor the designatedAutoCloseableto the end of the list of registered clean-up tasks.addTaskAsFirst(CleanupTask<? extends Exception> task) Adds the given clean-up task to the first position of the list of registered clean-up tasks.addTaskAsFirst(AutoCloseable... autoCloseables) Adds anCleanup.AutoCloseableCleanupTaskfor the designatedAutoCloseables to the first position of the list of registered clean-up tasks.addTaskAsFirst(String descriptor, AutoCloseable... autoCloseables) Adds anCleanup.AutoCloseableCleanupTaskfor the designatedAutoCloseables to the first position of the list of registered clean-up tasks.addTaskAsFirst(String descriptor, Collection<? extends AutoCloseable> autoCloseables) Adds anCleanup.AutoCloseableCleanupTaskfor the designatedAutoCloseables to the first position of the list of registered clean-up tasks.addTaskAsFirst(String descriptor, Supplier<AutoCloseable> autoCloseable) Adds anCleanup.AutoCloseableCleanupTaskfor the designatedAutoCloseables to the first position of the list of registered clean-up tasks.addTaskAsFirst(Collection<? extends AutoCloseable> autoCloseables) Adds anCleanup.AutoCloseableCleanupTaskfor the designatedAutoCloseables to the first position of the list of registered clean-up tasks.addTaskAsFirst(Supplier<AutoCloseable> autoCloseable) Adds anCleanup.AutoCloseableCleanupTaskfor the designatedAutoCloseableto the first position of the list of registered clean-up tasks.static Cleanup.Builderbuilder()Starts building aCleanup.voidclean()Triggers the clean-up and invokes all registered clean-up tasks.static Cleanup<RuntimeException>Creates aCleanupas post-mortem action for the designated object.Creates aCleanupas post-mortem action for the designated object.static Cleanup<RuntimeException>Creates aCleanupas post-mortem action for the designated object with the designated descriptor.Creates aCleanupas post-mortem action for the designated object with the designated descriptor.protected voidrunTasks()Executes all registered clean-up tasks.protected static voidrunTasks(Collection<CleanupTask<? extends Exception>> tasks, boolean explicitlyCleaned, String objectDescriptor, Logger logger) Executes the designated tasks by callingCleanupTask.cleanup(boolean)and collecting the corresponding exceptions.
-
Field Details
-
DEFAULT_CLEANER
The defaultCleanerused when no other is specified via theCleanup.Builder. -
DEFAULT_LOGGER
The defaultLoggerused when no other is specified via theCleanup.Builder.
-
-
Method Details
-
register
Creates aCleanupas post-mortem action for the designated object. This only registers the notification mechanism, actual clean-up tasks need to be registered yet. No exception will be expected by any clean-up task.- Parameters:
object- The object for which to register post-mortem clean-up.- Returns:
- A new
Cleanupregistered for as post-mortem action for the designated object. - See Also:
-
register
Creates aCleanupas post-mortem action for the designated object with the designated descriptor. This only registers the notification mechanism, actual clean-up tasks need to be registered yet. No exception will be expected by any clean-up task.- Parameters:
object- The object for which to register post-mortem clean-up.descriptor- A string that meaningfully describes this clean-up when used for logging etc. ornullto use the class name of the designated object.- Returns:
- A new
Cleanupregistered for as post-mortem action for the designated object. - See Also:
-
register
public static <EX extends Exception> Cleanup<EX> register(Object object, Class<EX> checkedExceptionType) Creates aCleanupas post-mortem action for the designated object. This only registers the notification mechanism, actual clean-up tasks need to be registered yet. There will be transparent pass-through for the provided checked exception type inclean().- Parameters:
object- The object for which to register post-mortem clean-up.checkedExceptionType- The exception that is expected by the clean-up. This will be forwarded when manually triggering clean-up.- Returns:
- A new
Cleanupregistered for as post-mortem action for the designated object. - See Also:
-
register
public static <EX extends Exception> Cleanup<EX> register(Object object, String descriptor, Class<EX> checkedExceptionType) Creates aCleanupas post-mortem action for the designated object with the designated descriptor. This only registers the notification mechanism, actual clean-up tasks need to be registered yet. There will be transparent pass-through for the provided checked exception type inclean().- Parameters:
object- The object for which to register post-mortem clean-up.descriptor- A string that meaningfully describes this clean-up when used for logging etc. ornullto use the class name of the designated object.checkedExceptionType- The exception that is expected by the clean-up. This will be forwarded when manually triggering clean-up.- Returns:
- A new
Cleanupregistered for as post-mortem action for the designated object. - See Also:
-
builder
Starts building aCleanup.- Returns:
- The
Builderfor building aCleanup.
-
addTask
Adds the given clean-up task to the end of the list of registered clean-up tasks.If an exception thrown by the
taskis not aRuntimeExceptionand is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException.- Parameters:
task- The clean-up task to be executed as post-mortem or when explicitly triggered. For proper/normal exception handling, let its exception beEXor a subclass thereof.- Returns:
- This
Cleanup.
-
addTaskAsFirst
Adds the given clean-up task to the first position of the list of registered clean-up tasks.If an exception thrown by the
taskis not aRuntimeExceptionand is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException.- Parameters:
task- The clean-up task to be executed as post-mortem or when explicitly triggered. For proper/normal exception handling, let its exception beEXor a subclass thereof.- Returns:
- This
Cleanup.
-
addTask
Adds anCleanup.AutoCloseableCleanupTaskfor the designatedAutoCloseableto the end of the list of registered clean-up tasks. Uses the class name(s) of theSupplierto generate a descriptor for the task.If an exception thrown by the
AutoCloseableis not aRuntimeExceptionand is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException.- Parameters:
autoCloseable- TheSupplierto get theAutoCloseableto close when cleaning-up. For proper/normal exception handling, make sureEXisIOException(orException).- Returns:
- This
Cleanup.
-
addTask
Adds anCleanup.AutoCloseableCleanupTaskfor the designatedAutoCloseables to the end of the list of registered clean-up tasks. Uses the class name(s) of theAutoCloseable(s) to generate a descriptor for the task(s).If an exception thrown by an
AutoCloseableis not aRuntimeExceptionand is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException.- Parameters:
autoCloseables- TheAutoCloseables to be added as clean-up task. For proper/normal exception handling, make sureEXisIOException(orException).- Returns:
- This
Cleanup.
-
addTask
Adds anCleanup.AutoCloseableCleanupTaskfor the designatedAutoCloseables to the end of the list of registered clean-up tasks. Uses the class name(s) of theAutoCloseable(s) to generate a descriptor for the task(s).If an exception thrown by an
AutoCloseableis not aRuntimeExceptionand is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException.- Parameters:
autoCloseables- TheAutoCloseables to be added as clean-up task. For proper/normal exception handling, make sureEXisIOException(orException).- Returns:
- This
Cleanup.
-
addTask
Adds anCleanup.AutoCloseableCleanupTaskfor the designatedAutoCloseables to the end of the list of registered clean-up tasks.If an exception thrown by the
AutoCloseableis not aRuntimeExceptionand is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException.- Parameters:
descriptor- A string that meaningfully describes the clean-up task ornullfor the class name(s) of theAutoCloseable(s).autoCloseable- TheSupplierto get theAutoCloseableto close when cleaning-up. For proper/normal exception handling, make sureEXisIOException(orException).- Returns:
- This
Cleanup.
-
addTask
Adds anCleanup.AutoCloseableCleanupTaskfor the designatedAutoCloseables to the end of the list of registered clean-up tasks.If an exception thrown by an
AutoCloseableis not aRuntimeExceptionand is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException.- Parameters:
descriptor- A string that meaningfully describes the clean-up task ornullfor the class name(s) of theAutoCloseable(s).autoCloseables- TheAutoCloseables to be added as clean-up task. For proper/normal exception handling, make sureEXisIOException(orException).- Returns:
- This
Cleanup.
-
addTask
Adds anCleanup.AutoCloseableCleanupTaskfor the designatedAutoCloseables to the end of the list of registered clean-up tasks.If an exception thrown by an
AutoCloseableis not aRuntimeExceptionand is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException.- Parameters:
descriptor- A string that meaningfully describes the clean-up task ornullfor the class name(s) of theAutoCloseable(s).autoCloseables- TheAutoCloseables to be added as clean-up task. For proper/normal exception handling, make sureEXisIOException(orException).- Returns:
- This
Cleanup.
-
addTaskAsFirst
Adds anCleanup.AutoCloseableCleanupTaskfor the designatedAutoCloseableto the first position of the list of registered clean-up tasks. Uses the class name(s) of theSupplierto generate a descriptor for the task.If an exception thrown by the
AutoCloseableis not aRuntimeExceptionand is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException.- Parameters:
autoCloseable- TheSupplierto get theAutoCloseableto close when cleaning-up. For proper/normal exception handling, make sureEXisIOException(orException).- Returns:
- This
Cleanup.
-
addTaskAsFirst
Adds anCleanup.AutoCloseableCleanupTaskfor the designatedAutoCloseables to the first position of the list of registered clean-up tasks. Uses the class name(s) of theSupplier(s) to generate a descriptor for the task(s).If an exception thrown by the
AutoCloseableis not aRuntimeExceptionand is not compatible with thisCleanup'spass-through exception type, it will be wrapped in anUndeclaredThrowableException.- Parameters:
autoCloseables- TheAutoCloseables to be added as clean-up task. For proper/normal exception handling, make sureEXisIOException(orException).- Returns:
- This
Cleanup.
-
addTaskAsFirst
Adds anCleanup.AutoCloseableCleanupTaskfor the designatedAutoCloseables to the first position of the list of registered clean-up tasks. Uses the class name(s) of theAutoCloseable(s) to generate a descriptor for the task(s).If an exception thrown by the
AutoCloseableis not aRuntimeExceptionand is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException.- Parameters:
autoCloseables- TheAutoCloseables to be added as clean-up task. For proper/normal exception handling, make sureEXisIOException(orException).- Returns:
- This
Cleanup.
-
addTaskAsFirst
Adds anCleanup.AutoCloseableCleanupTaskfor the designatedAutoCloseables to the first position of the list of registered clean-up tasks.If an exception thrown by the
AutoCloseableis not aRuntimeExceptionand is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException.- Parameters:
descriptor- A string that meaningfully describes the clean-up task ornullfor the class name(s) of theAutoCloseable(s).autoCloseable- TheSupplierto get theAutoCloseableto close when cleaning-up. For proper/normal exception handling, make sureEXisIOException(orException).- Returns:
- This
Cleanup.
-
addTaskAsFirst
Adds anCleanup.AutoCloseableCleanupTaskfor the designatedAutoCloseables to the first position of the list of registered clean-up tasks.If an exception thrown by an
AutoCloseableis not aRuntimeExceptionand is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException.- Parameters:
descriptor- A string that meaningfully describes the clean-up task ornullfor the class name(s) of theAutoCloseable(s).autoCloseables- TheAutoCloseables to be added as clean-up task. For proper/normal exception handling, make sureEXisIOException(orException).- Returns:
- This
Cleanup.
-
addTaskAsFirst
public Cleanup<EX> addTaskAsFirst(String descriptor, Collection<? extends AutoCloseable> autoCloseables) Adds anCleanup.AutoCloseableCleanupTaskfor the designatedAutoCloseables to the first position of the list of registered clean-up tasks.If an exception thrown by an
AutoCloseableis not aRuntimeExceptionand is not compatible with this Cleanup's pass-through exception type, it will be wrapped in anUndeclaredThrowableException.- Parameters:
descriptor- A string that meaningfully describes the clean-up task ornullfor the class name(s) of theAutoCloseable(s).autoCloseables- TheAutoCloseables to be added as clean-up task. For proper/normal exception handling, make sureEXisIOException(orException).- Returns:
- This
Cleanup.
-
runTasks
protected void runTasks()Executes all registered clean-up tasks. SeeCleanupfor details on the behaviour. -
runTasks
protected static void runTasks(Collection<CleanupTask<? extends Exception>> tasks, boolean explicitlyCleaned, String objectDescriptor, Logger logger) Executes the designated tasks by callingCleanupTask.cleanup(boolean)and collecting the corresponding exceptions. SeeCleanupfor details on the behaviour.
If one or more exceptions occur, the most critical one will be selected having all others as suppressed ones. In an explicit clean it will be thrown, otherwise it will be logged together with the corresponding description.- Parameters:
tasks- The tasks to be run.explicitlyCleaned- Whether the tasks are run due to an explicit call toclean(). In this case it is not called as post-mortem action of the monitored object.objectDescriptor- A string that meaningfully describes the monitored object for logging purpose.logger- The logger for messages and/or problems, e. g. unexpected exceptions.
-
clean
Triggers the clean-up and invokes all registered clean-up tasks. These tasks are invoked at most once regardless of the number of calls toclean(). You may call it for clean-up normally, that is, before post-mortem which will deregister it from post-mortem cleaning.This method is the replacement of
Cleaner.Cleanable.clean()when using the vanillaCleaner.- Throws:
EX- the exception(s) passed through from the clean-up tasks.
-
Cleanup.CollCloseTaskinstead.