Package com.j_spaces.kernel.pool
Class Resource
java.lang.Object
com.j_spaces.kernel.pool.Resource
- All Implemented Interfaces:
IResource
- Direct Known Subclasses:
BusPacket,ByteBufferRedoLogFileStorage.ByteArrayResource,CompressedMarshObjectConvertor,ConnectionResource,ConnectionResource,MarshObjectConvertor,PacketSerializer.InputStreamsResource,ReadWriteLockResource,TraceableResource
Resources should extend this class and implement all abstract methods. When done using the
resource, it should be released.
Usage:
Usage:
MyResourceFactory factory = new MyResourceFactory();
ResourcePool pool = new ResourcePool(factory, 10, 50);
MyResource resource = pool.getResource();
try {
...
}finally {
resource.release();
}
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanacquire()Atomically acquires a free resource.abstract voidclear()Cleans the resource of its contents so it can be returned to the pool.booleanAn indication if this resource is currently acquired.booleanAn indication if this resource originated from the pool or was allocated on the fly.voidrelease()Atomically sets this resource as free; usually when returning a resource to the pool.voidsetAcquired(boolean acquired) An indication if this resource is acquired.voidsetFromPool(boolean fromPool) An indication if this resource originated from the pool or was allocated on the fly.
-
Constructor Details
-
Resource
public Resource()
-
-
Method Details
-
setFromPool
public void setFromPool(boolean fromPool) Description copied from interface:IResourceAn indication if this resource originated from the pool or was allocated on the fly. The latter, will not return to the pool.- Specified by:
setFromPoolin interfaceIResource- Parameters:
fromPool-trueif originated from the pool;falseif not part of the pool.
-
isFromPool
public boolean isFromPool()Description copied from interface:IResourceAn indication if this resource originated from the pool or was allocated on the fly. The latter, will not return to the pool.- Specified by:
isFromPoolin interfaceIResource- Returns:
trueif originated from the pool;falseif allocated.
-
acquire
public boolean acquire()Description copied from interface:IResourceAtomically acquires a free resource. But, due to race conditions, it may fail. -
setAcquired
public void setAcquired(boolean acquired) Description copied from interface:IResourceAn indication if this resource is acquired.- Specified by:
setAcquiredin interfaceIResource- Parameters:
acquired-trueif resource is acquired.falseif resource is not acquired.
-
isAcquired
public boolean isAcquired()Description copied from interface:IResourceAn indication if this resource is currently acquired. Usually a resource becomes acquired after a call toIResource.acquire(), or by use of it's status indicatorIResource.setAcquired(boolean).- Specified by:
isAcquiredin interfaceIResource- Returns:
trueif resource is acquired.falseif not yet acquired.
-
release
public void release()Description copied from interface:IResourceAtomically sets this resource as free; usually when returning a resource to the pool.Before releasing the resource back into the pool,
is invoked.IResource.clear() -
clear
public abstract void clear()Description copied from interface:IResourceCleans the resource of its contents so it can be returned to the pool.You may use the
IResource.isFromPool()indication to decide of a special action before returning a resource to the pool, or discard any actions on resources not returning to the pool.
-