Design Pattern Sample Application - Service Locator Design Pattern Sample Application - Service Locator


Date: 05-Nov-2004


Table of Contents

Introduction
Application Overview
Software Requirements
Terminology
Configuring the Application
Deploying and Running the Application
Sample Application Files
Additional References

Introduction

Prerequisite

To understand this sample application the user is expected to have knowledge in the following area,

Technical Overview

In an enterprise application, clients are always required to interact with various service components, such as Enterprise Javabeans (EJB) and Java Message Service (JMS) as they provide the business methods to run the service. This is possible only after locating a business service (referred as a lookup operation). J2EE uses the JNDI tree to lookup, access and invoke business services on passing a unique registered JNDI name. If the service is used by various clients, then the code for looking up the object gets duplicated in various forms which makes it difficult to maintain the application.
The Service Locator Design Pattern solves this issue by abstracting the complexities and network dependencies into a single Service Locator class. This service layer pattern can be used for storing lookup values for all services and provide it on request. A caching mechanism can also be incorporated within the service to enhance performance as the JNDI lookup is made once only.

Application Overview

This application demonstrates the implementation of Service Locator design pattern. In an enterprise application, generally there is a module for getting the feedback from the customer. The sample application targets this scenario to explain how to implement the Service Locator pattern.
In this application, the customer provides the feedback with his/her name and e-mail ID. Using an Entity bean (Feedback), the application first enters the feedback in the database table. After this is successfully executed, the information is entered in a queue (MailQueue). The lookup for the EJB, QueueConnectionFactory and the Queue is done through a Service Locator class. An MDB (MailMDB) is registered to listen to the queue. The MDB is asynchronously invoked and sends an e-mail to the customer thanking for the feedback.
The objects are also cached within the Service Locator class which improves performance as the lookup is done only once during the first request and not for subsequent requests.

The following class diagram shows the important classes in the Service Locator pattern.


The following sequence diagram depicts the interactions in this sample application.



Software Requirements

Following is the list of software's required for configuring and running this sample application.

Terminology

Term Definition

Directory where OC4J is installed. For example. D:\oc4j10g

Directory where JAVA is installed

Directory where Apache Ant is installed
The directory where the sample application is extracted.

Configuring the Application

  • Unzip the provided ServiceLocator.zip. This creates ServiceLocator folder with all the source files. We will refer to this folder as <SAMPLE_HOME>.
  • Install the database table. Connect to the SCOTT user in the database and run the following command
    SQL> @<SAMPLE_HOME>/sql/Install.sql
  • Edit <SAMPLE_HOME>/config/mail.properties file in your favorite editor. Change the value of SMTPServer to connect to your mail server.
  • To setup the database connection, edit the data-sources.xml file in the <SAMPLE_HOME>\src\META-INF directory and edit the following connection information within the <data-source>...</data-source> tags.
      <data-source 
    name="ServiceLocatorDataSource"
    class="com.evermind.sql.DriverManagerDataSource"
    location="jdbc/SLScottCoreDS"
    xa-location="jdbc/xa/SLScottXADS"
    ejb-location="jdbc/SLScottDS"
    pooled-location="jdbc/SLScottPooledDS"
    connection-driver="oracle.jdbc.driver.OracleDriver"
    username="scott"
    password="tiger"
    url="jdbc:oracle:thin:@<dbHostName>:<dbPort>:<dbSID>"
    inactivity-timeout="30"
    />
    Replace <dbHostName>:<dbPort>:<dbSID> in the url, with your database values and save the file. If required, replace the username and password attributes. where,

    dbHostName

    =

    hostname of the machine where Oracle database is running

    dbPort

    =

    port on the host machine on which the database listener is listening

    dbSID

    =

    SID of the database


 

Deploying and Running the Application

This section describes the steps required in deploying this application to the Standalone OC4J using ANT Tool and running using the browser.

Note: Make sure that the environment variables[<JAVA_HOME>, <ANT_HOME>/bin in the PATH;, <JAVA_HOME>/bin in the PATH] have been set before proceeding further. For more information on how to setup these environment variables, please refer environment set up readme document.

  • Edit <OC4J_HOME>/j2ee/home/config/jms.xml in your favorite XML editor. Add the following lines under the <jms-server> tag.
    <queue-connection-factory name="mailQueueConnectionFactory" location="jms/mailQueueConnectionFactory"/>
    <queue name="mailQueue" location="jms/mailQueue"/>
  • Ensure that OC4J is up and running. To start the OC4J server, navigate to <OC4J_HOME>/j2ee/home and execute the following command,
    > java -jar oc4j.jar
  • Open the <SAMPLE_HOME>/config/server.properties in a text editor. Change the following parameters to with your OC4J details.

    OC4J_HOME

    =

    Folder where OC4J is installed. Ex: D:\oc4j10g

    OC4J_HOST

    =

    Machine name or IP address on which OC4J is running

    OC4J_PORT

    =

    ORMI port on which your OC4J listens for ORMI requests

    OC4J_USERNAME

    =

    User name of OC4J server, default is admin.

    OC4J_PASSWORD

    =

    Password of OC4J server.


  • Build and deploy the EAR file using ANT. From <SAMPLE_HOME> directory, execute the command,
    > ant


  • Open your favorite browser and access the sample application, using the following URL,
    http://hostName:port/locator/Feedback.jsp

    where,
    <host_name> is the machine on which OC4J is running and <port> is Port in which the OC4J server listens to HTTP requests. By default OC4J listens for HTTP requests in port # 8888
    Example: http://localhost:8888/locator/Feedback.jsp

Sample Application Files 

This section provides a tabular listing of the sample application files, along with their respective directory locations and a description of what they do in the overall scheme of the application.

Directory File Description
Readme.html

This file

*.java
Java source files used in this sample application
*.jsp
JSP files used in this sample application
web.xml
Web deployment descriptor
mail.properties
The properties file that holds the mail server details
server.properties
The properties file that holds the OC4J server details
ejb-jar.xml
EJB deployment descriptor
orion-ejb-jar.xml
OC4J specific EJB deployment descriptor
data-sources.xml
Datasource configuration file
application.xml
Application deployment descriptor.
orion-application.xml
OC4J specific deployment descriptor.
build.xml
The ANT build file

 

Additional References 


Please enter your comments about this sample application here.

 

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy