Class ResourcePool
- All Implemented Interfaces:
SharedResource
,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.
- Author:
- Simon Thoresen Hult
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.yahoo.jdisc.SharedResource
SharedResource.Debug
-
Field Summary
Fields inherited from interface com.yahoo.jdisc.SharedResource
DEBUG, SYSTEM_PROPERTY_NAME_DEBUG
-
Constructor Summary
ConstructorDescriptionCreates a new instance of this class without a backingContainerBuilder
.ResourcePool
(ContainerBuilder builder) Creates a new instance of this class. -
Method Summary
Modifier and TypeMethodDescription<T extends SharedResource>
Tadd
(T t) Adds the givenSharedResource
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>
Tget
(com.google.inject.Key<T> key) Returns the appropriate instance for the given injection key.<T extends SharedResource>
TReturns the appropriate instance for the given injection type.<T extends SharedResource>
Tretain
(T t) Retains and adds the givenSharedResource
to this ResourcePool.Methods inherited from class com.yahoo.jdisc.AbstractResource
currentState, refer, refer, release, retainCount
-
Constructor Details
-
ResourcePool
public ResourcePool()Creates a new instance of this class without a backing
ContainerBuilder
. A ResourcePool created with this constructor will throw a NullPointerException if eitherget(Class)
orget(Key)
is called. -
ResourcePool
Creates a new instance of this class. All calls to
get(Class)
andget(Key)
are forwarded to theContainerBuilder
given to this constructor.- Parameters:
builder
- The ContainerBuilder that provides the injection functionality for this ResourcePool.
-
-
Method Details
-
add
Adds the given
SharedResource
to this ResourcePool. Note that this DOES NOT callSharedResource.refer()
, as opposed toretain(SharedResource)
. When this ResourcePool is destroyed, it will release the main reference to the resource (by callingSharedResource.release()
).- Type Parameters:
T
- The class of parametert
.- Parameters:
t
- The SharedResource to add.- Returns:
- The parameter
t
, to allow inlined calls to this function.
-
get
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()
).- Type Parameters:
T
- The class of the injection type.- Parameters:
key
- The injection key to return.- Returns:
- The appropriate instance of T.
- Throws:
NullPointerException
- If this pool was constructed without a ContainerBuilder.
-
get
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()
).- Type Parameters:
T
- The class of the injection type.- Parameters:
type
- The injection type to return.- Returns:
- The appropriate instance of T.
- Throws:
NullPointerException
- If this pool was constructed without a ContainerBuilder.
-
retain
Retains and adds the given
SharedResource
to this ResourcePool. Note that this DOES callSharedResource.refer()
, as opposed toadd(SharedResource)
.When this ResourcePool is destroyed, it will release the resource reference returned by the
SharedResource.refer()
call.- Type Parameters:
T
- The class of parametert
.- Parameters:
t
- The SharedResource to retain and add.- Returns:
- The parameter
t
, to allow inlined calls to this function.
-
destroy
protected void destroy()Description copied from class: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.
- Overrides:
destroy
in classAbstractResource
-
close
- Specified by:
close
in interfaceAutoCloseable
- Throws:
Exception
-