+ All Categories
Home > Documents > EJb Design Pattern

EJb Design Pattern

Date post: 09-Apr-2018
Category:
Upload: muralidharpalda
View: 226 times
Download: 0 times
Share this document with a friend

of 16

Transcript
  • 8/8/2019 EJb Design Pattern

    1/16

    Detailed Description

    See the Core J2EE Patterns

    Detailed Example

    The Java Pet Store sample application, v1.3.1 has two service locators: a Web-tier classServiceLocator, and an Enterprise JavaBeansTM (EJB) tier class, also calledServiceLocator

    . Both classes manage lookup and caching of enterprise bean home interfaces, JMS and database

    connection factories, and environment entries within their respective tiers. The only differencebetween them is that the Web-tier class is a singleton, and it caches the objects it looks up. The

    EJB-tier class is not a singleton, and does not cache.

    The following code discussion uses examples from the Web-tierServiceLocator:

    Clients use ServiceLocator to access services.

    The sample application class AdminRequestBDis a business delegate that uses the Web-

    tierServiceLocator to access the order processing center enterprise bean

    OPCAdminFacade. (See the Business Delegatedesign pattern for a more detailed

    description ofAdminRequestBD.)

    Figure 1 is a structure diagram that demonstrates how AdminRequestBD uses

    ServiceLocator to find the remote home interface of the OPCAdminFacade enterprise

    bean. The ServiceLocator returns a the remote enterprise bean interface

    OPCAdminFacadeHome, by either retrieving it from the cache or looking it up using an

    internal InitialContext instance. The client then uses the OPCAdminFacade to find or

    create a remote component interface to an OPCAdminFacade

    Figure 1. Structure diagram of ServiceLocator sample code

    http://java.sun.com/blueprints/corej2eepatterns/Patterns/ServiceLocator.htmlhttp://java.sun.com/blueprints/code/index.html#petstorehttp://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/servicelocator/web/ServiceLocator.java.htmlhttp://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/servicelocator/ejb/ServiceLocator.java.htmlhttp://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/servicelocator/ejb/ServiceLocator.java.htmlhttp://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/servicelocator/web/ServiceLocator.java.htmlhttp://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/servicelocator/web/ServiceLocator.java.htmlhttp://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/admin/web/AdminRequestBD.java.htmlhttp://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/opc/admin/ejb/OPCAdminFacade.java.htmlhttp://java.sun.com/blueprints/patterns/BusinessDelegate.htmlhttp://java.sun.com/blueprints/patterns/BusinessDelegate.htmlhttp://java.sun.com/blueprints/code/index.html#petstorehttp://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/servicelocator/web/ServiceLocator.java.htmlhttp://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/servicelocator/ejb/ServiceLocator.java.htmlhttp://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/servicelocator/web/ServiceLocator.java.htmlhttp://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/admin/web/AdminRequestBD.java.htmlhttp://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/opc/admin/ejb/OPCAdminFacade.java.htmlhttp://java.sun.com/blueprints/patterns/BusinessDelegate.htmlhttp://java.sun.com/blueprints/corej2eepatterns/Patterns/ServiceLocator.html
  • 8/8/2019 EJb Design Pattern

    2/16

    In the following code excerpt, AdminRequestBD calls the ServiceLocator static method

    getInstance to get the singleton instance of the service locator, then calls

    getRemoteHome to get the remote home interface of the OPCAdminFacade enterprise

    bean. Notice that the caller must typecast the remote home interface toOPCAdminFacadeHome because getRemoteHome returns type EJBHome.

    public class AdminRequestBD {...public AdminRequestBD() throws AdminBDException {try {OPCAdminFacadeHome home =(OPCAdminFacadeHome)

    ServiceLocator.getInstance().getRemoteHome(OPC_ADMIN_NAME,OPCAdminFacadeHome.class);

    opcAdminEJB = home.create();} catch (ServiceLocatorException sle) {...}

    }

    The service locator greatly simplifies the lookup of the enterprise bean home interface.

    The singleton and caching strategies (discussed below) also improve performance,

    because they avoid constructing unnecessary InitialContext and enterprise bean home

    interfaces.

    Public methods look up distributed resources.

    The public methods of the service locator look up distributed resources by their JNDI

    names. There are methods that find and return enterprise bean local home interfaces,

    JDBCTM data sources, JMS queues and topics, and JMS queue and topic connectionfactories. There are also convenience methods that look up and perform type conversions

    on environment entries.

    As an example, method getLocalHome (for finding enterprise bean local home

    interfaces) appears below. Each method that locates a particular type of resource returns

    either a cached reference to the requested resource, or uses JNDI to find the resource,

    placing a reference in the cache before returning it.

    // Enterprise bean lookupspublic EJBLocalHome getLocalHome(String jndiHomeName)throws ServiceLocatorException {EJBLocalHome home = null;try {if (cache.containsKey(jndiHomeName)) {

    home = (EJBLocalHome) cache.get(jndiHomeName);

    http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/ejb/EJBHome.htmlhttp://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/ejb/EJBHome.html
  • 8/8/2019 EJb Design Pattern

    3/16

    } else {home = (EJBLocalHome) ic.lookup(jndiHomeName);cache.put(jndiHomeName, home);

    }} catch (NamingException ne) {

    throw new ServiceLocatorException(ne);} catch (Exception e) {

    throw new ServiceLocatorException(e);}return home;

    }

    Methods that return enterprise bean home interface references are only type-safe to the

    platform interface level; for example, getLocalHome returns a EJBLocalHome, but the

    client must typecast the result.

    Method getRemoteHome is similar to getLocalHome, except that it returns an enterprise

    bean remote, instead of local, home interface. It also requires a reference to a class object

    for the specific remote home interface, because remote home lookups use methodPortableRemoteObject.narrow to perform the type conversion from the object

    returned from the JNDI lookup to the actual home interface type. The client that callsgetRemoteHome must still typecast the result to the remote home interface type, as shown

    in the first example above.

    public EJBHome getRemoteHome(String jndiHomeName, Class className)throws ServiceLocatorException {EJBHome home = null;try {if (cache.containsKey(jndiHomeName)) {

    home = (EJBHome) cache.get(jndiHomeName);} else {

    Object objref = ic.lookup(jndiHomeName);Object obj = PortableRemoteObject.narrow(objref, className);home = (EJBHome)obj;cache.put(jndiHomeName, home);

    }} catch (NamingException ne) {

    throw new ServiceLocatorException(ne);} catch (Exception e) {

    throw new ServiceLocatorException(e);}

    return home;}

    As mentioned above, the service locator returns JMS resources, JDBC data sources, andperforms type conversion on values in environment entries. The table below summarizes

    the names and return types of these methods.

  • 8/8/2019 EJb Design Pattern

    4/16

    Table 1. Additional ServiceLocator methods

    Method

    Name

    Return

    Type

    Resource

    Type

    getQueueConnectionFactory QueueConnectionFactory JMS

    getQueue Queue JMS

    getTopicConnectionFactory TopicConnectionFactory JMS

    getTopic Topic JMS

    getDataSource DataSource JDBC

    getUrl URL env-entry

    getBoolean boolean env-entry

    getString String env-entry

    Improving performance with the Singleton pattern and caching.

    The Singleton pattern [ GHJV95] ensures that only a single instance of a class exists in an

    application. The meaning of the term "singleton" is not always clear in a distributed

    environment; in ServiceLocator it means that only one instance of the class exists per

    class loader.

    The Singleton pattern improves performance because it eliminates unnecessary

    construction ofServiceLocator objects, JNDI InitialContext objects, and enables

    caching (see below).

    The Web-tier service locator also improves performance by caching the objects it finds.The cache lookup ensures that a JNDI lookup only occurs once for each name.

    Subsequent lookups come from the cache, which is typically much faster than a JNDIlookup.

    The code excerpt below demonstrates how the ServiceLocator improves performance

    with the Singleton pattern and an object cache.

    public class ServiceLocator {

    private InitialContext ic;

    private Map cache;

    private static ServiceLocator me;

    static {try {me = new ServiceLocator();

    } catch(ServiceLocatorException se) {System.err.println(se);se.printStackTrace(System.err);

    http://java.sun.com/blueprints/patterns/references.html#GHJV95http://java.sun.com/blueprints/patterns/references.html#GHJV95
  • 8/8/2019 EJb Design Pattern

    5/16

    }}private ServiceLocator() throws ServiceLocatorException {

    try {ic = new InitialContext();cache = Collections.synchronizedMap(new HashMap());

    } catch (NamingException ne) {throw new ServiceLocatorException(ne);

    }}

    static public ServiceLocator getInstance() {return me;

    }

    A private class variable me contains a reference to the only instance of the

    ServiceLocator class. It is constructed when the class is initialized in the static

    initialization block shown. The constructor initializes the instance by creating the JNDI

    InitialContext and the HashMap that is used a cache. Note that the no-argumentconstructor is private: only class ServiceLocator can construct a ServiceLocator.

    Because only the static initialization block creates the instance, there can be only oneinstance per class loader.

    Classes that use service locator access the singleton ServiceLocator instance by calling

    public method getInstance.

    Each object looked up has a JNDI name which, being unique, can be used as a cacheHashMap key for the object. Note also that the HashMap used as a cache is synchronized

    so that it may be safely accessed from multiple threads that share the singleton instance.

    EX 2---Design Pattern Sample Application - Service Locator

    Date: 05-Nov-2004

    Table of Contents

    Introduction

    Application Overview

    Software RequirementsTerminology

    Configuring the ApplicationDeploying and Running the Application

    Sample Application FilesAdditional References

    Introduction

    Prerequisite

    http://www.oracle.com/technology/sample_code/tech/java/j2ee/designpattern/businesstier/servicelocator/readme.html#introhttp://www.oracle.com/technology/sample_code/tech/java/j2ee/designpattern/businesstier/servicelocator/readme.html#overhttp://www.oracle.com/technology/sample_code/tech/java/j2ee/designpattern/businesstier/servicelocator/readme.html#reqnhttp://www.oracle.com/technology/sample_code/tech/java/j2ee/designpattern/businesstier/servicelocator/readme.html#termshttp://www.oracle.com/technology/sample_code/tech/java/j2ee/designpattern/businesstier/servicelocator/readme.html#configurehttp://www.oracle.com/technology/sample_code/tech/java/j2ee/designpattern/businesstier/servicelocator/readme.html#deployhttp://www.oracle.com/technology/sample_code/tech/java/j2ee/designpattern/businesstier/servicelocator/readme.html#fileshttp://www.oracle.com/technology/sample_code/tech/java/j2ee/designpattern/businesstier/servicelocator/readme.html#addrefhttp://www.oracle.com/technology/sample_code/tech/java/j2ee/designpattern/businesstier/servicelocator/readme.html#prehttp://www.oracle.com/technology/sample_code/tech/java/j2ee/designpattern/businesstier/servicelocator/readme.html#introhttp://www.oracle.com/technology/sample_code/tech/java/j2ee/designpattern/businesstier/servicelocator/readme.html#overhttp://www.oracle.com/technology/sample_code/tech/java/j2ee/designpattern/businesstier/servicelocator/readme.html#reqnhttp://www.oracle.com/technology/sample_code/tech/java/j2ee/designpattern/businesstier/servicelocator/readme.html#termshttp://www.oracle.com/technology/sample_code/tech/java/j2ee/designpattern/businesstier/servicelocator/readme.html#configurehttp://www.oracle.com/technology/sample_code/tech/java/j2ee/designpattern/businesstier/servicelocator/readme.html#deployhttp://www.oracle.com/technology/sample_code/tech/java/j2ee/designpattern/businesstier/servicelocator/readme.html#fileshttp://www.oracle.com/technology/sample_code/tech/java/j2ee/designpattern/businesstier/servicelocator/readme.html#addrefhttp://www.oracle.com/technology/sample_code/tech/java/j2ee/designpattern/businesstier/servicelocator/readme.html#pre
  • 8/8/2019 EJb Design Pattern

    6/16

    Technical Overview

    Prerequisite

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

    area,

    JSP

    EJB

    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 businessservice (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 variousclients, then the code for looking up the object gets duplicated in various forms which makes itdifficult 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 forstoring 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 anenterprise 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.

    http://www.oracle.com/technology/sample_code/tech/java/j2ee/designpattern/businesstier/servicelocator/readme.html#techhttp://www.oracle.com/technology/sample_code/tech/java/jsps/index.htmlhttp://www.oracle.com/technology/sample_code/tech/java/ejb_corba/index.htmlhttp://www.oracle.com/technology/sample_code/tech/java/j2ee/designpattern/businesstier/servicelocator/readme.html#techhttp://www.oracle.com/technology/sample_code/tech/java/jsps/index.htmlhttp://www.oracle.com/technology/sample_code/tech/java/ejb_corba/index.html
  • 8/8/2019 EJb Design Pattern

    7/16

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

  • 8/8/2019 EJb Design Pattern

    8/16

    Software Requirements

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

    Oracle Containers for J2EE (OC4J) - version 9.0.4

    andANT - version 1.5 or above for building the EAR fileandJ2ESDK1.4.x

    Oracle Database 10g

    Terminology

    Term Definition

    http://www.oracle.com/technology/software/content.htmlhttp://ant.apache.org/http://ant.apache.org/http://java.sun.com/productshttp://java.sun.com/productshttp://www.oracle.com/technology/software/content.htmlhttp://www.oracle.com/technology/software/content.htmlhttp://www.oracle.com/technology/software/content.htmlhttp://www.oracle.com/technology/software/content.htmlhttp://ant.apache.org/http://java.sun.com/productshttp://www.oracle.com/technology/software/content.html
  • 8/8/2019 EJb Design Pattern

    9/16

  • 8/8/2019 EJb Design Pattern

    10/16

    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[, /bin in the

    PATH;, /bin in the PATH] have been set before proceeding further. For moreinformation on how to setup these environment variables, please referenvironment set up

    readme document.

    Edit /j2ee/home/config/jms.xml in your favorite XML

    editor. Add the following lines under the tag.

    Ensure that OC4J is up and running. To start the OC4J server, navigate to/j2ee/home and execute the following command,

    > java -jar oc4j.jar

    Open the /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. Fromdirectory, execute the command,

    > ant

    Open your favorite browser and access the sample application, using thefollowing URL,

    http://hostName:port/locator/Feedback.jsp

    http://www.oracle.com/technology/sample_code/tech/java/j2ee/designpattern/businesstier/servicelocator/envsetup.htmlhttp://www.oracle.com/technology/sample_code/tech/java/j2ee/designpattern/businesstier/servicelocator/envsetup.html
  • 8/8/2019 EJb Design Pattern

    11/16

    where,

    is the machine on which OC4J is running and is Port in

    which the OC4J server listens to HTTP requests. By default OC4J listens forHTTP 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 respectivedirectory locations and a description of what they do in the overall scheme of the application.

    Directory File Description

    ServiceLocator\doc Readme.html This file

    ServiceLocator\src\oracle\otnsamples\designpatterns\servicelocator *.java

    Java source

    files used inthis sample

    application

    ServiceLocator\public_html *.jsp

    JSP files

    used in thissample

    application

    ServiceLocator\public_html\WEB-INF\web.xml web.xml

    Web

    deploymentdescriptor

    ServiceLocator\config mail.properties

    Theproperties

    file that holds

    the mailserver details

    ServiceLocator\configserver.propertie

    s

    The

    properties

    file that holds

    the OC4Jserver details

    ServiceLocator\src\META-INF ejb-jar.xml

    EJB

    deployment

    descriptor

    ServiceLocator\src\META-INF orion-ejb-

    jar.xml

    OC4J

    specific EJBdeployment

  • 8/8/2019 EJb Design Pattern

    12/16

    descriptor

    ServiceLocator\src\META-INFdata-

    sources.xml

    Datasourceconfiguration

    file

    ServiceLocator\src\META-INF application.xml

    Application

    deploymentdescriptor.

    ServiceLocator\src\META-INForion-

    application.xml

    OC4Jspecific

    deployment

    descriptor.

    ServiceLocator build.xmlThe ANTbuild file

    Business Delegate

    Brief Description

  • 8/8/2019 EJb Design Pattern

    13/16

    In distributed applications, lookup and exception handling for remote business components can

    be complex. When applications use business components directly, application code must change

    to reflect changes in business component APIs.

    These problems can be solved by introducing an intermediate class called a business delegate,

    which decouples business components from the code that uses them. The Business Delegatepattern manages the complexity of distributed component lookup and exception handling, and

    may adapt the business component interface to a simpler interface for use by views.

    Detailed Description

    See the Core J2EETM Patterns

    Detailed Example

    Sample application business delegate class AdminRequestBDhandles distributed

    lookup and catches and adapts exceptions in the sample application orderprocessing center (OPC).

    The AdminRequestBD business delegate manages distributed componentlookup and handles exceptions.

    The structure diagram in Figure 1 shows the ApplRequestProcessor servlet using

    AdminRequestBD to find and use distributed business components.

    Figure 1. AdminRequestBD locates and adapts other business

    components

    The code sample below shows the constructor forAdminRequestBD. It uses class

    ServiceLocatorto acquire the remote home interface of session facade

    OPCAdminFacade. (See the Service Locatorand Session Facadedesign patterns.) It uses

    the remote home interface to create a OPCAdminFacade remote component interface,

    which it maintains in a private field. The block that locates and creates the enterprise

    http://java.sun.com/blueprints/corej2eepatterns/Patterns/BusinessDelegate.htmlhttp://java.sun.com/blueprints/corej2eepatterns/Patterns/BusinessDelegate.htmlhttp://java.sun.com/blueprints/corej2eepatterns/Patterns/BusinessDelegate.htmlhttp://java.sun.com/blueprints/corej2eepatterns/Patterns/BusinessDelegate.htmlhttp://java.sun.com/blueprints/corej2eepatterns/Patterns/BusinessDelegate.htmlhttp://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/admin/web/AdminRequestBD.java.htmlhttp://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/servicelocator/web/ServiceLocator.java.htmlhttp://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/opc/admin/ejb/OPCAdminFacade.java.htmlhttp://java.sun.com/blueprints/patterns/ServiceLocator.htmlhttp://java.sun.com/blueprints/patterns/ServiceLocator.htmlhttp://java.sun.com/blueprints/patterns/SessionFacade.htmlhttp://java.sun.com/blueprints/patterns/SessionFacade.htmlhttp://java.sun.com/blueprints/corej2eepatterns/Patterns/BusinessDelegate.htmlhttp://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/admin/web/AdminRequestBD.java.htmlhttp://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/servicelocator/web/ServiceLocator.java.htmlhttp://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/opc/admin/ejb/OPCAdminFacade.java.htmlhttp://java.sun.com/blueprints/patterns/ServiceLocator.htmlhttp://java.sun.com/blueprints/patterns/SessionFacade.html
  • 8/8/2019 EJb Design Pattern

    14/16

    bean reference catches exceptions related to finding the home interface and to creating

    the component interface. Any exception that occurs is then wrapped in anAdminBDException, which effectively hides the implementation details of the business

    delegate from its clients.

    public AdminRequestBD() throws AdminBDException {try {

    OPCAdminFacadeHome home = (OPCAdminFacadeHome)ServiceLocator.getInstance().getRemoteHome(OPC_ADMIN_NAME,OPCAdminFacadeHome.class);

    opcAdminEJB = home.create();} catch (ServiceLocatorException sle) {

    throw new AdminBDException(sle.getMessage());} catch (CreateException ce) {

    throw new AdminBDException(ce.getMessage());} catch (RemoteException re) {

    throw new AdminBDException(re.getMessage());}

    }

    The OPC request processor uses AdminRequestBD for simple access to businesscomponents components.

    OPC servlet class ApplRequestProcessorreceives service requests from the admin

    client in the form of XML messages transmitted using HTTP. One of these request is for

    statistics about orders that have a given status.

    Method ApplRequestProcessor.getOrders receives part of an XML DOM tree

    representing a Web service request. It extracts the status code from the document anduses AdminRequestBD.getOrdersByStatus to retrieve a list of order information. The

    interface to that list is transfer object interface OrdersTO. (See the Transfer Object

    pattern.) The code in the request processor that retrieves the order information appears inthe following code sample.

    public class ApplRequestProcessor extends HttpServlet {...

    String getOrders(Element root) {try {AdminRequestBD bd = new AdminRequestBD();NodeList nl = root.getElementsByTagName("Status");String status = getValue(nl.item(0));OrdersTO orders = bd.getOrdersByStatus(status);

    ...}

    http://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/admin/web/AdminBDException.java.htmlhttp://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/admin/web/ApplRequestProcessor.java.htmlhttp://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/opc/admin/ejb/OrdersTO.java.htmlhttp://java.sun.com/blueprints/patterns/TransferObject.htmlhttp://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/admin/web/AdminBDException.java.htmlhttp://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/admin/web/ApplRequestProcessor.java.htmlhttp://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/opc/admin/ejb/OrdersTO.java.htmlhttp://java.sun.com/blueprints/patterns/TransferObject.html
  • 8/8/2019 EJb Design Pattern

    15/16

    Because it has already created the reference to an OPCAdminFacadeEJB, the

    AdminRequestBD object can simply forward the call to the enterprise bean's method

    getOrdersByStatus, as follows:

    public class AdminRequestBD {...

    public OrdersTO getOrdersByStatus(String status)throws AdminBDException {

    try {return opcAdminEJB.getOrdersByStatus(status);

    } catch (RemoteException re) {throw new AdminBDException(re.getMessage());

    } catch (OPCAdminFacadeException oafee) {throw new AdminBDException(oafee.getMessage());

    }}

    ...}

    Notice again that the method catches any exceptions that the enterprise bean may throw

    and re-throws an exception type that is specific to the business delegate's interface. Thishides the business delegate's implementation details from the client.

    J2EE DESIGN PATTERNS - Business Delegate

    When the presentation-tier components access the business services implemented by thebusiness-directly through the exposed API of the services. The clients are exposed to the

    complexity of dealing with distributed components. The client is tightly coupled to the EJBlayer, creating dependencies between client and server that affect both development, run-

    time and project management concerns. Due to this

    1. The presentation-tier components are vulnerable to changes in the implementation of thebusiness services, when ever the implementation of the business services change, the

    exposed implementation code in the presentation tier must change too.

    2. All the client side components have to be aware of the location details of the business

    services say that each components has to use the JNDI lookup service to locate the requiredremote interfaces.

    3. There may be a detrimental impact on network performance because presentation-tiercomponents that use the business service API make too many invocations over the network.

    This happens when presentation-tier components use the underlying API directly, with noclient-side caching mechanism or aggregating service.

    The solution is to create a layer of Business Delegate to reduce coupling between

    presentation-tier clients and business services. The Business Delegate hides the underlyingimplementation details of the business service, such as lookup and access details of the EJB

    architecture.

  • 8/8/2019 EJb Design Pattern

    16/16

    Business delegate handle all the code that access the business services in the selected

    vendor software. When the vendor or the vendor software changes only changes that needto be made is to the companys allocation software are changes to Business delegate, to

    access the business services. The presentation layer need not to be modified.

    The business delegate object abstracts the business services API and provides standardinterface to al the client components. It hides the underlying implementation detail such aslookup mechanism and the API of the business services. This reduces the coupling between

    the clients and the business services.

    Another benefit is that the delegate may cache results and references to remote business

    services. Caching can significantly improve performance, because it limits unnecessary andpotentially costly round trips over the network.

    The responsibilities of the components participating in this pattern are:

    Client components : The client components which are jsp pages and servlets in the

    presentation tier, delegate the work of locating the business service providers and the work

    of invoking the business service API methods to the Business Delegate objects.

    Business Delegate : The business Delegate acts as a representative of the clinet

    components. It knows how to look up and access the business services. It invokes theappropriate business services method in the required order . If the API of the business

    service component changes, only Business Delegate needs to be modified without affecting

    the client components.

    Business Service : A business service component implements the actual business logic.

    Some examples of the business service components are stateless session EJB and entity EJBa CORBA object or an RPC server.


Recommended