Class CloseableResource<T>
java.lang.Object
org.apache.beam.runners.portability.CloseableResource<T>
- All Implemented Interfaces:
AutoCloseable
An
AutoCloseable that wraps a resource that needs to be cleaned up but does not implement
AutoCloseable itself.
Recipients of a CloseableResource are in general responsible for cleanup. Ownership
can be transferred from one context to another via transfer(). Transferring relinquishes
ownership from the original resource. This allows resources to be safely constructed and
transferred within a try-with-resources block. For example:
try (CloseableResource<Foo> resource = CloseableResource.of(...)) { // Do something
with resource. ... // Then transfer ownership to some consumer.
resourceConsumer(resource.transfer()); }
Not thread-safe.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAn exception that wraps errors thrown while a resource is being closed.static interfaceA function that knows how to clean up after a resource. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the underlying resource.get()Gets the underlying resource.static <T> CloseableResource<T> of(T resource, CloseableResource.Closer<T> closer) Creates aCloseableResourcewith the given resource and closer.transfer()Returns a newCloseableResourcethat owns the underlying resource and relinquishes ownership from thisCloseableResource.
-
Method Details
-
of
Creates aCloseableResourcewith the given resource and closer. -
get
Gets the underlying resource. -
transfer
Returns a newCloseableResourcethat owns the underlying resource and relinquishes ownership from thisCloseableResource.close()on the original instance becomes a no-op. -
close
Closes the underlying resource. The closer will only be executed on the first call.- Specified by:
closein interfaceAutoCloseable- Throws:
CloseableResource.CloseException- wrapping any exceptions thrown while closing
-