Deploying the Session Bean

Deploying the session bean is similar to deploying the entity bean, but we don’t have to worry about a datasource, because entity beans are not backed up by persistent storage. We can modify the ejb.ini file as before and add just one line specifying the location of the “ejbJar”, but this is also not necessary. Since no datasource is involved, we can entirely skip the ejb.ini file. Just copy the .jar file to the “ejbdir” folder of your Blazix installation.Now restart the server (or tell the server to rescan its “ejbdir” folder by using the EJB remote administration tool.)
You should see the following line in the output from the EJB server showing your EJB was deployed:
Deploying EJB-Home class “stocks.StockQuotesHome” at JNDI name “StockQuotes”
Exercise: Deploy your BankAccount EJB.

Using the Session Bean

We are already familiar with looking up home interfaces and using them to create beans, and then using beans.  Everything is identical in this case, but there is no method to find by a primary key.

There is a sample file included here that shows how to access the session bean.  Again, there are two versions of the file, for JDK 1.2 and for JDK 1.3.  The JDK 1.2 version does not use PortableRemoteObject.

Remember to add your StockQuoteEjb.jar file to the classpath, before compiling and using the sample client.
Try running the client program without the stock-quotes file being present.  This should cause the EJB to throw a to the client program.  Now put the stock-quotes file in place and run the program.  Now it should correctly return the prices.

Exercise: Write and test a client program for your bank-accounts EJB that lets you query balances, and credit and debit some amounts to to the balances.

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  We only need one method,

public float getPrice( String stock )
throws java.rmi.RemoteException,;

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

public float getPrice( String stock )
throws java.rmi.RemoteException,
// Print 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());
return Float.parseFloat( line.trim());
throw new java.rmi.RemoteException( "Not found" );

Remember to import* 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!)