In this lab you will work with a Web Bank Online Stock Purchase Web application. This Web application accepts customer's account number, stock symbol and number of shares and forwards a stock purchase request to a server side component. The server side component is a MDB EJB that listens on Stock Purchase request messages and performs the processing required to accomplish the stock purchase transaction.
The architecture of the server side component is an example of a recommended practice for building MDB EJBs. The MDB uses a façade Session EJB via local interfaces. The façade Session EJB (StockPurchase) performs all the required processing by invoking (also via local interfaces) several CMP EJBs (Account, Stocks, Stock_prch_txn) to log the Stock Purchase transaction.
Note: This workshop requires
that the following products are installed:
|
Note: The reason why we are using IBM MQSeries as the JMS provider for WebSphere Unit Test Client is due to the fact that the WebSphere Studio Application Developer we are using in does not include an embedded JMS provider. An embedded JMS provider will be provided in the GA version of WebSphere Studio Application Developer. |
___ 1. Import WBStockPurchase.EAR
a. Switch to J2EE Perspective and J2EE Hierarchy View.
b. From main menu bar select File>Import...
c. From Select page select EAR file and press Next> button.
d. On Enterprise Application Import page press the Browse... button to navigate to WS50STEW>hands-on>J2EE Part I MDB>MDB Part II directory and select WBStockPurchase.ear and for Project name enter WBStockPurchase.
![]()
e. Press the Finish button.
The EJBs for WBStockPurchaseEJB should look similar to this:
![]()
Note: You will find a detailed description of the EJBs you have just imported in the next section.
___ 2. Add WBStockPurchase EAR to WebSphere V5 Unit Test Server Configuration
a. Switch to Server Perspective.
b. In Server Configuration View, select WebSphere V5 Unit Test Server configuration and from the pop up menu select Add>WBStockPurchase.
___ 3. Create STOCK_PRCH_TXN and STOCKQUOTES Tables
These two tables will be used in this lab. The STOCK_PRCH_TXN is for recording stock purchase transactions and the STOCKQUOTES table contains the stock details such as price, symbol, etc.
a. Run WS50STEW>hands-on>J2EE Part I MDB>MDB Part II>AddTables.bat
___ 1. Create StockPurchaseListener MDB.
a. Switch to J2EE Perspective, J2EE Hierarchy View.
b. From the main menu toolbar click on Create an Enterprise Bean icon
.
c. On Select page select WBStockPurchaseEJB for EJB project and press the Next> button.
d. On Create a 2.0 Enterprise Bean page select Message-driven bean radio button and enter StockPurchaseListener for Bean name.
![]()
Press the Next> button.
e. On Enterprise Bean Details page select Queue for Destination Type and enter JMSListener for ListenerPort name.
![]()
Note: Recall that you defined ListenerPort name of JMSListener in part one of this lab This is how WebSphere associates the queue Q1 with the StockPurchaseListener MDB.
Press the Finish button.
f. In J2EE Hierarchy View, expand EJBModules>WBStockPurchaseEJB
![]()
Let's discuss the purpose of the various EJBs in this EJB project, including the MDB you have just created.
EJB Name EJB Type Comments Account Entity CMP This Entity CMP EJB represents ACCOUNT table in AIS database. Used to deduct stock purchase costs from customer's account. StockPurchase Session This façade Session EJB is invoked in onMessage() method of StockPurchaseListener MDB. This bean contains the transaction logic and accesses via local interfaces the Entity CMP EJBs. StockPurchaseListener MDB This MDB EJB listens to stock purchase requests issued by the Stock Purchase Web application and invokes the StockPurchase Session EJB. Stock_prch_txn Entity CMP This Entity CMP EJB represents the STOCKQUOTES table in the AIS database. Used to retrieve the stock's unit cost per share. Stocks Entity CMP This Entity CMP represents the STOCK_PRCH_TXN table in the AIS database. Used to log the stock purchase transaction details.
___ 2. Add EJB References to Access StockPurchase Session EJB
The StockPurchase Session EJB contains the business logic for handling the stock purchase transaction. Rather than coding the business logic in the MDB, it is a recommended practice and a well known design pattern to call a facade Session EJB, in our case StockPurchase Session EJB. We will be using local interfaces to access it (another recommended practice). In order to call an EJB via local references we need to provide a reference JNDI name for the target EJB.
a. Double click on EJB Modules>WBStockPurchaseEJB EJB module to open EJB Deployment Descriptor editor.
b. Click on the References tab.
![]()
c. Select StockPurchaseListener MDB.
![]()
d. Press the Add... button.
e. On Reference page select EJB local reference radio button
and press Next> button.
f. On EJB Local Reference enter Name of ejb/StockPurchase.
g. Press the Browse... button to the right of Link entry field.
h. In Reference Selection dialog select Enterprise bean in current EJB project radio button and then from the Link drop down menu select StockPurchase EJB.
![]()
Press the OK button.
i. Back on EJB Local Reference page press the Finish button.
j. Press CTRL-S to save and close the EJB Deployment Descriptor editor.
___ 3. Generate Deploy Code
a. Select EJB Modules>WBStockPurchaseEJB EJB project.
b. Click right mouse button and from the pop up menu select Generate>Deploy Code and RMIC Code...
c. On Generate Deploy and RMIC Code dialog press the Select all button
![]()
and then press the Finish button.
___ 1. Add Logic to onMessage() Method.
The key to understanding how MDBs is the onMessage() method. MDBs significantly simplify JMS programming. Rather than coding a JMS listener (see example ) for managing queue connections, all you need to do is to provide the code you want to execute when a message is retrieved from a queue.
a. Expand StockPurchaseListener MDB and click on StockPurchaseListenerBean.
![]()
This will open Java editor for StockPurchaseListenerBean.java.
b. In Outline view click on onMessage(Message) method.
![]()
c. Add the following code to the body of the onMessage(Message) method:
// Retrieve message from the queue and parse it // -------------------------------------------- int account = 0; String symbol = ""; int shares = 0; String text = ""; try { TextMessage textMessage = (TextMessage) msg; text = textMessage.getText(); StringTokenizer tokens = new StringTokenizer(text, " ", false); account = Integer.parseInt(tokens.nextToken()); symbol = tokens.nextToken(); shares = Integer.parseInt(tokens.nextToken()); } catch (Exception e) { System.err.println("Parsing error text[" + text + "]"); e.printStackTrace(); } // Delegate message processing to StockPurchase Session EJB // -------------------------------------------------------- try { StockPurchaseLocalHome stockpurchaseHome = null; Context ctx = new InitialContext(); Object homeObject = ctx.lookup("java:comp/env/ejb/StockPurchase"); stockpurchaseHome = (StockPurchaseLocalHome) homeObject; StockPurchaseLocal stockpurchase = null; stockpurchase = stockpurchaseHome.create(); stockpurchase.purchaseStock(account, symbol, shares); stockpurchase.remove(); } catch (Exception e) { System.err.println("Error accessing StockPurchase Session EJB"); e.printStackTrace(); }Note how much simpler this code is when compared to the JMSReceiver's receiveMessages() method!
d. In Outline view click on package name
![]()
e. In the Java editor, below the package statement, add following import statements:
import java.util.StringTokenizer; import javax.jms.JMSException; import javax.jms.TextMessage; import javax.naming.Context; import javax.naming.InitialContext;f. Press CTRL-S and close JMSReceiverMDBBean.java file.
___ 2. Examine JMS Code for Posting Stock Purchase Requests
The code responsible for posting JMS request resides in StockPurchaseFrom.jsp, and three Java classes: MessageSender, DataBean and WBStockPurchaseServlet:
![]()
File Type Comments StockPurchaseForm.jsp JSP Accepts the stock purchase transaction input parameters and invokes WBStockPurchaseServlet. DataBean Java Java bean to pass data between the servlet and the JSP. WBStockPurchaseServlet Java Servlet that accepts input parameters of the stock purchase transaction and posts messages to the JMS queue via MessageSender class. MessageSender Java Java class with a single method sendMessage(). Its purpose is to manage the JMS input queue and format and send messages to it.
a. Let's briefly examine the code in WbStockPurchaseServlet. To do this expand WBStockPurchaseWeb>Java Source>com>ibm>com and double click on WBStockPurchaseServlet.java.
b. Note that the servlet delegates posting of JMS messages to MessageSender class. Also not how the servlet immediately returns and updates the JSP without waiting for the stock purchase request to be received and processed by the MDB EJB that is listening to the stock purchase request messages.
![]()
d. Close WBStockPurchaseServlet.java.
e. Let's briefly examine the code in Message Sender Java class. To do this expand WBStockPurchaseWeb>Java Source>com>IBM>com and double click on MessageSender.java.
f. Note that steps required to post a message to a JMS queue:
![]()
g. Close WBStockPurchaseServlet.java.
___ 3. Test the Stock Purchase Web Application
a. Before you begin testing the Stock Purchase Web application let's look at the original ACCOUNT table. To do this run WS50STEW>hands-on>J2EE Part I MDB>MDB Part II>ShowAccount.bat
![]()
Note the BALANCE in for ACCT_NUMBER 1 and 2.
b. Switch to Server Perspective.
c. Select WBStockPurchaseWeb>Web Content>StockPurchaseApplication>StockPurchaseForm.jsp click on the right mouse button and from the pop up menu select Run on Server.
d. On Server Selection dialog, select WebSphere V5 Unit Test server, set Do not show this dialog next time (Set this server as the preferred server) check box and press the Finish button.
If you happen to see an error in the Console:
ResourceMgrIm E WSVR0017E: Error encountered binding the J2EE resource, AIS, as jdbc/AIS from cells/localhost/nodes/localhost/resources.xml com.ibm.ws.runtime.component.binder.ResourceBindingException: invalid configuration passed to resource binding logic. REASON: Invalid Configuration! The DataSource: AIS has a null RelationResourceAdapter property.that error can be ignored.e. On Select a Server Client dialog, select Web browser for the client.
f. Enter Account Number of 1, Stock Symbol of IBM and Number of Shares of 1.
![]()
g. Press the Submit order button. You should see a confirmation message below the form:
![]()
h. Repeat step e-g using the following input:
Account Number Stock Symbol Number of Shares 1 ABC 1 2 IBM 1 2 ABC 100 i. Examine the contents of STOCK_PRCH_TXN table. It should have four rows each row representing one stock purchase transaction. To do this run WS50STEW>hands-on>J2EE Part I MDB>MDB Part II>ShowStockPrchTxn.bat
![]()
j. Finally check the BALANCE column for ACCT_NUMBER 1 and 2; it should reflect the deductions made to cover the cost of the stock purchase transactions. To do this run WS50STEW>hands-on>J2EE Part I MDB>MDB Part II>ShowAccount.bat
![]()
Congratulations, you've completed the MDB Lab Part II - Stock Purchase MDB Project.