Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
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
Page 4 of 5
The code for the ServiceLocatorVerifier class follows:
class ServiceLocatorVerifier implements WaitingAlarm
{
private VerifiedServiceLocator serviceLocator;
private AlarmClock myAlarmClock;
private long serverVerifierPeriod;
private ServiceVerifiable appSpecificServiceVerifiable;
public ServiceLocatorVerifier(
int _serverVerifierPeriod_Minutes,
ServiceVerifiable _appSpecificServiceVerifiable)
{
serviceLocator = VerifiedServiceLocator.getInstance();
// Convert minutes to milliseconds
serverVerifierPeriod = _serverVerifierPeriod_Minutes * 60 * 1000;
appSpecificServiceVerifiable = _appSpecificServiceVerifiable;
this.startAlarmClock();
}
private void startAlarmClock()
{
// Create the alarmClock
myAlarmClock = new AlarmClock(this, serverVerifierPeriod);
// Start the alarmClock
myAlarmClock.start();
}
/**
* This method is called when the alarmClock rings.
* So if the alarm is ringing, it is time to check
* the cached services.
*/
public void alarmRinging()
{
try
{
appSpecificServiceVerifiable.checkServices();
}
catch( Exception exc)
{
serviceLocator.cleanCache();
appSpecificServiceVerifiable.followError(exc);
}
}
protected void finalize() throws Throwable
{
// Turn off my alarm clock
myAlarmClock.interrupt();
}
}
ServiceVerifiableServiceLocatorVerifier uses ServiceVerifiable to reach client-specific verifications and treatment. To use the Verifiable Service Locator enhanced pattern, you must code
a class that implements the ServiceVerifiable interface. To implement this interface, you must provide two methods:
checkServices() throws Exception: The code inside this method should invoke the VerifiedServiceLocator.lookup() for each service you consider important to verify. This method must throw Exception (or just not catch it). The ServiceLocatorVerifier acts only when checkServices() throws Exception raises an Exception.
followError(Exception exc): This method is invoked when ServiceLocatorVerifier gets an Exception from checkServices(); this method implementation allows the developer to act on the Exception (to log it, for instance). You can opt to have an empty method—{}—so nothing happens and the interface is still respected.
The code for the ServiceVerifiable interface follows below:
public interface ServiceVerifiable
{
public void checkServices() throws Exception;
public void followError(Exception exc);
}
App-specific class
The application-specific class implements the ServiceVerifiable interface. An object of this class is passed as a parameter to the initialization method (see Listing 1).
Listing 3 provides a sample application-specific class that implements the ServiceVerifiable interface. In this simple sample, the only service to be verified is an EJBHome.
Listing 3. A sample application-specific class implements ServiceVerifiable
public class MyAppServiceVerifiable implements ServiceVerifiable
{
public void checkServices() throws Exception
{
VerifiedServiceLocator serviceLocator =
VerifiedServiceLocator.getInstance();
MyEJBHome myEJBHome =
(MyEJBHome) serviceLocator.lookup
(MyEJBHome.class, "myEJBJNDI");
MyRemote myRemote = myEJBHomee.create();
try
{
myRemote .remove();
}
catch (javax.ejb.RemoveException e)
{
e.printStackTrace();
}
}
public void followError(Exception exc) { }
}
The app-specific class is specific to each application using Verified Service Locator. By implementing that class, you select the cached services that you want verified.