public class CloseableGroup
extends java.lang.Object
implements java.io.Closeable
It can take both closeable and autocloseable objects, and handle closeable as autocloseable and guarantee close idempotency by ensuring that each resource will be closed once even with concurrent close calls. It will also wrap checked non-IO exceptions into runtime exceptions.
Users can choose to suppress close failure with this class. By default such failures are not suppressed.
Constructor and Description |
---|
CloseableGroup() |
Modifier and Type | Method and Description |
---|---|
void |
addCloseable(java.lang.AutoCloseable autoCloseable)
Register an autocloseables to be managed by this class.
|
void |
addCloseable(java.io.Closeable closeable)
Register a closeable to be managed by this class.
|
void |
close()
Close all the registered resources.
|
void |
setSuppressCloseFailure(boolean shouldSuppress)
Whether to suppress failure when any of the closeable this class tracks throws exception during
closing.
|
public void addCloseable(java.io.Closeable closeable)
public void addCloseable(java.lang.AutoCloseable autoCloseable)
public void setSuppressCloseFailure(boolean shouldSuppress)
shouldSuppress
- true if user wants to suppress close failurespublic void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.io.IOException