📄 chapter6.htm
字号:
au.com.tusc.session.StoreAccess. </B></P>
<P><B>This will generate required methods for you in your SessionCMPClient class
and you only have to invoke loginUser, getCustomerData, getManagerData (Manager
Bean was developed as part of an exercise in the previous chapter) and
getOutOfStockItems as shown below.</B></P>
<P><B>Now, the last step is to write the code for your client.</B></P>
<P><B>In order to access out of stock items we need an Iterator and an ArrayList
for items. So, declare two variables of these respective types, and import the
packages for these types (which are java.util.ArrayList and java.util.Iterator).
</B></P><PRE><FONT face=Sans><FONT size=3><FONT color=#000000>Iterator</FONT> <FONT color=#000000>itemsIterator</FONT> <FONT color=#000000>=</FONT> <FONT color=#7f0055>null</FONT><FONT color=#000000>;</FONT></FONT></FONT>
<FONT face=Sans><FONT size=3><FONT color=#000000>ArrayList</FONT> <FONT color=#000000>items</FONT> <FONT color=#000000>=</FONT> <B><FONT color=#7f0055>null</FONT></B><FONT color=#000000>;</FONT></FONT></FONT></PRE>
<P><B>And now add these lines of code to invoke the methods mentioned above.
</B></P><PRE><FONT face=Sans><FONT size=3><FONT color=#000000>System.out.println(</FONT><FONT color=#2a00ff>"Request from client : "</FONT><FONT color=#000000>);</FONT></FONT></FONT>
<FONT face=Sans><FONT size=3><FONT color=#000000>String</FONT> <FONT color=#000000>userID</FONT> <FONT color=#000000>=</FONT> <FONT color=#000000>myBean.loginUser(</FONT><FONT color=#2a00ff>"ANDY"</FONT><FONT color=#000000>,</FONT><FONT color=#2a00ff>"PASSWD"</FONT><FONT color=#000000>);</FONT></FONT></FONT>
<FONT face=Sans><FONT size=3><FONT color=#000000>System.out.println(</FONT><FONT color=#2a00ff>"Reply from Server: Your userid is "</FONT> <FONT color=#000000>+</FONT> <FONT color=#000000>userID</FONT> <FONT color=#000000>);</FONT></FONT></FONT>
<FONT face=Sans><FONT size=3><FONT color=#000000>CustomerData</FONT> <FONT color=#000000>cd</FONT> <FONT color=#000000>=</FONT> <FONT color=#000000>myBean.getCustomerData(userID);</FONT> </FONT></FONT>
<FONT face=Sans><FONT size=3><FONT color=#000000>System.out.println</FONT> <FONT color=#000000>(</FONT><FONT color=#2a00ff>"Andy your details with MyStore are "</FONT> <FONT color=#000000>+</FONT> <FONT color=#000000>cd</FONT> <FONT color=#000000>);</FONT></FONT></FONT>
<FONT face=Sans><FONT size=3>String mgrID = myBean.loginUser(<FONT color=#2a00ff>"RUSTY"</FONT>,<FONT color=#2a00ff>"PASSWD"</FONT>);</FONT></FONT>
<FONT face=Sans><FONT size=3><FONT color=#000000>System.out.println(</FONT><FONT color=#2a00ff>"Reply from Server: Your mgrid is "</FONT> <FONT color=#000000>+</FONT> <FONT color=#000000>mgrID</FONT> <FONT color=#000000>);</FONT></FONT></FONT>
<FONT face=Sans><FONT size=3><FONT color=#000000>ManagerData</FONT> <FONT color=#000000>md</FONT> <FONT color=#000000>=</FONT> <FONT color=#000000>myBean.getManagerData(mgrID);</FONT> </FONT></FONT>
<FONT face=Sans><FONT size=3><FONT color=#000000>System.out.println</FONT> <FONT color=#000000>(</FONT><FONT color=#2a00ff>"Rusty your details with MyStore are "</FONT> <FONT color=#000000>+</FONT> <FONT color=#000000>md</FONT> <FONT color=#000000>);</FONT> </FONT></FONT>
<FONT face=Sans><FONT size=3><FONT color=#000000>System.out.println(</FONT><FONT color=#2a00ff>"Manager Request : List items out of stock "</FONT><FONT color=#000000>);</FONT></FONT></FONT>
<FONT face=Sans><FONT size=3><FONT color=#000000>items</FONT> <FONT color=#000000>=</FONT> <FONT color=#000000>myBean.getOutOfStockItems();</FONT></FONT></FONT>
<FONT face=Sans><FONT size=3><FONT color=#000000>itemsIterator</FONT> <FONT color=#000000>=</FONT> <FONT color=#000000>items.iterator();</FONT></FONT></FONT>
<FONT face=Sans><FONT size=3><FONT color=#000000>System.out.println(</FONT><FONT color=#2a00ff>"List Of Out Of stock Items "</FONT><FONT color=#000000>);</FONT> </FONT></FONT>
<FONT face=Sans><FONT size=3><B><FONT color=#7f0055>while</FONT></B> <FONT color=#000000>(</FONT> <FONT color=#000000>itemsIterator.hasNext()</FONT> <FONT color=#000000>)</FONT> <FONT color=#000000>{</FONT> </FONT></FONT>
<FONT color=#000000> <FONT size=3><FONT face=Sans>ItemData</FONT></FONT></FONT><FONT size=3><FONT face=Sans> <FONT color=#000000>itemData=</FONT> <FONT color=#000000>(</FONT> <FONT color=#000000>ItemData</FONT> <FONT color=#000000>)</FONT> <FONT color=#000000>itemsIterator.next();</FONT></FONT></FONT>
<FONT color=#000000> <FONT size=3><FONT face=Sans>System.out.println</FONT></FONT></FONT><FONT size=3><FONT face=Sans> <FONT color=#000000>(</FONT><FONT color=#2a00ff>"Item Data "</FONT> <FONT color=#000000>+</FONT> <FONT color=#000000>itemData</FONT> <FONT color=#000000>);</FONT></FONT></FONT>
<FONT color=#000000><FONT face=Sans><FONT size=3>}</FONT></FONT></FONT></PRE>
<H3>Test your Client :</H3>
<P style="MARGIN-BOTTOM: 0in"><B>Now, in order to test your client, Select
SessionCMPClient node > Go at top level menu and select the icon with the
'Running Man'</B>.</P>
<P><B>On that select 'Run as' > select Java Application, as shown
below.</B></P>
<H3><IMG height=258 src="chapter6.files/RunTestClient.png" width=570 align=left
border=0 name=Graphic14><BR clear=left><BR><BR></H3>
<P><B>Now on your console, if you get a reply saying two items out of stock
which are 'CALCULATOR' and 'CLOCK', then your call is successful as shown
below</B>.</P>
<H3><IMG height=152 src="chapter6.files/ResultTestClient.png" width=626
align=left border=0 name=Graphic21><BR clear=left><BR><BR></H3>
<H3>Exercise :</H3>
<P>Now, here is an exercise for you. In order to proceed further, implement
Supplier as a CMP Entity Bean. The tasks are given below:</P>
<OL>
<LI>
<P><FONT face="Nimbus Roman No9 L">Create a CMP Bean named Supplier under
package au.com.tusc.cmp. </FONT></P>
<LI>
<P><FONT face="Nimbus Roman No9 L">Implement the ejbCreate method, with all
attributes passed as arguments and then assigned to attributes using mutator
methods.</FONT></P>
<LI>
<P><FONT face="Nimbus Roman No9 L">Add a find method named findUserID with
query and signature: </FONT></P>
<P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><FONT size=3><B>query
"SELECT OBJECT(b) FROM MyStoreSupplier as b where b.userID = ?1"
</B></FONT></FONT></FONT></P>
<P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><B>method
au.com.tusc.cmp.SupplierLocal findUserID(java.lang.String
userID)</B></FONT></FONT></P>
<P><FONT color=#800000><FONT face="Nimbus Roman No9 L"><I>Note : The method
signature is find<cmp attribute> instead of findByPrimaryKey, because
finder methods for non-key CMP attributes use this convention. Accordingly the
return type for finder methods will be either Collection or <entity
type>, as specified in the EJB specification, sections 10.5.6 and 10.5.2
respectively.</I></FONT></FONT></P>
<LI>
<P><FONT face="Nimbus Roman No9 L">Add a business method to get supplier
details with signature:</FONT></P>
<P><FONT face="Nimbus Roman No9 L"><B>public SupplierData
getSupplierData()</B></FONT></P>
<LI>
<P><FONT face="Nimbus Roman No9 L">Add a business method to get request items
from various suppliers with signature:</FONT></P>
<P><FONT face="Nimbus Roman No9 L"><B>public void requestItem(String itemID,
Integer quantity)</B></FONT></P>
<LI>
<P><FONT face="Nimbus Roman No9 L">Add callback methods, required for
setting/unsetting bean context with signatures:</FONT></P>
<P><FONT face="Nimbus Roman No9 L"><B><FONT color=#000000>public void</FONT>
<FONT color=#000000>setEntityContext(EntityContext ctx)</FONT></B></FONT></P>
<P><FONT face="Nimbus Roman No9 L"><B>public void
unsetEntityContext()</B></FONT></P>
<LI>
<P><FONT face="Nimbus Roman No9 L">Deploy the Supplier Bean.</FONT></P>
<LI>
<P><FONT face="Nimbus Roman No9 L">Add a field to StoreAcess Bean to store its
reference:</FONT></P>
<P><FONT face="Nimbus Roman No9 L"><B><FONT color=#000000>private
SupplierLocalHome</FONT> supplier<FONT
color=#000000>LocalHome</FONT></B></FONT></P>
<LI>
<P><FONT face="Nimbus Roman No9 L">In the ejbCreate method of StoreAccess Bean
store a reference in supplierLocalHome variable by invoking the getLocalHome
static method in supplierUtil.</FONT></P></LI></OL>
<OL start=12>
<LI>
<P><FONT face="Nimbus Roman No9 L">Add a business method to StoreAccess Bean
with signature:</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>
<LI>
<P><FONT color=#000000><FONT face="Nimbus Roman">Add the following tags for
deployment at class level for linking/referencing
Supplier</FONT>.</FONT></P></LI></OL>
<OL><PRE><FONT color=#000000><FONT face=Sans><FONT size=3><FONT color=#3f5fbf>1. @ejb.ejb</FONT><FONT color=#7f7f9f>-</FONT><FONT color=#3f5fbf>ref</FONT> <FONT color=#3f5fbf>ejb</FONT><FONT color=#7f7f9f>-</FONT><FONT color=#3f5fbf>name="Supplier"</FONT></FONT></FONT></FONT>
<FONT size=3><FONT face=Sans><FONT color=#3f5fbf>view</FONT><FONT color=#7f7f9f>-</FONT><FONT color=#3f5fbf>type="local"</FONT></FONT></FONT>
<FONT size=3><FONT face=Sans><FONT color=#3f5fbf>ref</FONT><FONT color=#7f7f9f>-</FONT><FONT color=#3f5fbf>name="SupplierLocal"</FONT></FONT></FONT>
<FONT face=Sans><FONT size=3><FONT color=#3f5fbf>2.</FONT> <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="SupplierLocal"</FONT></FONT></FONT>
<FONT size=3><FONT face=Sans><FONT color=#3f5fbf>jndi</FONT><FONT color=#7f7f9f>-</FONT><FONT color=#3f5fbf>name="SupplierLocal"</FONT> </FONT></FONT></PRE></OL>
<OL start=14>
<LI>
<P><FONT color=#000000><FONT face="Nimbus Roman No9 L">Test your Supplier Bean
by running your Test Client created for Item named
SessionCMPClient.</FONT></FONT></P></LI></OL>
<P><FONT color=#800000><I>Note : All these steps are same as were done for Item.
Implement this Bean which will be used in subsequent chapters.</I></FONT></P>
<P><FONT color=#800000><I>In case you have difficulty, we have provided a
SupplierBean class, modified StoreAccessBean class and SesiomCMPClient class.
You can download these files under downloads below.</I></FONT></P>
<P align=left><FONT color=#800000><I>Downloads :</I></FONT></P>
<P align=left><A
href="http://www.tusc.com.au/tutorial/data/chap6/SupplierBean.java"><I><FONT
color=#0047ff>SupplierBean</FONT></I></A></P>
<P align=left><A
href="http://www.tusc.com.au/tutorial/data/chap6/StoreAccessBean.java"><I><FONT
color=#0047ff>StoreAccessBean</FONT></I></A></P>
<P align=left><A
href="http://www.tusc.com.au/tutorial/data/chap6/SessionCMPClient.java"><I><FONT
color=#0047ff>SessionCMPClient</FONT></I></A></P>
<P align=left><BR><BR></P>
<P style="MARGIN-BOTTOM: 0in"><BR></P>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<THEAD>
<TR vAlign=top>
<TD width="33%"><A href="http://www.tusc.com.au/tutorial/html/chap5.html"
name=Chapter5><IMG height=35 src="chapter6.files/PrevArrow.png" width=51
align=left border=0 name=Graphic15><BR clear=left></A></TD>
<TD width="33%">
<P align=center><A name="Table of Contents"></A><A
href="http://www.tusc.com.au/tutorial/html/index.html"
name="Table of Contents"><IMG height=34 src="chapter6.files/TOC.png"
width=51 align=bottom border=0 name=Graphic2></A></P></TD>
<TD width="33%"><A href="http://www.tusc.com.au/tutorial/html/chap7.html"
name="Chapter 7"><IMG height=36 src="chapter6.files/NextArroww.png"
width=49 align=right border=0 name=Graphic52><BR
clear=right></A></TD></TR></THEAD>
<TBODY></TBODY></TABLE>
<P align=center><IMG height=2 src="chapter6.files/blurulr6.gif" width=796
align=bottom border=0 name=Graphic56></P>
<TABLE cellSpacing=0 cellPadding=4 width="100%" bgColor=#0a6a91 border=0>
<COLGROUP>
<COL width=51>
<COL width=74>
<COL width=82>
<COL width=49>
<TBODY>
<TR>
<TD width="20%">
<P><!-- Footer --><A href="http://www.tusc.com.au/"><IMG height=73
alt=TUSC src="chapter6.files/logo.gif" width=149 align=bottom border=0
name=Graphic1></A> </P></TD>
<TD width="29%">
<P align=left><IMG height=73 alt="Reliable, On-Time Delivery."
src="chapter6.files/reliable.gif" width=267 align=bottom border=0
name=Graphic2> </P></TD>
<TD width="32%">
<P align=left><!--copyright details--><FONT color=#ffffff>Copyright 2003
TUSC Pty. Ltd.</FONT> </P></TD>
<TD width="19%"></TD></TR></TBODY></TABLE>
<P><!-- footer finishes --><BR><BR></P></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -