📄 chapter6.htm
字号:
to write any descriptors for the data sources as we did with Session and BMP
beans, as the EJB container is responsible for that.</I></FONT></FONT></P>
<P><IMG height=578 src="chapter6.files/ejbJarFirst.png" width=460 align=left
border=0 name=Graphic12><BR clear=left><B><FONT face="Nimbus Roman No9 L"><FONT
color=#000000>As shown in the code snippet above from the ejb-jar.xml file, all
abstract methods are generated as persistent fields under the tag
<cmp-field> because of the tag @persistence-field declared at each
accessor method. Also the primary key class descriptor is
generated</FONT></FONT></B> <B>under the <primary-key-class> tag and the
primary key field is generated under the <primarykey-field> because of the
tag @field-pk declared for the relevant attribute(s)</B>. </P>
<P><B>Descriptors for finder methods are generated along with the query defined
to fetch the data as shown below</B>. <B>These finder tags are generated by the
@ejb.finder tag declared at class level. </B></P>
<P><IMG height=484 src="chapter6.files/ejbJarSecond.png" width=630 align=left
border=0 name=Graphic13><BR clear=left><B>And in the jboss.xml file, the
following descriptors are generated due to the tag @ejb.bean declared at class
level, as shown below.</B></P>
<P><IMG height=88 src="chapter6.files/jboss.png" width=322 align=left border=0
name=Graphic15><BR clear=left><FONT color=#800000><FONT
face="Nimbus Roman No9 L"><I>Note : @ejb.bean tag has been covered in previous
chapters.</I></FONT></FONT></P>
<P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><B>Item Bean
functionality is complete in and ready for deployment.</B></FONT></FONT></P>
<H3><FONT color=#000000><FONT face="Nimbus Roman No9 L"><B>Deploy Item Bean
:</B></FONT></FONT></H3>
<P><B>Go to Lomboz J2EE View > expand node MyStore > expand MyStoreMgr
> select Jboss 3.2.1 ALL.</B></P>
<P><B>Right click > select Debug Sever on the pop up menu.</B></P>
<P><FONT color=#800000><I>Note : This is to start your server, if you are
already running your server then skip these steps and go to the next
one.</I></FONT></P>
<P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><B>Go to MyStoreMgr node
in LombozJ2EE view > right click > select Deploy on the pop up menu as
shown below.</B></FONT></FONT></P>
<P><IMG height=230 src="chapter6.files/LombozJ2EEViewDeployBean.png" width=444
align=left border=0 name=Graphic19><BR clear=left><BR><BR></P>
<P><FONT color=#800000><I>Note : All these steps have been detailed in previous
chapters (1 and 3), so, please refer to them.</I></FONT></P>
<P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><B>Messages in the
console will indicate whether deployment of your bean has been successful or
not.</B></FONT></FONT></P>
<P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><B>Now, lets modify
StoreAccessBean to invoke methods on ItemBean.</B></FONT></FONT></P>
<P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><B>Add a field to store
our Item reference (obtained from JNDI lookup).</B></FONT></FONT></P><PRE style="MARGIN-BOTTOM: 0.2in"><FONT face=Sans><FONT size=3><SPAN style="BACKGROUND: none transparent scroll repeat 0% 0%"><FONT color=#000000>private</FONT></SPAN><FONT color=#7f0055> </FONT><FONT color=#000000>ItemLocalHome</FONT> item<FONT color=#000000>LocalHome</FONT>;</FONT></FONT></PRE>
<P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><B>In the ejbCreate
method store this reference in the itemLocalHome variable by invoking the
getLocalHome static method in the ItemUtil class as shown in this code snippet
below from StoreAccess Bean.</B></FONT></FONT></P>
<P><IMG height=282 src="chapter6.files/StoreAccessEjbCreate.png" width=448
align=left border=0 name=Graphic20><BR clear=left><BR><BR></P>
<H3><B>Add a Business Method to StoreAccess :</B></H3>
<P><B>Add another business method to the StoreAccess Bean which will invoke this
business method on our Item Bean.</B></P>
<P><FONT face="Nimbus Roman No9 L"><B>Now, add a business method with this
signature:</B></FONT></P>
<P><FONT face="Nimbus Roman No9 L"><B><FONT color=#000000>public ItemData</FONT>
<FONT color=#000000>getItemData(String itemID)</FONT></B></FONT></P>
<P><FONT face="Nimbus Roman No9 L"><B><FONT color=#000000>.. with Interface type
as Remote. As managers will log on to MyStore with username, once
authe</FONT>nticated they will be identified by their userid. They can then
retrieve their account details from MyStore using that userid. A Manager can
invoke methods on the Item Bean to examine the inventory of
MyStore.</B></FONT></P>
<P><FONT color=#800000><FONT face="Nimbus Roman No9 L"><I>Note : Steps to create
business methods are covered in previous chapters.</I></FONT></FONT></P>
<P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><FONT size=3><B>Now
invoke one of Item's finder methods via the reference variable we have created
in the ejbCreate method.</B></FONT></FONT></FONT></P><PRE style="MARGIN-BOTTOM: 0.2in"><FONT size=3><FONT face=Sans>ItemLocal item = itemLocalHome.findByPrimaryKey(itemID)</FONT> </FONT> </PRE>
<P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><FONT size=3><B>Now
invoke the business method of Customer on this
reference.</B></FONT></FONT></FONT></P><PRE style="MARGIN-BOTTOM: 0.2in"><FONT face=Sans><FONT size=3>ItemData myItem = item.getItemData() </FONT></FONT></PRE>
<P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><B>Code snippet for this
business method is shown below.</B></FONT></FONT></P>
<P><IMG height=356 src="chapter6.files/StoreAccessGetItemData.png" width=500
align=left border=0 name=Graphic31><BR clear=left><BR><BR></P>
<P><B>Add another business method to StoreAccess Bean.</B></P>
<P><B>Add a business method with this signature:</B></P>
<P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><B>public
java.util.ArrayList getOutOfStockItems()</B></FONT></FONT></P>
<P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><B>.. with Interface type
as Remote. This will return the items which are out of stock in
MyStore.</B></FONT></FONT></P>
<P style="FONT-STYLE: normal"><FONT color=#000000><FONT
face="Nimbus Roman No9 L"><FONT size=3><B>Create two variables of type
Collection and ArrayList respectively, as the finder method for Items returns a
Collection and this method will return an ArrayList, after populating items
which are out of stock from the returned
Collection.</B></FONT></FONT></FONT></P><PRE style="MARGIN-BOTTOM: 0.2in"><FONT face=Sans><FONT size=3>Collection items = null;</FONT></FONT>
<FONT size=3><FONT face=Sans>ArrayListItemsOutOfStock = null;</FONT> </FONT> </PRE>
<P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><B>Now invoke one of the
finder methods of Item on the reference variable we have created in the
ejbCreate method.</B></FONT></FONT></P><PRE style="MARGIN-BOTTOM: 0.2in"><FONT face=Sans><FONT size=3>items = itemLocalHome.findByOutOfStock()</FONT></FONT></PRE>
<P><B>Now iterate through the collection of out of stock items and add to the
ArrayList.</B></P><PRE style="MARGIN-BOTTOM: 0.2in"><FONT color=#000000><FONT face=Sans><FONT size=3><B>I</B>temLocal myItemLocal = (ItemLocal) iterate.next();</FONT></FONT></FONT>
<FONT color=#000000><FONT face=Sans><FONT size=3>itemsOutOfStock.add(myItemLocal.getItemData()); </FONT></FONT></FONT></PRE>
<P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><B>Code snippet for this
business method is shown below.</B></FONT></FONT></P>
<P><IMG height=392 src="chapter6.files/StoreAccessGetItemOutOfStock.png"
width=558 align=left border=0 name=Graphic36><BR clear=left><BR><BR></P>
<P><B>Add another business method to StoreAccess Bean.</B></P>
<P><B>Add a business method with this signature:</B></P>
<P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><B>public
java.util.ArrayList getItemBySupplier(String supplierID)</B></FONT></FONT></P>
<P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><B>.. with Interface type
as Remote. This will return the items which are provided to MyStore by a given
supplier.</B></FONT></FONT></P>
<P style="FONT-STYLE: normal"><FONT color=#000000><FONT
face="Nimbus Roman No9 L"><FONT size=3><B>Create two variables of type
Collection and ArrayList respectively as the finder method for Items returns a
Collection and this method will return ArrayList, after populating items which
are supplied by a particular supplier from the returned
Collection.</B></FONT></FONT></FONT></P><PRE style="MARGIN-BOTTOM: 0.2in"><FONT face=Sans><FONT size=3>Collection suppliedItems = null;</FONT></FONT>
<FONT size=3><FONT face=Sans>ArrayList itemsBySupplier = null;</FONT> </FONT> </PRE>
<P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><B>Now invoke one of the
finder methods of Item on the reference variable we have created in the
ejbCreate method.</B></FONT></FONT></P><PRE style="MARGIN-BOTTOM: 0.2in"><FONT face=Sans><FONT size=3>suppliedItems = itemLocalHome.findBySupplierID(supplierID)</FONT></FONT></PRE>
<P><B>Now iterate through the collection of items for this supplier and add to
the ArrayList.</B></P><PRE style="MARGIN-BOTTOM: 0.2in"><FONT color=#000000><FONT face=Sans><FONT size=3><B>I</B>temLocal myItemsLocal = (ItemLocal) iterate.next();</FONT></FONT></FONT>
<FONT color=#000000><FONT face=Sans><FONT size=3>itemsBySupplier.add(myItemsLocal.getItemData()); </FONT></FONT></FONT>
<FONT color=#000000><FONT face="Nimbus Roman No9 L"><FONT size=3><B>Code snippet for this business method is shown below.</B></FONT></FONT></FONT></PRE>
<P><IMG height=396 src="chapter6.files/StoreAccessGetItemBySupplier.png"
width=538 align=left border=0 name=Graphic37><BR clear=left><BR><BR></P>
<P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><B>Now all the methods in
StoreAccess Bean for accessing Item's business methods have been added. The only
remaining bit is the deployment descriptors required for linking/referencing of
StoreAccess and Item Bean. So we will the add two tags shown
below.</B></FONT></FONT></P>
<P><IMG height=172 src="chapter6.files/StoreAccessDeploymentDescriptors.png"
width=446 align=left border=0 name=Graphic38><BR clear=left><BR><BR></P>
<P><B>First add the tag shown below at class level in StoreAccess Bean.</B></P><PRE><FONT color=#3f5fbf><FONT face=Sans><FONT style="FONT-SIZE: 11pt" size=2>@ejb.ejb<FONT color=#7f7f9f>-</FONT>ref ejb<FONT color=#7f7f9f>-</FONT>name="Item"</FONT></FONT></FONT>
<FONT face=Sans><FONT style="FONT-SIZE: 11pt" size=2><FONT color=#3f5fbf>view</FONT><FONT color=#7f7f9f>-</FONT><FONT color=#3f5fbf>type="local"</FONT></FONT></FONT>
<FONT face=Sans><FONT style="FONT-SIZE: 11pt" size=2><FONT color=#3f5fbf>ref</FONT><FONT color=#7f7f9f>-</FONT><FONT color=#3f5fbf>name="ItemLocal"</FONT></FONT></FONT></PRE>
<P><B>This tag will generate deployment descriptors in 'ejb-jar.xml', as
StoreAccessBean has to know which bean it is referring to, what is its view-type
and ref-name. This will generate these descriptors as shown below.</B></P>
<P><FONT color=#800000><FONT face="Nimbus Roman No9 L"><I>Note : View type is
local as both are in the same Java Virtual Machine, otherwise it would be
Remote. Secondly ref-name is generated as ItemLocalHome, as we are using that
rather than ItemHome (which was also generated, but is used in the Remote
case).</I></FONT></FONT></P>
<P><IMG height=576 src="chapter6.files/ejbJarStoreAccess.png" width=466
align=left border=0 name=Graphic39><BR clear=left><BR><BR></P>
<P><B>Now add a second tag (shown below) at class level in StoreAccess
Bean.</B></P><PRE><FONT face=Sans><FONT size=3><FONT color=#3f5fbf>@jboss.ejb</FONT><FONT color=#7f7f9f>-</FONT><FONT color=#3f5fbf>ref</FONT><FONT color=#7f7f9f>-</FONT><FONT color=#3f5fbf>jndi</FONT> <FONT color=#3f5fbf>ref</FONT><FONT color=#7f7f9f>-</FONT><FONT color=#3f5fbf>name="ItemLocal"</FONT></FONT></FONT>
<FONT face=Sans><FONT size=3><FONT color=#3f5fbf>jndi</FONT><FONT color=#7f7f9f>-</FONT><FONT color=#3f5fbf>name="ItemLocal"</FONT></FONT></FONT></PRE>
<P>This tag will generate deployment descriptors in 'jboss.xml', as the
application server has to know what jndi-name the Item bean has been registered
with. This will generate these descriptors as shown below.</P>
<P><FONT color=#800000><I>Note : Ref-name and jndi-name are used for bean as
local (in same JVM).</I></FONT></P>
<P><IMG height=344 src="chapter6.files/jbossStoreAccess.png" width=352
align=left border=0 name=Graphic40><BR clear=left><FONT color=#800000><I>Note :
We can see in the code snippet above the deployment descriptors generated by tag
@jboss. For the view type 'local' it generates incorrect deployment descriptors,
as discussed in the previous chapter. So every time we use this tag we have to
change the <ejb-ref> to <ejb-local-ref> before deployment.
<B>Caution here</B>, that you do this change manually when you finally finish
regenerating your EJB classes, because every time you regenerate your classes,
'jboss.xml' will be overwritten.</I></FONT></P>
<P><B>Now our Item Bean is complete after these changes, so deploy your bean
again now, from the Lomboz J2EE View, as per the steps shown above and in
previous chapters. Messages will appear in the console showing the status of
deployment.</B></P>
<P><B>Once the bean is deployed successfully, create a test client which will
invoke the loginUser method on StoreAccess Bean , getCustomerData on Customer
Bean, getManagerData on Manager Bean and getOutOfStockItems on Item
Beam.</B></P>
<H3 style="MARGIN-TOP: 0in; MARGIN-BOTTOM: 0in"><BR></H3>
<H3 style="MARGIN-TOP: 0in; MARGIN-BOTTOM: 0in"><B>Create your Test Client
:</B></H3>
<P style="MARGIN-BOTTOM: 0in"><BR></P>
<P>G<B>o to Project MytStore node > select src node and expand it > select
au.com.tusc.client package > right click.</B></P>
<P><B>Select New on the pop up menu > select Lomboz EJB Test Client
Wizard</B>. </P>
<P><B>Select package name au.com.tusc.client, name as SessionCMPClient and
select Ejb Home as au.com.tusc.session.StoreAccessHome and Ejb Interface as
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -