All Examples  All EJB Examples

Package examples.ejb.basic.statefulSession

Enterprise JavaBean stateful session
example package and classes

about this example

This example is a package that demonstrates an Enterprise JavaBean. Please run this example before attempting to create your own Enterprise JavaBeans, as it will show you the different steps involved. The example is a stateful session EJBean called TraderBean.

The example demonstrates:

The Client application performs these steps:
  1. Contacts the Trader home ("TraderHome") through JNDI to find the EJBean
  2. Creates a Trader ("Terry")
  3. The application then starts a loop that it executes five times:
    1. Buys shares of "WEBL"
    2. Sells shares of "INTL"
    3. Gets the change in the cash account
      (The EJBean does not actually "buy" or "sell"; it simulates the actions of accessing a database)
  4. Gives the change in the cash account
  5. Removes the Trader
The application demonstrates how repeated calls to the same session EJBean have a persistent state -- the change in the cash account -- that is maintained across all the calls. Notice that neither the client nor the EJBean do anything to maintain that state: the container handles it transparently. All the logic for the cash account is encapsulated in the EJBean, unlike the stateless session example, where all persistence is provided by the client.

The MultiClient application performs similar steps by launching multiple threads.

Passivation and persistent storage

You can change the parameters in the deployment descriptor, and see instances of the bean passivated using file persistence by running the MultiClient. You'll want to change these properties to values that will trigger passivation:
    maxBeansInFreePool            10
    maxBeansInCache               10
    idleTimeoutSeconds            1
    persistentDirectoryRoot       c:\mystore
You should set the persistentDirectoryRoot to an appropriate location for your installation. When you run the example, passivated beans will be stored (for the example above) in c:\mystore\examples_ejb_basic_statefulSession_TraderBean.dat.

how to use this example

To get the most out of this example, first read through the source code files to see what is happening. Start with DeploymentDescriptor.txt to find the general structure of the EJBean, which classes are used for the different objects and interfaces, then look at Client.java to see how the application works.

In general, you'll need to adjust certain properties to match your setup. You'll need to edit the entry for the property that begins with "weblogic.ejb.deploy" in the weblogic.properties file to deploy the EJBean. The property is commented out in the default properties file; make sure that you uncomment out all the lines of the property.

This example is shipped "pre-built"; you can either run it as shipped, or build the example and run it to test that you are able to successfully build and run EJBeans.

These three sections cover what to do:

  1. Build the example
  2. Set your environment
  3. Run the example

Build the example

Set up your development environment as described in Setting your development environment.

We provide separate build scripts for Windows NT and UNIX:

The "build" scripts build individual examples, such as this entry for Windows:

$ build basic statefulSession
To build under Microsoft's JDK for Java, use
$ build statefulSession -ms
These scripts will build the example and place the files in the correct locations:

Set your environment

Deploy the EJBean by adding the path to the .jar file to the "weblogic.ejb.deploy" property.

We provide a commented-out version in the property that begins with "weblogic.ejb.deploy" that you can use. You'll need to adjust the property depending on which EJBeans you're building and are deploying, or if the location of the files differs from the installed location.

Note: If you're running under the Microsoft SDK for Java, you'll also need to add the path to the .jar to the CLASSPATH for your WebLogic Server.

Run the example

  1. Start the WebLogic Server. You can check that the EJBean has been deployed correctly either by checking the server command line window, or by opening the Console and examining "EJB" under the "Distributed objects"; you should see statefulSession.TraderHome deployed, and can monitor its activity.

  2. Run the client in a separate command line window. Set up your client as described in Setting your development environment, and then run the client by entering:
    $ java examples.ejb.basic.statefulSession.Client

    If you're not running the WebLogic Server with its default settings, you will have to run the client using:

    $ java examples.ejb.basic.statefulSession.Client "t3://WebLogicURL:Port"

    where:

    WebLogicURL
    Domain address of the WebLogic Server
    Port
    Port that is listening for connections (weblogic.system.ListenPort)

    Parameters are optional, but if any are supplied, they are interpreted in this order:

    Parameters:
    url - URL such as "t3://localhost:7001" of Server
    user - User name, default null
    password - User password, default null
    accountID - String Account ID to test, default "10020"

  3. If you're running the Client example, you should get output similar to this from the client application:
    Begin basic.statefulSession...
    
    Creating trader Terry
    
    Start of Transaction 1 for Erin
    Buying 100 of WEBL
    ...Bought 100 at $10.0
    Selling 200 of INTL
    ...Sold 200 at $15.0
    Change in Cash Account: $2000.0
    End of Transaction 1
    
    Start of Transaction 2 for Erin
    Buying 200 of WEBL
    ...Bought 200 at $10.0
    Selling 300 of INTL
    ...Sold 300 at $15.0
    Change in Cash Account: $4500.0
    End of Transaction 2
    
    Start of Transaction 3 for Erin
    Buying 300 of WEBL
    ...Bought 300 at $10.0
    Selling 400 of INTL
    ...Sold 400 at $15.0
    Change in Cash Account: $7500.0
    End of Transaction 3
    
    Start of Transaction 4 for Erin
    Buying 400 of WEBL
    ...Bought 400 at $10.0
    Selling 500 of INTL
    ...Sold 500 at $15.0
    Change in Cash Account: $11000.0
    End of Transaction 4
    
    Start of Transaction 5 for Erin
    Buying 500 of WEBL
    ...Bought 500 at $10.0
    Selling 600 of INTL
    ...Sold 500 at $15.0
    Change in Cash Account: $13500.0
    End of Transaction 5
    
    Change in Cash Account: $13500.0
    
    Removing trader Terry
    
    End basic.statefulSession...
Notice that when the trading limit of 500 shares was exceeded in Transaction 5, the actual sale was reduced to the limit. The change in the cash account is maintained across all the transactions automatically by the EJBean. You can also run the MultiClient (multiple client application):
 $ java examples.ejb.basic.statefulSession.MultiClient
Note: you can only run one instance of the MultiClient at a time; it creates the multiple clients as separate threads, and is dependent on there being only one instance of the MultiClient to monitor the outcome of the example.

there's more

Read more about EJB in the Developers Guide, Using WebLogic Enterprise JavaBeans.

Copyright © 1997-1999 BEA Systems, Inc. All rights reserved.

Last updated 09/09/1999