public abstract class ResourcePool extends Object
Modifier and Type | Field and Description |
---|---|
protected LinkedList |
alloced
The alloced list
|
protected LinkedList |
freed
The free list
|
protected String |
identifier
Unique identifier for a resourcePool instance
|
protected int |
max
The maximum # of resources allowed
|
protected int |
min
The minimum # of resources to create at init time
|
protected int |
numCreated
The total number of resources currently created
|
Constructor and Description |
---|
ResourcePool() |
Modifier and Type | Method and Description |
---|---|
protected void |
allocResource(Object resource)
allocates a Resource and performs any initilization required to manage the alloced resource
|
protected abstract Object |
create()
Abstract method for creating a specific resource in the sub-class
|
protected Object |
createResource()
creates a new Resource and performs any initilization required to manage the resource
|
protected void |
createResources()
Pre-creates the minimum number of resources
|
protected void |
createResources(int number)
Pre-creates the specified number of resources
|
protected abstract void |
dispose(Object obj)
Abstract method for disposing a specific resource in the sub-class
|
protected void |
disposeResource(Object resource)
disposes of a Resource and performs any clean-up required to manage the resource
|
protected void |
freeResource(Object resource)
frees a Resource and performs any clean-up required to manage the resource
|
Object |
get()
Get a Resource from the pool, blocking forever until a resource becomes available
|
Object |
get(long timeout)
Get a Resource from the pool, blocking for a specified time until a resource becomes
available, a zero timeout blocks forever.
|
<T> List<T> |
getAllocedOfType(Class<T> type) |
int |
getAllocedSize()
Gets the number of resources currently assigned
|
int |
getFreedSize()
Gets the number of resources currently available for assignment
|
String |
getIdentifier()
Gets the unique identifier for this instance
|
static ResourcePool |
getInstance(String key)
Gets a registered ResourcePool
|
int |
getMax()
Gets the maximum number of resources allowed
|
int |
getMin()
Gets the minimum number of resources
|
int |
getSize()
Gets the total number of resources created
|
protected void |
register()
Registers this resource into the common ResourcePool
|
protected void |
register(String key)
Registers this resource into the common ResourcePool
|
static void |
register(String key,
ResourcePool pool)
Registers a resource into the common ResourcePool
|
void |
release(Object obj)
Release a resource back to the pool after its use is completed.
|
void |
remove(Object obj)
Remove a resource all together from the pool and dispose of it
|
void |
setIdentifier(String identifier)
Sets the unique identifier for this instance
|
void |
setMax(int max)
Sets the maximum number of resources
|
void |
setMin(int min)
Sets the minimum number of resources, this method has no effect on the number of created
resources, if the new minimum is less than the old minimum.
|
String |
toString()
Returns a string representation of the ResourcePool
|
protected abstract boolean |
validate(Object obj)
Abstract method for validating a resource
|
protected LinkedList freed
protected LinkedList alloced
protected int min
protected int max
protected int numCreated
protected String identifier
protected abstract Object create() throws ResourceUnavailableException
ResourceUnavailableException
- if an error occured in creating the resourceprotected abstract void dispose(Object obj)
obj
- the resource to disposeprotected abstract boolean validate(Object obj)
obj
- the resource to validatepublic void release(Object obj)
obj
- the resource objectpublic void remove(Object obj)
obj
- the resource objectpublic Object get() throws ResourceUnavailableException
ResourceUnavailableException
- if an error occured in creating the resourcepublic Object get(long timeout) throws ResourceUnavailableException
timeout
- the time in milliseconds to wait for a resource to become availableResourceUnavailableException
- if an error occured in creating the resourceResourceUnavailableException
- if no resource becomes available before the timeout
period expiresprotected Object createResource() throws ResourceUnavailableException
ResourceUnavailableException
protected void allocResource(Object resource)
resource
- the resource to allocateprotected void freeResource(Object resource)
resource
- the resource to freeprotected void disposeResource(Object resource)
resource
- the resource to disposepublic int getFreedSize()
public int getAllocedSize()
public int getSize()
public int getMin()
public int getMax()
public void setMin(int min)
min
- the minimum number of resourcesIllegalArgumentException
- if minimum is less than zero or the minimum is is greater
than the maximumpublic void setIdentifier(String identifier)
identifier
- the unique identifier for this instancepublic String getIdentifier()
protected void createResources() throws ResourceUnavailableException
ResourceUnavailableException
protected void createResources(int number) throws ResourceUnavailableException
number
- the number of resources to createResourceUnavailableException
public void setMax(int max)
max
- the maximum number of resourcesIllegalArgumentException
- if maximum is less than the minimumpublic static ResourcePool getInstance(String key)
key
- the identifier for the type of resource poolprotected void register()
protected void register(String key)
key
- the identifier for the type of resource poolpublic static void register(String key, ResourcePool pool)
key
- the identifier for the type of resource poolpool
- the ResourcePool being registeredCopyright © GigaSpaces.