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 2 of 5

Some critical points:

  • You might ask: Why not verify the service immediately before returning it to the client? To do so, Service Locator would need to know how to verify each service, making the pattern application specific and less reusable. Furthermore, when invoking a service, the client would need to wait for the service verification, decreasing performance.
  • The solution must be simple and not affect Service Locator usage and benefits.


The proposed enhanced Service Locator has a verifier mechanism that checks the cached services' validity by monitoring them during a cycle of a specified frequency. A low-priority thread executing during the verification cycle triggers the validation, identifying and eliminating invalid cached services, and ensuring the services' validity before a client attempts to use them. That way, the consumed validation time happens later, not while a client waits for a service response. The enhanced Service Locator is called Verified Service Locator, and I present its usage, structure, and participants below.

Usage

Figure 3 shows how a client uses Verified Service Locator.

Figure 3. Sequence diagram: Verified Service Locator usage

Verified Service Locator differs from Service Locator in the following ways:

  • You must develop an application-specific class that implements a ServiceVerifiable interface (explained in the "Participants and Responsibilities" section below).
  • You must invoke VerifiedServiceLocator.setVerifier(...) to start the verification mechanism. The parameters are an object of the application-specific class and a verifying frequency (also detailed in "Participants and Responsibilities"). Listing 1 below shows a sample JSP (JavaServer Page) initializing VerifiedServiceLocator:


Listing 1. Initialize VerifiedServiceLocator

 <%@ page import="enhancedServiceLocator.VerifiedServiceLocator" %>
<%@ page import="enhancedServiceLocator.test.MyAppServiceVerifiable" %>
<%!
  public void jspInit()
  {
    // Sets the VerifiedServiceLocator
    // to verify cached services            
    // every 5 minutes
    VerifiedServiceLocator.setVerifier(5, new MyAppServiceVerifiable ());
  }
%>    


After initialization, Verified Service Locator works as Service Locator does. Listing 2 shows a sample JSP invoking the lookUp() method:

Listing 2. Normal Service Locator usage

<%@ page import="enhancedServiceLocator.VerifiedServiceLocator" %>
<%@ page import="enhancedServiceLocator.test.MyEJBHome" %>
<%@ page import="enhancedServiceLocator.test.MyRemote" %>
<%
  VerifiedServiceLocator  serviceLocator =   
    VerifiedServiceLocator.getInstance();
  MyEJBHome myEJBHome =   
    (MyEJBHome) serviceLocator.lookup
      (MyEJBHome.class, "myEJBJNDI");
  MyRemote myRemote =   myEJBHomee.create();   
  out.println( "output from ejb: "+myRemote.greetings( ) );
%>


Structure

Figure 4 shows the relationships among this solution's participants.

Figure 4. Class diagram: Verified Service Locator structure

The class diagram presents the classes VerifiedServiceLocator, ServiceLocatorVerifier, and AlarmClock, plus an app-specific class; the interfaces ServiceVerifiable and WaitingAlarm; and the relationships between them. They form the solution that enhances the Service Locator pattern.

  • Print
  • Feedback

Resources