EJB

EJB Projects

EJB Project 1

EJB Examples

EJB EXAMPLE

Creating A Session Bean
Previous Home Next

Session beans have a very simple framework and it's similar to entity beans. In Session bean contain have a remote interface and a home interface, but no primary key.

They also do not need to be backed by a database or other form of permanent storage. Again, we will be using Blizzard to generate our basic framework.

Our session bean will be a stock-price quoting agent. It will live on a machine where current stock prices are available. The clients of the session bean will be able to specified a stock and obtain its current price.

It's create the session bean, and run the Blazix EJB wizard Blizzard, and select Create a Session bean. Fill in the bean name, package name and directory name. Select stateful and let the container manage transaction boundaries. Add data items stock(String) and price (float). We do not want a setter for price, but make sure to provide a setter for stock.

After that to build the framework examine the files created by the wizard. It's Look in the remote interface, the home interface and the bean class. Everything is similar to the entity beans, but there is no primary key and therefore no find ByPrimary key.

 

In the bean have implementation file StockQuotesBean.java, add an import for java.io.* and delete the member variable price. Also remove the price argument to the ejbCreate method and remove the statement where the price member variable is getting set.

public float gettingPrice() throws java.rmi.RemoteException, 
java.io.IOException 
{ 
 BufferedReader br; 
     br = new BufferedReader( 
             new FileReader( "C:\\StockPrices.txt" )); 
	 String line; 
     String prefix = stock.toLowerCase() + ":"; 
     while (( line = reader.readLine()) != null ) { 
         if ( line.toLowerCase().startsWith( prefix )) { 
             line = line.substring( prefix.length()); 
             reader.close(); 
             return Float.parseFloat( line.trim()); 
         } 
     } 
     reader.close(); 
     throw new java.rmi.RemoteException( "Not found" ); 

     // In product-level coding we would define an exception 

     // class for this, but for this sample we 

     // will just put RemoteException to dual use! 
 }
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: