ariba.util.core
Class SelfCleaningPool<T>

java.lang.Object
  extended by ariba.util.core.SelfCleaningPool<T>
All Implemented Interfaces:
Target
Direct Known Subclasses:
BufferPool

public abstract class SelfCleaningPool<T>
extends java.lang.Object
implements Target

Generic resource pooling mechanism, that uses Timer to return allocated storage to be garbage collected when it is no longer needed for current usage patterns. By default resources are released from the pool 10 minutes after they were last acquired or released to the pool. There is no upper bound to the pool size, it will grow to meet simultaneous demand for the resources, and then shrink over time when simultaneous demand is not so high. The resources are remembered through soft references, so the GC can reclaim them at any time if there are no strong references to them, and the code here defends against the resource having been reclaimed this way. This class is completely thread-safe, there is expected to be only one instance of a given pool across all realms on a node, so the pool of resources can be reused without generating garbage by different realms and threads at different times as needed.


Nested Class Summary
static class SelfCleaningPool.PoolFloat<T>
          Nested class that wraps up an actual resource instance.
 
Constructor Summary
SelfCleaningPool()
           
 
Method Summary
 T get()
          Returns an instance of the resource T.
 EventLoop getEventLoop()
          Returns EventLoop previously registered by the singleton BaseServer constructor.
 int getIdleTimeoutSeconds()
          Returns int number of seconds after the time when a given resource was most recently used, at which the resource will be declared idle and reclaimed for garbage collection.
 void performCommand(java.lang.String command, java.lang.Object data)
          This method is called when the timer fires.
static void registerEventLoop(EventLoop eventLoop)
          This static method is called by the BaseServer constructor to register the one true master event loop for the node with us, so we can create timers, and so our client code in util.core doesn't have to figure out how to get an event loop to pass us.
 void release(T item)
          Releases a previously allocated instance of the resource item back to the pool.
 void setIdleTimeoutSeconds(int seconds)
          Sets int number of seconds after the time when a given resource was most recently used, at which the resource will be declared idle and reclaimed for garbage collection.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SelfCleaningPool

public SelfCleaningPool()
Method Detail

get

public T get()
Returns an instance of the resource T. Synchronizes on this instance for thread safety while calling internal methods to do the work. Never returns null.


release

public void release(T item)
Releases a previously allocated instance of the resource item back to the pool. If the resource item is null or was not previously returned by get, it is ignored. Instance identity via the == operator is used to recognize resource instances, so we do not depend on any properties of the hashcode or equals methods of T.


getIdleTimeoutSeconds

public int getIdleTimeoutSeconds()
Returns int number of seconds after the time when a given resource was most recently used, at which the resource will be declared idle and reclaimed for garbage collection.


setIdleTimeoutSeconds

public void setIdleTimeoutSeconds(int seconds)
Sets int number of seconds after the time when a given resource was most recently used, at which the resource will be declared idle and reclaimed for garbage collection. This defaults to 10 minutes, but can be set as desired. While it is negative or zero, timeouts are not used, and resources are never reclaimed by the pool.


registerEventLoop

public static void registerEventLoop(EventLoop eventLoop)
This static method is called by the BaseServer constructor to register the one true master event loop for the node with us, so we can create timers, and so our client code in util.core doesn't have to figure out how to get an event loop to pass us. We synchronize just to be squeeky clean in race conditions during startup, and to make sure multi-chip cpus on a board flush their chip cache line for the static variable.


getEventLoop

public EventLoop getEventLoop()
Returns EventLoop previously registered by the singleton BaseServer constructor. Never returns null.


performCommand

public final void performCommand(java.lang.String command,
                                 java.lang.Object data)
This method is called when the timer fires. We simply delegate to calling refreshTimer in a synchronized loop, since this is another public method that will be called by a timer thread from the event loop.

Specified by:
performCommand in interface Target


AribaWeb User Interface Development Framework
Copyright © 2000-2014 Ariba, Inc. All Rights Reserved.