Tutorials
EnterPrise Java Beans
Tutorial Index Page

Stateless Session Beans

In the stock-quoting session bean we just created, we have a data member named "stock".  The client of the session bean can specify a particular stock name, and the bean will keep track of it, so that when the client calls getPrice(), the price will be returned for that particular stock.

If we had ten clients using ten session beans simultaneously, the "stock" would have been distinct for each session bean.  The server would maintain ten separate instances of the stock name.

The "stock" in this  case, is considered a "state" of the bean.  We specified "stateful" when creating the bean.  That simply means that the bean maintains distinct data for each client.

If the bean does not need to maintain separate data items for each client, we can use a "stateless" session bean instead.  This can be more efficient, because the server does not need to maintain separate data for each bean client.

A stateless bean can also have member variables.  But if one client changes the member variable, the change can be seen by other clients (somewhat like static data in a class.)  However, this behavior cannot be depended upon.  It is up to the EJB container to use only a single bean instance for a stateless bean, or not.  While many EJB containers will use a single bean instance for a stateless bean, you still cannot depend upon this because if you do, your code may not work in a clustered environment.

Our StockQuotes bean, in fact, could very easily have been made stateless.  We don't really need a data member called "stock".  Instead, we can define "getPrice" to take the stock name as an argument.

Let us create a new session bean, StockQuotes2, but this time let us make it stateless.

 

We will need to add members to its remote interface in the file StockQuotes2.java.  We only need one method,
public float getPrice( String stock )
          throws java.rmi.RemoteException, java.io.IOException;

To see its stateless behavor, add a member function "int count = 0;" in the bean's class in StockQuotesBean.java file.  Let us also add the implementation of getPrice as shown:

public float getPrice( String stock )
     throws java.rmi.RemoteException,
                                java.io.IOException
{
     // Print count
     count++;
     System.out.println( "Count = " + count );

     // Fetch price as usual.
     BufferedReader reader;
     reader = new BufferedReader(
                 new FileReader( "C:\\StockPrices.txt" ));
     String line;
     stock = stock.toLowerCase() + ":";
     while (( line = reader.readLine()) != null ) {
         if ( line.toLowerCase().startsWith( stock )) {
             line = line.substring( stock.length());
             reader.close();
             return Float.parseFloat( line.trim());
         }
     }
     reader.close();
     throw new java.rmi.RemoteException( "Not found" );
 }

Remember to import java.io.* package.

As before, create the bean by running the commands in the .cmd file, and deploy it by copying the JAR file to the ejbdir folder.

To test this bean, we can use the same test programs as before with minor changes.

Exercise:  Make a modified version of the EJB session bean client, to use the stateless bean just created.  Run it and notice the output from the stateless bean in the EJB server's console.  (Keep the existing files and work with new copies -- we will need the stateful version for future exercises!)
 

Next tutorial: Ejb Handles