Once you have defined a resource pool component, other Nucleus components may use that resource pool to check out resources and check them back in when they are done. A component that needs to use the resource pool would probably be passed a pointer to the resource pool through a properties file. It would define a property for that resource pool:

ResourcePool resourcePool;
public ResourcePool getResourcePool ()
{ return resourcePool; }
public void setResourcePool (ResourcePool resourcePool)
{ this.resourcePool = resourcePool; }

When the component requires a resource from the pool, it calls checkOut:

try {
  // Get a resource
  ResourceObject resource =
    getResourcePool ().checkOut (getAbsoluteName ());
catch (ResourcePoolException exc) {
  if (isLoggingError ()) logError (exc);
}

This line gets the resource pool and checks out a resource from the pool. When it calls checkOut, it must pass an identifying string, in this case the name of the service checking out the resource. This is required so that the administrator can look at the resource pool and see which parts of the application are using which resources.

The object returned by checkOut is of type ResourceObject. This object contains the resource you wanted in the first place - you obtain the resource by calling getResource:

try {
  // Get a resource
  ResourceObject resource =
    getResourcePool ().checkOut (getAbsoluteName ());
  ReallyExpensiveObject obj = (ReallyExpensiveObject)
    resource.getResource ();
}
catch (ResourcePoolException exc) {
  if (isLoggingError ()) logError (exc);
}

Once you have obtained the resource, it is yours to use. You can work on the assumption that no one else will be using the resource at the same time.

When you are done with the resource, you must check it back in:

try {
  // Get a resource
  ResourceObject resource =
    getResourcePool ().checkOut (getAbsoluteName ());
  ReallyExpensiveObject obj = (ReallyExpensiveObject)
    resource.getResource ();

  ...

  getResourcePool ().checkIn (resource);
}
catch (ResourcePoolException exc) {
  if (isLoggingError ()) logError (exc);
}

After checking in the resource, you are expected to no longer use that resource. If you need the resource again, then you must check out another resource from the pool.

 
loading table of contents...