⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 7.mht

📁 Enterprise Java Bean的PPT文档,很好的东西.
💻 MHT
📖 第 1 页 / 共 5 页
字号:
<P>Now, before we add any functionality, create a class or immutable =
value=20
object for extracting information from the message.</P>
<H3><B>Create Immutable Value Object RequestItem : </B></H3>
<P><B>Go to src &gt; under package <SPAN=20
style=3D"TEXT-DECORATION: none">au.com.tusc.mdb &gt; New =
&gt;Class</SPAN>.</B></P>
<P><IMG height=3D136=20
src=3D"http://www.tusc.com.au/tutorial/images/chap7/CreateClass.png" =
width=3D650=20
align=3Dleft border=3D0 name=3DGraphic27><BR clear=3Dleft><BR><BR></P>
<P>The java class wizard will pop up &gt; Add class name as RequestItem, =

Superclass as java.lang.Object and Interfaces as Serializable as as =
shown below=20
in figure. </P>
<P><IMG height=3D518=20
src=3D"http://www.tusc.com.au/tutorial/images/chap7/CreateClassWizard.png=
"=20
width=3D762 align=3Dleft border=3D0 name=3DGraphic28><BR =
clear=3Dleft><BR><BR></P>
<P>This will generate a RequestItem class under au.com.tusc.mdb as shown =

below.</P>
<P align=3Dleft><B>Add the following attributes to the RequestItem class =
as shown=20
below</B>. </P><PRE><FONT color=3D#000000><FONT face=3DSans><FONT =
size=3D3>private String username;</FONT></FONT></FONT>
<FONT color=3D#000000><FONT face=3DSans><FONT size=3D3>private String =
passwd;</FONT></FONT></FONT>
<FONT color=3D#000000><FONT face=3DSans><FONT size=3D3>private String =
itemID;</FONT></FONT></FONT>
<FONT color=3D#000000><FONT face=3DSans><FONT size=3D3>private int =
quantity;</FONT></FONT></FONT></PRE>
<P><B>Add accessor and mutator method for these attributes.</B></P>
<P><B>Select all attributes &gt; Right click&gt; Select source &gt; =
Generate=20
Getter and Setter as shown below.</B></P>
<P><IMG height=3D330=20
src=3D"http://www.tusc.com.au/tutorial/images/chap7/GenaerateMethods.png"=
=20
width=3D600 align=3Dleft border=3D0 name=3DGraphic32><BR =
clear=3Dleft><BR><BR></P>
<P><B>Add a constructor for the class which has parameters with the same =
type as=20
the attributes and assigns them to the attributes as shown =
below.</B></P>
<P><IMG height=3D120=20
src=3D"http://www.tusc.com.au/tutorial/images/chap7/RequetItemAddConstruc=
tor.png"=20
width=3D488 align=3Dleft border=3D0 name=3DGraphic33><BR =
clear=3Dleft><BR><BR></P>
<P>The RequestItem constructor is complete. Now to implement the =
onMessage=20
method in the RequestItem Bean.</P>
<H3><BR><BR></H3>
<H3>Implement onMessage Method :</H3>
<P><B>This method is responsible for extracting the information from the =
message=20
and transferring it to the relevant bean.</B></P>
<P><B>The MyStore Manager will check for items which are out of stock =
and=20
generate a request to suppliers specifying the itemID and quantity =
required.=20
</B></P>
<P><B>First import the packages java.util.ArrayList and =
java.util.Iterator=20
.</B></P>
<P><B>Add the following variables to store references</B>. =
</P><PRE><FONT color=3D#000000><FONT face=3DSans><FONT =
size=3D3>ArrayList outOfStockItems =3D null;       </FONT></FONT></FONT>
<FONT color=3D#000000><FONT face=3DSans><FONT size=3D3>Iterator =
itemsIterator =3D null;</FONT></FONT></FONT>
<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 =
SupplierLocalHome suppLocalHome =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 the immutable value object =
as shown=20
below</B>.</P><PRE style=3D"MARGIN-BOTTOM: 0.2in"><FONT =
face=3DSans><FONT size=3D3><FONT color=3D#000000>RequestItem</FONT> =
<FONT color=3D#000000>ri</FONT> <FONT color=3D#000000>=3D</FONT>  <FONT =
color=3D#000000>(RequestItem)</FONT> <FONT =
color=3D#000000>((ObjectMessage)</FONT> <FONT =
color=3D#000000>message).getObject();</FONT></FONT></FONT></PRE>
<P style=3D"MARGIN-BOTTOM: 0in"><B>Add the lines of code shown below, so =
that the=20
manager can login</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>String</FONT> =
<FONT color=3D#000000>mgrAccessID</FONT> <FONT =
color=3D#000000>=3D</FONT> <FONT =
color=3D#000000>access.loginUser(ri.getUsername(),ri.getPasswd());</FONT>=
                 </FONT></FONT>
                          </PRE>
<P><FONT color=3D#000000><FONT face=3D"Nimbus Roman No9 L"><FONT =
size=3D3><B>Get items=20
with zero quantity (no stock), by invoking getOutOfStockItems() on the=20
StoreAccess Bean (which returns a =
Collection)</B>.</FONT></FONT></FONT></P><PRE><FONT face=3DSans><FONT =
size=3D3><FONT color=3D#000000>outOfStockItems</FONT> <FONT =
color=3D#000000>=3D</FONT> <FONT =
color=3D#000000>access.getOutOfStockItems();</FONT></FONT></FONT>
                                                                         =
       </PRE>
<P><FONT color=3D#000000><FONT face=3D"Nimbus Roman No9 L"><FONT =
size=3D3><B>Now,=20
iterating over each item, get the supplierId associated with it by =
invoking the=20
finder methods on the Supplier Bean, finally sending the required =
message to=20
that supplier by invoking the business method requestItem() on the =
Supplier=20
Bean.</B></FONT></FONT></FONT></P><PRE><FONT face=3DSans><FONT =
size=3D3><FONT color=3D#000000>itemsIterator</FONT> <FONT =
color=3D#000000>=3D</FONT> <FONT =
color=3D#000000>outOfStockItems.iterator();</FONT></FONT></FONT>

<FONT face=3DSans><FONT size=3D3><B><FONT =
color=3D#7f0055>while</FONT></B> ( itemsIterator.hasNext() ) {           =
          </FONT></FONT>
<FONT color=3D#000000>    <FONT size=3D3><FONT face=3DSans>ItemData =
itemData=3D ( ItemData ) itemsIterator.next();</FONT></FONT></FONT>
<FONT color=3D#000000>    <FONT size=3D3><FONT =
face=3DSans>String</FONT></FONT></FONT> <FONT size=3D3><FONT =
face=3DSans><FONT color=3D#000000>suppID</FONT> <FONT =
color=3D#000000>=3D</FONT> <FONT =
color=3D#000000>itemData.getSupplierID();</FONT></FONT></FONT>
<FONT color=3D#000000>    <FONT size=3D3><FONT =
face=3DSans>SupplierLocal</FONT></FONT></FONT> <FONT size=3D3><FONT =
face=3DSans><FONT color=3D#000000>supplier</FONT> <FONT =
color=3D#000000>=3D</FONT> <B><FONT color=3D#7f0055>this</FONT></B><FONT =
color=3D#000000>.suppLocalHome.findByPrimaryKey(suppID);</FONT></FONT></F=
ONT>
<FONT color=3D#000000>    <FONT size=3D3><FONT =
face=3DSans>Integer</FONT></FONT></FONT> <FONT size=3D3><FONT =
face=3DSans><FONT color=3D#000000>quantity</FONT> <FONT =
color=3D#000000>=3D</FONT> <B><FONT color=3D#7f0055>new</FONT></B> <FONT =
color=3D#000000>Integer</FONT> <FONT =
color=3D#000000>(ri.getQuantity());</FONT></FONT></FONT>
<FONT color=3D#000000>    <FONT size=3D3><FONT =
face=3DSans>String</FONT></FONT></FONT> <FONT size=3D3><FONT =
face=3DSans><FONT color=3D#000000>itemID</FONT> <FONT =
color=3D#000000>=3D</FONT> <FONT =
color=3D#000000>ri.getItemID();</FONT></FONT></FONT>
<FONT color=3D#000000>    <FONT size=3D3><FONT =
face=3DSans>supplier.requestItem(</FONT></FONT></FONT> <FONT =
size=3D3><FONT face=3DSans><FONT color=3D#000000>itemID,</FONT> <FONT =
color=3D#000000>quantity);</FONT> </FONT></FONT>
<FONT size=3D3><FONT face=3DSans><FONT color=3D#000000>}</FONT>   =
</FONT></FONT>                                    </PRE>
<P><B>Code snippet of onMessage is shown below.</B></P>
<P><IMG height=3D488=20
src=3D"http://www.tusc.com.au/tutorial/images/chap7/RequetItemsBeanOnMess=
age.png"=20
width=3D556 align=3Dleft border=3D0 name=3DGraphic5><BR =
clear=3Dleft><BR><BR></P>
<P>Now our bean is complete, and all that remains are the deployment =
descriptors=20
for the bean.</P>
<H3>Deploy RequestItems Bean :</H3>
<P><B>In order to deploy this bean we have to add a few deployment =
descriptors.=20
As shown below, five tags have been added.</B></P>
<P><IMG height=3D504=20
src=3D"http://www.tusc.com.au/tutorial/images/chap7/DeploymentDescriptors=
.png"=20
width=3D554 align=3Dleft border=3D0 name=3DGraphic35><BR =
clear=3Dleft><BR><BR></P>
<P><B>First add the tag shown below at class level in the RequestItems =
Bean, to=20
obtain a reference to StoreAccessBean, so that methods can be invoked on =
that=20
bean.</B></P><PRE><FONT color=3D#3f5fbf><FONT face=3DSans><FONT =
size=3D3>@ejb.ejb<FONT color=3D#7f7f9f>-</FONT>ref</FONT></FONT></FONT>
<FONT face=3DSans><FONT size=3D3><FONT color=3D#3f5fbf>ejb</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>view</FONT><FONT =
color=3D#7f7f9f>-</FONT><FONT =
color=3D#3f5fbf>type=3D"remote"</FONT></FONT></FONT>
<FONT face=3DSans><FONT size=3D3><FONT color=3D#3f5fbf>ref</FONT><FONT =
color=3D#7f7f9f>-</FONT><FONT =
color=3D#3f5fbf>name=3D"StoreAccess"</FONT></FONT></FONT></PRE>
<P><B>This tag will generate deployment descriptors in ejb-jar.xml when =
you=20
generate your ejb classes, as this message bean has to authenticate, =
before=20
transferring information to the relevant bean. This will generate these=20
descriptors as shown below.</B></P>
<P><IMG height=3D366=20
src=3D"http://www.tusc.com.au/tutorial/images/chap7/ejbJarStoreAccess.png=
"=20
width=3D488 align=3Dleft border=3D0 name=3DGraphic41><BR =
clear=3Dleft><BR><BR></P>
<P><B>Add another tag as shown below, to obtain a reference to the =
Supplier Bean=20
from this bean.</B></P><PRE><FONT color=3D#3f5fbf><FONT =
face=3DSans><FONT size=3D3>@ejb.ejb<FONT =
color=3D#7f7f9f>-</FONT>ref</FONT></FONT></FONT>
<FONT face=3DSans><FONT size=3D3><FONT color=3D#3f5fbf>ejb</FONT><FONT =
color=3D#7f7f9f>-</FONT><FONT =
color=3D#3f5fbf>name=3D"Supplier"</FONT></FONT></FONT>
<FONT face=3DSans><FONT size=3D3><FONT color=3D#3f5fbf>view</FONT><FONT =
color=3D#7f7f9f>-</FONT><FONT =
color=3D#3f5fbf>type=3D"local"</FONT></FONT></FONT>
<FONT face=3DSans><FONT size=3D3><FONT color=3D#3f5fbf>ref</FONT><FONT =
color=3D#7f7f9f>-</FONT><FONT =
color=3D#3f5fbf>name=3D"SupplierLocal"</FONT></FONT></FONT></PRE>
<P><B>This tag will generate also generate descriptors in ejb-jar.xml =
when you=20
generate your ejb classes, as this message bean transfers information to =
the=20
Supplier bean. The following descriptors are generated as shown below. =
</B></P>
<P><IMG height=3D498=20
src=3D"http://www.tusc.com.au/tutorial/images/chap7/ejbJarSupplier.png" =
width=3D450=20
align=3Dleft border=3D0 name=3DGraphic42><BR clear=3Dleft><BR><BR></P>
<P><FONT color=3D#800000><I>Note : Another descriptor which has been =
generated is=20
&lt;ejb-name&gt;, which is generated by the tag @ejb.bean that was added =
by the=20
EJB creation wizard.</I></FONT></P>
<P style=3D"FONT-STYLE: normal"><IMG height=3D96=20
src=3D"http://www.tusc.com.au/tutorial/images/chap7/ejbJarEjbBean.png" =
width=3D278=20
align=3Dleft border=3D0 name=3DGraphic43><BR clear=3Dleft><FONT =
color=3D#800000>This tag=20
generates the following descriptors in ejb-jar.xml:</FONT></P>
<P><IMG height=3D290=20
src=3D"http://www.tusc.com.au/tutorial/images/chap7/ejbJarBeanNameDD.png"=
=20
width=3D388 align=3Dleft border=3D0 name=3DGraphic44><BR =
clear=3Dleft><BR><BR></P>
<P><B>Add another tag as shown below. This tag is JBOSS-specific, and is =
used to=20
register a message-driven bean with a jndi-name, using the format=20
"queue/name".</B></P><PRE style=3D"MARGIN-BOTTOM: 0.2in"><FONT =
face=3DSans><FONT size=3D3><FONT =
color=3D#3f5fbf>@jboss.destination</FONT><FONT =
color=3D#7f7f9f>-</FONT><FONT color=3D#3f5fbf>jndi</FONT><FONT =
color=3D#7f7f9f>-</FONT><FONT color=3D#3f5fbf>name </FONT></FONT></FONT>
<FONT color=3D#3f5fbf><FONT face=3DSans><FONT =
size=3D3>name=3D"queue/MdbQueue"</FONT></FONT></FONT></PRE>
<P><B>This will generate the following deployment descriptors in=20
jboss.xml:</B></P>
<P><IMG height=3D64=20
src=3D"http://www.tusc.com.au/tutorial/images/chap7/jbossDestinationName.=
png"=20

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -