Stateful Simple EJB Cart Sample Application

  Samples Index

 

Stateful Simple EJB Cart Sample Application
 

This document describes how to utilize the Stateful Simple EJB Cart sample application in conjunction with Sun Java  System Application Server Enterprise Edition 8.1 2005Q1. This document also describes how to test the high availability feature in appserver for Stateful session bean with web client and rich client.

This sample application document contains the following sections:

Overview

The cart sample demonstrates the use of stateful session bean. Typical use of a stateful session bean is a shopping cart. The session bean (CartEJB) represents a shopping cart in an online bookstore. The bean's client may add a book to the cart, remove a book, or retrieve the cart's contents. The sample is comprised of an EJB and a servlet. The appserver makes the Cart EJB highly available. If one server goes down the conversational state (i.e the cart contents) is migrated transparently to another available server instance in the cluster.
 

 
 

Compiling and Assembling the Sample Application



To recompile, assemble, and deploy the application, see the Sample Application Build Facility document for details on using a build facility to quickly perform these tasks.

To rebuild the entire application from scratch, follow these steps:

  1. Compile and Assemble Web Application. For example:
  2. Execute asant under <install_dir>/samples/ee-samples/failover/apps/sfsbfailover/simple/
    The default target core will be executed to rebuild the .jar and .ear files.
  3. Deploy the application.
  4. Once you have re-created the sample application from scratch proceed to Deploying the Sample Application.
  5. Clean the web application project area. For example:
  6. Execute asant clean
    This will remove the sample application assemble and build directories.

     
Deploying the Sample Application

A pre-built sfsbFailover.ear file is supplied with the application server installation. The pre-built sfsbFailover.ear file is an Enterprise Archive (.ear) file that contains, the EJB .jar file,  web application .war file and the client jar file of the application. Within each file resides the XML deployment descriptor files, application class files, and other content required by the application. You can use the pre-built .ear file to deploy the application. If you want to experience compiling and assembling the application from scratch, follow the instructions in Compiling and Assembling the Sample Application.

To deploy the application, the administrative server needs to be running. To start the administrative server if it is not already started, do one of the following, depending on your oeprating system.

Select one of the following approaches to deploying the application: Command Line-based Deployment

Command Line Interface is the fastest means of deploying the application. If you want to deploy through a GUI tool, follow the instructions for GUI Based Deployment. CLI deployment of the application can be done by either the Using the asant script or by Using the asadmin command.
 
 

Using the asant script
 

  1. Go to the src directory of the sample. For example:
  2. cd <install_dir>/samples/ee-samples/failover/apps/sfsbfailover/simple
  3. Run asant using deploy task. For example:
  4. % asant deploy
    It is an interactive command that may prompt you for the admin-server host, admin-server port, admin user name, admin password, and appserv instance. This also registers the resources to the Sun Java(TM) System Application Server.

     
Using the asadmin command
 
  1. Go to the root of the sample directory. For example:
  2. cd <install_dir>/samples/ee-samples/failover/apps/sfsbfailover/simple
  3. Execute asadmin to deploy application to the local application server instance. For example:
  4. asadmin deploy --user <adminuser> --password <adminpassword> --host <DAShost> -port <admin server port> --availabilityenabled=true --target <cluster-name> sfsbFailover.ear


Where <adminuser> is the Domain Administration Server's admin username, <adminpassword> is the admin user's password, <DAShost> is the machine on which Domain Administration Server is installed and running, <DASport> is port at which Domain Administration Server is running and target> is the application cluster on which the sample application is to be deployed. If you would like to verify the registration of the application, you may proceed to Verifying Deployment. Otherwise, proceed to Running the Sample Application.
 


GUI-based Deployment

Since a pre-built Enterprise Archive (EAR) file for the sample application is included with the application server, you can use the Sun Java (tm) System Application Server Administration Tool to quickly deploy it to the Application Server.

     
  1. From your web browser, access the Sun Java (tm) System Application Server by entering the URL. For example:
  2. https://<DAShost>:<DASport>
  3. Enter the server administrator's username and password to access the admin server.
  4. On the left panel, navigate the tree by clicking on the following:
  5. Cluster -> <cluster-name>
  6. On the right panel, the Applications tab will be pre-selected. In the " --New--" drop down list select "Enterprise Application". The "Deployment Page" will load in the right frame.
  7. Set "Upload" to "Yes" and click on `Browse' to select the .ear file (from disk) to be deployed (sfsbFailover.ear) Click `Next' on the top right hand corner of the frame. .
  8. Enter the application name as sfsbFailover. Enable "Availability" checkbox and add a target cluster to deploy the application on from the displayed list in the "Targets" section.
  9. Click OK


  If the deployment succeeds, you will see the `Enterprise Applications' panel and under `Deployed Enterprise Applications' you will see the sfsbFailover application as deployed.


Verifying Deployment


As an optional step, you can use the Sun Java(TM) System Application Server Administration Tool to verify that the application has been registered. Otherwise, proceed directly to Running the Sample Application.

To verify the registration of the application, follow these steps:

  1. Execute the command asadmin list-components to look at applications deployed with a server instance. For example:
  2. asadmin list-components --user <adminuser> --password <adminpassword> --host <DAShost> --port DASport> --target <cluster-name>

Generating Javadocs

To better understand the sample application source code, you can refer to the related javadocs.

To generate javadocs run one of the following commands:

asant javadocs

or

asant all

After javadocs are generated, you can access them at <install_dir>/samples/ee-samples/failover/apps/sfsbfailover/simple/javadocs/index.html
 
 

Running the Sample Application


1.   Web-based client.

To see the failover of the SFSB happening, configure your loadbalancer in the web server for the cluster comprising multiple appserver instances. This is needed to make request independent of which server instance is serviceing the request.

From your browser, access the application page from http://Whost:Wport/sfsbFailover
where Whost is the hostname of webserver having the loadbalancer plugin setup and configured and Wport is the port at which that webserver is running.


Screen 1 You now enter the sfsbFailover bookstore . Enter the name of a book and add the book to your cart by clicking on 'Add Book To Cart'.
Similarly, you can remove a book from your cart. The updated contents of the cart are shown at the bottom of the page.

Screen 2


To test the failover of the SFSB happening,  add few books to the cart. The contents of the cart are shown. Then stop the appserver instance which served the previous requests.Add another book to your cart. The request is now served by another available server instance in the cluster. The conversational state of the session bean is transparently migrated to this server.You can see book is added to the the original contents of the cart.You can also test the failover behaviour by removing the book from the cart.



2.  RMI/IIOP-based client with ACC.

The Java client will be run on the same machine where the Application Server is running.
  • Modify XML configuration file of <install_dir>/domains/domain1/config/sun-acc.xml for ORB host and port. For example,
         .
         .
         .
         </message-security-config>
        <property name="com.sun.appserv.iiop.loadbalancingpolicy" value="ic-based" />
        <property name="com.sun.appserv.iiop.endpoints" value="instance-ONE-host:instance-ONE-orb-port,instance-TWO-host:instance-TWO-orb-port"/>
       </client-container>

    The values for the orb ports can be see from <install_dir>/samples/ee-samples/cluster.properties file.

  • Get client file, i.e.

      <as_install_dir>/domains/domain1/applications/j2ee-apps/sfsbFailover/sfsbFailoverClient.jar

  • Go to the directory,

      <as_install_dir>/bin

  • Run client application, e.g.

      appclient -client <path>/sfsbFailoverClient.jar

If the Java client is running on a different machine than where the Application Server is running, then follow these steps to package the APplication client container.
 

Copyright © 2004 Sun Microsystems, Inc. All rights reserved.
Last Updated October 25, 2004