Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Repair invalid cached services in the Service Locator pattern

The Verified Service Locator pattern ensures cached services' validity

  • Print
  • Feedback

Page 3 of 5

Participants and responsibilities

Figure 5 shows how this solution's participants interact. Notice the interaction numbers: interactions 1, 2, and 3 happen only during the initialization process; 4 and 5 happen once per verification cycle; and 6, 7, and 8 occur only when a cached service raises an exception.

Figure 5. Collaboration diagram: Verified Service Locator execution

VerifiedServiceLocator
To enhance Service Locator, Verified Service Locator adds two methods:

  • public static void setVerifier(int _verificationFrequency_Minutes, ServiceVerifiable _appSpecificObj): The client invokes this method to set up the verifier mechanism; the parameters are the frequency of the verification cycle and the object of the class that implements the ServiceVerifiable interface.
  • public void cleanCache(): Once the ServiceLocatorVerifier recognizes that a cached service has raised an exception during the checkServices() method execution, it will invoke the cleanCache() method to clean the services objects in the cache. Thus, the next time a client gets a service, it will be a valid cached service.


The code for the VerifiedServiceLocator class follows:

public class VerifiedServiceLocator
{
  private static ServiceLocatorVerifier serviceLocatorVerifier;
  private Map cachedServices;
  ...
  public static void setVerifier(
    int _verificationFrequency_Minutes,  
    ServiceVerifiable _appSpecificObj   
  {
     if (serviceLocatorVerifier == null)  
     {
       serviceLocatorVerifier =
         new ServiceLocatorVerifier(
           _verificationFrequency_Minutes,
           _appSpecificObj);
      }
  }
  public void cleanCache()   
  {
      cachedServices.clear();
  }
  ...
  // Normal methods of a Service Locator
}


ServiceLocatorVerifier
The ServiceLocatorVerifier handles the verification process for the cached services in Service Locator. A ServiceLocatorVerifier object is internally constructed when the client invokes VerifiedServiceLocator.setVerifier(int _verificationFrequency_Minutes, ServiceVerifiable _appSpecificObj).

Once constructed, ServiceLocatorVerifier starts its alarm clock, which triggers itself every verificationFrequency_Minutes, signaling that it's time to invoke appSpecificObj.checkServices(). When this method execution raises any Exception, ServiceLocatorVerifier invokes appSpecificObj.followError(Exception exc) and VerifiedServiceLocator.cleanCache().

When ServiceLocatorVerifier invokes appSpecificObj.followError(Exception exc), the specific application can treat the error in any way it wishes—by logging the exception or sending an automatic email, for instance.

When ServiceLocatorVerifier invokes VerifiedServiceLocator.cleanCache(), the services in the cache are cleaned, so the next time a client invokes lookup(), a new service returns. That solves the problems caused by invalid cached services. The solution does not solve other error types, but a discussion of those types would reach beyond the ServiceLocatorVerifier's scope, as its solution is specific: the Service Locator Verifier enhances Service Locator only to manage the invalid cached services problem.

  • Print
  • Feedback

Resources