Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

Search: 

  Tutorial: Repair invalid cached services in the Service Locator pattern

Repair invalid cached services in the Service Locator pattern

Tutorial Details:

Repair invalid cached services in the Service Locator pattern
Repair invalid cached services in the Service Locator pattern
By: By Paulo Caroli
The Verified Service Locator pattern ensures cached services' validity
he Service Locator pattern locates J2EE (Java 2 Platform, Enterprise Edition) services for clients and thus abstracts the complexity of network operation and J2EE service lookup as EJB (Enterprise JavaBean) Home and JMS (Java Message Service) component factories. The Service Locator hides the lookup process's implementation details and complexity from clients. To improve application performance, Service Locator caches service objects to eliminate unnecessary JNDI (Java Naming and Directory Interface) activity that occurs in a lookup operation.
When you opt to use the Service Locator pattern in J2EE development, you expect no side effects. However, cached services can be invalid, generating unexpected errors in your application. To avoid these errors, enhance the Service Locator pattern to neutralize potential problems with cached services. In this article, I present a way to empower the Service Locator pattern with a verification mechanism that recognizes and eliminates invalid cached services.
Context
Figure 1 shows the Service Locator pattern behavior.
Figure 1. Service Locator execution
For example, to acquire an EJB Home object for the first time, Service Locator first creates a JNDI initial context object and performs a lookup on the EJB Home object. Because this operation utilizes significant resources and because multiple clients repeatedly require the same EJB Home object, Service Locator caches the object, and consecutive calls use the cached service.
Problem
I used Service Locator in several projects without stumbling across any problems. However, when I used the pattern with my last project, the service invocation failed after the server restarted.
Figure 2 shows how a server restart generated the undesired error.
Figure 2. Service failure after a server restart
When a service has been invoked, Service Locator returns the cached service for consecutive invocations. In my project environment, the server restart invalidated the cached services; the client noticed this invalid service when it received an error while attempting to execute the service (Step 4 in Figure 2).
Unfortunately, Service Locator doesn't realize that it betrays the client by returning an invalid cached service. This is a contradictory situation: Service Locator, with its great benefits, brings this drawback. This article doesn't focus on the possible situations that invalidate the Service Locator's cached services; rather, it explores a solution for overcoming this problem.
You could address this situation in one of the following ways:
Don't use Service Locator: This approach solves the problem, but you lose the pattern's benefits.
Treat the error when it happens: With this approach, you add more code to the client code to solve the problem created by Service Locator; however, your reasoning for using Service Locator in the first place was to reduce code complexity. Furthermore, each different client will have to solve the same problem.
Use the proposed enhanced Service Locator instead: I explain this solution below in detail.
Solution
Enhance Service Locator by empowering it with a mechanism for periodically verifying the cached services.
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.
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.
The code for the ServiceLocatorVerifier class follows:
class ServiceLocatorVerifier implements WaitingAlarm
{
private VerifiedServiceLocator serviceLocator;
private AlarmClock myAlarmClock;
private long serverVerifierPeriod;
private ServiceVerifiable appSpecificServiceVerifiable;
public Serv


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Repair invalid cached services in the Service Locator pattern

View Tutorial:
Repair invalid cached services in the Service Locator pattern

Related Tutorials:

Displaying 1 - 50 of about 873 Related Tutorials.

J2EE Web Service Development with Attachments Using Axis
Web Services,Web Services Developement,J2EE Web Services,J2EE Web service... WSDL file for this service at http://localhost:8080/AttachmentServices/services... Service Development with Attachments Using Axis
 
BI: Improving performance in financial service
, interpreting transaction pattern and efficient customer service with quick... BI: Improving performance in financial service GPS Capability,GPS... service           
 
Web Services - Web Services Tutorials
protocol that all platforms tend to agree on. Thus , using Web services, a Web service... is a Web Service Web services constitute a distributed computer architecture made up.... Implementing Web Services Here comes a brief step-by-step on how a Web service
 
What is Service-Oriented Architecture?
architecture,soa web services What is Service... be classified into two terms: Services and Connections. Services: A service... services. The following figure is a typical example of the service-oriented
 
J2EE Web Service Development with Attachments Using Axis
Web Services,Web Services Developement,J2EE Web Services,J2EE Web service... WSDL file for this service at http://localhost:8080/AttachmentServices/services... Service Development with Attachments Using Axis
 
Repair and Prevent Errors on Your Computer
Free Technical Support Repair invalid registry entries that are a common... Repair and Prevent Errors on Your Computer... will scan your entire registry for any invalid entries and provide a list
 
SOA and Web Services
SOA and Web Services,Web Services Tutorials,SOA Tutorial,Service Oriented Architecture,Service Oriented Architecture - SOA,Web Services Tutorials SOA and Web Services SOA and Web Services Tutorials
 
SOA and Web Services
SOA and Web Services,Web Services Tutorials,SOA Tutorial,Service Oriented Architecture,Service Oriented Architecture - SOA,Web Services Tutorials SOA and Web Services SOA and Web Services Tutorials
 
Web Services - Web Services Tutorials
on the service interfaces. Web services technologies support to the Service-Oriented... the services description. A service requestor can bind and use the service... a service running on it. From the client's point of view, web services's life cycle
 
Design patterns interview questions3
processing. Q20. What is Service Locator pattern? Ans. It provides a solution... of the application? Ans. VO, Session Façade, Business Delegate and Service Locator...;ade pattern? Ans. This pattern hides the complexity of business components
 
Why Web Services?
Why Web Services, Web Services, Web Service... and Service Discovery layers) uses the well defined protocol in the Web Services... the Service Providers. This also helps your customer to find your services
 
Location Based Service (LBS)
for various location based services. So in nutshell, LBS is a wireless service... Location Based Service (LBS) Location Based Service...;    This is one of the most popular services based
 
Web Services Tutorials and Links
, you can access services. From a service API point of view, the majority... Web services-Links to web services tutorials Web Services Tutorials and Links
 
Data Service and Query Builder Tool
or creator. Although Data Service 1.0 is only for supporting JDBC Services, it still... Data Service and Query Builder Tool Data Service...;       Data Service is more than
 
Location Based Service (LBS) in Tourism
. In spite of the conventional services like vehicle navigation, emergency service... Location Based Service (LBS) in Tourism Location Based Service (LBS) in Tourism       
 
Security and Privacy Issues in Location Based Service (LBS)
Security and Privacy Issues in Location Based Service (LBS) Security and Privacy Issues in Location Based Service (LBS...; Location Based Service (LBS) is primarily based on user’s location
 
VoIP Web Services
VoIP Web Services VoIP Web Services...; The Avaya Joins VoIP Web Services Communications software...;  VoIP Services with interconnect VoIP Services Interconnected VoIP
 
Eclipse Plunging/Web Services
Eclipse Plunging/Web Services Eclipse Plunging/Web Services         ... IDE for BEA WebLogic? Service-Oriented Architecture (SOA) enablement
 
Developing Axis Web services with XML Schemas.
Developing Axis Web services with XML Schemas Developing Axis Web services with XML Schemas. Developing SOAP based web services (Note
 
GPS services in mobile phones
GPS services in mobile phones GPS services... or simply find the nearest hotel or hospital. Now finally those kinds of services... life simpler and smother. Besides the so-called social networking service
 
Data Provider or other service returned an E_FAIL status Error
Data Provider or other service returned an E_FAIL status Error Data Provider or other service returned an E_FAIL status Error... or other service returned an E_FAIL status Error" while moving the Record Set
 
Design Pattern
Design Pattern Design Pattern  ...; ?Pattern? word suggests a series of events occurring in a definite order. Many... earlier, by people frequently). This solving technique gradually becomes a pattern
 
Design patterns interview questions1
Handler, and Service Locator. Integration tier patterns are: Data Access... patterns? Ans. A pattern is a proven (and recurring) solution to a problem in a context. Each pattern describes a problem which occurs over and over again
 
Design patterns interview questions2
the business classes or DAO class. This pattern can be used with Service Locator.... Q13. What is Service to Worker pattern? Ans. This is used in larger applications... is Dispatcher View pattern? Ans. This is similar to Service to Worker pattern
 
Matching Pattern using Regularexpression
Matching Pattern using Regularexpression Matching Pattern using Regularexpression      ... to match a pattern with the text by using Regularexpression.The steps involved
 
Compiling a Pattern with Multiple Flags
Compiling a Pattern with Multiple Flags Compiling a Pattern with Multiple Flags      ... how to compile a pattern with multiple flags like multiline, case insensitivity
 
Pattern and Matcher
Java: Pattern and Matcher Java: Pattern and Matcher In addition to the regular expression methods... pattern can be reused by many Matcher objects. Pattern pat
 
Compiling a Pattern with Multiple Flags
Compiling a Pattern with Multiple Flags Compiling a Pattern with Multiple Flags      ... how to compile a pattern with multiple flags like multiline, case insensitivity
 
VoIP Providers
; VoIP Provided PC to phone services With our free VoIP provider search engine and VoIP services directory you will be able to find the cheapest Voice over IP service and compare, review and rank VoIP services from almost all known
 
Factory Pattern
Factory Pattern Factory Pattern  ....  Factory Pattern:  One of the goals of object-oriented design..., not what kind of subclass to create. Creational design pattern , more
 
Pattern resetting using regular expression
Pattern resetting using regular expression Pattern resetting using regular expression    ... are going to make program named Pattern_Resetting.java. The steps involved in program
 
GPS Smart Shoes
services. There are several companies involve in manufacturing different models... and enters the unsafe one, the service automatically sends an cell phone alert... that makes the user communicate to a central location tracking service. This in turn
 
Types of LBS
; At a Glance - Location based service enables us to find the geographical location of a mobile device with its user and provide various services related to the location... LBS. Location-based service uses several technologies for determining the exact
 
Web Site promotion services at roseindia.net
Web Site Promotion,Web Promotion in India,Web Promotion Company in India Welcome to RoseIndia.net Web Promotion Company Our Web site services will help you get listed
 
Java Building a Simple Web Service ? A Tutorial Tutorial
Building a Simple Web Service ? A Tutorial Building a Simple Web Service ? A Tutorial... service and a client web application using eclipse IDE along with Lomboz plug in. We
 
VoIP DSL
and landline phone services are in the process of unbundling DSL and phone services. Depending on who your DSL provider is, you may now choose to get only DSL service instead of needing to pay for phone services to get DSL service. The rate for DSL
 
LBS
Service (LBS) This is one of the most popular services based on a different... for various location based services.       ...; How does LBS work? Location based service (LBS) is the application
 
Conference Call Services - The Affordable Conference Call
Complete Hibernate 3 Conference Call Services - The Affordable Conference Call These services provides the interactive communication among several people at different locations. It may also
 
Singleton Design Pattern
Singleton Pattern Java,Singleton Design Pattern,Java Singleton Design Pattern Example Singleton Design Pattern... in java. The Singleton design pattern ensures that only one instance
 
Escaping Special Characters in a Pattern
Escaping Special Characters in a Pattern Escaping Special Characters in a Pattern                   
 
Escaping Special Characters in a Pattern
Escaping Special Characters in a Pattern Escaping Special Characters in a Pattern                   
 
Xcarecrows4 Web Services
Xcarecrows4 Web Services Xcarecrows4 Web Services.... Manage Web Applications Manage Tomcat users Deploy and undeploy Web Services Download new Web Applications Download new Web Services Apache Tomcat
 
VoIP Residential
of services they can offer their subscribers, service providers need a completely... now receive from circuit-switched voice service. Yes, virtual PBX services... over Internet Protocol (VoIP) solution allows service providers to deliver
 
WSMO Studio
Studio is an open source Semantic Web Service and Semantic Business Process modelling environment for the Web Service Modeling Ontology. WSMO Studio... Editor for WSMO elements (web services, goals, mediators) SAWSDL editor
 
Hibernate 3.1.1 Released
persistence and query service. Hibernate lets you develop persistent classes... to the cache key, so cached values get collected prematurely ForeignGenerator
 
Hibernate 3.1.2 Released
performance object/relational persistence and query service. Hibernate lets you develop... with ThreadLocalSessionContext * [HHH-1391] - Invalid parameter index SQLException when using named
 
Your homepage for Cheapest domain name registration, Domain Forwarding Services, Web Designing | Roseindia Domain Regist
Services, Web Designing | Roseindia Domain Registration DOMAIN REGISTRATION SERVICES... of cheapest Domain registration services in India. Roseindia has
 
Table Maintenance Statements
, Checksum Table, Optimize Table, Repair Table, Restore Table Syntax..., OPTIMIZE TABLE, REPAIR TABLE, RESTORE TABLE Syntax. ANALYZE TABLE Syntax... or split rows, repair the table.  If the index pages are not sorted, sort them
 
VoIP Comparisons
; The Best VoIP Internet Phone Services Although we provide an extensive table of comparative features for the three VoIP service providers...; VoIP Broadband phone Services Broadband phone services are the next
 
VoIP Billing Solution
Billing solution is the strategic choice for service providers seeking telco... services, billing for call usage, offer flexible call plans, and comprehensive call... to accommodate the demand and service providers to expand their service offerings
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.