Create an Immutable Value Object named DeliverItem under the package au.com.tusc.mdb. Add some attributes and implement their accessor and mutator methods. The attributes are:
private String username
private String passwd
private String itemID
private int quantity
Implement the onMessage method in your DeliverItems Bean.
Add these two variables to store references.
private StoreAccessHome storeAccess = null;private ItemLocalHome itemLocalHome = null;
Extract the data from the message into your immutable value object as shown below.
DelieverItem di = (DeliverItem) ((ObjectMessage) message).getObject();
Get the references for the StoreAccess and Item beans.
StoreAccess access = StoreAccessUtil.getHome().create();itemLocalHome = ItemUtil.getLocalHome();
Invoke the loginUser method for supplier to get the Supplier's userid(accessID) and then find the Supplier's ID by invoking the getSupplierData method.
String suppAcessID = access.loginUser(di.getUsername(), di.getPasswd());SupplierData sd = access.getSupplierData(suppAccessID);String suppID = sd.getSupplierID();
If suppID is not equal to null, then invoke the finder method on the Item bean to get the details of the item to be delivered, by extracting itemID from message.
ItemLocal item = this.itemLocalHome.findByPrimaryKey(di.getItemID());
Get the supplier ID associated with the item found, so we can fill up the stock.
String itemSuppID = item.getSupplierID();
Compare itemSuppID and ItemID, if these are the same then fill the stock for that item by invoking the fillStock method in Item bean.
if ( suppID.equals(itemSuppID)) { System.out.println ("Delivering items in store now... :"); Integer quantity = new Integer (di.getQuantity()); item.fillStock(quantity); System.out.println ("Stock of iten after dleivery is :" + item.getItemData());}
Add the following tags for deployment at the class level for linking/referencing Supplier.
1. @ejb.ejb-ref ejb-name="StoreAccess" view-type="remote" ref-name="StoreAccess"2. @ejb.ejb-ref ejb-name="Item" view-type="local" ref-name="ItemLocal"3. @jboss.ejb-ref-jndi ref-name="ItemLocal" jndi-name="ItemLocal"4. @jboss.ejb-ref-jndi ref-name="StoreAccess" jndi-name="StoreAccessBean" 5. @jboss.destination-jndi-name name="queue/DelMdbQueue"
-
Deploy your DeliverItems Bean.
-
Create a test client named DeliverMDBClient under the package au.com.tusc.mdb.
-
Add a method named testMDBBean with the following signature.
public void testMDBBean
-
Implement testMDBBean, for this a few steps have to be carried out.
Add a Data Object which is to be sent as the message.
Create Initial context.
Create Connection factory.
Using this context perform the JNDI lookup, where the lookup string is "queue/DelMdbQueue".
Create QueueConnection.
Create QueueSender.
Create QueueSession for the bean.
Create Object Message and pass the Data Object in the message.
Send the message.
Finally, commit the session and close both the session and the connection.






