public final class ResourcePool extends AbstractResource implements AutoCloseable
This is a utility class to help manage SharedResource
s while configuring a ContainerBuilder
. This
class can still be used without a ContainerBuilder, albeit with the injection APIs (i.e. get(Class)
and
get(com.google.inject.Key)
) disabled.
The core problem with SharedResources is that they need to be tracked carefully to ensure exception safety in the code that creates and registers them with a ContainerBuilder. The code for this typically looks like this:
MyServerProvider serverProvider = null; MyRequestHandler requestHandler = null; try { serverProvider = builder.getInstance(MyServerProvider.class); serverProvider.start(); containerBuilder.serverProviders().install(serverProvider); requestHandler = builder.getInstance(MyRequestHandler.class); containerBuilder.serverBindings().bind("http://host/path", requestHandler); containerActivator.activateContainer(containerBuilder); } finally { if (serverProvider != null) { serverProvider.release(); } if (requestHandler != null) { requestHandler.release(); } }
The ResourcePool helps remove the boiler-plate code used to track the resources from outside the try-finally block. Using the ResourcePool, the above snippet can be rewritten to the following:
try (ResourcePool resources = new ResourcePool(containerBuilder)) { ServerProvider serverProvider = resources.get(MyServerProvider.class); serverProvider.start(); containerBuilder.serverProviders().install(serverProvider); RequestHandler requestHandler = resources.get(MyRequestHandler.class); containerBuilder.serverBindings().bind("http://host/path", requestHandler); containerActivator.activateContainer(containerBuilder); }
This class is not thread-safe.
Modifier and Type | Field and Description |
---|---|
private ContainerBuilder |
builder |
private List<ResourceReference> |
resources |
DEBUG, SYSTEM_PROPERTY_NAME_DEBUG
Constructor and Description |
---|
ResourcePool()
Creates a new instance of this class without a backing
ContainerBuilder . |
ResourcePool(ContainerBuilder builder)
Creates a new instance of this class.
|
Modifier and Type | Method and Description |
---|---|
<T extends SharedResource> |
add(T t)
Adds the given
SharedResource to this ResourcePool. |
void |
close() |
protected void |
destroy()
This method signals that this AbstractResource can dispose of any internal resources, and commence with shut
down of any internal threads.
|
<T extends SharedResource> |
get(Class<T> type)
Returns the appropriate instance for the given injection type.
|
<T extends SharedResource> |
get(com.google.inject.Key<T> key)
Returns the appropriate instance for the given injection key.
|
<T extends SharedResource> |
retain(T t)
Retains and adds the given
SharedResource to this ResourcePool. |
currentState, refer, release, retainCount
private final List<ResourceReference> resources
private final ContainerBuilder builder
public ResourcePool()
Creates a new instance of this class without a backing ContainerBuilder
. A ResourcePool created with
this constructor will throw a NullPointerException if either get(Class)
or get(Key)
is
called.
public ResourcePool(ContainerBuilder builder)
Creates a new instance of this class. All calls to get(Class)
and get(Key)
are forwarded to
the ContainerBuilder
given to this constructor.
builder
- The ContainerBuilder that provides the injection functionality for this ResourcePool.public <T extends SharedResource> T add(T t)
Adds the given SharedResource
to this ResourcePool. Note that this DOES NOT call SharedResource.refer()
, as opposed to retain(SharedResource)
. When this ResourcePool is
destroyed, it will release the main reference to the resource (by calling SharedResource.release()
).
T
- The class of parameter t.t
- The SharedResource to add.public <T extends SharedResource> T get(com.google.inject.Key<T> key)
Returns the appropriate instance for the given injection key. Note that this DOES NOT call SharedResource.refer()
. This is the equivalent of doing:
t = containerBuilder.getInstance(key); resourcePool.add(t);
When this ResourcePool is destroyed, it will release the main reference to the resource
(by calling SharedResource.release()
).
T
- The class of the injection type.key
- The injection key to return.NullPointerException
- If this pool was constructed without a ContainerBuilder.public <T extends SharedResource> T get(Class<T> type)
Returns the appropriate instance for the given injection type. Note that this DOES NOT call SharedResource.refer()
. This is the equivalent of doing:
t = containerBuilder.getInstance(type); resourcePool.add(t);
When this ResourcePool is destroyed, it will release the main reference to the resource
(by calling SharedResource.release()
).
T
- The class of the injection type.type
- The injection type to return.NullPointerException
- If this pool was constructed without a ContainerBuilder.public <T extends SharedResource> T retain(T t)
Retains and adds the given SharedResource
to this ResourcePool. Note that this DOES call SharedResource.refer()
, as opposed to add(SharedResource)
.
When this ResourcePool is destroyed, it will release the resource reference returned by the
SharedResource.refer()
call.
T
- The class of parameter t.t
- The SharedResource to retain and add.protected void destroy()
AbstractResource
This method signals that this AbstractResource can dispose of any internal resources, and commence with shut down of any internal threads. This will be called once the reference count of this resource reaches zero.
destroy
in class AbstractResource
public void close() throws Exception
close
in interface AutoCloseable
Exception
Copyright © 2018. All rights reserved.