?? 5.mht
字號:
clear=3Dleft><B>First add the tag=20
shown below at class level in CustomerBean.</B></P><PRE><FONT =
face=3DSans><FONT style=3D"FONT-SIZE: 11pt" size=3D2><FONT =
color=3D#3f5fbf><B>@</B>ejb.resource</FONT><FONT =
color=3D#7f7f9f>-</FONT><FONT color=3D#3f5fbf>ref</FONT> <FONT =
color=3D#3f5fbf>res</FONT><FONT color=3D#7f7f9f>-</FONT><FONT =
color=3D#3f5fbf>ref</FONT><FONT color=3D#7f7f9f>-</FONT><FONT =
color=3D#3f5fbf>name=3D"jdbc/DefaultDS<B>"</B></FONT></FONT></FONT>
<FONT face=3DSans><FONT style=3D"FONT-SIZE: 11pt" size=3D2><FONT =
color=3D#3f5fbf>res</FONT><FONT color=3D#7f7f9f>-</FONT><FONT =
color=3D#3f5fbf>type=3D"javax.sql.Datasource"</FONT></FONT></FONT>
<FONT face=3DSans><FONT style=3D"FONT-SIZE: 11pt" size=3D2><FONT =
color=3D#3f5fbf>res</FONT><FONT color=3D#7f7f9f>-</FONT><FONT =
color=3D#3f5fbf>auth=3D"Container"</FONT></FONT></FONT></PRE>
<P><B>This tag will generate deployment descriptors in ejb-jar.xml, as =
the bean=20
has to know which datasource you are going to connect to, and what is =
its ts=20
type, etc. This will generate these descriptors as shown below.</B></P>
<P><IMG height=3D376=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/ejbJarDSTag.png" =
width=3D446=20
align=3Dleft border=3D0 name=3DGraphic22><BR clear=3Dleft><BR><BR></P>
<P><B>Add this second tag required for the JBOSS application server. =
</B></P><PRE style=3D"MARGIN-BOTTOM: 0.2in"><FONT face=3DSans><FONT =
style=3D"FONT-SIZE: 11pt" size=3D2><FONT =
color=3D#3f5fbf>@jboss.resource</FONT><FONT =
color=3D#7f7f9f>-</FONT><FONT color=3D#3f5fbf>ref</FONT> <FONT =
color=3D#3f5fbf>res</FONT><FONT color=3D#7f7f9f>-</FONT><FONT =
color=3D#3f5fbf>ref</FONT><FONT color=3D#7f7f9f>-</FONT><FONT =
color=3D#3f5fbf>name=3D"jdbc/DefaultDS"</FONT></FONT></FONT>
<FONT color=3D#3f5fbf> <FONT style=3D"FONT-SIZE: 11pt" size=3D2><FONT =
face=3DSans>jndi<FONT =
color=3D#7f7f9f>-</FONT>name=3D"java:/DefaultDS<B>"</B></FONT></FONT></FO=
NT></PRE>
<P><B>This tag will generate deployment descriptors in jboss.xml, as the =
application server has to know with what jndi-name the datasource has =
been=20
registered with. This will generate these descriptors as shown =
below.</B></P>
<P><IMG height=3D182=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/jbossDDTag.png" =
width=3D346=20
align=3Dleft border=3D0 name=3DGraphic23><BR clear=3Dleft><BR><BR></P>
<P><B>Now everything is complete, and it's time to deploy the bean. So,=20
regenerate your EJB classes.</B></P>
<P><FONT color=3D#800000><I>Note : As noted in earlier chapters we are=20
regenerating classes over and over to properly illustrate each step and =
its=20
result. Once you are proficient you will be able to forgo most of=20
this.</I></FONT></P>
<P><B>Go to Lomboz J2EE View > expand node MyStore > expand =
MyStoreMgr=20
> select 'Jboss 3.2.1 ALL' .</B></P>
<P><B>Right click > select 'Debug Sever' on pop up menu.</B></P>
<P><FONT color=3D#800000><I>Note : This is to start your server, if you =
are=20
already running your server then skip these steps and go to the next=20
one.</I></FONT></P>
<P><B>Go to MyStoreMgr node in LombozJ2EE view > right click > =
select=20
'Deploy' on pop up menu as shown in figure below.</B></P>
<P><IMG height=3D224=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/LombozJ2EEViewCustome=
rBeanDeploy.png"=20
width=3D416 align=3Dleft border=3D0 name=3DGraphic24><BR =
clear=3Dleft><BR><BR></P>
<P><FONT color=3D#800000><I>Note : All these techniques have been shown =
in=20
previous chapters (1 and 3) so please refer to them.</I></FONT></P>
<P><B>The messages in the console will show whether your bean has been=20
successfully deployed or not.</B></P>
<P><B>Now our Customer Bean is complete, and in order to create a client =
to=20
invoke operations on this bean we have make some modifications to our=20
StoreAccess Bean.</B></P>
<P><FONT color=3D#800000><I>Note : As shown in the diagram at the =
beginning of=20
this chapter that client will invoke operations on Customer Bean through =
Store=20
AccesBean, as it is a good practice to access Entity Beans in this =
manner. As a=20
result we need a Local view of Customer Bean rather than Remote, because =
both=20
are in the same Java Virtual Machine.</I></FONT></P>
<H3>Add Create Method in StoreAccess :</H3>
<P><B>In StoreAccess Bean add an ejbCreate method, which will create a =
BMP=20
Entity Bean (in this case a Customer Bean) with the following signature =
</B></P><PRE style=3D"MARGIN-BOTTOM: 0.2in"><FONT face=3DSans><FONT =
style=3D"FONT-SIZE: 11pt" size=3D2><FONT color=3D#7f0055>public</FONT> =
<FONT color=3D#7f0055>void</FONT> <FONT color=3D#000000>ejbCreate</FONT> =
<FONT color=3D#000000>()</FONT> <FONT color=3D#7f0055>throws</FONT> =
<FONT color=3D#000000>javax.ejb.CreateException</FONT> =
</FONT></FONT></PRE>
<P><B>Now, add a field to store the reference obtained by locating =
Customer in=20
JNDI.</B></P><PRE style=3D"MARGIN-BOTTOM: 0.2in"><FONT face=3DSans><FONT =
style=3D"FONT-SIZE: 11pt" size=3D2><B><FONT color=3D#7f0055>private =
</FONT></B><FONT color=3D#000000>CustomerLocalHome</FONT> <FONT =
color=3D#000000>customerLocalHome</FONT>;</FONT></FONT></PRE>
<P><B>In ejbCreate method store the reference in the customerLocalHome =
variable=20
by invoking the getLocalHome static method in CustomerUtil class as =
shown in the=20
code snippet below from StoreAccess Bean.</B></P>
<P><IMG height=3D244=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/StoreAccessBeanEjbCre=
ate.png"=20
width=3D416 align=3Dleft border=3D0 name=3DGraphic25><BR =
clear=3Dleft><BR><BR></P>
<H3><B>Add Business Method in StoreAccess :</B></H3>
<P><B>Add another business method in StoreAccess Bean which will invoke =
the=20
corresponding business method on Customer Bean.</B></P>
<P>Now, add a business method with this signature <FONT=20
face=3D"Nimbus Roman No9 L"><B><FONT color=3D#000000>public =
CustomerData</FONT>=20
<FONT color=3D#000000>getCustomerData(String userID) </FONT></B><FONT=20
color=3D#000000>with Interface type as Remote. As customers will log on =
to MyStore=20
with username, once they are authe</FONT></FONT>nticated they will be =
identified=20
by userid and they can retrieve their account details from MyStore using =
this=20
userid.</P>
<P><FONT color=3D#800000><FONT face=3D"Nimbus Roman No9 L"><I>Note : =
Steps to create=20
business methods are covered in previous chapters.</I></FONT></FONT></P>
<P><FONT color=3D#000000><FONT face=3D"Nimbus Roman No9 L"><FONT =
size=3D3><B>Now=20
invoke one of the finder methods of Customer on the reference variable =
we have=20
created in the ejbCreate method.</B></FONT></FONT></FONT></P><PRE =
style=3D"MARGIN-BOTTOM: 0.2in"><FONT style=3D"FONT-SIZE: 11pt" =
size=3D2><FONT face=3DSans>CustomerLocal myCustomer =3D =
customerLocalHome.findByUserID(userID)</FONT></FONT> </PRE>
<P><FONT color=3D#000000><FONT face=3D"Nimbus Roman No9 L"><FONT =
size=3D3><B>Now=20
invoke the business method on Customer using the myCustomer reference=20
variable.</B></FONT></FONT></FONT></P><PRE style=3D"MARGIN-BOTTOM: =
0.2in"><FONT face=3DSans><FONT style=3D"FONT-SIZE: 11pt" =
size=3D2>CustomerData cd =3D myCustomer.getCustomerData() =
</FONT></FONT></PRE>
<P>Code snippet of this business method is shown below.</P>
<P><BR><BR></P>
<P><IMG height=3D312=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/StoreAccessBeanGetCus=
tomerData.png"=20
width=3D506 align=3Dleft border=3D0 name=3DGraphic26><BR =
clear=3Dleft><BR><BR></P>
<P><B>All methods in StoreAccess Bean have now been added for the =
accessing the=20
Customer's business method. All that remains are the deployment =
descriptors=20
required for linking/referencing the StoreAccess and Customer beans. We =
will add=20
the two tags shown below.</B></P>
<P><IMG height=3D562=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/StoreAccessBeanDDTag.=
png"=20
width=3D442 align=3Dleft border=3D0 name=3DGraphic27><BR =
clear=3Dleft><BR><BR></P>
<P><B>First add this tag at class level in StoreAccess =
Bean.</B></P><PRE><FONT color=3D#3f5fbf><FONT face=3DSans><FONT =
size=3D3>@ejb.ejb<FONT color=3D#7f7f9f>-</FONT>ref ejb<FONT =
color=3D#7f7f9f>-</FONT>name=3D"Customer"</FONT></FONT></FONT>
<FONT size=3D3><FONT face=3DSans><FONT color=3D#3f5fbf>view</FONT><FONT =
color=3D#7f7f9f>-</FONT><FONT =
color=3D#3f5fbf>type=3D"local"</FONT></FONT></FONT>
<FONT size=3D3><FONT face=3DSans><FONT color=3D#3f5fbf>ref</FONT><FONT =
color=3D#7f7f9f>-</FONT><FONT =
color=3D#3f5fbf>name=3D"CustomerLocal"</FONT></FONT></FONT></PRE>
<P><B>This tag will generate deployment descriptors in ejb-jar.xml, as=20
StoreAccessBean has to know which bean it's referring to, and what is =
its=20
view-type and ref-name. This will generate these descriptors as shown=20
below.</B></P>
<P><FONT color=3D#800000><I>Note : View type is local as both are in the =
same Java=20
Virtual Machine, otherwise it would be Remote. Note that ref-name is =
generated=20
as CustomerLocalHome rather than CustomerHome. Both are generated but we =
are=20
using Local in this case.</I></FONT></P>
<P><IMG height=3D472=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/ejbjarwithStoreAccess=
.png"=20
width=3D472 align=3Dleft border=3D0 name=3DGraphic28><BR =
clear=3Dleft><B>Now add the=20
second tag shown below at class level in StoreAccess Bean.</B></P><PRE =
style=3D"MARGIN-BOTTOM: 0.2in"><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"CustomerLocal"</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"CustomerLocal"</FONT></FONT></FONT></PRE>
<P><B>This tag will generate deployment descriptors in jboss.xml, as the =
application server has to know what jndi-name Customer Bean has been =
registered=20
with. This will generate these descriptors as shown below.</B></P>
<P><FONT color=3D#800000><I>Note : Ref-name and jndi-name are used for =
bean as=20
local (in same JVM).</I></FONT></P>
<P><IMG height=3D218=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/jbossWithStoreAccessW=
rong.png"=20
width=3D378 align=3Dleft border=3D0 name=3DGraphic29><BR =
clear=3Dleft><BR><BR></P>
<P><FONT color=3D#800000><I>Note : As we can see from the code snippet =
above, the=20
deployment descriptor generated by tag @jboss is wrong, because for =
local=20
referencing of Customer tag <ejb-ref> should be =
<ejb-local-ref>.=20
There seems to be a bug in this tag, so we will correct this manually by =
changing the tag in the jboss.xml file as shown below.</I></FONT></P>
<P><IMG height=3D222=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/jbossWithStoreAccess.=
png"=20
width=3D370 align=3Dleft border=3D0 name=3DGraphic30><BR =
clear=3Dleft><FONT=20
color=3D#800000><I><B>Caution here:</B> Make sure that you do this =
change manually=20
after you finish regenerating your EJB classes, because every time you=20
regenerate your classes, 'jboss.xml' will initially have the wrong =
descriptors=20
generated by this tag.</I></FONT></P>
<P>Now our changes to StoreAccess Bean are complete, so deploy your bean =
again=20
now from the Lomboz J2EE View. The steps to do that are shown above and =
in=20
previous chapters. Messages will appear in the console showing the =
status of=20
deployment.</P>
<P>Once your bean is deployed successfully, create a test client which =
will=20
invoke the loginUser method on StoreAccessBean and getCustomerData on=20
CustomerBean.</P>
<H3 style=3D"MARGIN-TOP: 0in; MARGIN-BOTTOM: 0in"><BR></H3>
<H3 style=3D"MARGIN-TOP: 0in; MARGIN-BOTTOM: 0in"><B>Create your Test =
Client=20
:</B></H3>
<P style=3D"MARGIN-BOTTOM: 0in"><BR></P>
<P>G<B>o to Project MytStore node > select src node abd expand it =
> select=20
au.com.tusc.client package > right click.</B></P>
<P><B>Select New on pop up menu > select Lomboz EJB Test Client =
Wizard</B>.=20
</P>
<P><FONT color=3D#800000><I>Note : These steps are covered in previous=20
chapters.</I></FONT></P>
<P><B>Select package name au.com.tusc.client, name as SessionBMPClient =
and=20
select Ejb Home as au.com.tusc.session.StoreAccessHome and Ejb Interface =
as=20
au.com.tusc.session.StoreAccess</B> as shown in figure below.</P>
<P><IMG height=3D352=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/TesTClientCreateEJB.p=
ng"=20
width=3D866 align=3Dleft border=3D0 name=3DGraphic14><BR =
clear=3Dleft>T<B>his will=20
generate required methods for you in your SessionBMPClient class and you =
simply=20
invoke the loginUser and getCustomerData methods as shown</B>.</P>
<P><IMG height=3D582=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/TestClientGeneratedVi=
ew.png"=20
width=3D458 align=3Dleft border=3D0 name=3DGraphic32><BR =
clear=3Dleft><BR><BR></P>
<P><B>Now to add some code to your client</B>.</P>
<P><B>Add these lines under the testBean method as shown below. =
</B></P><PRE><FONT face=3DSans><FONT style=3D"FONT-SIZE: 11pt" =
size=3D2><FONT color=3D#000000>System.out.println(</FONT><FONT =
color=3D#2a00ff>"Request from client : "</FONT><FONT =
color=3D#000000>);</FONT></FONT></FONT>
<FONT face=3DSans><FONT style=3D"FONT-SIZE: 11pt" size=3D2><FONT =
color=3D#000000>String</FONT> <FONT color=3D#000000>userID</FONT> <FONT =
color=3D#000000>=3D</FONT> <FONT =
color=3D#000000>myBean.loginUser(</FONT><FONT =
color=3D#2a00ff>"ANDY"</FONT><FONT color=3D#000000>,</FONT><FONT =
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -