📄 7.mht
字号:
width=3D480 align=3Dleft border=3D0 name=3DGraphic45><BR =
clear=3Dleft><FONT=20
color=3D#800000><I>Note : For further documentation on MDB development =
with JBOSS,=20
please refer to the JBOSS documentation.</I></FONT></P>
<P><B>Add another tag as shown below, required by JBOSS, for finding the =
Supplier bean using its JNDI NAME.</B></P><PRE><FONT face=3DSans><FONT =
size=3D3><FONT color=3D#3f5fbf>@jboss.ejb</FONT><FONT =
color=3D#7f7f9f>-</FONT><FONT color=3D#3f5fbf>ref</FONT><FONT =
color=3D#7f7f9f>-</FONT><FONT color=3D#3f5fbf>jndi</FONT> <FONT =
color=3D#3f5fbf>ref</FONT><FONT color=3D#7f7f9f>-</FONT><FONT =
color=3D#3f5fbf>name=3D"SupplierLocal"</FONT></FONT></FONT>
<FONT size=3D3><FONT face=3DSans><FONT color=3D#3f5fbf>jndi</FONT><FONT =
color=3D#7f7f9f>-</FONT><FONT =
color=3D#3f5fbf>name=3D"SupplierLocal"</FONT></FONT></FONT></PRE>
<P><I><FONT color=3D#800000>Note : As discussed in chapter5, this tag =
generates=20
incorrect descriptors within jboss.xml. For view-type=3D"local" it =
generates an=20
<ejb-ref> tag rather than an <ejb-local-ref> tag.</FONT></I> =
</P>
<P><B>Correct the following tags:</B></P>
<P><IMG height=3D132=20
src=3D"http://www.tusc.com.au/tutorial/images/chap7/jbossSupplierWrong.pn=
g"=20
width=3D430 align=3Dleft border=3D0 name=3DGraphic46><BR =
clear=3Dleft><B>Find these tags=20
in jboss.xml and change them to <ejb-local-ref> as shown =
below.</B></P>
<P><IMG height=3D130=20
src=3D"http://www.tusc.com.au/tutorial/images/chap7/jbossSupplier.png" =
width=3D406=20
align=3Dleft border=3D0 name=3DGraphic47><BR clear=3Dleft><BR><BR></P>
<P><B>Now add this last tag as shown below for our StoreAccess Bean, to=20
reference the StoreAccess bean using its JNDI NAME .</B></P><PRE><FONT =
face=3DSans><FONT size=3D3><FONT color=3D#3f5fbf>@jboss.ejb</FONT><FONT =
color=3D#7f7f9f>-</FONT><FONT color=3D#3f5fbf>ref</FONT><FONT =
color=3D#7f7f9f>-</FONT><FONT color=3D#3f5fbf>jndi</FONT> <FONT =
color=3D#3f5fbf>ref</FONT><FONT color=3D#7f7f9f>-</FONT><FONT =
color=3D#3f5fbf>name=3D"StoreAccess"</FONT></FONT></FONT>
<FONT face=3DSans><FONT size=3D3><FONT color=3D#3f5fbf>jndi</FONT><FONT =
color=3D#7f7f9f>-</FONT><FONT =
color=3D#3f5fbf>name=3D"StoreAccessBean"</FONT></FONT></FONT></PRE>
<P><B>This tag will generate the following descriptors in =
jboss.xml:</B></P>
<P><IMG height=3D194=20
src=3D"http://www.tusc.com.au/tutorial/images/chap7/jbossStoreAccess.png"=
=20
width=3D430 align=3Dleft border=3D0 name=3DGraphic48><BR =
clear=3Dleft><BR><BR></P>
<P>Now our RequestItems Bean is complete, so now add your bean and =
generate your=20
EJB classes.</P>
<P><B>Go to the RequestItemsBean node under au.com.tusc.mdb > right =
click=20
> select Lomboz J2EE... > Add EJB to Module > Ok.</B></P>
<P><B>Go to the MyStoreMgr node in Package Explorer > right click =
> select=20
Lomboz J2EE... > Generate EJB classes.</B></P>
<P><FONT color=3D#800000><I>Note : Since you have generated your classes =
you have=20
to again fix the incorrectly-generated deployment descriptors in =
jboss.xml,=20
under <message-driven> and <session>.</I></FONT></P>
<P><B>Now, let's deploy our bean. Go to Lomboz J2EE View, start your =
server if=20
it is not running, and deploy your bean.</B></P>
<P><B>Messages will appear in console showing deployment status.</B></P>
<P><FONT color=3D#800000><I>Note : Steps for deploying beans have been =
detailed in=20
previous chapters.</I></FONT></P>
<P>Once the bean is deployed successfully, create your test client.</P>
<P style=3D"MARGIN-BOTTOM: 0in"><BR></P>
<H3>Create Test Client :</H3>
<P><B>Now, to create a test client in this case, the Test Client Wizard =
can't=20
help us, because it requires a Home interface and EJB interface to be =
selected,=20
whereas for Message Driven Beans there is no Home interface and EJB =
interface=20
required (as discussed above).</B></P>
<P><B>So, write a class and the necessary methods to invoke operations =
on the=20
RequestItems Bean</B>.</P>
<P><B>Add a class named RequestMDBClient under the package =
au.com.tusc.mdb</B>.=20
</P>
<P><B>Add a method named getContext with the following =
signature:</B></P><PRE style=3D"MARGIN-BOTTOM: 0.2in"><FONT =
face=3DSans><FONT size=3D3><B><FONT color=3D#7f0055>private</FONT></B> =
<FONT color=3D#000000>InitialContext</FONT> <FONT =
color=3D#000000>getContext()</FONT> <B><FONT =
color=3D#7f0055>throws</FONT></B> <FONT =
color=3D#000000>NamingException</FONT> </FONT></FONT></PRE>
<P><B>Add the following lines of code to get the instance of =
IntialContext</B>=20
as shown below.</P>
<P><IMG height=3D232=20
src=3D"http://www.tusc.com.au/tutorial/images/chap7/TestClientGetContext.=
png"=20
width=3D522 align=3Dleft border=3D0 name=3DGraphic49><BR =
clear=3Dleft><BR><BR></P>
<P><B>Add a method named testMDBBean with the following =
signature.</B></P><PRE style=3D"MARGIN-BOTTOM: 0.2in"><FONT =
color=3D#000000><FONT face=3DSans><FONT size=3D3>public void =
testMDBBean()</FONT></FONT></FONT></PRE>
<P><B>Now implement this method, in which the following steps are=20
needed:</B></P>
<OL>
<LI>
<P><B>Add a Data Object which is to be sent as the message.</B></P>
<LI>
<P><B>Create the Initial context reference.</B></P>
<LI>
<P><B>Create the Connection factory reference. </B></P>
<LI>
<P><B>Use this context to perform the lookup, where the lookup string =
is=20
"queue/MdbQueue".</B></P>
<LI>
<P><B>Create the QueueConnection.</B></P>
<LI>
<P><B>Create the QueueSender.</B></P>
<LI>
<P><B>Create the QueueSession for the bean.</B></P>
<LI>
<P><B>Create the Object Message and pass the Data Object in =
message.</B></P>
<LI>
<P><B>Send the message.</B></P>
<LI>
<P><B>Finally, commit the session, and then close both the session and =
the=20
connection. A code snippet of the testMDBean method is shown=20
below.</B></P></LI></OL>
<P><IMG height=3D490=20
src=3D"http://www.tusc.com.au/tutorial/images/chap7/TestClientTesMDBBean.=
png"=20
width=3D614 align=3Dleft border=3D0 name=3DGraphic50><BR =
clear=3Dleft><BR><BR></P>
<P>Now your test client is complete, so let's test the client. </P>
<H3>Test your Client :</H3>
<P style=3D"MARGIN-BOTTOM: 0in"><B>To test your client, Select =
RequestMDBClient=20
node > Go to the top level menu and select the 'Running Man' =
icon</B>.</P>
<P><B>On that select 'Run as' > select Java Application.</B></P>
<P><FONT color=3D#800000><I>Note : The steps to run test clients have =
been covered=20
in previous chapters.</I></FONT></P>
<P><B>Now, under your console, you should get the following =
messages:</B></P>
<P><IMG height=3D142=20
src=3D"http://www.tusc.com.au/tutorial/images/chap7/TestClientRun.png" =
width=3D260=20
align=3Dleft border=3D0 name=3DGraphic3><BR clear=3Dleft><B>This message =
on the console=20
doesn't tell us whether the message has been delivered to relevant beans =
or not.=20
In order to verify that, go to the database using JMX Management Console =
View=20
> Hypersonic > Invoke Database Manager and then run a query on =
table=20
'supplier' to see if a message has been added for the supplier.</B> </P>
<P><IMG height=3D576=20
src=3D"http://www.tusc.com.au/tutorial/images/chap7/TestClientRunDatabase=
.png"=20
width=3D622 align=3Dleft border=3D0 name=3DGraphic4><BR =
clear=3Dleft><FONT=20
color=3D#800000><I>Note: Details on accessing this Database Manager are =
provided=20
in chapter 1. </I></FONT></P>
<P><B>Since a supplier named Sebastian has received our message, our =
message has=20
been transferred successfully.</B></P>
<H3>Exercise :</H3>
<P>Now, to progress further, please complete the following exercise. =
Implement=20
DeliverItems as an MD bean. Detailed tasks are given below.</P>
<OL>
<LI>
<P><FONT face=3D"Nimbus Roman No9 L">Create an MD Bean named =
DeliverItems under=20
the package au.com.tusc.mdb. </FONT></P>
<LI>
<P>Create an Immutable Value Object named DeliverItem under the =
package=20
au.com.tusc.mdb. Add some attributes and implement their accessor and =
mutator=20
methods. The attributes are: </P>
<P><B>private String username</B></P>
<P><B>private String passwd</B></P>
<P><B>private String itemID</B></P>
<P><B>private int quantity</B></P>
<LI>
<P>Implement the onMessage method in your DeliverItems Bean.</P>
<P><B>Add these two variables to store references. </B></P><PRE><FONT =
color=3D#000000><FONT face=3DSans><FONT size=3D3>private StoreAccessHome =
storeAccess =3D null;</FONT></FONT></FONT>
<FONT color=3D#000000><FONT face=3DSans><FONT size=3D3>private =
ItemLocalHome itemLocalHome =3D null;</FONT></FONT></FONT></PRE>
<P><B>Extract the data from the message into your immutable value =
object as=20
shown below.</B></P><PRE style=3D"MARGIN-BOTTOM: 0.2in"><FONT =
face=3DSans><FONT size=3D3><FONT color=3D#000000>DelieverItem</FONT> =
d<FONT color=3D#000000>i</FONT> <FONT color=3D#000000>=3D</FONT> <FONT =
color=3D#000000>(DeliverItem)</FONT> <FONT =
color=3D#000000>((ObjectMessage)</FONT> <FONT =
color=3D#000000>message).getObject();</FONT></FONT></FONT></PRE>
<P><B>Get the references for the StoreAccess and Item =
beans.</B></P><PRE><FONT face=3DSans><FONT size=3D3><FONT =
color=3D#000000>StoreAccess</FONT> <FONT color=3D#000000>access</FONT> =
<FONT color=3D#000000>=3D</FONT> <FONT =
color=3D#000000>StoreAccessUtil.getHome().create();</FONT></FONT></FONT>
<FONT face=3DSans><FONT size=3D3><FONT =
color=3D#000000>itemLocalHome</FONT> <FONT color=3D#000000>=3D</FONT> =
<FONT =
color=3D#000000>ItemUtil.getLocalHome();</FONT></FONT></FONT></PRE>
<P><B>Invoke the loginUser method for supplier to get the Supplier's=20
userid(accessID) and then find the Supplier's ID by invoking the=20
getSupplierData method.</B></P><PRE style=3D"MARGIN-BOTTOM: =
0.2in"><FONT face=3DSans><FONT size=3D3><FONT =
color=3D#000000>String</FONT> suppAcess<FONT color=3D#000000>ID</FONT> =
<FONT color=3D#000000>=3D</FONT> <FONT =
color=3D#000000>access.loginUser(di.getUsername(), =
di.getPasswd());</FONT></FONT></FONT>
<FONT face=3DSans><FONT size=3D3>SupplierData sd =3D =
access.getSupplierData(suppAccessID);</FONT></FONT>
<FONT face=3DSans><FONT size=3D3><FONT color=3D#000000>String</FONT> =
<FONT color=3D#000000>suppID</FONT> <FONT color=3D#000000>=3D</FONT> =
<FONT color=3D#000000>sd.getSupplierID();</FONT></FONT></FONT></PRE>
<P><B>If suppID is not equal to null, then invoke the finder method on =
the=20
Item bean to get the details of the item to be delivered, by =
extracting itemID=20
from message.</B></P><PRE style=3D"MARGIN-BOTTOM: 0.2in"><FONT =
face=3DSans><FONT size=3D3>ItemLocal item =3D<FONT color=3D#000000> =
<B>this</B>.</FONT>itemLocalHome.findByPrimaryKey(di.getItemID());</FONT>=
</FONT></PRE>
<P><FONT color=3D#000000><FONT face=3D"Nimbus Roman No9 L"><FONT =
size=3D3><B>Get the=20
supplier ID associated with the item found, so we can fill up the=20
stock.</B></FONT></FONT></FONT></P><PRE><FONT face=3DSans><FONT =
size=3D3><FONT color=3D#000000>String</FONT> <FONT =
color=3D#000000>itemSuppID</FONT> <FONT color=3D#000000>=3D</FONT> <FONT =
color=3D#000000>item.getSupplierID();</FONT></FONT></FONT>
<FONT color=3D#000000> </FONT></PRE>
<P><FONT color=3D#000000><FONT face=3D"Nimbus Roman No9 L"><FONT =
size=3D3><B>Compare=20
itemSuppID and ItemID, if these are the same then fill the stock for =
that item=20
by invoking the fillStock method in Item =
bean.</B></FONT></FONT></FONT></P><PRE><FONT face=3DSans><FONT =
size=3D3><B><FONT color=3D#7f0055>if</FONT></B><FONT color=3D#000000> =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -