354 5 12MB
English Pages 847 Year 2003
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Copy right Pref ace
Lear n about t he EJB 2 .0 specif icat ion The t eam th at wrot e th is r edbo ok
a pu blished DevBecome elop EJBs w it h au Sttuhor dio App licat ion Dev eloper Com ment s welcome
Deploy EJBs t oect Wure eb and Sp her e Applicat ion Ser v er Part 1: EJB archit concept s Cha pter 1. I nt rod uct ion t o Ent erprise JavaBean s
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Ser ver- side com pon en t arch it ect u re App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans Why EJBs? ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Ja va 2 Plat form , Ent erprise Edition ( J2 EE) specif icat ion Thr oug houtEJBt he book , ws e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ingEJB r elat dat ab ase. 2 .0ional ov erview A brief look a t hist ory
I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Adopt ing EJB t echnology concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he En d-t o- end en t erp rise applicat ion developm ent EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed mar per sist enceSum ent it yy b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or Cha pter 2. itect u re con cepts successful imp lemEJB entarch at ions of and EJBs. I n tr oduct ion
I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session EJB roles beans, an dMain m essag e- ewo d r ivrken beans EJB f ram com ponent susing WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At Remot e and local in terf aces: com parison t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Bea n-cont ainer cont ract rev isit ed EJB f ramework sum ma ry En t er prise bean t y pes: r ev isit ed EJB dev elop ment and deploym ent p rocess Dev eloping an EJB: a n ex am ple applicat ion Sum mar y Cha pter 3. En tit y b ean s I n tr oduct ion En t ity bea n concept s Bea n-cont ainer cont ract
Lif e cycle of an en tit y bean Abst ract p ersisten ce sch em a mappin gs Dev eloping an ent it y bean: Cu st omer En t ity bea n d esign an d const ru ction best pract ices Client view of an en tit y bean Sum mar y Cha pter 4. En tit y b ean s a dvan ced: relat ion ships, in herit ance, cu st om queries
• •
Table of Content s
I n tr oduct ion
I ndex
EJB con t ainer-m anag ed relat ionsh ips ( CMR) EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper EJB inh erita nce By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss EJB cu st om query m et hods EJB qu er y langu age ( EJB QL) Pub lish er: I BM Dev eloping ent it y beans wit h a dvan ced co ncept s Pub Dat e: April 1 1, 20 03 En t ity bea n d esign an d const ru ction best pract ices I SBN: 0- 73 84- 26 09 -1 Client v iew of ad van ced ent it y bean con cepts Pages: 75 0 Sum mar y Cha pter 5. Sessio n b ean s I n tr oduct ion Session bea n concept s Bea n-cont ainer cont ract e-cy cle tsequ ce of session bean s ion Lear nLifabout he en EJB 2 .0 specif icat Dev eloping a sessio n b ean
Dev elop EJBs h St u licat ion Session beaw n it d esign andio d coApp nst ruction best Dev pract eloper ices Client v iew of a session bean
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Sum mar y
pter 6. Messagedriven beans This I BMCha Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio tr oduct ion App licat ionI nDeveloper f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it u essaging re, an dser d ep ents of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Jaect va m viceloym concept Message- driv en bean con cept s
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Dev eloping a m essage-d riven bean un derly ing r elat ional dat ab ase. MDB client program min g
Messagedriv en bean int eract pat t erns I n Part 1, we in t r oduce EJBs as aion p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Messageen itbean best icest icu lar w e point out t he n ew fu nct ions int r od uced w it h t he concep t s and t h e driv ar ch ect ur e. pIract n par EJB 2. 0 sp Sum ecif mar icatyion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent7. it yEJB b ean s sand m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or Cha pter client successful Client imp lem ent at ions of EJBs. t y pes How t o access EJBs
I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session g accesse-bean beans, an dUsin m essag d r ivs en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also a session cade dif t o en y bean im plem ent Usin fin gder m et hfa ods, fert iten t m sapp in g st r at egies, and sim p le client s t h at u se t he EJBs. At co mpariso n t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. t he en d, w Client e d escrib e how Hom e f act ory pa tt ern Sum mar y Cha pter 8. Addition al con cept s: t ransaction s, except ion s, securit y I n tr oduct ion Tran sact ions EJB exception h andling Securit y Sum mar y Part 2: Dev elo pin g a nd test ing EJBs wit h Applicat io n Dev eloper Cha pter 9. WebSph ere St udio Applicat ion Developer
WebSp here St udio App lica tion Developer WebSp here St udio Work ben ch Workbench wind ow Perspect iv es and v iews Proj ect s Ser vers EJB dev elop ment en v ir onm en t
• •
Table of Content s
Cha pter 10 . I n t roducin g an d p rep aring for t he sample a pplicat ion
I ndex
Bank m odel EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper Bank d ata base By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss Preparin g Applicat ion Developer Sum mar y Pub lish er: I BM Cha pter 11 . Con t ainer-m ana ged ent it y bean developm ent Pub Dat e: April 1 1, 20 03 Dev eloping t he accoun t CMP ent it y bean I SBN: 0- 73 84- 26 09 -1 Test ing t he ent erprise bea n Pages: 75 0 Dev eloping t he cust om er and t ran sact ion record EJBs Developing relat io nships Dev eloping inh erit an ce st ru ct ures Cleaning t he model Dev eloping custom finders elopingt he a h om e met d Lear nDev about EJB 2 .0hospecif icat ion Code g en er at io n f or fin der an d select m et h ods
DevCha elop w it h Ststuratdio App pterEJBs 12 . Mapping eg ies for licat CMPsion Dev eloper Mapp in g str ategies
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Mapp in g ex am ples
Con vert ers pr and comp osers This I BM Redbook ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Cha pter 13 . Bean-fman y bean dev App licat ion Developer or taged he dent evitelopm en elop t ofment ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Bea n-m an aged persist ence (BMP) Why BMP?
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Recom menda tion s un derly ing r elat ional dat ab ase. Dev eloping a BMP ent it y bean
Gen erat th e deploy ed as code I n Part 1, we in t ring oduce EJBs a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Creatt ing he ch datitaba concep t s and h e tar ectseurt able e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp Test ecifing icatt he ionBMP t hat ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed ent itpr y bean per sist enceRead-o ent itnly y bmean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or et h ods successful Sum impmar lemy ent at ions of EJBs. Cha pter 14 . Session bean developm en t
I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session n beans, an dDesig m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also teless dif session bean im plem ent Dev fineloping der maetsta h ods, fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At tef ult sessio n b eanEJB ap plicat ion s t o a WebSph er e App licat ion Serv er. t he en d, w Dev e deloping escribaesta how o deploy Creat ing a Web Serv ice f rom a session bean Sum mar y Cha pter 15 . EJB access bean s What are access beans? Dev eloping access beans Sum mar y Cha pter 16 . Message- driv en bean dev elopmen t Desig n Dev eloping a m essage-d riven bean Set t ing up t he ser ver
Message- driv en bean client s Test ing t he MDB Tran sact ion t ypes Sum mar y Cha pter 17 . Client developm en t Ty pe of client s References
•
Table of Content s
Dev eloping a ser vlet client
•
I ndex
Dev eloping a St ru ts-based Web client EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper J2 EE client cont ainer By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss Dev eloping a m essage-d riven bean client GUI clien t u sing access bean s Pub lish er: I BM Client u sing a Web ser vice Pub Dat e: April 1 1, 20 03 Ext ernalizin g st rin gs I SBN: 0- 73 84- 26 09 -1 Sum mar y Pages: 75 0 Cha pter 18 . Deploym ent of en t er prise applicat ion s En t er prise applicat ion Con figur in g t h e WebSp here Applicat ion Serv er I n st allin g an ent erprise applicat ion Test ing t he I TSO ba nk ing applicat io n Assem bly2Tool Lear nApplicat aboutiont he EJB .0 specif icat ion Com man d-lin e t ools
Dev elop EJBs Sum mar y w it h St u dio App licat ion Dev eloper Cha pter 19 . Applicat ion Dev eloper t eam dev elopm ent en vir onm en t
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er I n tr oduct ion
Set t ing up pr a team r epo d sitor y This I BM Redbook ovides et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio curren t Ver sions App licat ionCon Developer f or tSy hest em d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re,enan d dario ep loym ent su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Dev elopm t scen f or a sin gle of user Dev elopm en t scen ario f or a t eam
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Part 3: Appen dixes un derly ing r elat ional dat ab ase. Appendix A. Set t ing up th e env iron ment
I n st allat plan ning I n Part 1, we in t rion oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic I n st allin DB2 concep t s and t h eg ar chUDB it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp Iecif icatgion hat prhere ov id e, forionexServ amerple, en hanced fun ct ion alit y for cont ain er - m anag ed n st allin I BM tWebSp Applicat per sist enceI n ent it yionb of ean s and mStessagedr ivion enDev beans. st allat WebSphere udio Applicat elop er We also pr ovid e best pr act ice guidelines f or successful Set imp lem ent at ions of EJBs. t ing up t he EJBBANK dat abase Appendix B. Addit ion al m at erial
I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session catessag in g t h eeWeb at erial beans, an dLom d r ivmen beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also the m Web at er ia ldif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At im plem ent Usin fin gder et hmods, tion s an a crony s t he en d, Abbrev w e d ia escrib e dhow t om deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Relat ed pub licat ion s I BM Redbook s Referenced Web sit es How t o get I BM Redbook s Back cov er I n dex
Copyright •
Table of Content s
•
I nt er nat ionalI ndex Techn ical Su ppor t Or ganizat ion EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
EJB . 0 De v el er opm ener, t w W nk e bSp he reShaddon St u dio App li ca t i on D e ve l oper By Ueli2Wah li, Wout Denay Lari ts h Schu , Deborah , Mart in Weiss Apr il 2 003
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Not e Befor e using t his in for m at ion an d t he p rod uct it su ppor t s, r ead t he inf orm at ion in " Not ices" on p age x ix. Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper First Edition (April 2003) Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This edit ion app lies t o Ver sion 5 of WebSp here St udio Applicat ion D ev eloper and WebSph er e This I BMion Redbook ovides d etEn ailed forJav m ataBean ion onspecificat h ow t o efion fect2ively App licat Ser v er pr and t o t he t erp in rise .0 . use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (This EJB)b ook ar ch is it ect u re, d epr edbook loym ent EJB of su ch elop ap plicat a WebSphfor er eJav App ion Serv er. sim ilar an t o dt he Dev m ention w its ht oVisualAge a flicat or WebSp here App licat ion Ser v er, SG2 4- 6 144, but uses WebSph er e St u dio Ap plicat ion Dev eloper as t he Thr book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an devoug elophout mentt he t ool. un derly ing r elat ional dat ab ase. © Copy ri ght I nt e rna t i ona l Bu si ne ss Ma ch ine s Corp or a t ion 2 0 0 3 . Al l ri ght s re se rv e d. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t h e arnm ch it ectUsers ur e. I nRest parr tict icued larRig w ehtpoint out tdhe n ew ion fu nct int r odeuced h tby he Not e t ot sU.and S. Gover ent s - - Use, uplicat or ions disclosur r est r w ictited EJB 2. 0 spSchedu ecif icatle ionCont t hatr act pr ov idhe,I BM for Cor ex am GSA ADP w it p. ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
Notices
I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also This in for m at ion w as d ev eloped f or pr od uct s and ser v ices of fer ed in t he U. S. A. im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. I BM m ay n ot of fer t h e pr oduct s, serv ices, or feat ur es discu ssed in t h is docum ent in ot h er count r ies. Consult y our local I BM r epr esent at iv e f or inf orm at ion on t he p rod uct s and serv ices cur r en t ly available in y our ar ea. Any r efer en ce t o an I BM p r oduct , p rog ram , or ser v ice is n ot int end ed t o st at e or im ply t hat only t h at I BM pr odu ct , pr ogr am , or serv ice may be used. An y fu nct ionally equiv alen t p rod uct , pr ogr am , or ser vice t h at does n ot inf r inge an y I BM int ellect u al pr oper t y r ight m ay be u sed inst ead. How ever , it is t he user 's r esponsibilit y t o ev aluat e an d v erif y t he operat ion of any non- I BM pr oduct , pr ogr am, or ser v ice. I BM m ay h ave pat ent s or pending pat en t app licat ions cov er in g su bject m at t er d escrib ed in t his docum ent . The f ur nish ing of t his d ocu ment does not giv e y ou any license t o t hese pat ent s. You can send license inq uir ies, in w r it in g, t o:
I BM Direct or of Licen sing, I BM Cor por at ion , Nor t h Cast le Dr iv e Ar m onk , NY 1050 4- 1 785 U.S. A . The f oll ow i ng pa ra gr ap h d oes n ot a ppl y t o t he U nit e d Kin gdom or a ny ot h er count ry w he re such prov isi ons a re i nconsi st en t w it h l oca l l aw : I NTERNATI ONAL BUSI NESS MACHI NES CORPORATI ON PROVI DES THI S PUBLI CATI ON " AS I S" WI THOUT WARRANTY OF ANY KI ND, EI THER EXPRESS OR I MPLI ED, I NCLUD I NG, BUT NOT LI MI TED TO, THE I MPLI ED WARRANTI ES OF NON- I NFRI NGEMENT, MERCHANTABI LI TY OR FI TNESS FOR A PARTI CULAR PURPOSE. Some st at es d o n ot allow d isclaim er of ex pr ess or im plied war r ant ies in cer t ain • Table of Content s t r ansact ions, t her ef or e, t his st at em en t m ay not app ly t o y ou. •
I ndex
EJB 0 for D e ve e nt w it h W ebSphe rt eechn St udio ica ti on De v elor oper This2 .in mlopm at ion could include icalAppl in accur acies t y pogr aph ical
er r or s. Chan ges are perUeli By iodically Wah li, Wout m ade er Denay t o t her,e Lar infor s Schu m at nkion , Deborah her ein; Shaddon t hese , Mart chang in Weiss es w ill b e incorp or at ed in new edit ions of t he pub licat ion. I BM m ay m ake im pr ov ement s and/ or ch anges in t he p r oduct ( s) an d/ or t h e pr ogr s) I d escr ib ed in t his p ublicat ion at any t im e w it h out n ot ice. Pubam lish (er: BM Pub Dat e: April 1 1, 20 03
Any r efer en ces in t his in for m at ion t o non - I BM Web sit es ar e p rov ided for conv enience only and SBN: 0- 73 84- 26 09 -1 do not I in an y m anner serv e as an end orsement of t hose Web sit es. Th e m at er ials at t hose Web Pages: sit es are n ot75p0 art of t he mat er ials for t his I BM p r oduct and use of t hose Web sit es is at y our ow n r isk . I BM m ay u se or dist r ib ut e an y of t he in for m at ion y ou supply in any way it believ es app rop riat e w it hout incur r ing any ob lig at ion t o y ou. I nf orm at ion concert he ninEJB g n onp r oduct s w as ob t ained f r om t he su ppliers of t hose pr odu ct s, Lear n about 2 .0I BM specif icat ion t heir p ublished an noun cem ent s or ot her pub licly av ailab le sour ces. I BM h as not t est ed t hose pr oduDev ct s elop and cannot t h App e accu racy p er for m ance, com pat ibilit y or any ot her claim s EJBs w itconfir h St umdio licat ion of Dev eloper r elat ed t o n on- I BM pr oduct s. Qu est ions on t he capabilit ies of non- I BM pr odu ct s sh ould be add ressed Deploy t o EJBs t he sup to W plier eb Sp s of her t heose Applicat p r oduct ions.Ser v er This in I BM for Redbook m at ion cont pr ovides ain s exam d et ailed ples of in for datma at and ion ron ep or h ow t s used t o ef fect in daily ively buse usinWebSp ess op er here at ions. St udio To App licat illust rat eion t hem Developer as com plet f or tely he as d evpossib elopmle, en t he of exam ap plicat ples ioninclud s based e t he on nt h ames e Entof er pr indise ividu Javals, aBeans ( EJB)panies, com ar ch it ect br ands, u re, an andddpr epoduct loym s. entAllofofsut ch h ese ap plicat nam es ionarset of ict a itWebSph ious ander any e App sim licat ilarion it y Serv t o t he er. nam es and add r esses used by an act ual business ent erp r ise is ent ir ely coin cident al. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing COPYRI GHTr elat LI CENSE: ional dat ab ase. IThis n Part in for 1,mwe at ion in t rcont oduce ainEJBs s samp as le a papplicat art of Jav ionap2r ogr En tams er p rise in sour Ed itce ionlangu ( J2 EE) age, and w hich coverillust t her at basic es concep pr ogr am t smand ing tthech e arniques ch it ecton ur e. v ariou I n par s oper t icu lar at ing w e plat point forout m s. t You he n m ewayfucop nct y, ions m odify int r od , and uceddwistitrhibu t he te tEJB hese 2.sam 0 sp ple ecif icat pr ogr ionam t hat s inprany ov idf orm e, forw it ex h am out ple, pay en m en hanced t t o I BM, fun ct for iont he alit py ur for poses cont ain of developing er - m anag ed , per sist using , ence m ark et ent ing it y or b ean distsr ibut and in mgessageap plicat drion iv enprbeans. og ram sWe conf also or min pr ovid g t oet he bestapplicat pr act ice ionguidelines f or successful pr ogr am m ing imp int lemerf ent ace at ions for t he of EJBs. oper at ing plat f orm for w h ich t he sam ple p r ogr ams are wr it t en. These exam ples hav e n ot been t hor ough ly t est ed un der all condit ions. I BM, t her ef ore, cann ot Igu n aran Part t2, in tply r oduce t h e sam p le bank ingy ,applicat t hen pr imp lem en t itmy ay beans, ee we or im r eliabilit y , ser v iceabilit or f unction ion and of t hese ogr ament s. You copy ,session beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We m odify , an d dist ribu t e t hese sam ple pr ogr am s in any f orm w it hout pay m en t t o I BM f or also t he im plem entoffin der m et h ods, min app r at egies, sim pion le client t h atcon u sefort he EJBs. pu rp oses d ev elop ing, usindif g,fer m en ar kt et g, in org dstist r ibu t ing and applicat pr ogrsams m ing t o At tI he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. BM's applicat ion p r ogr amm ing int er faces.
Trademarks The f ollow in g t er m s ar e t r adem ark s of t h e I nt ern at ional Bu sin ess Machin es Corp or at ion in t he Unit ed St at es, ot h er cou nt r ies, or bot h:
Redb ooks( logo) ™
DB2 Univer sal Dat ab ase™
Lot u s®
Redb ooks™
DB2 ®
MQSer ies®
AI X®
En cin a®
Redb ooks™
I nf orm ix ®
SP™
I BM®
VisualAge®
Cloud scap e™ • •
CI CS®
Table of Content s I ndex
EJB 2 . 0ino™ D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper Dom I MS™
WebSph er e®
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
The f ollow in g t er m s ar e t r adem ark s of ot h er com pan ies: Pub lish er: I BM
Act ionMedia, LANDesk , MMX, Pen t ium an d Pr oShar e ar e t r ad em ar ks of I n t el Cor por at ion in t h e Pub Dat e: April 1 1, 20 03 Unit ed St at es, ot h er cou nt r ies, or bot h. I SBN: 0- 73 84- 26 09 -1
Pages: 75 0 ow s, Window s NT, an d t he Wind ow s logo are t rad em ar k s of Micr osoft Cor por at ion Micr osof t , Wind in t he Unit ed St at es, ot her count r ies, or bot h .
Jav a an d all Java- b ased t r adem ark s an d logos are t rad em ar k s or r egist er ed t rad em ar k s of Sun Micr osy st em s, I nc. in t he Unit ed St at es, ot her coun t ries, or bot h. C- b usLear is ant rabout ademar k of Corollar y, I n c. ion in t he Unit ed St at es, ot h er cou nt r ies, or bot h. t he EJB 2 .0 specif icat UNI X Dev is aelop r egist er edw titrad em ar k App of The Gr eloper oup in t he Unit ed St at es an d ot her count r ies. EJBs h St u dio licatOpen ion Dev SET, SET Secur e Elect ic Tran ion , and t hevSET Deploy EJBs t o Wron eb Sp her esact Applicat ion Ser er Logo ar e t r ad em ar ks ow ned b y SET Secur e Elect ron ic Tr ansact ion LLC. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Ot h er com y, pr oduct ice n ames ayplicat b e tion r ademar k s on or serv ice er m pr ark s of otaBeans h er s. App licat ionpan Developer f or, and t he dser ev velopm en t ofmap s based t h e Ent ise Jav ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Preface This I BM® Redbook pr ovid es d et ailed infor m at ion on h ow t o ef fect ively use WebSp here® St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (• EJB) ar ch it ect u re,of an d d eps loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Table Content •
I ndex
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper un derly ing r elat ional dat ab ase. By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
I n Par t 1 , w e int rod uce EJBs as a par t of Jav a 2 Ent er pr ise Edit ion ( J2 EE) and cover t h e basic concep t s er: and Pub lish I BMt h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJBPub 2. 0Dat spe:ecif icat t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed April 1 1,ion 20 03 per sist Ience ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or SBN: 0- 73 84- 26 09 -1 successful imp lem ent at ions of EJBs. Pages: 75 0
I n Par t 2 , w e int rod uce t he sam ple b ank in g applicat ion an d t hen im plem en t ent it y beans, session b ean s, and m essage- dr iv en beans usin g Web Spher e St ud io Applicat ion Developer . We also im plement fin der m et h ods, dif fer en t m app in g st r at egies, and simp le client s t h at u se t he EJBs. At t he en d, w e describ e how t o deploy EJB app licat ions t o a WebSpher e App licat ion Ser v er. Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
The team that wrote this redbook This r edbook was pr oduced by a t eam of sp ecialist s fr om ar oun d t h e w or ld w or k in g at t h e I nt er nat ional Techn ical Su ppor t Or ganizat ion , San Jose Cent er. •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
U el i W a hl i is a Con su lt ant I T Specialist at t he I BM I nt er nat ional Technical Su ppor t Or ganizat ion in San Jose, Calif orn ia. Bef or e joinin g t he I TSO 1 8 year s ago, Ueli w or ked in t echn ical sup por t at I BM Sw it zer land. He wr it es ext ensiv ely an d t eaches I BM classes wor ldw ide on ap plicat ion dev elop ment , obj ect t echn ology , Visu alAg e® f or Jav a, WebSph er e App licat ion Serv er, and lat ely WebSph er e St u dio p rod uct s. I n h is I TSO car eer , Ueli has pr od uced ov er 25 Redbook s™. Ueli Lear n about t he EJB 2 .0 specif icat ion holds a degr ee in Mat hem at ics fr om t h e Sw iss Feder al I nst it ut e of Technolog y. Dev elop EJBs w it h St u dio App licat ion Dev eloper W out e r De na y er is an I T Archit ect w it h I BM Belgium . He cur r ent ly f ocu ses on app licat ion ar ch it ect u re wit h in t h e WebSph er e f am ily w h ile u sin g ex ist in g op en sour ce comp onent s an d t ools Deploy EJBs t o W eb Sp her e Applicat ion Ser v er w her ev er possible. Wout er st ar t ed w or k in g wit h Web t ech nology in 19 93 and has since acquir ed k nowIled of m anyprdovides iff er entd et plat for minsfor and p ion rod uct This allowWebSp him t here o bet tSt erudio This BMge Redbook ailed m at on s. h ow t o ex ef perience fect ively use un derst t h e client env on ment on applicat ionplicat in t egr p rojon ect ts. App licatan iond Developer f or irt he d ev elopm en t of ap ionatsion based h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. La rs Sch unk is a Con su lt ant I T Specialist in I BM Glob al Serv ices Ger m any . He h as t hr ee y ear s of ience in book d esign d ev m enples t of based I nt er net in t rle anet app ion solu Threxp ougerhout t he , wand e pr ov idelop e exam onan a dsimp bank inglicat app licat iont ions. w it h He an hold s a d eg r ee in bu sin ess inf or mat ics f rom t he Univ er sit y of App lied Sciences in Nor t h East Low er un derly ing r elat ional dat ab ase. Sax ony , Ger man y. His areas of ex pert ise includ e J2EE, EJB, Web Spher e, and Tom cat . I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic D e borat sh and Shatddon is itan T e. Archit ectt icu in tlar he wI BM Global ernct y Gr oupint b rased in Chicago, concep h e ar ch ectIur I n par e point outAMS t he nDeliv ew fu ions od uced w it h t he IEJB llin ois. Sh e is a coau t hor of t h e r ed book Develop in g Ser vlet s and JSPs w it h VisualAge for Jav 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag eda and WebSp here St udio , SG2 45 755. She h as over 1 5 y ear s of applicat ion developm ent an d per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or ar ch it ect u re exlem per ent ience, includ in g m ainf r ame, client / ser v er, and e- b usiness t echn ology successful imp at ions of EJBs. solut ions. Debor ah w or ks closely w it h I BM cu st om er s on applicat ion an d en t er pr ise ar ch it ect ur al solut ions, mu lt ip le ors clu ding ion r et ail, su ran est at e session I n Part 2, acr we oss in t r oduce t h eindu samstpr yle sect bank inginapplicat andb tank hening, impinlem ent ce, en triteal y beans, m anagem an d cr Herbeans cur r en t ar eas of ex per iseu dio incluAp deplicat d ist rion ibutDev ed elop com er ponent b ased beans, an dent m,essag e-edit d r iv.en using WebSph er e tSt . We -also ar it ect u res, pr immar J2 EE, v iceient chit ect u res, in tteg ion At imch plem ent fin der etily h ods, difser fer en t mor app ined g starr at egies, and J2EE sim p applicat le client sion t h an at d u se her at EJBs. fr amen ewd,ork agile d evtelopm en t EJB t echniq ues. ion She wit h merany I BM pr ion odu Serv ct s, in clud ing t he w es, dand escrib e how o deploy ap plicat s twoor a ks WebSph e App licat er. WebSph er e Ap plicat ion Serv er , Applicat ion Developer , WebSph er e MQ, and Lot u s® Dom ino™. Debor ah holds a degr ee in Bu sin ess I nf or mat ion Sy st em s f r om Br adley Univ ersit y , Peor ia, I llin ois, an d is cu rr ent ly p ur su ing a Mast er s in Sof t w ar e Eng ineer ing f rom DePau l Univ er sit y, Ch icago, I llinois. Ma r t in W ei ss w or k ed in I BM Sw it zer lan d for m or e t han 2 0 y ears, m ost recen t ly in t he f ield of WebSph er e ap plicat ion dev elop ment . I n 2 001 he est ablished his ow n com p any , Mar t in Weiss I nf orm at ik ( w w w .m w - inf or mat ik . ch) . Mart in has sou nd ex per ien ce in Ent erp r ise Jav aBeans t echnology an d has been wor k ing w it h t he WebSp here d ev elopm en t t ools since t h e early d ays of EJBs. He is a Cer t ified W eb Spher e Specialist an d p rov ides consult ing serv ices an d ed ucat ion in Sw it zerland . Mar t in is a co- aut hor of t he Redbook s Design and I m plem ent Serv let s, JSPs, an d EJBs, SG2 4- 5 754, and En t er p rise JavaBeans Dev elopm ent Using VisualAge for Jav a , SG2 4- 5 429.
Than ks t o t h e follow ing people f or t heir cont r ibut ion s t o t his pr oject : Dan iel Ber g, Chuck Br idgh am , Just in Hill, an d Kev in W illiam s of t he Ap plicat ion D ev elop er t eam in I BM Raleigh Ch ris Br ealey an d Tim ot h y Deboer of t he Applicat ion D ev elop er t eam in I BM Tor ont o •
Table of Content s
Kar ri Car lson , Er ic Er p en bach, Todd Joh nson, Alber t Lee, an d Mich ael Sch mit t of t he I ndex ereentt eam inebSphe I BM Roch est erAppl ica ti on De v el oper EJB 2 . 0WebSph D e ve lopm w it h W r e St udio •
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Become a published author Join us f or a t wo- t o six - w eek r esid en cy pr ogr am! Help w r it e an I BM Red book dealing w it h specif ic pr odu ct s or solu t ions, w hile get t ing h ands- on ex per ien ce wit h leading - ed ge t echnologies. You' ll t eam w it h I BM t echnical pr of essionals, Business Part n er s an d/ or cust om er s. •
Table of Content s
•Your ef for t s w I ndex ill help increase pr odu ct accept ance and cust om er sat isfact ion. As a b onus, y ou' ll EJB . 0 D eavenlopm e ntk wof it hcont W ebSphe r e St udiodevelopm Appl ica ti on De vlab el oper dev 2elop et wor act s in I BM ent s, and increase y our p rod uct ivit y an d m ar ket abilit y . By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Find out mor e ab out t he r esidency p rog ram , br ow se t he r esidency ind ex , an d ap ply online at : Pub lish er: I BM Dat e: April 1 1, 20 ib mPub .com / r edbook s/ 03 residencies. ht m l I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Comments welcome Your com m ent s ar e im por t ant t o u s! We w ant our Redbook s t o be as h elp ful as possib le. Sen d u s y our com ment s about t his or ot her •Redb ooks in Table of tContent s ing w ay s: one of he f ollow •
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Use t he online Cont a ct us r ev iew r edb ook for m f ound at :
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
ib m .com / r edbook s Pub lish er: I BM
Send s Pub Dat e: you Aprilr 1com 1, 20 ment 03
in an I nt er net n ot e t o:
I SBN: 0- 73 84- 26 09 -1
r ed book @us. ibm .com Pages: 75 0
Mail y our com m ent s t o:
I BM Cor por at ion, I n t er nat ion al Tech nical Sup por t Org anizat ion D ep t . QXXE Building 8 0- E2 Lear 6 50 n about Har ry tRoad he EJB 2 .0 specif icat ion San Jose, Califor nia 951 20- 6 099 Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Part 1: EJB architecture and concepts I n Par t 1 , w e int rod uce En t er pr ise Jav aBean s as par t of J2EE. We d escrib e t he EJB ar ch it ect u re and con cept s, and in t r oduce t h e diff erent t y pes of EJBs. •
Table of Content s
We d iscuss in det ail each t y pe of EJB an d p oint out w h at is n ew w it h t h e EJB 2. 0 I ndex specif icat ion . I n each ch apt er w e conclude w it h best p r act ice gu idelin es f or successfu l EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper im plem ent at ion of EJB- based app licat ions. •
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Chapter 1. Introduction to Enterprise JavaBeans •This chap t er Table of Content int r oduces En st er p rise Jav aBean s and show s h ow t hey ar e int egr at ed in t h e J2EE •plat f or m. I ndex EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Reader s w ho are alr eady f am iliar t o t he EJB st an dar d can skip t his chap t er. By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Server-side component architecture En t er p rise Jav aBean s ( EJB) is an ar ch it ect ur e f or ser v er- side com pon en t based dist rib ut ed app licat ions wr it t en in Jav a. •Since it s int r od Table Content uctof ion a fews y ears ago, Ent erp r ise Jav aBeans t echnology has gain ed m om en t um •am ong plat f orm I ndexpr ovid er s an d ent er pr ise developm ent t eam s. Th is is becau se t he EJB EJB . 0 D e ve lopm e nt w it hpWlifebSphe r eelopm St udio en Appl icabtiusiness on De v el oper com2ponent m odel sim ies d ev t of com pon en t s t hat ar e t r ansact ion al, scalab le, an d por t able. Ent er pr ise Jav aBean ser v ers redu ce t he com plex it y of d ev elop ing By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss bu sin ess com ponent s by pr ov iding aut om at ic sup por t f or sy st em - lev el serv ices, such as t r ansact ions, secur it y , and dat abase connect ivit y , t h us allow in g t he d ev elop er s t o concen t r at e on Pub lish er: I BM dev elop in g t he bu sin ess logic. Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 d -1escr ib es a serv er- side com pon en t - based ar chit ect ur e: The EJB specificat ion Pages: 75 0
The EJB ar chit ect u re is a com p onent ar chit ect u r e for t he dev elopm ent and deploym ent of com ponent - b ased dist ribut ed bu siness app licat ions The sp ecificat ion has been cr eat ed b y diff erent comp anies. They ar e m en t ion ed in t he fir st pag es of t he EJB specificat ion. n about he EJBion 2 .0 isspecif icat The gLear oal of t h is sptecificat t o d ef ineion a st an dar d, so t hat diff er ent vend or s ar e able t o im plem ent t hese st andar ds. Because t his st an dar d defines ever y essent ially det ail of t h e Dev elop EJBs w it h St u dio App licat ion Dev eloper ar ch it ect u re, an app licat ion w r it t en using t h e Ent er pr ise JavaBeans archit ect ur e is scalab le, t r ansact ional,EJBs and tm i- user secu re. Su ch app Deploy o ult W eb Sp her e Applicat ionanSer v erlicat ion may be w r it t en on ce, an d t hen deploy ed on any ser ver p lat for m t h at sup por t s t he Ent er pr ise JavaBeans sp ecificat ion. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Figur e 1-ion 1 gDeveloper ives an ov ferorv iew basicenEJB onmion ents: based on t h e Ent er pr ise Jav aBeans App licat t he for d evaelopm t ofen apvir plicat ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Figu r e 1 - 1 . EJB e nv ir on m en t Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
The EJB com ponent s ar e ru nnin g in side t he cont ain er of an EJB ser v er.
The con t ain er has t h e connect ion t o t he dat abase or t o ot her com pon en t s. An EJB client can access t h e EJBs f r om t he sam e Jav a Vir t ual Machine ( JVM) or f r om anot h er JVM ov er r em ot e in t er f aces. The EJB h om e com p onent is com par able t o a fact or y for t he EJB obj ect s. Th e EJB obj ect s r et r iev ed f r om t he h om e com p onent s can be also local or r emot e obj ect s. •
Table of Content s
Background • I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Ar ect uli,res h er ave u nder gone sev al m ajShaddon or evolut ion sh ift s. The sh if t fr om one- t ier , By ch Ueliit Wah Wout Denay er, Lar s Schu nk ,er Deborah , Mart in ary Weiss m ainfr am e- t y pe syst em s t o t w o- t ier client / serv er based sy st em s, add r essed t h e need t o sep arat e out applicat ion t ier f r om t he r esour ce t ier . D ur in g t he ear ly 90s, t r adit ion al ent erp r ise Pub lish er: I BM inf orm at ion syst em pr ov ider s began r espon ding t o cu st om er r equir em ent s by shift in g fr om t he Pub Dat e: April 1 1, 20 03 t w o- t ier, client / ser ver app licat ion m odel t o mor e f lex ible t h ree- t ier and m ult i- t ier applicat ion I SBN: 0- 73 09 odel -1 m odels. This n-84t ier26 m is t he cur r en t m odel w h er eby w e dist r ibut e our soft war e ov er a set of Pages: all 75 0of w hich com pr ise a par t - of t he t ot al applicat ion. m achines, The n ew m odels separ at ed business logic f rom sy st em ser v ices and t h e user int erf ace, placin g it in a m iddle t ier b et w een t he t w o. Th e ev olut ion of new m idd lew ar e ser v ices—t r ansact ion m onit or s, m essage- or ient ed m iddlew are, ob ject r equest b rok er s, and Web ap plicat ion ser v ers—gav e ad dit ional im pet us t o t h is new ar chit ect u re. I n add it ion , t he gr ow in g use of t he I nt er net in t rant he et sEJB for 2ent pr iseicat applicat Learan n dabout .0 er specif ion ions cont r ibu t ed t o a g reat er em phasis on light w eight , easy - t o- d ep loy client s t h at r un in Web b r owser s. Dev elop EJBs w it h St u dio App licat ion Dev eloper Mu lt i- t ier d esign simp lif ies developing , deploy in g, an d maint ain ing en t erp rise app licat ions. I t enables dev elop er st ot oWfocus on et hApplicat e specifion ics of Deploy EJBs eb Sp her Serp vrog er r amm ing t h eir b usiness log ic, r elyin g on v ariou s back - en d ser vices t o p rov ide t h e inf rast r uct ur e, and clien t - sid e app licat ions ( bot h st andThis pr ovides d ets) ailed foridmeat ionuser on hin ow o efion. fect ively use WebSp here St udio aloneI BM an dRedbook w it h in Web br ow ser t o prinov t he t ert act App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans On ce darevcheloped, b usin ic can deployed on ion ser sv er r opr iatereet oApp ex ist in ion g r equir ( EJB) it ect u re, an dess d eplog loym ent be of su ch ap plicat t os aapp WebSph licat Servem er. en t s of an or ganizat ion. Howev er , n o st and ar d com ponent ar chit ect u re ex ist ed f or t he m idd le t ier , Thr oug hout developer t he book ,s wt oe fpr ov idon e exam a simpt ole t bank ingicular app licat it hfor anm , and f orced ocus plum ples bin g based d et ailson specific he p art mixion of w p lat un derly ingsyst r elat ional ase. are ser vices. This limit ed dev eloper s t o deploy a sing le oper at ing em , anddat mab iddlew app licat ion on a w ide v ar iet y of p lat for m s, or t o readily scale applicat ions t o m eet ch anging I n sin Part 1,condit we inion t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic bu ess s. concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Why EJBs? Num erou s Web sit es ar e up an d r un ning using Jav a w it hou t an y EJB t echn ology . Dev eloper s hav e been using ser vlet / JSP m odels and man aging t r ansact ions t h em selv es using comm it and r ollb ack fun ct ion alit y t hat is built int o JDBC wit h out t he h elp of applicat ion ser ver s. •
Table of Content s
•But w hen doing I ndex so, t he ap plicat ion d ev elop er s ar e conf r ont ed wit h m any challenges. Som e of t he EJB 2 . 0im D epor ve lopm nt w it harWeebSphe r e Stgudio Applrica ti on De v el oper m ost t ante ones m anagin concur ency, persist ence and t r ansact ion s. As a r esult , t h e dev elop er s hav e t o eit her develop pr opr iet ary cod e or bu y su ppor t ing f r amew or ks. By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
These pr oblem s ar e solv ed b y using ent erp r ise b ean s. The use of ent er pr ise beans allow lish er: I BM devPub elop er s t o focus on t h e bu siness logic an d r elease t h em fr om coding inf rast r uct u re an d Pub Dat e: April 1 1, and 20 03 developer s become mor e p rod uct ive and ef ficien t . m iddlew are log ic, I SBN: 0- 73 84- 26 09 -1
As w it Pages: h m ost 75 0ot her t echnolog ies, ent er pr ise b ean s do not pr ovid e t he u niqu e solu t ion t o all pr oblem s. Using ent er pr ise b ean s has ad van t ages and disadv ant ag es. How ev er , t h e adv ant ag es out w eigh t he disad van t ages, esp ecially for m or e com plex applicat ions t h at r equir e a sophist icat ed r obu st and dist r ibut ed per sist ent mod el. On ce ag ain, n ot ever y app licat ion env ir onm ent m ay benefit fr om u sing ent er pr ise beans. To h elp y ou d ecide wh et her t his t echnolog y is appr opr iat e, t h is sect ion pr ovides som e r easons t o Lear n about t he EJB 2 .0 specif icat ion consider using it . Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Object distribution This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio When using En t er pr ise Jav aBean s, dist rib ut ed ob ject s ar e used f or bu ild ing an en t er pr ise- scale App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans syst em . I n sh ort , t his m eans t h at t h e par t s of y our p r ogr am can b e deploy ed t o as m any ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. dif fer en t ph ysical m achines and in as m any separ at e OS pr ocesses as appr opr iat e t o achiev e t h e per ance, scalabilit ilit yples goals of yon oura sy st em Thr for ougmhout t he book , wy ,e and pr ovav id ailab e exam based simp le. bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, wecomponent-based in t r oduce EJBs as a p artarchitecture of Jav a 2 En t er p rise Ed it ion Portable
( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed For m any f orw ar d- look ing cust om er s, t h e key issu e is t hat t hey h ave t o achieve in dependence per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or fr om plat for m , v en dor , and ap plicat ion - ser v er im plem en t at ion. Th e EJB arch it ect ur e, w hich is an successful imp lem ent at ions of EJBs. indu st ry - st and ard com ponent ar ch it ect u re, can help achiev e t hese goals. En t er p rise beans dev elop2, ed we for inWebSpher being d epapplicat loy ed on plicat and vice I n Part t r oduce t he ecan samusually p le bank ionn onandI BM t henapimp lemion entserv en ter it ys,beans, session vbeans, er sa. This w as demon st rat ed at t he Jun e 19 99 JavaOne conf er ence w her e t he sam e car dealer an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also app licatent ion fin w as ondif mfer ulten iple erin prgise ap plicat ion pserv ers vend ors. W hile in t h eAt im plem derdeploy m et h ed ods, t ment app st rbean at egies, and sim le client s t h at u se t he EJBs. shor t t er m it is of t en easier and fast er t o t ake ad van t age of f eat ur es t hat may pr ecede t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. st andar d izat ion , st andar d izat ion p rov ides t he best long- t er m ad van t age. Also, cust omer s m ust con sider t he incr easing av ailab ilit y of t ools and opt im ized im plement at ions of t he EJB st an dar d t hat y ou w ould not get w it h hom e- gr ow n man aged object f r amew or ks. Becau se most cu st omer s ar e not in t he m iddlew ar e business, t heir ef for t s can be mor e eff ect iv ely t ar get ed at act ivit ies t h at ar e m or e dir ect ly r elat ed t o t heir bu siness.
Object persistence Mak ing an object p er sist en t m eans t h at it s st at e ( t he v alues of it s v ariab les) can be p r eserv ed
across m u lt ip le in vocat ions of a pr og ram t hat ref er ences t hat object . I n m ost cases, t he st at e of a p er sist en t ob ject is st or ed in a r elat ional dat abase. Unfor t u nat ely, object s and r elat ion al dat ab ases d iff er a lot f r om each ot h er . Relat ional dat abases hav e lim it ed m odeling cap abilit ies, such as obj ect inher it ance and encapsulat ion, com par ed t o Jav a. Add it ion ally , SQL d at a t y pes d o not exact ly m at ch Java dat a t y pes, lead in g t o conv er sion pr oblem s. All t hese pr oblem s ar e solv ed w h en using CMP ent it y b ean s. •
Table of Content s
•
I ndex
Independence database schema EJB 2 . 0 D e ve lopm e nt w from it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
EJB t ech nology enab les a clear separ at ion of business log ic fr om d at abase access. The b usiness logic is ind ep en dent of t he d at abase sch em a and can b e deploy ed int o or ganizat ions wit h Pub lish er: I BM dif fer en t or chan ging dat ab ase schem as. Pub Dat e: April 1 1, 20 03
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Transaction management Con cu rr ent access t o shar ed dat a can be one of t he biggest h ead ach es t o a developer . The considerat ion of all t h e r elat ed issues as d at abase locking , concur r en cy , or ev en loss of dat a int egr it y can lead t o t he cr eat ion of hig hly comp lex schem es for m anag ing access t o shar ed dat a at t heLear d at nabase el. EJB 2 .0 specif icat ion aboutlev t he En t er p riseelop beans aut h andle h ese com plex t hr eading and simu lt aneous shar ed d at a Dev EJBs w om it h at Stically u dio App licattion Dev eloper issu es. As m ent ion ed pr eviou sly , t h e EJB con t ainer pr ov id es all t he n ecessar y t r ansact ion ser v ices t o ent erp rt ise ean forecon t rol access b ack - end dat a in a t r ansact ional m ann er . Deploy EJBs o Wbeb Spsher Applicat ion Sert ov er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion data Developer f or t he dwith ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans Multiple sources transactional capabilities ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Man y applicat ions r equir t he y t o ples access m u lton ip le a le sou rces. exlicat am ple, a itphr ogr Thr oug hout t he book , w ee pr ov abilit id e exam based a dat simp bank ingFor app ion w an am m ay use dat a in bot h a m idd let ier DB2 ® or Or acle d at abase and a mainf r am e CI CS® or I MS™ un derly ing r elat ional dat ab ase. syst em accessible t hr oug h Web Spher e MQ. The k ey is t hat som e ap plicat ion s r eq uir e t hat t his access al—tas hat in tJav egrait 2y is t ained he dand at a sour I n Part is 1,f ully we int rtansact r oduceion EJBs a pdat arta of Enm t erain p rise Ed itacr ionoss ( J2tEE) coverces. t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he For m id aye,dem t hat g a user orion deralit w ill consist of st g t h eed EJB exam 2. 0 spple, ecif an icatapp ion licat t hation pr ov for and ex am ple,placin en hanced fun ct y for cont ain erorin - m anag det ailed or der inf or mat ion in an Or acle d at abase and sim ult aneously placing a shipm ent or derf or per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines w it h a CI CS syst em t h rou gh WebSpher e MQ. I f eit her t h e dat ab ase upd at e or t h e MQ en queuing successful imp lem ent at ions of EJBs. fails, t h en t h e en t ire t ran sact ion should r oll back . I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session Ibeans, n t he past on lye- ch w it h wusing hich t WebSph o build syst likeAp t hplicat ese wion er eDev t r ansact m onit an d, mt he essag d r oices iv en beans er e em St usdio elop erion . We alsoor s such as Encina® , CI CS, or Tu xedo, w hich u sed nonst an dar d in t erf aces an d requ ir ed im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At dev su che lang as COBOL, C, or ion C+s+t .o a WebSph er e App licat ion Serv er. t he elop en d,ment w e d in escrib how uages t o deploy EJB ap plicat En t er p rise beans su ppor t mu lt ip le concur rent t r ansact ions w it h fu ll com m it an d rollback capabilit ies acr oss m ult iple D B2 d at a sour ces in a fu ll t w o- phase com m it - cap able env iron ment .
Middle-tier architecture Very oft en com pan ies con sider t h eir applicat ion sof t w are, p art icular ly t he business ru les an d dat a st r uct ur es t hat mak e u p t he bu sin ess logic, as cor por at e asset s. Th er efor e, t h ey ar e concer n ed w it h pr ot ect in g t h ese asset s fr om t h e pu blic I n t ern et .
En t er p rise beans en able a com pany t o u se a m iddle- t ier archit ect ur e so t hat p r esent at ion logic can be sep arat ed f rom business logic. This sep ar at ion m ak es possib le t he u se of a second fir ew all bet w een t hese t w o d iff er ent lay er s for h igher isolat ion of all ap plicat ion com ponent s t hat cont ain business logic.
Multiple client types accessing shared data •
Table of Content s
•Of t en, a sing Ile ndex applicat ion will h ave mu lt iple client t y pes t hat need access t o t he sam e set of EJB 2 . 0 at D eion. ve lopm it h ple, W ebSphe r e St udioion Appl ica ti on inf orm Fore ntexwam an applicat may havDeevael oper Web- b ased
HTML fr ont end for ex t ern al cust om er li, s, Wout anderaDenay m ore a ap plicat ion f ron end f or int er nal user s. Trad it ionally, t his By Ueli Wah er,com Lar s plet Schue nkJav , Deborah Shaddon , Mart in tWeiss pr oblem h as been solved by w r it ing t w o v er sions of t he sam e app licat ion t hat sh ar e t he sam e dat aPubsour ces ( d at abase t ables) . How ever , t his is n ot ef ficient eit her in pr ogr am ming t im e or lish er: I BM ut ilizing t he dat abase, if m ult iple dat ab ase lock s could be h eld at one t im e. Pub Dat e: April 1 1, 20 03
I SBN: 0- 73 84-t 26 -1 pr oblem is t o pu t com mon d at a and b usin ess log ic in a sin gle set of EJBs The EJB solut ion o 09 t his 75 0 t hat arPages: e accessed by d iff er ent client t yp es ( for ex am ple, HTML/ serv let an d ap plicat ion ) . EJBs cont r ol access t o t he b ack- end dat a and int er nally m anage t he cur r en t t r an sact ions an d dat ab ase lock ing. This r ed uces t he t ot al pr ogr am m in g ef for t by r em ov ing du plicat e cod e in t he app licat ion and by r educing t h e am ount of ef for t sp en t in w r it in g dat abase cont r ol log ic.
Lear n aboutread t he EJB 2 .0update specif icataccess ion Concurrent and to shared data Dev elop EJBs w it h St u dio App licat ion Dev eloper Tr adit ionally , fat client solu t ions r equir e t h e applicat ion t o m anag e access t o shar ed dat a at t h e dat abDeploy ase lev el. This enSp r esu in h ighly EJBs t o oft W eb herltesApplicat ioncom Serplex v er schem es t o d eal w it h d at abase locking an d concur r en cy , or alt er nat iv ely , w it h t he loss of d at a int eg rit y w h en t h ese issu es are n ot This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio considered. App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans En t er par rise aut ically andle t h ese com plex and simu lt aneous shar ed-er. dat a ( EJB) ch itbeans ect u re, anom d datep loymhent of su ch ap plicat iont hr s teading o a WebSph er e App licat ion Serv issu es. As m ent ion ed pr eviou sly , ent er pr ise b ean s cont r ol access t o back - en d d at a an d man age tThr he oug cu rrhout ent ttrhe ansact bookion , ws eand pr ov dat id e abexam ase lock plesing based int eron nally. a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Method-level object security concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Cer t ainence t yp es ofit yapbplicat e secu dr rit iv y en r estbeans. rict ionsWe t hat hav pr ev ade em diff icu lt per sist ent ean s ion ands hav m essagealso preovid e iously best prmact icet hguidelines f or tsuccessful o im p lem ent in Jav a. For exam p le, cer t ain insur an ce ap plicat ion s m ust r est rict access t o p at ien t imp lem ent at ions of EJBs. dat a in ord er t o m eet r eg ulat or y gu id elines. Unt il t h e adv ent of ent er pr ise beans, t h er e w as n o w t o rest r ictinaccess ob ject m eting hodapplicat by a par user imp . Pr ev iously, ict in g access I nayPart 2, we t r oducet ot han e sam p le or bank iont icular and t hen lem ent enrest t it y r beans, session at t he dan at d abase lev el, cat using ch ing WebSph er r or s t hrerow n uatdio t h eApJDBC or elop b y rest r ict inalso g access beans, m essag e- dand r iv ent hen beans e St plicatlevel, ion Dev er . We at t he ap plicat ion m level b y cudif stfer omensecur it y in code ld h ave on ly simp lemu ent im plem ent fin der et h ods, t m app g st rwatou egies, andb een sim pt he le client t h at se tat heion EJBs. At opt t he ion en s. d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. How ev er , ent erp r ise b ean s now allow m et hod - lev el secur it y on any en t er pr ise bean or m et hod. User s and user gr oup s can b e creat ed t hat can b e gr ant ed or denied ex ecu t ion r igh t s t o any EJB or m et hod. I n WebSp here, t hese sam e user gr oup s can be gr ant ed or d en ied access t o Web r esour ces ( ser v let s, JSPs, an d HTML pages) , and t he user I D s can b e in a seam less way passed fr om t he Web resour ces t o t h e EJBs b y t he u nder lying secu rit y fr am ew or k .
Multiple servers to handle throughput and availability Ov er t he p ast sev er al y ear s, cust om er s h ave foun d t hat f at client sy st em s sim ply do not scale t o
t he t housand s or m illion s of users t hat Web - based sy st em s m ay hav e. At t he sam e t im e, soft w ar e dist r ibut ion pr ob lem s hav e led t o a desir e t o t r im dow n fat client s. Th e 24- h our , sevenday - a- w eek nat ur e of t h e Web h as also m ade u pt im e a cr ucial issu e for b usinesses. How ev er, not ever yon e needs a sy st em desig ned f or 24 x7 op er at ion or t hat is able t o han dle m illions of concur r en t u ser s. Developer s sh ould be ab le t o design a sy st em so t hat scalabilit y can be achieved w it hout sacr ificing ease of d ev elopm en t , or st and ar dizat ion. What cust om er s n eed is a w ay t o w r it e business logic t h at can scale t o meet t hese k ind s of • Table of Content s r eq uir em ent s. WebSp here' s EJB su ppor t can p rov ide t h is kin d of h ighly scalab le, highly av ailable • I ndex syst em . I t d oes t his by ut iliz in g t he f ollow ing f eat u res: EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Ob ject cach ing and pooling : W eb Spher e Applicat ion Ser v er aut om at ically p ools en t er p rise beans at t h e ser v er lev el, r edu cing t h e am ount of t im e sp en t in obj ect creat ion and gar bag e Pub lish er: I BM collect ion. Th is result s in m or e pr ocessin g cy cles b ein g av ailable t o do r eal w or k . Pub Dat e: April 1 1, 20 03
I SBN: 0- 73 8409izat -1 ion at ser ver : W eb Spher e Applicat ion Ser v er Net w or k Deploy m en t Wor k load op26 t im Pages: 75 0EJB ser v er clu st er m an agement . Using Net w ork Deploym ent you can cr eat e serv er feat u res gr oup s t hat span nodes. I n addit ion, you can con figu re and r un mu lt iple inst ances ( called clones) of a WebSp here ser ver on one machin e, t akin g ad van t age of m ult ipr ocessor ar ch it ect u res. You can ad min ist er a set of nodes r un ning WebSph er e serv ers using a single adm inist r at ion f acilit y . A set u p u sin g m ult ip le nodes im pr ov es av ailabilit y and pr event s a single p oin t of f ailu re in t h e app licat ion ser v er . Lear n about t he EJB 2 .0 specif icat ion Clonin g sup por t s aut omat ic failover : W it h sever al clones av ailab le t o h andle r equest s, it is m or elop e lik ely t hat ill not dam h r oughp ut and r eliabilit y . Wit h clon es dist ribu t ed Dev EJBs w itfailur h St ues diow App licat ionage Devteloper t o var ious n odes, an en t ir e m achine can f ail w it hout pr odu cing d ev ast at ing conseq uences. Deploy All of t hEJBs ese f eat to W u res eb Sp h appen her e Applicat w it hoution specif Ser ically v er being pr ogr am m ed int o t he sy st em . No chang es t o t h e ser v er - side code ar e n ecessar y t o t ake adv ant age of t his k in d of scalabilit y. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Appelicat Not t h at ionWebSp Developer here Applicat f or t he dion ev elopm Ser verenNet t ofwap or kplicat Deploy ion sment based supp on ort t h es Ent dister r ibprutise ion, Jav clonin aBeans g, an d ( EJB) aut omar atch ic it f ailover ect u re, of anot d hder epser loym verent - sidofe su Jav cha ap t echnologies, plicat ion s t osuch a WebSph as Java er e serApp v letlicat s and ion JSPs. Serv er. How ev er , t hese ar e m ore pr esent at ion- or ient ed t ech nologies and ser v e as a com plem en t t o EJBs ougthout t hea book w eit or pr ov e exam plesupt based simp le bank app licat ion w it hbe ana par t rThr at her h an as com ,pet t o id EJBs. When im e on an da scalabilit y ar ing e key , EJBs sh ould un tderly ing all r elat ional of he over solu t ion.dat ab ase.
I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he Integration with EJB 2. 0 sp ecif icat ion t CORBA hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or En t er p rise imp beans b uilt t hat is a com binat ion of Jav a Rem ot e Met h od successful lemare ent at ionsonoft echnology EJBs. I nv ocat ion ( RMI ) and Comp onent Obj ect Request Br ok er Ar chit ect ur e ( CORBA) . Client s access an Ient n Part 2, we in t rusing oduceRMI t h e ov sam bank ingallows applicat and t hen imp ent en ten it yt erp beans, session er pr ise b ean er pI IleOP. This purion e CORBA clien t s lem t o access rise beans beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also as EJB client s. im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Development roles Dev elopin g an d ru nning com ponent s w it h a w ell- def in ed m app ing bet w een t asks and roles allow s t he r igh t p er son t o do t he r igh t j ob. Th e v arious t asks w it h in a EJB developm ent an d pr odu ct ion p rocess are assigned t o diff er ent roles, w hich are p ar t of t h e sp ecificat ion. The EJB ar ch it ect u re t heref ore d ef ines six roles. From a t echnical p oin t of v iew , each r ole may be per for m ed b y a d if fer en t p er son. To ensur e t his, t h e r oles ar e d escrib ed in t he specificat ion. I n a r eal- lif e scenar io, m or e t han one r ole m ay be perf or med by on e per son . I n our book you ar e t he bean p rov ider , because y ou ar e ab out t o d ev elop b ean s w it h WebSp her e St ud io App licat ion Developer , and I BM ar e a ser v er vend or per for m ing t he r oles of t he cont ain er
and serv er pr ov id er . While t est ing and deploy ing you r beans sev er al t im es t o t he t est ser ver s, you ar e per for m ing m or e t han one r ole, bu t sever al, as f ollow s: En t er p rise bean pr ov id er • •
of Content App licatTable ion assem blers I ndex
deploy EJB 2 . 0EJB D e ve lopm e er nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
EJB ser v er pr ovid er
PubEJB lish er: I BMainer cont
pr ov ider
Pub Dat e: April 1 1, 20 03
Sy st em0-s73adm I SBN: 84- 26inist 09 -1r at or Pages: 75 0
For a d et ailed descr ipt ion of t he r oles see " EJB r oles" on p age 34 .
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Java 2 Platform, Enterprise Edition (J2EE) The Java 2 Plat f or m , Ent er pr ise Edit ion ( J2 EE) is a r obu st suit e of m iddlew ar e applicat ion ser v ices f or ser v er- side ap plicat ion d ev elop m en t . J2EE is an ex t en sion of t h e Java 2 Plat for m , St and ard Edit ion ( J2SE) . •
Table of Content s
•J2EE mak es all I ndex Jav a ent er pr ise API s and fun ct ion alit y available an d accessible in a w ellEJB 2 . 0at D ed e ve lopm e nt wThis it h W helps ebSphetroe sim St udio ica ti on oper s in t he developm ent , d ep loy m ent , int egr fashion. p lifAppl y comp lexDepvrel oblem and m anag em ent of mu lt it ier , serv er cent r ic ent er pr ise solu t ion s. Let 's look at t h e t ech nologies By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss included w it h J2EE. Pub lish er: I BM Pub Dat e: April 1 1, 20 03
J2EEI SBN: services summary 0- 73 84- 26 09 -1 Pages: 75 0
Table 1 - 1 sh ows t he t echnolog ies p rov ided by t h e J2EE specificat ion.
Tab le 1 -1 . Techn ologi es of J2 EE specif ica t i on
Lear n about t he EJB 2 .0 specif icat ion
Dev elop EJBs w it h St u dio App licat ion Dev eloper
J2 EE spe ci fi ca t i on
ions, Ser v er Web Deploy EJBs t o W eb Sp her e Applicat Serv let JSPs EJB I BM Redbook pr ovides d et ailed En t er m p rise com it ect ur euse WebSp here St udio This in for at ion onponent h ow t oarefch fect ively App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans Dat ab ase JDBC ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Nam ing and dir ect or y JNDI Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Messag in g r elat ional dat ab ase. JMS un derly ing e- m ail Jav a Mail, JAF I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h ect e ars ch it ect ur e. I n par t icu e point he n ew fu nct ions int r od uced w it h t he Dist r ibut ed obj Jav aIlar DL,wRMI , RMIout - I I tOP EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Tr ansact ionent s it y b ean s and m essageJTA,drJTS per sist ence iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
Java and t JavaServer (JSP) I n Part servlets 2, we in t r oduce h e sam p le bank Pages ing applicat ion and
t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent finbe dert hough m et h ods, dif fer m app st r satin egies, and le client s tshpr at ovide u se t he A ser vlet can t of as an en apt plet t h in atgr un t he ser v ersim sidp e. Ser v let a EJBs. At tcom he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. ponent - b ased, plat for m - ind ep en dent m et hod f or bu ild in g Web - based ap plicat ion s w it hout t he lim it at ions of CGI p rog ram s. JSP t echn ology is an ext ension of ser vlet t echn ology cr eat ed t o sup por t au t hor ing of HTML and XML pag es. I t en ables com b in ing fix ed t em plat e d at a w it h dy nam ic cont ent dat a and is a g ood alt ern at ive t o serv let s. Serv let s and JSPs ar e suit ed for sim ple r equest / r espon se W eb m odels. We r ecom m en d t hat app licat ions developed wit h EJB follow t he m odel- v iew - cont r oller ( MVC) design p at t ern . This pat t er n defines an ar chit ect u ral t hr ee- w ay sp lit for an ap plicat ion , sep arat in g d at a element s ( t he m odel) fr om t h e dat a pr esent at ion ( t he v iew ) and fr om t h e m anipulat ion of t he dat a in t o t h e pr esent at ion ( t he cont r oller) .
The J2EE r elies heav ily on JSPs as t he v iew par t of t he MVC pat t er n t o b r id ge t h e gap bet w een EJB and HTML code, placing JSPs at t he k ey boun dar y bet w een dat a and pr esen t at ion. Sim ilarly , ser v let s are used as t h e cont r oller an d EJBs as t h e m odel par t of t he MVC p at t er n , r espect ively .
Enterprise JavaBeans (EJB) •
of Content s EJB defines hTable ow ser v er- side com pon en t s ar e w rit t en and pr ov ides a st an dar d ar chit ect ur al •cont r act b et wI ndex een t he com ponent s an d t he ap plicat ion ser v ers an d con t ainers t hat man age t h em . EJB . 0 D especificat ve lopm e nt ion w it hp W ebSphe r e solu St udio Appl De vsep el oper The2EJB rov ides a t ion f oricaati on clear arat ion of t h e business logic and t h e int Ueli By r icacies Wah li, of Wout dealing er Denayw er, it hLarpser Schu sistnken , Deborah cy , t r ansact Shaddon ions, , Martan in Weiss d ot h er mid dlew ar e- r elat ed ser v ices. Pub lish er: I BM Pub Dat e: April 1 1, 20 03 JDBC I SBN: 0- 73 84- 26 09 -1 75 0 unif or m access t o a w ide r ang e of relat ional dat abases. JDBC en ables Jav a JDBC Pages: pr ov ides pr ogr am m er s t o r ep r esent dat abase conn ect ions, SQL st at em en t s, and r et r ievin g r esult s in a por t ab le way . JD BC 2 . 0 h as built - in supp ort f or dat ab ase conn ect ion pooling .
Java remote method invocation and RMI-IIOP Lear n about t he EJB 2 .0 specif icat ion
RMI isDev am ech EJBs anismw for in vok m licat et hods ely on ot her m ach in es. EJB r elies on Jav a RMI elop it h St u dioing App ion rem Devot eloper as a com m un icat ions API bet w een com ponent s and t heir client s. Su n Micr osyst em s w it h I BM and ot her sDeploy has recen eloped or e porion t abSer le ver EJBst lyt odev W eb Sp herae m Applicat v ersion of RMI , wh ich uses t he Obj ect Man agement Gr oup 's ( OMG) I n t er n et I nt er- ORB pr ot ocol. I I OP is necessary for J2 EE deploy m en t s t o be pr intovides er operdable w it hinCORBA sy ston ems. This I BM Redbook et ailed for m at ion h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Java Naming and Directory Interface (JNDI) Thr oug hout t he book , w e pr ov id e exam ples based on a simp le
bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. JNDI is a st andar d f or nam ing and dir ect or y serv ices. I n EJB- b ased app licat ions w h en t h e client code r equest t o EJBs an EJB ponent , JNDI u sed t oEd locat e an r et rand iev ecover t h e com onent t o I n Part 1, wesinaccess t r oduce as com a p art of Jav a 2 Enist er p rise it ion ( J2dEE) t hepbasic ser v ice t he clien t . JNDI enab les w r it ing por t ab le direct or y and nam ing serv ice cod e t hat w concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it hortks he w it h 2. mu serv such LD AP CosNamfun ing. EJB 0 lt spiple ecifdir icatect ionortyhat prices, ov id e, for as ex am ple,and en hanced ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
Java Messaging Service (JMS)
I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also By com ent bining Jav amtet echnolog y fer w iten h tent er prinise mressaging t he sim JMS API p rov new, erf ul im plem fin der h ods, dif m app g st at egies, ,and p le client s ides t h at au se t hepow EJBs. At tt ool f or solvin g ent er pr ise com p ut ing pr oblem s. The JMS API im pr oves p rog r amm er pr odu ct iv it y he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. by d ef ining a com m on set of messaging concep t s and pr ogr am m ing st rat egies t h at w ill be supp ort ed by all JMS t echn olog y- com pliant m essaging sy st em s.
Java Mail The Java Mail API p rov ides a plat for m - an d pr ot ocol- in dependent fr am ewor k t o bu ild Jav a- based m ail and m essaging ap plicat ion s. I t basically allow t he app licat ions t o hav e e- m ail capabilit ies. Jav a Mail depends on Jav a Act iv at ion Fr am ew or k t o encapsulat e m essage d at a an d t o han dle int er act ion s.
Java Activation Framework (JAF) JAF enab les d ev elop er s t o det er min e t he t y pe of an ar bit r ar y piece of dat a, t o encapsulat e access t o t he d at a, t o discov er t he f unct ional oper at ion s av ailab le on it , and t o in st an t iat e t he app rop riat e bean t o per for m t h e oper at ion( s) . I t also enab les you t o dy nam ically r egist er t yp es of ar bit r ar y dat a and act ion s associat ed wit h par t icular k inds of dat a. Ad dit ionally, it enables a namofically prsovide or r et r ieve JavaBeans t hat im plem en t act ions associat ed w it h •pr ogr am t o dy Table Content d at a. •som e kin d ofI ndex EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Java Transaction API (JTA) and Java Transaction Service (JTS) Pub lish er: I BM
JTAPub is aDat hig lev1 1, el 20 t r 03 ansact ion API t hat allow s applicat ions and J2 EE ser v er s t o m anage e: hApril t r ansact ions. JTS specifies t he im plem en t at ion of a t r ansact ion man ager, w hich sup por t s JTA and I SBN: 0- 73 84- 26 09 -1 im plem ent s t he Jav a m ap ping of t he OMG Obj ect Tran sact ion Serv ice ( OTS) 1. 1 specificat ion at Pages: 75 0 t he lev el below t he API .
Java IDL about heit ion EJBLangu 2 .0 specif ionis an im plement at ion of t h e CORBA specif icat ion and Jav a ILear nt ernface Deftin age (icat I DL) enables int er oper abilit y and con nect iv it y w it h het erog en eou s obj ect s. I t is basically an obj ect Devbrelop h St App1.licat ione Dev r eq uest ok erEJBs pr ovwidited w uit dio h JDK 2. Th Jav aeloper I DL enables dist r ib ut ed Web ap plicat ion s t o t r anspar ent ly inv ok e oper at ions on r em ot e n et w or k serv ices usin g t he ind ust r y st andar ds I DL EJBs t o W eb Sp her e Applicat ion Ser v er and I Deploy I OP f r om OMG. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans J2EE ( EJB) ararchitecture ch it ect u re, an d d overview ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout book e all pr ov id e of exam basedJ2EE on at echn simpolog le bank Figur e 12 sht he ows an ,ovwer view t h eples diff erent ies. ing app licat ion w it h an un derly ing r elat ional dat ab ase.
e a1p- art 2 . of J2Jav EEa a2 rEn cht er itperise ct ur di ag amand cover t he basic I n Part 1, we in t r oduceFigu EJBs ras Edeit ion ( J2rEE) concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
Web containers and EJB containers
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Con t ainers are b asically r unt im e env iron m en t s t hat pr ov ide com ponent s wit h specif ic ser v ices. This I BM Redbook pr ovides d et ailed for m at ionainer on hsow t o idefefect ively For exam ple, as show n in Figur e 1- 2 in , Web cont pr ov r unt im euse supWebSp por t t o here clientSt s udio by App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans pr ocessing r eq uest s t hr oug h inv ok ing JSPs and serv let s and r et ur ning r esult s fr om t he (com EJB)ponent ar ch itsect an d d.ep loym of su ch ap t o au a WebSph e App Serv er. t outre, h e client Sim ilarent ly EJB cont ainplicat er s pion rovside t om at edersup por licat t f orion t r ansact ion and st at e m anag em ent of EJB com pon en t s, as w ell as looku p and secur it y ser v ices. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase.
IBM andEJBs J2EE I n Partproducts 1, we in t r oduce as a p art
of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2.10 - sp ecif icattion t hatpor pr tovinidWebSph e, for exer am en hanced funvctarion y for tcont er -ies. m anag ed Table 2 sh ows he sup e ple, Ver sion 5 for t he ioualit s J2EE echnain olog per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an dlem1essag d r iv en beans using er e St ues dio by Ap plicat ion Dever elop We also Tab -2 . eSuppor t of J2 EEWebSph t e ch nologi W ebSph e er pr. odu ct s im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Te ch nolog y
• •
V e rsion
Com m e nt s
JDK
1. 3
Fully supp ort ed
JDBC
2. 0
WebSph er e ad ds a uniqu e pr epar ed st at em en t s cach e im plem ent at ion .
JMS
1. 0. 1
WebSph er e MQ sup por t s nat iv e JMS and can b e used b y WebSph er e.
Table of Content s
JNDI
1. 2
I ndex
WebSph er e 3 .5 sup por t s m ost of t h e JNDI 1. 2 . I t u ses CosNaming as a ser vice pr ov ider.
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
EJB 0 er, LarWebSph e supShaddon por t s EJB plus: By Ueli Wah li, Wout er2. Denay s Schu nk , er Deborah , Mart2. in0Weiss Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
Access beans:
Sim plify clien t ap plicat ion using EJBs
I nher it ance:
Sup por t p oly m or phism and r euse
Pages: 75 0
Serv let
2. 3
Fully supp ort ed
JSP
1. 2
I t also h as high - qualit y t ool supp ort t hr oug h Web Spher e St ud io.
JTA and JTS
1. 0
Sup por t ed w it h ( t w o- ph ase com m it ) d ist r ibu t ed t r ansact ions
RMI - I I OP 1. 0 Fully supp ort ed Lear n about t he EJB 2 .0 specif icat ion JAF 1. 1 Fully supp ort ed Dev elop EJBs w it h St u dio App licat ion Dev eloper Jav a Mail 1. 1 Not su ppor t ed Deploy EJBs t o W eb Sp her e Applicat ion Ser v er XML Fully supp ort ed DOM/ SAX This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or tFully he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans HTTP 1. 1 supp ort ed ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
EJB specifications The Ent er pr ise JavaBeans ( EJB) specificat ion is a fou ndat ion f or t he Java 2 Plat for m , Ent er pr ise Ed it ion ( J2 EE) defin ed by Su n. Vend ors use t his specificat ion t o im plem ent an infr ast r uct ur e in w hich com pon en t s can be deploy ed, and u se a set of ser v ices such as dist r ibut ed t r ansact ion s, •secur it y , or lif Table s e- cyof cleContent m an agement . As a d ev eloper, you j ust r euse t he ser vices det ailed in t he •specif icat ion .I ndex For ex am ple, y ou do n ot n eed t o in clud e any code in y our com pon en t s t o mak e EJB 2 . 0t ran D e ve lopm e nt w it h is W ebSphe r e St udio Appl ica on De t hem sact ional. Th let s you concent r at e tion t hev elboper usiness log ic of t he ap plicat ion . By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
En t er p rise beans are d esign ed t o be p ort ab le fr om on e vend or' s ex ecut ion env iron ment t o anot h er , in dependent of t he choices m ade b y t he v endor t o im plement t he ser vices descr ibed in Pub lish er: I BM t he sp ecificat ion. Th e qu alit y of ser vice r equir ed b y an en t er pr ise bean is descr ibed out side of t he Dat e: April 1 1, 20 03 comPub ponent in a deploy m en t descript or. The deploy m en t d escr ip t or is analy zed at deploy m en t I 0- 73p8426ided 09 -1 by t h e vend or . This f eat u r e pr ovid es a gr eat lev el of flexibilit y f or r eu sin g t im e bySBN: a t ool rov Pages: 75 0 . For ex am ple, if you w ish t o chang e t he t ran sact ional behav ior of an en t er p rise y our com ponent bean, you hav e t o chang e only t he t ran sact ion at t r ibut e st ored in t he deploy m en t d escr ip t or , not t he EJB b usiness log ic. Chang es are t aken int o accoun t w h en y ou r e- deploy t h e ent erp r ise b ean in t he cont ain er .
Lear n about t he EJB 2 .0 specif icat ion Enterprise JavaBeans architecture Dev elop EJBs w it h St u dio App licat ion Dev eloper The Ent er pr ise JavaBeans specificat ion d ef ines t he archit ect ur e show n in Figur e 1- 2 on p age 15 . Deploy EJBs t o W eb Sp her e Applicat ion Ser v er I n t his sect ion, w e descr ibe t h e feat ur es and lim it at ions of t h e diff erent EJB ver sion s t hat hav e been by Sun and t hd eet Jav a com nition y . on h ow t o ef fect ively use WebSp here St udio This I sp BMecified Redbook pr ovides ailed in formu m at App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
EJB 1.0
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. Here are t he f eat u res an d lim it at ions of EJB 1. 0 . I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Features per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful lem ent at ions EJBs. EJB 1. 0 int imp r oduced t he f ollowofing f eat ur es: I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, essag e- dinst r iv en beans WebSph er e aged St u dio plicat elop erer . We also Enan t erdpm rise bean ances are using cr eat ed an d man at Ap r unt im eion b y Dev a cont ain . im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he enAn d, en w et erp d escrib rise bean e howcan t o deploy b e custEJB om ized ap plicat at deploym ion s t o ent a WebSph t im e byeredit e App inglicat it s ion env Serv ir onm er. en t pr oper t ies. Client access is mediat ed by t h e cont ainer an d t h e EJB ser v er on w h ich t he ent er pr ise bean is deploy ed. Flex ible com ponent m odel. Sup por t f or com ponent dist r ib ut ions an d secur it y. St at eless an d st at ef ul session beans ar e def in ed an d m ust be sup por t ed. Con t ainer- m an aged p er sist ence an d b ean - m anaged per sist ence ent it y beans are defined
and ar e not m andat ory in t h is ver sion . For t r ansact ion sup por t , eit her t he orig in al j avax . jt s p ackag e or t he new javax.transaction p ackag e can b e used .
Limitations •
Table of Content s
Alt e of t h e feat u res ar e at t r act ive, t here ar e som e lim it at ion s as f ollow s: • hou gh somI ndex EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
En t it y bean su ppor t is not man dat or y .
PubJav lish a er:RMI I BM- I I OP
suppor t is not m andat or y , w hich defies int er operab ilit y w it h ot h er
het ere:ogeneous com ponent s. Pub Dat April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
The d ep loym ent descr ipt or is n ot av ailable in t ext for m at . Pages: 75 0
Requ ir es a separ at e d ep loy m ent file for each en t erp rise bean, cau sin g lar ge applicat ions com posed of m any b ean s t o slow d ow n. I nt er operab ilit y bet w een con t ainers is not defined. No dar d tcont ain er . Learstnan about he EJB 2 .0API specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
EJBDeploy 1.1 EJBs t o W eb Sp her e Applicat ion Ser v er EJB 1 atRedbook t emp t s t oprpr ovid edaethailed ig h degr ap plicat ion tcom pat ibilit f or ent erp rhere ise b ean s t hat This 1. I BM ovides in foree m of at ion on h ow o ef fect ivelyy use WebSp St udio w erelicat w r ion it t enDeveloper for t he EJB 0 spd ecif icat ion. App f or1.t he ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Differences over 1.0 un derly ing r elat ional dat ab ase. EJB 1. 1 1, addr m anyEJBs of t he at ion and f ound in EJB 1. 0.and Th ecover m ost tsignificant I n Part weesses in t r oduce as limit a p art of sJav a 2looph En t eroles p rise Ed it ion ( J2 EE) he basic chang es in clu de: concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or En t it y bean su ppor t , bot h con t ainer - and b ean - man aged persist en ce, is r equir ed. successful imp lem ent at ions of EJBs. Jav a RMI - I I OP ar gum ent and ref er ence t y pes m ust b e supp ort ed. That is, t he client API I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session m ust sup por t t he Jav a RMI - I I OP pr ogr am m in g mod el for p or t abilit y, but t he u nder lying beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also com mu nicat ion p rot ocol can be any t hing. im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he enThe d, javax.ejb.deployment w e d escrib e how t o deploy EJB ap plicat t oopped a WebSph ionbServ p ackag e has b ion eens dr in faverore App of anlicat XMLaseder. deploy m en t d escrip t or. Declar at iv e secur it y au t hor izat ion ( access con t r ol) is now mor e r ole dr iv en . I solat ion levels ar e now man aged ex plicit ly t hr ough JD BC ( BMP) , t he d at abase or ot h er v en dor - specif ic mechanism s. The b ean - cont ainer cont r act h as been enh anced t o include a defau lt JNDI con t ex t for accessin g pr opert ies an d r esou r ces, for ex am ple, JDBC and JMS. The b asic EJB r oles hav e been ex pan ded an d r edefin ed t o bet t er separ at e responsibilit ies
inv olved in t he dev elop ment , deploy ment , and host ing of ent er pr ise b ean s. Allow s using java.lang.String as a pr im ar y key t y pe. Allow s a session bean in st an ce t o b e r em ov ed up on a t im eout w h ile t he inst ance is in t he passivat ed st at e. Allow s en t erp rise beans t o r ead sy st em pr opert ies. •
Table of Content s
•The EJB 1 .0 ent I ndex er pr ise bean code has t o be chan ged or recom piled t o ru n in an EJB 1. 1 EJB . 0 D e ve lopm e ntf ollow w it h W ebSphe r e ion St udio cont2ainer , in t he ing sit uat s: Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
An en t erp rise bean t hat uses t he javax.jts.UserTransaction in t er f ace need s t o b e u se t he new javax.transaction.UserTransaction.
Pubmlish er: I BM t o odified
Pub Dat e: April 1 1, 20 03
An en t0erp rise26bean I SBN: 73 8409 -1 w r it t en t o t he EJB 1. 0 specif icat ion has t o b e m odified t o use t h e getCallerPrincipal() an d isCallerInRole(String roleName) m et hod s inst ead of t h e Pages: 75 0 depr ecat ed getCallerIdentity() an d isCallerInRole(Identity) m et hod s t o wor k in all EJB 1. 1 con t ainer s. An en t erp rise bean w it h con t ainer- m an aged p er sist ence w r it t en t o t h e EJB 1. 0 sp ecificat ion has t o be recom piled t o w ork w it h all EJB 1. 1 com pliant cont ain er s, b ecause t h e requ ired r et ur n v alue of ejbCreate(...) in EJB 1. 1 is dif ferent f rom it s valu e in EJB 1 .0 . Lear n about t he EJB 2 .0 specif icat ion An en t it y bean in EJB 1 .0 , w hose f in ders do n ot d ef ine t h e FinderException in t h e Dev w sit hclauses, St u dio mu Appstlicat Dev eloper m et elop hods'EJBs t hr ow be ion chan ged. EJB 1 .1 r equir es t hat all fin ders define t h e FinderException. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er I n EJB 1 . 1, an ent it y bean m ust n ot u se t he UserTransaction in t er f ace. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat f or tinheEJB d ev1. elopm entthe of UserTransaction ap plicat ion s basedinon e Ent er pr iselem Javent aBeans Theion entDeveloper er pr ise bean 0 uses t ert h f ace and imp s the ( EJB) SessionSynchronization ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.. in t er f ace at t h e sam e t im e, w hich is not allow ed in EJB 1 .1 Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase.
Limitations
I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic t hye add ar chedit ect urue. I nov par icu lar w eEJB point n ewt hfuenct ionsing int rlim odituced w it h t he Iconcep n sp it et sofand man f eat res er t EJB 1. 0, 1 .1out st illt he f aces follow at ions: EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or Dat a imp m odelin g capab is ver y sim p le. successful lem ent at ionsilitofy EJBs. Map he CMP b ean o tbank he dat abapplicat ase schem is ou t sid imp e oflem t heent sp ecificat ion. I n Part 2, ping we inoft r toduce the samsptle ing ion aand t hen en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Relat in herdif it an not insug ppor im plem entionships fin der man et hd ods, ferce en are t m app st r att ed. egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Specificat ion of f inder m et hods is not in t h e specif icat ion.
EJB 2.0 EJB 2. 0 is t he lat est v ersion of t he specificat ion. Th e m ost im por t ant ch anges in t he specificat ion ar e t hose m ade t o cont ainer - m anaged per sist ence ( CMP) and t h e int r oduct ion of a com plet ely new b ean t y pe, t h e m essage- dr iven bean.
New features supported in EJB 2.0
EJB 2. 0 pr ov id es t hese en hancement s: I nt egr at ion of EJB wit h JMS. Messag e- d riv en b ean s. • •
of Content I m plemTable ent addit ion als bu siness m et hods in t h e hom e int er face t hat ar e not specif ic for b ean I ndex inst ance.
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
quer yerlang uage ( EJB QL) w hich Shaddon en ables sear ches By Ueli EJB Wah li, Wout Denay er, Lar s Schu nk , ,Deborah , Mart in Weiss
based on t h e obj ect schem a in st ead
of dat a sch em a. Pub lish er: I BM Pub Dat e: April 1 1, 20 03
Changes in EJB 2.0 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
EJB 2. 0 con t ains t h ese chan ges ov er EJB 1 .1 : The n ew CMP comp onent mod el is rad ically d if fer en t f r om t he old CMP m odel, because it int r odu ces an ent ir ely n ew par t icip ant , t he per sist ence m anager. Ther ne about Lear is a com t he plet EJB ely2 new .0 specif w ayicat of ion defin in g con t ainer- m an aged f ields, as w ell as r elat ion sh ip s w it h ot her beans an d dependent obj ect s. Dev elop EJBs w it h St u dio App licat ion Dev eloper The in t r oduct ion of message- dr iven beans p rov ides a com pon en t m odel f or t h e en t er p rise Deployact EJBs o WJMS eb Sp hertes,Applicat iont hSer beans in g tas clien allow ing em vter o b e deploy ed in t he r ich an d rob ust env iron ment of t he EJB cont ain er sy st em. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Limitations
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Ev w iting h EJB 2.ional 0 w e dat face un en derly r elat absom ase.e limit at ion s: I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic I nher it ance supp concep t s and t h e is ar not ch it ect ur ort e. Ied n .par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed The m ap ent y beans t o r elat dat abases is v endor ecific. per sist ence entping it y bof ean s it and m essagedr ivion enalbeans. We also pr ovid sp e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. IGeneral n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session restrictions of EJB beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At EJB dev elopm ent places som e r est r ict ions on t h e developer s for bet t er com p onent m an agement t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. and easier serv ice. The r est r ict ions includ e t he f ollow in g: An en t erp rise bean m ust n ot u se r ead/ w r it e st at ic f ields. Th er efor e, w e r ecom m en d t hat all st at ic fields in t he en t er pr ise bean class ar e declared as final. An en t erp rise bean m ust n ot u se t hr ead sy nchr onizat ion pr imit iv es t o sy nchr onize execut ion of m ult iple inst ances. An en t erp rise bean m ust n ot u se t he AWT fun ct ion alit y t o at t em pt t o out pu t inf or mat ion t o a d isp lay, or t o inp ut infor m at ion fr om a key boar d.
An en t erp rise bean m ust n ot u se t he java.io p ackag e t o at t em pt t o access files and dir ect or ies in t he f ile sy st em . An en t erp rise bean m ust n ot at t em pt t o list en on a sock et , accept connect ions on a sock et , or u se a sock et f or m ult icast in g.
• •
The ent er pr ise bean mu st not at t em pt t o q uery a class t o obt ain inf or mat ion abou t t h e declared m em ber s t hat ar e not ot her w ise accessible t o t he ent er pr ise bean b ecau se of t h e Content s secur it yTable r ulesof of t h e Java langu age. I ndex
EJB 2 . 0The D e ve lopm e nt w itbean h W ebSphe St udio Appl De veela oper ent er pr ise mu str enot at t em pticat oti on cr eat class
load er , obt ain t he cur r en t class cr eat e a new secu r it y m anager , st op t he JVM, or chan ge t he in put , out p ut , an d er r or st ream s.
, seter tDenay he con ex st Schu class t he, secur y m anager , By Ueli loader Wah li, Wout er, tLar nk ,loader Deborah, set Shaddon Mart in it Weiss Pub lish er: I BM
The ent er pr ise bean mu st not at t em pt t o set t h e sock et f act ory u sed by ServerSocket, hand ler f act ory used by URL.
Pub Dat e: April 20 03 Socket, or1 1, t he st r eam I SBN: 0- 73 84- 26 09 -1
The Pages: ent 75er 0 pr ise bean mu st not at t em pt t o m anag e t hr eads. The ent er pr ise bean mu st not at t em pt t o d ir ect ly read or w r it e a f ile descr ipt or . The ent er pr ise bean mu st not at t em pt t o ob t ain t h e secur it y p olicy inf or mat ion f or a par t icular code sour ce. Lear n about t hebean EJB mu 2 .0 stspecif icat ionpt t o load a n at ive libr ar y . The ent er pr ise not at t em Dev it h Stmu u dio ionptDev The elop ent erEJBs pr isewbean st App not licat at t em t o geloper ain access t o pack ages and classes t hat t he u su al r ules of t he Jav a pr ogr am min g lang uage mak e u nav ailab le t o t h e en t erp rise bean. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er The ent er pr ise bean mu st not at t em pt t o d ef ine a class in a pack age. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat f or tmu he std ev elopm enpt t of plicat ion s based t h e Ent pr ise Jav Theion entDeveloper er pr ise bean not at t em t oap access or mod if y t hon e secur it yerconf ig ur ataBeans ion object s ( EJB) ( ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Policy, Security, Provider, Signer, an d Identity) . Thr oug hout heprbook , w e pr e exam ples simp le bank appsubst licat ion it hf an The entt er ise bean muovstidnot at t em pt based t o u se on t heasubclass and ing obj ect it utw ion eat u res un derly ing r elat ional dat ab ase. of t he Java ser ializ at ion p rot ocol. I n Part 1, ent weerinpr t rise oduce EJBs a p art of Jav En t er p rise J2 EE) andmet cover The bean mu as st not at t em pt tao 2p ass this asEd anit ion ar g (um en t or hod trhe esubasic lt . The concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he ent er pr ise b ean m ust in st ead pass t h e r esult of SessionContext.getEJBObject or EJB 2.EntityContext.getEJBObject. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
EJB 2.0 overview I n t his sect ion, w e hav e a shor t look at t he new an d ch anged fun ct ion in EJB 2. 0. More det ails ar e pr esent ed in Ch apt er 2, " EJB ar chit ect u r e and concep t s" on page 33. •
Table of Content s
•
I ndex
Local interfaces EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
An EJB client never d ir ect ly in t eract s w it h an EJB object . Th e client uses t he com ponent int er face, w hich defines t h e met h ods t h at ar e av ailab le t o t h e client . The im plem en t at ion of t h is int er face is lish er: pr ovPub ided b yI BM t he con t ain er . Pub Dat e: April 1 1, 20 03
Wit houI tSBN: h aving t h26 e 09 new 0- 73 84-1 local int er faces, t he cont ain er has t o pr ovid e only one im p lem ent at ion of t his com pon en t int er face—an imp lem ent at ion for in vocat ions over a net w or k pr ot ocol, becau se Pages: 75 0 t he EJB client h as t o be a rem ot e client . I f t he EJB client and t h e EJB object it self are in t h e sam e Jav a Vir t ual Mach ine ( JVM) , an avoidab le over head arises because of t he com m unicat ion lay er s. To av oid t his ov erh ead , one t yp e of com p onent in t er f ace is bein g added t o t h e EJB sp ecificat ion 2. 0: t he local com ponent int erf ace. about EJBof 2 .0 specif icat ion Ther eLear ar e nnow t w ot he t y pes comp onent int erf aces: a local and a rem ot e int er face. We can now choose t he t yp e t hat suit s our need s. Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Container-managed persistence (CMP) This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licatcon ion t Developer f orEJB t he hav d eveelopm of w apitplicat ionotshbased e Entoferan pr ise Jav aBeans An EJB ainer and an t o in ten er tact h each er . I n on t het hcase CMP ent it y bean, (t his EJB)in ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. t er act ion is quit e com plex . This com mu nicat ion is called a cont r act . Thr t he book pr ov id e exam ples based simp le bankning Theoug EJBhout specificat ion ,2w. 0e est ablishes new cont r act son f ora CMP. These ew app contlicat r action s arweit h an un derly ing r elat ional dat ab ase. fu ndam ent alsfor t he n ew fun ct ion alit y and t hey supp or t m or e efficient v endor imp lem ent at ions. IThe n Part we rin t r oduce asare a ptart Jav a 2t oEnan t erEJB p rise Ed it ionm( J2 anda cover t hembasic n ew1,cont act s t hemEJBs selv es ranof sp arent p rog ram er.EE) Fr om pr ogr am er ' s concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced it hcat t he point of v iew , t here ar e on ly som e chan ges in t he im plement at ions. One ch ange t hat mway ch EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er m anag t he at t ent ion of a pr ogr am m er is t hat t he b ean class is now an ab st r act class w it hout fields. ed The per y sb of eant hseand m essagedr iv en beans. ovid e best icet guidelines f or get tsist er sence and ent set it t er bean class describe t he at t rWe ibu talso es ofprt he b ean . I t pr is act t h en h e job of t he successful imp lem ent at ions of EJBs. cont ainer t o gener at e a concr et e class f or t his bean an d t o im plement t he field s and t he r elat ion sh ip s bet w een ent it y b ean s. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, d mpessag r iv enfubeans e St u dio ApQL. plicat ion Dev elop er . We also The t woannew rim are-y dCMP nct ionsusing ar e rWebSph elat ion sherips and EJB im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Container-managed relationships (CMR) The m ain pr inciples of CMR and CMP ar e comp ar able. I n CMP, w e descr ibe our cont ainer m anaged fields an d t he cont ainer is r esp onsible for persist ence. I n CMR, w e describ e t he r elat ion s bet w een our ent it y beans an d t h e cont ainer is r esp onsible for main t aining t he r ef erent ial in t egr it y. What a con t ainer does in diff er ent sit u at ions is clearly d escrib ed in t he specificat ion. For exam ple, if w e cr eat e a one- t o- m any relat ionship bet w een bean A an d b ean B and inv oke a set m et hod on in st ance A2 p assing t h e inst ance B1, t he cont ainer h as t o rem ove t he p rev iou s
r elat ion sh ip b et w een A1 and B1 because of t he 1: n relat ionship defin it ion . B1 can on ly belon g t o one in st an ce of bean A. The cont ainer h as t o rem ove t he p rior r elat ionship in t h e same t r ansact ion cont ex t t o m aint ain t he r efer en t ial int egr it y . The d escr ipt ions of t h e r elat ionship s ar e based on t h e abst r act per sist ence sch em a. Th e abst r act sch em a and t he d escr ip t ion of t he r elat ion sh ips ar e par t of t he d ep loy m ent d escr ipt or . The m em ber s of a cont ainer - m anaged r elat ionsh ip m ust hav e a local int er face. •
Table of Content s
•The con t ain erI ndex - m anag ed r elat ion ships sup por t on e- t o- man y ( 1: m ) , on e- t o- one ( 1 : 1) , an d m an yEJB 2 .any 0 D e ve lopm w it ionship h W ebSphe t o- m ( m: m )e nt r elat s. r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss Pub lish er: I BM language EJB query Pub Dat e: April 1 1, 20 03 I SBN:m 0-et 73hod 84- 26 The f inder s 09 in-1t he hom e int er f aces ar e r esp onsib le for locat ing par t icular ent it y ob ject s, bu t t h Pages: e nam75 e 0and t h e declar at ion of t he m et hod s ar e not su ff icient infor m at ion for t he cont ainer t o generat e an im plem en t at ion of t he f in der m et hod. Therefor e, t h e bean pr ovid er h as t o pr ovide a d escr ipt ion of t he f in der m et hod. The EJB Ar ch it ect ur e Ver sion 1. 1 does n ot specif y t h e for m at of t he find er m et hod descrip t ion . So ev ery v endor ( t hat is, ev ery con t ainer pr ov id er ) m ust of fer a w ay t o do t his.
Ther e ar e some obv iou s dr aw back s w it h t his app roach. Ev er y cont ain er p r ovider of fer s a Lear n about t he EJB 2 .0 specif icat ion dif fer en t w ay t o descr ibe an d im plem en t t h e fin der m et h ods, bu t all t he dif fer en t im plem ent at ion s hav e ha St big d r awb meloper on. Since t h e EJB 1. 1 beans hav e n o obj ect Dev elop EJBs w it u dio Appack licatin ioncom Dev sch em a, t hey hav e t o im p lem ent an d descr ibe t h e find er met hod s based on t h e dat a sch em a. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er The EJB Query Langu age ( EJB QL) enab les u s t o descr ibe t he find er met hod s based on t h e obj ect sch a inRedbook an ind eppr enovides dent man ner. Now our find on er m et hod e ively suppor t edWebSp by ev er y EJB 2. 0 Thisem I BM d et ailed in for m at ion h ow t o sefar fect use here St udio com pliant ain er . Wf or e do h ave t o en cust ourion finder m eton hodt h s eif Ent w e er are usinJav g anot her App licat ioncont Developer t henot d ev elopm t ofom apize plicat s based pr ise aBeans dat ab ase anot h eranEJB cont ain ent er . of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. ( EJB) ar chor it ect u re, d d ep loym The EJBhout QL ut he sesbook an SQLt o select obj ecton s or v alu es. I t is ing a t yapp pedlicat expion ression Thr oug , w elik preovsyidnt e ax exam ples based a simp le bank w it h an langu age. un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t hmethods e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB home EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or EJB 1. 1 allow onlyent creat e anofd EJBs. f inder m et hod s in t h e hom e int er face of an ent it y b ean . The successful imps lem at ions bu sin ess logic h as t o b e im plem ent ed in t he bean class an d can be accessed ov er t h e com p onent face. e can tinh vok e ap met h oding ov er t he com entthen int erimp face onent ly ifent here is a concr et e Iint n er Part 2, Since we in t w r oduce e sam le bank applicat ionpon and lem t it y beans, session im plem an entdatmion of teh is erf beans ace, w e hav eWebSph t o inv okereet his par t icu ion lar ent y objerect . also beans, essag d rin iv ten using St u on dio aAp plicat Devitelop . We im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At It he f we sinesselogic is d edEJB icat ed t o a bean and in add ion licat ind ep denter. of a enhav d, we ebu d escrib how tthoatdeploy ap plicat ion s tclass o a WebSph er e itApp ionenServ par t icular ent it y ob ject , w e can now p lace t h is cod e in a m et hod of t he b ean class and declar e t his m et hod in t he h om e int erf ace of t his ent it y b ean . I n t his m et hod, w e can f ind or creat e ent it ies and access t hem ov er t heir com ponent int er faces, as w e do fr om a session bean m et h od or an y ot her EJB client . I f w e inv oke t his m et hod on t he hom e int er f ace, t h e cont ainer select s an inst ance of t he bean and inv ok es t he m et hod independ en t of an y par t icu lar ent it y obj ect . I n an EJB hom e m et hod, no access is allow ed t o t he at t ribu t es of t he b ean .
Message-driven bean
This EJB t y pe is added in t h e EJB 2. 0 sp ecificat ion. A m essage- dr iv en bean is an asy nchr on ous m essage consum er , im plem ent ed as a Java Message Ser v ice ( JMS) consum er . I n pr ior EJB sp ecificat ions, it was ver y har d t o im plem en t an asy nchr onous ser vice r unn ing insid e t he con t ainer. The han dling of t hr eads is especially subj ect t o rest r ict ions in sid e of ev er y EJB cont ainer . I f w e need m ult ip le in st ances of a ser v ice class act ing as one asy nchr onou s ser v ice t o m eet t he d em and s of a g r owin g m essag e load, w e h ave t o han dle t he in st ances ou r selves, •hav ing r eg ard Table t o tof heContent lif et ims e of t h e diff erent inst ances and t o man y ot her issues. •
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er,classes Lar s Schu nk , Deborah Shaddon , Mart in Weiss Dependent value Puben lishdent er: I BM A d ep valu e class is an ent it y b ean at t r ibu t e t hat is a concr et e ser ializable Jav a class contPub aining num er03of d at a f ields. Th e dat a fields ar e st or ed in ser ialized f orm at in a t able Dat e: aApril 1 1,b20 colum n, or t0-hey can b-1 e m apped t o ind ividu al colum ns u sin g a comp oser . The in t ern al st r uct ur e I SBN: 73 8426 09 of t he Pages: dependent valu e class is n ot specified in t he deploym ent descr ipt or . 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
A brief look at history I n t his sect ion, w e ex plain how t he EJB t echnology developed fr om a com bin at ion of CORBA an d RMI , as w ell as t he b asic concept s of u sin g EJBs. •
Table of Content s
•
I ndex
Distributed component-oriented applications EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
A n um ber of dist r ibut ion t echnologies ar e av ailab le t oday . Som e of t he most w ell- k now n are t he Ob ject Man agement Gr oup ( OMG) Comp onent Obj ect Request Br ok er Archit ect ur e ( CORBA) , Su n Pub lish er: Jav a Rem otIeBMMet h od I nv ocat ion ( RMI ) pr ot ocol, and t he Micr osoft Dist ribu t ed Comp onent Obj ect Pub Dat e: April. 1I1, 03 Jav a w or ld, on ly t he f irst t w o t echnolog ies st and ou t as especially Mod el ( DCOM) n 20 t he I SBN: 073 8426 09 im por t ant . I n t h e next-1sect ion, w e br ief ly descr ibe CORBA and RMI an d sh ow how t hey r elat e t o t he mor e r ecent t echnology of EJB. Pages: 75 0
CORBA overview CORBA w as d ev elop ed b y a consor t ium of com panies ( t h e Obj ect Manag em ent Gr oup) du rin g t h e t he EJB 2 .0 mon specif icat ion early Lear 1 990nsabout t o pr ovide a com , langu age- an d v endor - n eu t r al st andar d for object dist r ibut ion . CORBA as an ar ch it ect u re has been well accep t ed an d successfu lly used in m any Dev elop EJBs w it h St u dio App licat ion Dev eloper pr oj ect s. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er The CORBA archit ect ur e is b uilt ar oun d a special lay er, t he ob ject r equest br ok er ( ORB) , t hat facilit esRedbook comm un pr icat ion bet w een objon ecths. ORB r espon le forhere h andling the This I at BM ovides d et ailedclien in fort smand at ion owTht oe ef fectis ively use sib WebSp St udio obj ect r eq uest s fr om a client and passin g ov er t he par am et er s fr om m et hod inv ocat ions. App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Low - lev el com m unicat ion bet w een dif fer en t obj ect spaces ( ORBs) is d one b y using t he I nt er net IThr nt er ORB ( I ,I OP) g t h isples st andar d pr based pr ogr oug houtPrtot heocol book w e .prBy ov uidsin e exam based onotaocol, simpaleCORBAbank ing app licat ionam w itf rom h an any vun enderly dor , ing on ralmost any com put er , op er at ing sy st em , pr og ram m ing langu age, and net w or k can elat ional dat ab ase. int er operat e w it h a CORBA- based p rog ram fr om t h e sam e or anot her v endor , on an y ot her com put er oper ing syst em , as pr ogr g lan e,pand or k.( J2 EE) and cover t he basic I n Part 1, , we in at t r oduce EJBs a p am art m ofinJav a 2guag En t er rise net Ed itwion concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he Apar t fr t h eicat ORB, hereprare t wo ot her k eyple, buildin g b lockfun s in t healit CORBA modain el, er t he nt er face EJB 2. 0 om sp ecif ion tt hat ov id e, for ex am en hanced ct ion y for cont - mIanag ed Defin it ion Languag e ( I DL) , w hich n orm alizes t he d iff er ences cau sed by lang uage or oper at ing f or per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines syst em d epimp en dencies; d t of he EJBs. CORBA serv ices, w h ich pr ov ide st andar d w ay s for CORBA obj ect s successful lem ent atan ions t o in t eract , such as n aming an d t ran sact ion. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session The g r eat van et ages of using is t h at iteris andAp ardplicat int erion face t h elop at enab les also beans, anest d mad essag d r iv en beansCORBA using WebSph ea Stst u dio Dev er . We int er operab ilit y bet w een diff erent v en dor s' p rod uct s, and t h at CORBA is lan guage neut r al. im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At CORBA clien t s and ser v ers can be wr it t en in a v ariet y of com put er lang uages, includin g t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.Jav a, C+ + , C, Sm allt alk , an d Ad a. This is p ossible b y imp lem ent ing r emot e in t erf aces f or t h e CORBA dist r ibut ed obj ect s in I DL. But w hen usin g CORBA t o b uild d ist r ibu t ed sy st em s in Jav a, t he developm ent ef for t is h igher , because m any p art s of t he sy st em h ave t o be im p lem ent ed in t w o lan guag es: I D L and Java. Add it ion ally , t h e dev elopm ent t ools and r un t ime env ir onm ent f or CORBA ap plicat ion s can also b e exp en siv e and m ay not f ully imp lem ent t he CORBA ser vices. All t hese, in com binat ion w it h t he fact t hat dev eloper s st ar t ed t o look for sim pler solut ions, r aised t he in t er est in Jav a RMI .
RMI overview Rem ot e Met hod I nv ocat ion ( RMI ) enab les t h e pr ogr am m er t o cr eat e dist rib ut ed Java app licat ions, in w hich t he m et hods of rem ot e Jav a ob ject s can b e inv oked fr om ot h er Java Vir t ual Mach in es, p ossibly on dif fer en t h ost s. A Java pr ogr am can mak e a call on a r em ot e ob ject on ce it obt ains a r efer en ce t o t he rem ot e •obj ect , eit h erTable of Content b y lookin g u ps t he r emot e object in t h e nam ing ser v ice or by r eceivin g t he •r ef erence as Ian ndex ar gu ment or a r et u rn v alue. RMI uses object ser ializat ion t o mar shal an d EJB . 0 sh D eal ve lopm e ntet wer it hs,Wsom ebSphe e St udio ica s ti on De vpr elog oper dem2ar par am et hr ing t hatAppl allow RMI ram s t o pass by value object s so t h at t he ser v er can op er at e on a local cop y. An ot her adv anced feat ur e of RMI is t he d ist r ibu t ed g arb age By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss collect ion, a feat ur e t h at is n ot av ailable in CORBA t oday . I BM HowPub evlish er er: , t her e ar e some kn own d ef iciencies of RMI , such as m ult iple- lan guag e supp ort , and t he Pub Dat e: 20vices 03 lack of all ofApril t he1 1, ser t hat CORBA supp ort s. For ex am ple, RMI sup por t s a nam ing ser v ice, bu t I SBN: 073 8426 09 no t r ansact ion s or per-1sist ence serv ices as CORBA does. Jav a RMI does not even cont ain any pr ov ision Pages: for75applicat 0 ion lev el secur it y . Also, t he conf igur at ion of RMI p ort s in f ir ew alls can b e v er y d if ficult . All t hese lim it at ions led t o t he com binat ion of t he t wo t ech nologies.
RMI over IIOP Lear n about t he EJB 2 .0 specif icat ion The t wo pr eviou s sect ions show ed t hat neit her CORBA n or Jav a RMI w er e suf ficien t enou gh t o add ress all t h e issues t h at a Jav a dev elop er f aces wh en b uilding en t erp rise app licat ions. Dev elop EJBs w it h St u dio App licat ion Dev eloper Alt hou gh t hese t w o t echnologies h ave som e holes, t hey can com p lem ent each ot her in a un ique w ay . Deploy EJBs t o W eb Sp her e Applicat ion Ser v er RMI I I OP ( RMIpr- Iovides I OP) com t hfor e best f eaton u res of tRMI w it hively t hose CORBA. Lik St e RMI This over I BM Redbook d et bines ailed in m at ion h ow o ef fect useofWebSp here udio, RMI licat - I I OP s d ev elopers on ly en Java. Developer not hon ave t oEnt d everelop botaBeans h Jav a App ionallow Developer f or t het od use ev elopm t of ap plicat ionssdo based the pr iseinJav - IuI OP elop er soft osubuild any ser le Java ect (and EJB)I DL. ar chRMI it ect re, allow an d dsepdev loym ent ch apclasses plicat iont hat s t opass a WebSph erializab e App licat ion obj Serv er.as r em ot e met hod ar gu ment or r et ur n valu e. By using I I OP as com mu nicat ion p rot ocol, RMI - I I OP app licathout ions tare in t er ,oper w it ot her CORBA plicat ion s. nt hesis of tion hese Thr oug he book w e able pr ov id e hexam ples basedapon a simp le The banksying app licat w itt w h oan tun echnologies r esu lt s indat a ab uniq ue com binat ion of pow er and ease of use, t he En t er p rise derly ing r elat ional ase. Jav aBean s. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 CORBA sp ecif icat ion pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed From tot hat EJBs per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. CORBA as an ar chit ect u r e has b een w ell accept ed an d su ccessf ully used in m any p roj ect s. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session How ev er has e dbeans raw b ack s: WebSph er e St u dio Ap plicat ion Dev elop er . We also beans, an, dCORBA m essag e- dsom r iv en using im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he enCORBA d, w e dhescrib e eep howlearn t o deploy s t o at hWebSph erele API Appslicat as a st ing cu EJB r ve. ap I n plicat m anyion cases e low - lev ar eion t ooServ comer. plex for enabling r apid applicat ion developm ent . The d iff er ent CORBA infr ast r uct ur es w ere less por t able t han ex pect ed. Unt il t h e int r odu ct ion of EJB in 1 998 , CORBA h ad n o com plet e ser v er - side com ponent m odel and dist r ibut ed obj ect fr am ewor k . The CORBA vend or s ( or dev elop er s) h ad t o int egr at e t h e CORBA serv ices w it h a ow n, non- st and ar d ap plicat ion lay er . I n 199 9, a year af t er t he int rod uct ion of Ent er pr ise JavaBeans 1 .0 , t h e OMG gr oup has pu blish ed t h e CORBA Com pon en t Mod el ( CCM) . How ev er , t he ap plicat ion serv er m ar ket has been f ocusing on EJB com ponent solut ions.
The an noun cem ent of EJB w as a big hit for t h e CORBA com mu nit y . EJB w as designed t o b e 10 0% CORBA- com pliant and aug ment s CORBA w it h a ser v er - side com pon en t m odel b y definin g int er faces bet w een t he com pon en t s ( EJBs) and t he m an aged ob ject f ram ew or k ( EJB con t ainer) . All t h e m ajor CORBA v endor s w er e heav ily in volv ed in t he d ev elopm en t of t h e EJB specif icat ions pu blish ed by Sun Micr osy st em s. The EJB t echnolog y comp lem ent s CORBA q uit e n icely. CORBA pr ov id es a gr eat st an dar ds- based inf rast r uct ur e on w hich t o b uild EJB serv er s. EJB t echn olog y m akes it easier t o b uild applicat ions • Table of Content s on t op of a CORBA in fr ast ru ct ur e. Wit h EJB t h e bu rd en of low - lev el API s has b een shift ed fr om • I ndex t he applicat ion d ev eloper t o t he app licat ion ser v er in fr ast r uct ur e ( EJB cont ainer ) . EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Enterprise JavaBeans Pub lish er: I BM
Pub Dat e: April 1 1, 20 03
En t er p rise Jav aBean s pr ov ided a w ay t o im pr ov e t he ex ist in g CORBA m odel by enh ancing it w it h I SBN: 84-Java 26 09 -1RMI feat ur es. The EJB m odel enables t he Java dev eloper t o pr odu ce pur e som e of t he0-b73 est Pages: 75 0 dist r ibut ed applicat ions in a sim ple m ann er . Th e key concept s of u sin g ent er pr ise beans ar e: EJBs are f oun d or cr eat ed b y using an object f act or y t hat is inher it ed f rom CORBA. An EJB dev elop er cr eat es a Jav a int er f ace, t h e hom e int er face, w hich defines t he w ays in w hich r em ot e object s ar e creat ed or fou nd. The EJB fact or y , w hich im p lem ent s t h ese int er faces, is called hom e. EJB Client s locat e icat EJBion h omes t h rou gh t he Java Namin g an d Direct or y Ser v ice Lear n EJB about t he 2 .0 specif ( JNDI ) . Dev elop EJBs w it h St u dio App licat ion Dev eloper EJBs are accessed t hr ough a sim ple Jav a in t erf ace, t he r em ot e int er face . Th e r em ot e int er face is inh edSp f rher om eJav a RMI ion andSer pr vov Deploy EJBs t o er Witeb Applicat erides t o t he pr ogr am m er all t he ex t ern ally accessib le m et h ods of t h e rem ot e ob ject . I n t he EJB w orld , it is t he EJB obj ect t hat ent s t he r emot ed et intailed erf ace, and allow s t he client o use t heuse b usiness ic tSt hat t he This Iim BMplem Redbook pr ovides in for m at ion on h ow t o eftfect ively WebSplog here udio r emion ot eDeveloper object im plem en tds.ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans App licat f or t he ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. EJBs use RMI - I I OP. This m eans t hat t here is a w ell- d ef ined, st and ar d m app ing of EJB inthout er faces o CORBA I Dov L. idTh assur es int er operab y bet w eening diffapp erent syst Thr oug t he tbook , w e pr e is exam ples based on a ilit simp le bank licat ionem w its ht hat an iming plem ent ional EJB ser er sase. or CORBA sy st ems. Th is also pr ov id es fir ew all sup por t t o EJB un derly r elat datvab app licat ions. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Ther e ar e wur ays b uilding ent out EJBs, h t hfur ough v enint dor - pr ov idwedit h t he concep t s and t hst e andar ar ch itdect e. for I n par t icu larper w esist point t hebot n ew nct ions r od uced s ion ( t h et hat contprainer m for anaged m odel, CMP) andcont t hr oug h -amuserEJB 2.fr0am spew ecifork icat ov id-e, ex ampersist ple, enence hanced fun ctorion alit y, for ain er anag ed dev elopent editper anismdr( tivhe ean - m anaged m odel, or BMP) . per sist ence y b sist eanence s andmmech essageenbbeans. We alsopersist pr ovidence e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. Ther e is a st an dar d t ran sact ion m odel for EJBs of fer ed t h rou gh t he Jav a Tran sact ion API ( JTA) . Thinet run derly ting r ansact ion serv allowion s anand ap plicat o upd e dat in mu ltsession ip le I n Part 2, we oduce h e tsam p le bank ing ice applicat t hen ion imptlem entaten t it y abeans, dat a dsour ces we-itdhin a sing le ( dist r ib WebSph ut ed) t r ansact . Ap plicat ion Dev elop er . We also beans, an m essag r iv en beans using er e St uion dio im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At b uilt ue pon secu r it y mod hat allow per son derepe loying t h ion e EJB t o er. d et er m in e t he enEJBs d, w are e d escrib howat o deploy EJB el aptplicat ion sst ot he a WebSph App licat Serv w hat access should be g ran t ed t o w hom at an EJB or m et h od lev el. An EJB is a com ponent t hat im plem ent s business log ic in a dist r ibut ed en t erp rise app licat ion. An EJB cont ain er is w h er e EJBs r eside. The EJB cont ain er is respon sible f or m akin g t he EJBs available t o t he client . EJBs ar e deploy ed int o EJB con t ainers and r un on En t er p rise Jav a Ser ver s ( EJS) .
JavaBeans versus Enterprise JavaBeans Jav aBean s is t he com pon en t m odel f or Jav a. Each Jav aBean h as pr oper t ies, m et hods, an d
event s. Ent er pr ise JavaBeans also d escr ib e, am ong ot her t hin gs, a com pon en t m odel. How ever , t hese t w o com pon en t m odels ar e not ident ical. Jav aBean s ar e int ended t o b e r eu sab le sof t w ar e com ponent s t hat can be visually m anipu lat ed in a bu ild er t ool. Ther ef or e, t he Jav aBeans specif icat ion describes in det ail t h e API s for con nect in g b ean s t o each ot her t hr ou gh t he ev en t m odel, w her e one b ean act s as an event list en er an d t he ot h er as an ev ent sou rce. Bean cust om izat ion is d one at assem bly t im e by u sin g p rop er t ies. Jav aBean s do not requ ir e a special cont ainer . They can ex ist inside a Jav a Vir t ual Mach ine. •
Table of Content s
En t er p rise Jav aBean s ar e st ron gly associat ed w it h a ser v ices f ram ew ork . No ev ent m odel is used • I ndex by Ent er pr ise JavaBeans. Cu st omizat ion is don e at ru nt im e by u sin g a deploy ment descr ipt or. EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper En t er p rise Jav aBean s do requ ire an EJB cont ain er , in w hich t hey are deployed. This cont ainer By Ueli Wah s Schudef nk ,in Deborah , Mart inorWeiss should pr li, ovWout ide er allDenay t h e er, serLar v ices ed b y Shaddon t he f r amew k and it m u st r eside w it hin an ent erp r ise Jav a serv er . Pub lish er: I BM
So, Pub t h eDat t we:o April com1ponent m odels ar e used in dif fer en t w ay s. The Jav aBeans m odel sup por t s 1, 20 03 app licat ion assem bly in a bu ild er t ool, w hile t he Ent er pr ise JavaBeans mod el su ppor t s a I SBN: 0- 73 84- 26 09 -1 dist r ibut ed obj ect mod el. JavaBeans d o n ot h ave t h eir own cont ain er , so t ry ing t o locat e t hem on 0 a r em Pages: ot e ph75 ysical m ach ine is t h eor et ically im possib le. Non - gr aph ical ser ver - sid e Jav aBean s ( for exam ple, RMI ser v er beans) could be also used inst ead of Ent er pr ise JavaBeans, b ut t hat wou ld r eq uir e t he im plem en t at ion of an ent er pr ise Java ser v er fr am ew or k .
EJBLear and CORBA interoperability n about t he EJB 2 .0 specif icat ion To ensu reelop in t erEJBs operwabilit y ubet EJB ion andDev CORBA t echn olog y, Sun Micr osy st em s cr eat ed a Dev it h St diow een App licat eloper m appin g b et ween t he EJB specif icat ion s and CORBA. The m ap ping includes obj ect dist r ib ut ion, Deploy to W eb Sp her e Applicat ion Ser v er nam ing , and EJBs t r ansact ions. This BM Redbook pr ovides aileding in for m at ion I nt erIoperab ilit y pr ovides t h ed et follow benefit s: on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. CORBA clien t s can access EJB com ponent s deploy ed in CORBA- based EJB ser v er s. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Client ogr am s dat canabmase. ix calls t o CORBA ob ject s w it h calls t o en t erp rise beans w it hin t he un derly ing rpr elat ional sam e t r ansact ion cont ex t . I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Trtansact an le beans ed onout m ult iple CORBAbased ser ver concep s and tion h esarcan ch itsp ect urm e.ult I nippar t icu lar locat w e point t he n ew fu nct ions intEJB r od uced w sit hfr om t he dif fer en t v endor s. EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or The EJB n amin g ser vice, w hich is b ased on t he CORBA Obj ect Ser v ices ( COS) nam ing serv ice, successful imp lem ent at ions of EJBs. m akes EJB com ponent s available t o CORBA clien t s. The Jav a Nam ing and Dir ect ory I n t er f ace (I JNDI int ertface t o COS for Java client s.ion and t hen imp lem ent en t it y beans, session n Part) pr 2, ovid we es in t ran oduce h e sam p le bank ing applicat beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also EJB t r ansact ion level sup d ep ds in ong tsthe t r ansact serv ice, sact ion im plem ent fin derlow m-et h ods, difpor ferten t men app r atCORBA egies, and sim pion le client s t hObj at uect se tTran he EJBs. At Serv ice ( OTS) . Th e Jav a Tr ansact ion Ser v ice ( JTS) r ep resent s t h e Jav a bind ing of OTS. CORBAt he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. based EJB cont ain er s m ust en su r e t r ansact ion bou ndar y set t ing fr om CORBA client s v ia OTS int er face as w ell as t r ansact ion demar cat ion by Jav a Tr ansact ion API ( JTA) , w h ich is t he app licat ion- lev el in t erf ace t o JTS. JTA also r epr esent s t he Jav a b in ding of t he Open Gr oup XA int er face f or r eg ist er ing an ap plicat ion r esou rce, f or exam ple a connect ion, t o an ex t ern al t r ansact ion m an ager ( fou ndat ion f or dist r ib ut ed t ran sact ion and t w o- p hase com m it r espect iv ely . ) RMI - I I OP en ables dist rib ut ion of EJBs an d CORBA object s. I m plem ent ing a scalable ser vice in pr ior EJB sp ecificat ions in side a cont ainer is sim ilar t o h ow t he con t ainer hand les en t er pr ise beans in t er nally . The in st ances of t he EJB classes and t he lifet im e of t hese inst ances are hand led by t h e cont ainer . This is on e of t he r easons f or addin g t he m essage- dr iv en bean t y pe t o t he specificat ion.
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Adopting EJB technology The EJB t echnolog y is r elat iv ely n ew comp ared t o CORBA and RMI . How ever , it has gain ed a lot of at t ent ion fr om m an y or ganizat ions and com pan ies. Tod ay, t here are man y par t ies t hat ar e com mit t ed t o t his new op en st and ard and t h ey are d oing big inv est m ent s in t his t echnolog y by •im pr ovin g anTable of Content d ad van cin g tshe EJB specif icat ion s. •
I ndex
EJB 2 . t0ing D e ve lopm e nt w it h W ebSphe e Sttudio ti on v el operur ed an d st an dar d w ay , an d t here Adop t his t echnology is a str ep owarAppl d s ica am or De e st r uct should not be an y hesit at ion in t he relat ively long er sk dev elopin g cy cle com par ed t o ot h er By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in ill Weiss t r adit ional appr oaches. This is an inv est m ent t hat w ill be r et ur ned in m any w ay s. Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
End-to-end enterprise application development Let ' s go t hr oug h a scenar io w h er e y ou, as an I T pr oject m anag er , h ave a pr oj ect wh er e y ou ar e going t o use EJBs. W hat st eps are y ou going t o follow t o dev elop y our app licat ion? •
Table of Content s
•
I ndex
Application server market analysis EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
First , y ou hav e t o analyz e t he m ar k et for EJB cont ain er / serv ers, also called ap plicat ion serv ers. The v endor s or con t ainer/ ser ver p rov ider s hav e lot s of inf or mat ion pu blish ed on t he Web . HowPub evlish er er: , beI BM aw ar e t h at som e comp anies only sell con t ainers, n ot ap plicat ion serv ers, w h ich Pub Dat e: April 20 03 m ean s y ou w ill 1h1,ave t o bu y t w o p rod uct s inst ead of one. I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Reuse or develop The n ex t st ep is t o det erm ine w h ich EJBs you could reuse inst ead of w rit in g t h em you rself. Search t he I n t er net t o f ind comp anies t hat pr ovid e EJB ( bean pr ovider s) , and v isit t heir sit es. Lear n about t he EJB 2 .0 specif icat ion
Development Dev elop EJBs w it h
St u dio App licat ion Dev eloper
EJBsch, t o yW Spe her e Applicat ion ySer erar t you r dev elopm ent and assem ble t h e Aft er Deploy t hat resear ouebar now finally r ead t ovst ent er pr ise b ean s t o f or m y our ap plicat ion ; y ou are now a bean assemb ler . This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Deployment
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un ing r elat ional ase. Aftderly er t he dev elop mentdat is ab don e, all t he ent er pr ise bean classes ar e cont ain ed in a JAR f ile, t h e EJB- JAR. Befor e an ent erp r ise b ean can be inst alled in an applicat ion ser ver , t he en t er pr ise bean Imnust Part we in ed t r oduce asloy am p art Jav aal2 ap Enplicat t er p rise ( J2 EE) and cover basic b e1,deploy . Du rinEJBs g d ep ent ,ofsever ion Ed seritvion er- specific classes ar et he gener at ed. concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or Deployment descriptor successful imp lem ent at ions of EJBs. IThe n Part d ep2, loym weent in t rdescr oduceiptt hore cont sam ain p le sbank at t r ibut ing eapplicat and env ion ir onm and ent t hensetimp t in lem gs t ent h at en d eft it ine y beans, how t he session beans, app licatan iond ser m essag v er ine-vok d r iv esenent beans er pr ise using b ean WebSph f unct ionalit er e Styu. dio Ev ery Ap plicat en t er ion pr ise Dev bean elopm erust . We h ave alsoa im plemment deploy en tfin d escrip der m tet orh ods, t hat cont dif ferains en t at m tapp r ib ut in es g stused r at egies, by t he and applicat sim p leion client ser ver s t h. at These u se tat het r ibut EJBs. es At t he en can oftd, enwbe e dset escrib f or et he how entt oir edeploy ent er pr EJB iseap bean plicat orion fors in t odivid a WebSph ual m et erhods e Appoflicat t heion b ean Serv . er. The d ep loym ent descr ipt or is p ar t of t h e con t r act bet w een t h e bean developer and t h e bean consum er. This cont r act v erif ies t h at all t he necessar y inf orm at ion for assem bling and deploy in g t he applicat ion, is p assed b et ween t he b ean pr ov ider, t he ap plicat ion assem bler , and t he app licat ion deploy er . The r ole of t h e deploy ment descr ipt or is t o capt ur e t he d eclar at ive in for m at ion ( for ex amp le, inf orm at ion t hat is not included dir ect ly in t he ent er pr ise beans code) t hat is int ended for t he consum er of t he b ean s. Ther e ar e t w o b asic k ind s of infor m at ion in t h e deploy m en t descrip t or:
En t er p rise bean st r uct u ral inf orm at ion— St ru ct u r al inf orm at ion descr ibes t h e st ru ct u r e of an ent er pr ise b ean and d eclar es an ent er pr ise bean's ex t er nal d ep en dencies. Pr ov id ing st r uct ur al inf or mat ion in t he d ep loym ent descr ipt or is m and at or y for t he bean dev eloper . The st r uct u ral in for m at ion can not , in gener al, be chan ged becau se d oing so could br eak t he ent er pr ise b ean . For exam ple, isolat ion lev els an d t r an sact ion at t r ibut es ar e st r uct ur al inf orm at ion. •
of Content App licatTable ion assem bly sin for m at ion — Ap plicat ion assem bly in for m at ion d escr ib es how t h e I ndex ent er pr ise b ean s ar e comp osed int o a lar ger app licat ion deploy ment un it . Pr ovidin g EJB 2 . 0assem D e ve lopm nt or w mat it h Wion ebSphe e Stdudio Applent ica ti on De vipt el oper bly einf in t rhe ep loym descr or is op t ional f or t h e bean d ev eloper. Assem bly lev el inf orm at ion can b e chang ed w it hou t b reak in g t he ent er pr ise bean f unct ion, By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss alt hou gh doing so m ay alt er t he behav ior of an assemb led app licat ion. •
Pub lish er: I BM Pub Dat e: April 1 1, 20 03
InstallI SBN: application 0- 73 84- 26 09 -1 Pages: 75 0
At t his poin t , t he deployer st ill has t o inst all t he app licat ion in t he cont ainer / ser v er en vir onm ent . At t he end of t his op er at ion, t he ap plicat ion is liv e and can b e eit h er t est ed or placed in pr odu ct ion . The sy st em adm inist rat or is now in char ge.
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Summary This chap t er pr ov id ed a high- lev el over v iew on Ent erp rise Jav aBeans, t he under lyin g J2 EE plat f or m, and t he EJB specif icat ion . •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Chapter 2. EJB architecture and concepts EJBs pr ovide t he ar chit ect ur e for ser ver - sid e comp onent - b ased dist r ib ut ed com pu t ing. EJBs t y pically repr esent a business pr ocess t hat can be shar ed acr oss applicat ions as w ell as acr oss t• he net w ork . Table of Content s •
I ndex
I n Ch apt er 1, " I nt rod uct ion t o En t er p rise Jav aBean s" on page 3, sever al dist r ibut ed t ech nology EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper app roaches w er e pr esent ed t hat pr ov ided t h e fou ndat ion f or dist r ib ut ed object s an d com ponent By Ueli Wah li, Wout er, Lar s Schuof nk J2 , Deborah Martnology in Weiss w er e int r oduced, as w ell as t he r ole based archit ecter urDenay es. The goals EE andShaddon EJB t ,ech I BM p lays in pr ov iding pr odu ct s t o sup por t t h ese t echn olog ies. Dist r ibut ed obj ect s ar e r eally t he fou ndat Pub lish ion er: of I BMEJBs. Pub Dat e: April 1 1, 20 03
This chap t er is all abou t EJBs. I t pr esent s at a high level all t he m ajor con cept s of t he EJB I SBN: 0- 73 84- 26 09 -1 fr am ew ork . Su ch t op ics as roles, t y pes of EJBs, and t h e API s ar e discu ssed. Th er e is a lot of Pages: 75 0 m at erial here t o cover , and it is a t echnical chap t er geared t ow ar d t he EJB ar ch it ect or dev elop er . For m ore in for m at ion on EJB t echnolog y, please see t he f ollowin g URLs: ht t p: / / j ava. sun. com / j2 ee/ Lear n about t he EJB 2 .0 specif icat ion ht t p: / / j ava. sun. com / pr odu ct s/ ejb / index .h t m l Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Introduction I n t his chapt er , w e int rod uce y ou t o t he basic building b lock s of EJBs. We fir st discuss how t he EJB ser v er and con t ainer w or k t oget her t o pr ov id e t he basic ru nt im e ar ch it ect u re n eeded t o hou se you r EJBs. You need t o hav e a basic und er st and in g of how t h e ser v ices of t he con t ainer •ar e m ade av ailable Table of tContent s o t he EJBs, an d t he cont r act s bet w een EJBs and t heir cont ainer s, in ord er t o •t ak e adv ant ag I ndex e of t hese ser v ices. EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
We t h Wah en dli,iscuss he elem ens tSchu s ofnkt he EJB com pon, en t s,in w hich ar e t h e act ual EJBs t hem selv es. By Ueli Wout er tDenay er, Lar , Deborah Shaddon Mart Weiss Topics such as t he classes an d in t er f aces t h at const it ut e an EJB, t h e diff erent t y pes of EJBs, an d som e of t he API s t h at sup por t t h e bean- cont ain er con t ract are cov ered. This f ir st look at t h e EJB Pub lish er: I BM ar ch it ect u re hopefu lly p r ovides b ot h t he v ocabular y and ov er v iew of EJB t echn ology , so y ou can Dat e: April 1 1, 20 03 see Pub how t h e m ajor element s collabor at e t o pr ov ide w hat ar e ult im at ely you r ser v ice- or ien t ed I SBN: 0- 73 84- 26 09 bu sin ess com ponent s.-1W e addr ess each t y pe of EJB, as w ell as such ad van ced t opics as Pages: t r ansact ion 75 an0 d secur it y, in m or e det ail in subsequent chapt er s. We conclud e t his chap t er by int r odu cin g y ou t o a samp le business app licat ion, and w e apply t h e k now led ge learn ed in t his chap t er t o m od el t his app licat ion as a set of EJBs. We pr ov ides some gener al design guid elines and b est pr act ices t hat can h elp y ou t o m ak e appr op riat e choices in designing y our ow n EJBs. Th is applicat ion also pr ovid es t he f oundat ion f or t he det ail w ork t hat is discussed lat er chap t ers. Lear ninabout t he EJB 2 .0 specif icat ion And f in ally , w eEJBs r ecognize at mApp anylicat of ion you Dev ar e eloper alr ead y fam iliar w it h EJB 1 .x t echn olog y, and ar e Dev elop w it h Stt uh dio look in g f or an int r odu ct ion t o EJB 2. 0. Sp ecial new EJB 2 .0 f eat ur es will b e high lig ht ed t hr ou Deploy ghout t hEJBs e ch apt ereb inSp a her shad ed box as owvnerb elow . W e felt t h at t h e best w ay t o int r odu ce to W e Applicat ionshSer y ou t o t h ese new f eat ur es w as t o d iscuss t h em in t h e cont ext of t he t opic t hat w as being This pr esent I BM edRedbook , as opp osed pr ovides t o just d etdum ailedpin ingfor t hmem at ion on on youh all ow at t o once. ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly * Ne ingwr elat EJB ional 2 .0 dat ab ase. I n Part A n1, ewwe EJB in t2. r oduce 0 feat ur EJBs e. To as haelp p art y ou of uJav nder a 2st and En t ert he p rise n ew Edfeat it ionur(es J2 EE) of t he andEJB cover t he basic concep specif t s and icattion h e ,arand ch itwect hich ur e.I BM I n par t echt icu nologies lar w e m point ay hav oute tchang he n ew ed fut onct supp ionsort inttrhem od uced . w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
EJB roles The EJB specificat ion d ef ines six roles f or EJB developm ent and deploy ment . Each role h as dif fer en t r esp onsibilit ies in t he EJB d ev elop m en t p rocess, so we in t r oduce t h em here fir st . •
Table of Content s
•
I ndex
Bean provider EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
The b ean p rov ider is t h e developer of t h e cod e, and is r espon sib le for conv ert in g t he bu sin ess r eq uir em ent s in t o act ual phy sical cod e. He or she m ust pr ovid e t he n ecessar y classes t hat Pubitlish I BM const ut eer:an EJB, as w ell as pr ov iding t he d ep loym ent file descr ibin g t h e r unt im e set t ings for t h e Pub Dat e: April 1pr 1, ov 20 03 bean. The bean id er ' s fin al pr odu ct is an EJB JAR f ile. I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Application assembler This m ay b e t he sam e as t h e bean pr ovid er , or a sen ior t eam lead . The applicat ion assem b ler 's r espon sib ilit y is t o pack age all t he dif fer en t EJB com ponent s, alon g w it h any ot her applicat ion com ponent s, t h e fin al pr odu ct being an en t er p rise app licat ion EAR file. Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
Deployer
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This p er son is responsible f or deploy ing t he EJB ap plicat ion on t he t arg et r unt im e env ir onm en t . This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio The d ep loyer shou ld b e fam iliar w it h all asp ect s of t h e env ir onm ent , inclu ding t r ansact ion an d App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans secur it y sup por t . ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ingprovider Server r elat ional dat ab ase. IThe n Part t r oduce EJBs of Javaar 2unEn t er riseserv Ed iter ionenv ( J2iron EE)ment and cover basic j ob 1, of we t heinserv er pr ov iderasisat op art pr ovide t im e pEJB t hat ist he com pliant concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w w it h t he EJB specif icat ion . I BM is a ser v er pr ovid er of t he WebSph er e Ap plicat ion Ser ver .it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
Container provider
I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, m essag e- derr ivpr enovider beansisusing WebSph ernt e im St ue dio plicat ion env Deviron elopment er . We also The j oban ofdt he cont ain t o pr ov ide a ru EJBAp con t ainer t hat is im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At com pliant w it h t h e EJB sp ecificat ion, an d is generally t h e sam e as t he serv er pr ov ider. The tser heven w econt d escrib how t oat deploy ap tplicat WebSph App licat er d, and ainere cooper e t o p rEJB ovide he quion alitsy-t oofa- ser v ice r uernteim e en virion onmServ ent er. for t he EJBs.
Systems administrator The sy st em adm inist rat or' s t ask is t o en su re t hat t he r un t ime en vir onm ent is con figu red in su ch a w ay t hat t he app licat ion can f un ct ion cor rect ly, and is int egr at ed w it h all of t h e r eq uired ext er nal com pon en t s.
Main EJB framework components Ther e ar e six main fr am ew ork com pon en t s of EJB t ech nology ( Figur e 2- 1 ) :
Figu r e 2 - 1 . M ai n EJB f r am e w or k com pone n t s
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er EJB se rv e r— Pr ov ides t he act u al pr im ar y ser v ices t o all EJBs. An EJB ser v er m ay h ost one This Ior BMmRedbook pr ovides d etI tailed in for m at ion on ically h ow tan o efEnt fect here St udio or e EJB cont ainer s. is also called gener erively pr iseuse JavWebSp a Serv er ( EJS) . App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans WebSph er e Ap plicat ion Serv er is an EJS. ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. EJB con t ai ne r—Pr ovid es t he r unt im e env ir onm en t f or t h e en t erp rise bean inst ances, and Thr oug book , wyebet pr ov id e exam ples based ont a bank ing app licat ion w it h an is hout t he int he t erm ediar w een t h e EJB com pon en ansimp d t heleser v er. un derly ing r elat ional dat ab ase. EJB com pon en t — Th ese r epr esent t he act u al EJBs t hemselves. Ther e ar e t hr ee t y pes of I n Part in tbr oduce EJBs a p art of 2 En teer dp riv rise it ions.( J2 EE) and cover t he basic ent1,erwe pr ise ean s: ent it yas , session, anJav d ma essag enEd b ean concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2.EJB 0 spint ecifer icat ions tahat ov idbea e, for ple, hanced fun ct ion alit y ( for er - mEJB anag ed ) fa ce nd pr EJB n—ex Tham e int er en faces for client access EJBcont h omain e and object per sist ence it y bean b ean sclass. and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or and t h eent EJB successful imp lem ent at ions of EJBs. EJB de ploy m en t d escrip t or— Th e descr ipt or defines t h e r unt im e qu alit y of ser v ice I n Part t h ewsam ing applicat t hen lem en tsu it ychbeans, set2, t inwe gs in f ort r oduce t he bean h enpitle isbank deployed. Manyion of and t h e set t in imp gs of anent EJB, as t hesession beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also t r ansact ional set t ings, ar e d ef ined in t he deploym ent descr ipt or . Th e deploy m en t descript or im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he is an ext er nal ent it y fr om t h e bean, an d t heref ore d ecoup les t he bean comp onent it EJBs. self At t he enfrd, d escrib e ehow deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. omw et he r un t im chart oact er ist ics. EJB cl ie nt — A client t hat accesses EJBs.
EJB server The EJB serv er is t h at par t of t he ap plicat ion serv er t hat host s EJB cont ain er s. This is som et imes r ef err ed t o as an Ent erp rise Jav a Ser v er ( EJS) , an d it is par t of t he app licat ion ser v er t hat host s t he EJB cont ain er s. Con t ainer s are t r anspar en t t o t he client —t h er e is no client API t o m anipulat e t he con t ainer, and t her e is no w ay for a client t o t ell in w hich con t ainer an en t er p rise bean is
deploy ed. The EJB serv er pr ov ides t he im plem en t at ion for t h e comm on ser v ices t o all EJBs. Th e EJB ser v er' s r espon sib ilit y is t o hid e t he com plexit ies of t hese ser v ices f r om t he ap plicat ion s r eq uir in g t hem , t h er eby ob t aining access t o t he ser v ice on b eh alf of t he EJB, but hidin g it fr om t h e EJB. The EJB specificat ion ou t lines sev en serv ices t hat mu st be pr ovided by an EJB ser v er: • •
Nam ingTable of Content s I ndex
EJB 2 . 0TrDansact e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper ion By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Secu rit y
PubPersist lish er: Ience BM Pub Dat e: April 1 1, 20 03
Con cu rr ency I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lif e cycle Messag in g Bear in mind t h at t h e EJB con t ainer and t h e EJB ser ver ar e n ot clear ly separ at ed const r uct s. EJBs do not int eract dir ect ly w it h t he EJB serv er , but r at her , do so t hr ou gh t he EJB cont ainer , w hich Lear n about .0 specif ionb y t he EJB serv er. So, fr om t he EJB' s p er spect iv e, it act s as a br oker t ot he t heEJB ser2vices pr ovicat id ed app ear s t h at t h e EJB con t ainer is in fact p r ovidin g t h ose ser v ices. Addit ionally , t h e sp ecificat ion elopa EJBs it h tSt u diocont Apprlicat iond Dev eloper only dDev ef ines b eanw - con ainer act an d oes not d ef ine a con t ainer- ser ver cont r act , so det er min in g w h o act ually does w h at is a lit t le amb iguou s. The cont ainer d oes n ot act u ally Deploy EJBs eb; Sp Applicat ion Seres v erwit h t h e EJB ser ver t hat act u ally im plem en t s pr ov ide t h e ser v icet oitW self r ather h ere it com mu nicat t he ser vice, bu t h ow t his is act u ally im plem en t ed is u p t he ind iv idu al EJB vend ors. Th er efor e, f or This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio t he sak e of simp licit y , t h e sev en serv ices defined abov e w ill sim ply b e r ef er r ed t o gener ically as App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans EJB ser v ices. ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. EJB container I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic The EJB cont ain er is a syst em t h at f unct ions as a r un t ime en vir onm ent for ent er pr ise beans b y concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he m anagin g an d apply ing t he p rim ar y ser v ices t h at ar e needed for b ean m anag em ent at r un t ime. EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed I n addit ion t o b ein g an int er m ed iary t o t he sev en serv ices ab ove pr ovid ed by t he EJB ser v er , t he per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or EJB cont ainer w ill also pr ov ide f or EJB in st ance life- cy cle m anagem en t , an d EJB inst ance successful imp lem ent at ions of EJBs. ident if icat ion. EJB con t ainers cr eat e bean inst ances, m an age pools of in st an ces, and d est r oy t hem . Th e con t ainer pr ov ides t he serv ice lev el specif ied at deploy ment t im e in t he deploy m en t I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session descr ipt or . For ex am ple, it w ill st ar t a t r ansact ion or check for secur it y policies. beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At All t h e ser v ices ar e w ell defined w it hin t he EJB fr am ew ork . Th e fr am ewor k is im plem en t ed t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. t hr ou gh t he b ean callb ack met h ods. These m et h ods ar e pu r ely f or syst em m anag em ent pur poses and t h ey ar e called on ly by t he con t ainer w hen it is int er act in g wit h t h e deploy ed beans, an d are inv ok ed w hen t he ap pr opr iat e life- cycle event occu rs. Th e specif icat ion is clear on w hat act ions t r igger w hat event s, and su bseq uent ly w hat callb ack met h ods ar e inv oked, and t his p rov ides t h e bean developer w it h a p redict able r u nt im e m odel. All t he in t er act ion t akes p lace in a w ay t r anspar ent t o t h e bean developer and also t o t he clien t .
Remote accessibility Ult im at ely , EJBs ar e ( u su ally ) rem ot e, dist r ibut ed obj ect s, and t h e cont ainer 's p rim ar y
r espon sib ilit y is t o pr ov ide t h e m ean s t o access t hese r em ot e obj ect s. Rem ot e accessibilit y enables r em ot e in vocat ion of a n at ive com pon en t b y con ver t ing it in t o a net w ork com pon en t . EJB cont ainer s use t h e Jav a RMI int erf aces t o specif y r em ot e accessibilit y t o client s of t h e EJBs.
Primary services •The r esponsibilit Tableies of Content t hat ans EJB cont ain er m u st sat isf y can b e defin ed in t er m s of t h e pr im ary •ser v ices. Specific I ndex EJB cont ainer r esp onsibilit ies ar e as f ollows: EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
N a m ing — A client can in vok e an ent erp r ise b ean by look ing up t h e nam e of t he bean in a cen t r alized nam ing space called a nam ing serv ice, and t his is accessed v ia t h e Jav a Nam ing Puband lish er: DirI BM ect or y I nt er f ace ( JND I ) API . The con t ainer is r espon sible f or r egist er ing t he ( uniq ue) Pub Dat e: 1 1,e 20 look upApril nam in03t he JNDI nam espace w h en t he ser ver st ar t s u p, an d b inding t h e app I SBN: rop0-riat 73 84e 26 object 09 -1 t yp e int o t he JNDI nam espace. Pages: 75 0
Tra nsa ct i on— A t r ansact ion is d ef ined as a set of t ask s t hat m ust ex ecut e t oget her ; eit her all m ust wor k or all m ust b e und one. The EJB cont ain er handles t h e t r ansact ion op er at ions, as w ell as t h e coor d in at ion act iv it ies b et ween t he t r ansact ions of t h e beans. Th e EJBs t r ansact ion b eh avior is descr ibed in t he deploym ent descr ipt or , an d t his is also r ef err ed t o as cont ain er - m anaged t r ansact ions ( CMT) . Becau se t he cont ainer m an ages t he t r ansact ions, app licat ions can be w r it t en w it h out ex plicit t r ansact ion demar cat ion. Lear n about t he EJB 2 .0 specif icat ion Se curi t y— Th e EJB cont ainer pr ov ides a secu r it y dom ain f or ent er pr ise b ean s. The Dev elop EJBs w it h St u dio App licat ion Dev eloper cont ainer is r esp onsible for en for cin g t he secu rit y policies defin ed at deploy ment t im e w henev EJBs er t here a Sp m et hod call, t hrion ough cont r ol list s ( ACL) . An ACL is a list of Deploy t o Wiseb her e Applicat Seraccess v er users, t he gr oup s t hey b elong t o, and t heir r ight s, an d it ensur es t hat user s access on ly hose r esou r ces and p er f orm t hose t askon s for t h eyively h aveuse been giv en p erSt m udio ission. This ItBM Redbook pr ovides d et ailedonly in for m at ion h oww thich o ef fect WebSp here App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans erch siitstect ence cont ainer r esp aging t here epersist ence a bean ( EJB) Par u re,—anTh d ed ep loym entisofalso su ch ap onsible plicat ionfor s t oman a WebSph App licat ionofServ er. ( all st or age and r et r iev al of dat a) by sy nchr onizing t h e st at e of t he b ean inst an ce in m emor y w it h t he r esp ect,ivweer ecor t h e dat a sour ce. on Concur rent access t helicat d ation a f rom ult iple Thr oug hout t he book pr ovdid in e exam ples based a simp le bank ingt oapp w it hman client s is m anaged t hr oug h t h e concur r en cy an d t ran sact ion ser v ices ( ent it y beans only ). un derly ing r elat ional dat ab ase. Concurr ent rcy —Con cu rr ency def ed aas2 access y t wEd o itorionmor clien t s tcover o t he sam e bean, I n Part 1, we in oduce EJBs as a pisart ofinJav En t er pbrise ( J2eEE) and t he basic and t h e con t ainer m anages concur r ency accor ding t o t he r u les of t h e bean t yp e. concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he ion ally f or t m essageen ex beans, con rr ency fun is defin m anag t he rocessing EJB 2.Add 0 spit ecif icat,ion hat pr ov iddr e,ivfor am ple, encuhanced ct ioned alitas y for conting ain er - mpanag ed of t he sam e m essage. per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. Li fe cy cl e— Th e cont ainer is r esp onsible for con t r olling t h e lif e cy cle of t h e deploy ed com As EJB st ar t g iving ingapplicat r eq uestion s t oand t het hen cont ainer , t he ainer dy nam ically I n Part 2, ponent we in t rs.oduce t h eclien samt sp le bank imp lem entcon en tt it y beans, session inst iatessag es, dest d r euses b ean seras app r oprAp iatplicat e. The ic lifecle also beans, anant dm e- d rr oys, iv en an beans usingt he WebSph e St u dio ionspecif Dev elop er .cyWe m anagem entmtet hat t he cont er f orm d egies, ep en dent h eclient t y pesof im plem ent fin der h ods, dif ferainer en t mp app in g sstisr at andupon sim ptle t hbean. at u se The t he EJBs. At m ay eu how lt im at pr ov ide e ion r esour ut iliz at ion t imizat ander.em ploy t he encont d, wainer e d escrib t oely deploy EJBfor apsom plicat s t o ce a WebSph er eopApp licat ion ion s,Serv t echniqu es for b ean inst ance p oolin g. Bean st at e- m anag em ent is a t y pe of lif e- cy cle m anagem ent , w h er e act ivat in g and passiv at ing of beans based on usage can be achiev ed . These t ech niqu es b ecom e par t of t he b ean life cy cle t hat t he cont ain er m u st m anage. Me ssa gi ng— Th e cont ainer m ust pr ovid e for asy nchr onou s m essaging . Specif ically , it m u st pr ov ide f or t he r eliable r out ing of m essages f rom JMS clien t s t o message- dr iven beans.
* Ne w EJB 2 .0 - m e ssa gin g se r vi ce
Messag in g is new in t he EJB 2. 0 specif icat ion . I t essent ially r equir es con t ainer pr ov iders t o pr ov ide t w o t hing s: Sup por t of m essage- d riv en EJBs Sup por t f or int er nal m essag in g ser vice •
Tablelian of Content To b e comp t w it h sEJB 2. 0, Web Spher e App licat ion Ser v er Ver sion 5 n ow pr ov ides bot h of tI hndex ese. The in t er n ally em bedded JMS messaging t hat is pr ov ided w it h t he EJB 2 .app 0 D elicat ve lopm h WisebSphe r e Staudio Appl ica ti on Deplement v el oper at ion of I BM Web Spher e MQ. ione nt serwviter act u ally light weigh t im Giv en t h at t h e m essaging ser v ice is int ended t o in onWeiss ly pr ovid e an in t ern al cont ainer - lev el By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart com mu nicat ion b us, it is n ot r equir ed t o be a f ully com p lian t m essagin g m idd lew ar e im plem ent at ion . How ev er , client s wishing t o int egr at e t h is messaging sch em a wit h in Pub lish er: I BM t heir ent er pr ise will f ind t hat t hey n eed mor e r obu st f unct ion, su ch as p rov ided by Pub Dat e: April 1 1, 20 03 WebSph er e MQ. •
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
EJB component (the actual EJB) The EJB com ponent s r ef er t o t h e beans t h em selves, and include all t he classes, int er faces, and const rLear uct sn t about hat m ak e up er pricat ise ion bean. Th er e ar e t h ree t y pes of ent er pr ise beans: ent it y , t he EJBt he 2 .0ent specif session, an d m essag e- d riv en b ean s. The r ules f or bean con st r u ct ion ( w hich int erf aces t o use, classes t oelop ex t end, t h )App ar elicat govion ern Dev ed beloper y t he t y pe of bean, so a qu ick int rod uct ion t o t he Dev EJBs and w it hsoStfor u dio t y pes of ent er pr ise b ean s is pr esent ed her e. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio EJBI types App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Ther e ar e t hr ee t y pes of EJBs ( Figur e 2- 2 ) : Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat Figu ab ase. r e 2 - 2 . Cl assi f ica t i on of EJB t y pe s I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Ent i t y be a ns— Ent it y b ean s ar e m odeled t o repr esent bu sin ess or d om ain - sp ecif ic concep t s, and ar e t y pically t he nou ns of y our sy st em , such as cust om ers an d accoun t s. They usu ally r epr esent dat a ( ent it ies) st ored in a dat ab ase. Se ssi on b ea ns— A session b ean is m odeled t o r ep resent a t ask or w or k flow of a sy st em, and pr ov ide coor dinat ion of t hose act iv it ies b et w een b ean s, su ch as a ban king serv ice, allow in g for a t r ansfer bet w een account s.
Me ssa ge - d ri ve n be ans— Like session beans, m essag e- d riv en b ean s ( MDB) m ay also be m odeled t o repr esen t t ask s. How ev er, t hey ar e inv oked by t h e r eceipt of asy nchr onou s m essages. The b ean eit her list en s for or su bscribes t o m essages t h at it is t o r eceiv e.
• •
* Ne w Table EJBof2Content .0 - m e ssa ge- dr i ve n be an s s I ndex Messag ed riv en b ean s ar e new in EJB 2. 0. I BM pr ov id es full su ppor t for MDBs in of t he WebSp here St udio UeliApp Wahlicat li, Wout ioner Developer Denay er, Lar sVer Schu sion nk , Deborah 5 developm Shaddon ent , Mart eninvir Weiss onm ent .
EJB 2 .WebSph 0 D e ve lopm e nt it h W ebSphe r e St Appl ica oper er e Apwplicat ion Serv erudio Version 5,ti on anDe d vaelpar t By
I n Web Spher e Applicat ion Ser v er Ver sion 4 , m essage- dr iv en beans wer e not av ailab le. How ev er , sim ilar f unct ionalit y w as p rov ided in t h e ext end ed m essagin g ser vice supp ort Pub Dat e: April 1 1, 20 03 for m essage beans, w hich w as av ailab le in t h e Ent er pr ise Edit ion app licat ion ser v er I SBN: 0- 73 84- 26 09 -1 env iron ment , and t he Ap plicat ion D ev elop er I n t egr at ion Edit ion developm ent Pages: 0 env iron75ment . Alt houg h t his supp ort pr ov ided t he capabilit ies for asy nchr onou s m essage list eners, it w as not an EJB 2 . 0 com pliant imp lem ent at ion.
Pub lish er: I BM
Synchronous versus asynchronous invocation Lear n about t he EJB 2 .0 specif icat ion
En t it yDev andelop session accessed roneloper ously t hr oug h a r em ot e or local EJB in t er f ace EJBs bwean it h sStaru edio App licatsyn ion ch Dev m et hod inv ocat ion . This is r efer red t o as sy nchr onou s inv ocat ion, because t her e is a requ est , an d a ( block ing ) w ait f tor t heb e rSp et uher rn .e Client s ofion EJBs Deploy EJBs oW Applicat Serinv v eroke met hod s on session an d ent it y beans. An EJB client m ay be an ext er nal con st ru ct like a ser v let ( r em ot e) or anot h er EJB w it hin t h e sam e This I( BM JVM local) Redbook . pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans The essag e- dur re, iv enanbean n ot ent accessible hr ou gh ion a r emot or local er inteerApp face. The ay for ( EJB)m ar ch it ect d d episloym of su ch t ap plicat s t o ae WebSph licat iononly Servwer. an EJB client t o com m unicat e w it h a m essage- dr iv en b ean is by send in g a JMS m essage. This is Thr ex an oug am hout ple tof heasy book nchr , w onous e pr ovcom id e m exam un icat ples ion.based The clien on at simp doeslen ot bank inving ok eapp t helicat metion hodw on it h tan h e bean un derly dir ect ly ,ing b utr elat r at hional er , u dat sesab JMS ase. const r uct s t o send a message. The cont ain er delegat es t he m essage t o a su it ab le m essage- dr iven bean inst ance t o h andle t he inv ocat ion. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed EJB interfaces and EJB bean per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. An EJB com pon en t consist s of t he follow in g pr im ar y elem ent s, d ep en ding on t he t y pe of bean: I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also EJB ponmen i nt e rfa ce— sedinby anr at EJB client t o sim gain ies ofAt im plem entcom fin der etth ods, dif fer en tI smuapp g st egies, and p leaccess client st ot ht hate ucapabilit se t he EJBs. t he bean. Th is is wh er e t he bu sin ess m et hods are d ef ined. Th e comp onent in t erf ace t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. is called t he EJB obj e ct . .
* Ne w EJB 2 .0 - com pone nt in t e r f ace I n EJB 1 . x, t he com ponent int er face w as called t he r em ot e int er face. This is because pr ior v er sions of EJBs suppor t ed only t h e not ion of r em ot e clien t accessib ilit y . New in EJB 2. 0 is t he idea of local accessibilit y of ent er pr ise b ean s. Ther e ar e t w o t y pes of com ponent int er faces, local or r em ot e.
EJB hom e in t er fa ce — I s u sed by an EJB client t o gain access t o t h e bean. Cont ains t he bean lif e- cy cle m et h ods of cr eat e, f ind, or r em ov e. The h ome in t erf ace is called t he EJB hom e .
• •
EJB be an cla ss— Cont ains all of t he act ual bean business logic. I s t he class t hat pr ov id es t he business logic im plem ent at ion . Met hod s in t h is bean class associat e t o m et hods in t h e com ponent and hom e int er faces. The b ean class is descr ibed in " EJB bean class" on p age Table of Content s 50 . I ndex
EJB 2 . 0En D teitve e nt also w it h W ebSphe r e im St udio De v oper r ep resen t s y lopm beans h ave a pr a ry Appl k e yicacltiaonss, welhich By
a uniqu e ent it y in a dat ab ase ( w e int r oduce t h e k ey class in Ch apt er 3, " Ent it y b ean s" on page 7 3) . Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
A b ean d ev elop er oft en develops t he classes and int er faces in t he abov e or der . Ther efor e, w e lish er: I BM havPub e chosen t o int r odu ce t hese t opics in t his or der. The bean pr ov ider is r espon sib le for b uilding Pub Dat e: ent April 03 dir ect ly or t hr oug h t h e use of dev elop ment t ools such as WebSph er e t hese elem s,1 1, eit20 her I SBN: 073 8426 09 -1 St ud io App licat ion Developer . Pages: 75 0
Ter m i nol ogy We u se t he t er ms EJB ob ject an d EJBObject ( and EJB hom e and EJBHome) Lear n about t he EJB 2 .0 specif icat ion int er ch angeably . EJBObject is a r eal Jav a int er face t h at is def in ed b y t he EJB specif icat EJBs ion and at uisdio exApp t en ded defined EJBs. Behind t he in t erf ace is an Dev elop w it thh St licat by ionuser Dev-eloper act ual class t hat im plem ent s t he int er face. This class is gener at ed b y t he cont ain er wDeploy hen you gener at eb e tSp h eher deploy ed code. EJBs to W e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (EJB EJB) component ar ch it ect u re, interface: an d d ep loym ent object of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. EJB Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an A pon t intional er facedat isab u sed uncom derly ingenr elat ase.by t h e client of t he ent er pr ise bean t o g ain access t o t he capabilit ies of t he b ean . I t d ef ines t he b usiness m et hod s t hat ar e v isible t o t he client . Th e bu ess1,mwe et hods of t heEJBs com as ponent erJav facea m h ave corEd r esp onding plement in t he I n sin Part in t r oduce a p artintof 2 ust En t er p rise it ion ( J2 EE)imand coverattion he sbasic bean class, alt hou gh t he b ean class never im plem en t s t he com pon en t int er face d ir ect ly ( t e concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w ither h t he ar e a lot of r easons f or t h is, b ut ju st at t r ibut e it t o one of t h ose t hings about t he EJB EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed specif ionent ) . Ait ymbessag d rivmen b ean ddroes h ave We a com ponent face, sinice ce itguidelines is not per sisticat ence ean s eand essageiv enn ot beans. also pr ovidint e er best pr act f or inv ok ed dir ect ly b y a clien t . successful imp lem ent at ions of EJBs. EJBs are2,d we ist r ibu ed ob ject An pEJB cliening t n ev er inv ion okesand an tinst of an b eansession class I n Part in t rtoduce t h es.sam le bank applicat henance imp lem entent enert itpry ise beans, dir ect ly an . Admet h od in ionbeans is int ercept ed by t h er e EJB t hationprDev ov ides he. ser beans, m essag e-vocat d r iv en using WebSph e St ucon diot ainer Ap plicat elopt er Wevices also t o delegat the et hm odetex ecu tdif ionfer t oent he pr in opr er prand ise bsim eanp le inst an ces at t heu se rig tht e. An im plem e ent finmder h ods, t map app g iat st reatent egies, client t h at het im EJBs. At is t heerhow eforteo adeploy passivEJB e com by t h eerEJB conlicat t ainer. tent he er enprd,ise w eb ean d escrib apponent plicat ionmsanaged t o a WebSph e App ion Serv er. The con cept of t he m et hod int er cep t or sim p lif ies t he com ponent dev elopm ent : an EJB d ev eloper can con cent r at e on w r it ing business logic. The ar t if act t hat enables t he decou pling of t he EJB client w it h t he ent er pr ise bean class is called t he EJBObject . EJBObjects ar e p art of t he EJB cont ainer an d ar e g en er at ed fr om t h e con t ainer t ools du r in g deploym ent . The EJBObject is t he act ual ob ject t h at im plem en t s t he com pon en t int er face. Ther e ar e t w o t y pes of com ponent int er faces: a r em ot e comp onent ( EJBObject) and a local com ponent ( EJBLocalObject) in t erf ace. A par t icular user - defined com p onent in t er f ace may only im plem ent one t y pe, eit her local or r em ot e, alt houg h it is possible t o h ave one of each t y pe per EJB ( Figur e 2- 3 ) .
Figu r e 2 - 3 . Com pon en t i nt er f a ce in he r it an ce
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Local versus remote interfaces
.0 specif ion *Lear Nenwabout EJBt he 2 .0EJB- 2l oca l v eicat r su s r em ot e Dev id elop w itand h St u diootApp licat ion Dev The ea EJBs of local r em e int er faces f oreloper t he h om e and comp onent in t erf aces is n ew t o EJB 2 . 0. Wh ich one t o use is inf luenced b y how t he bean it self is t o be used by t h e Deploy t o W eb Sp her e local Applicat Ser ev er client of EJBs t he bean, because andion r emot depict t h e client ' s v iew of t he bean. An EJB client m ay be an ext er nal ( rem ot e) clien t , such as a ser vlet r un ning on anot h er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio m achine, or m ay be an in t er nal ( local) client , su ch as an ot her EJB. App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Rem ot e int er faces hav e alw ays ex ist ed in EJBs, and p rov ide t h e conv en t ions f or accessin g un derly ing r elat ional dat ab ase. dist r ibut ed obj ect s t h at ar e u sed by EJB clien t s t hat ar e out side of t he cont ainer or JVM. I n r em ot e in vocat ion, met h od ar gu m en t s and r et ur n v alues ar e essent ially passed v ia p ass- by I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic v alue, w here a com plet e cop y of t he ob ject is m ade and ser ialized befor e b eing sent over t h e concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he net w or k t o t he r emot e ser vice. Bot h t he ob ject serializat ion and net w or k over head can b e a EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed cost ly pr oposit ion , ult im at ely r ed ucin g t he r esp onse t im e for t he r equest . How ev er , w it h r em ot e per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or int er faces, locat ion independ en ce can be ach iev ed b ecause t h e same call can occu r w het her t h is successful imp lem ent at ions of EJBs. client is inside or ou t sid e of t he con t ainer. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session Local in t er f aces, w hich are n ew t o EJB 2 .0 , pr ov ide a way for b ean s inside t he sam e JVM t o beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also int er act w it h each ot her locally . I n local inv ocat ion , m et hod arg um ent s ar e passed b y r ef er ence, im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At and t h e ex ecu t ion is done w it h in t h e same JVM, so n o ser ializat ion or net w ork over head is t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. assessed . How ev er, w it h local int er faces, t her e is now locat ion depend en ce, b ecause t h e t y pe of int er face used will on ly w ork fr om clien t s t hat execut e w it hin t he sam e JVM. Ch oosing w hich one t o pr ov ide for you r beans w ill d ep en d on h ow t he beans ar e t o be used. I f ent it y b ean s ar e only t o be called fr om ot her session b ean s and not dir ect ly f r om ext er nal clien t s ( as r ecom m ended) , t hen it m ight m ake sense t o pr ov id e only local int er faces for you r ent it y beans. I f session facade b ean s are called only by ex t er n al client s, t hen it m ig ht mak e sense t o pr ov ide r emot e- on ly in t er f aces f or t hese beans. You m ay pr ov ide eit her a local or rem ot e int er face, or bot h, f or ent it y and session beans. Alt hou gh t his is a new f eat u re of t he EJB 2. 0 specif icat ion, and t her ef or e pr ov id ed in WebSph er e App licat ion Ser v er Ver sion 5, pr ev iou s v er sions of WebSph er e Ap plicat ion Ser ver ( V4 .0 / V3. 5)
hav e alr ead y pr ov ided sim ilar local f un ct ion alit y ( sim ilar, but not t he sam e) . By enabling t h e NoLocalCopies set t ing f or t he ser ver , WebSph er e p rov ided sim ilar pass- by - r ef er ence fu nct ionalit y as a sp ecif ic feat ur e. We m ust ch oose w hich t y pe of com pon en t int er face t o ext end f or our ow n ent er pr ise b ean com ponent int erf ace, eit h er EJBLocalObject or EJBObject. Rem ot e int er f ace m et hod s m ust t hr ow a RemoteException, an d op t ionally any ap plicat ion except ions. Local int er face m et hods m ay only t h row opt ional applicat ion ex cep t ions. Bot h can t h r ow an EJB ex cep t ion ( r u nt im e • Table of Content s except ion) . •
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Inherited component interface methods
Pub lish er: I BM Alt hou gh w e do not cov er t hem in det ail in t his int r od uct ory discu ssion, t he f ollow ing ar e t he Datav e: April 1 1, 20 m etPub hods ailable t o03us f rom t he sup er class, d ep en ding on w hich in t er f ace t yp e w e choose. We I SBN: 073 8426 w ill see lat er t h at t h09 e -1 exist ence of t hese m et h ods h elps our EJB client s t o g ain im por t ant inf ormPages: at ion 75 abou 0 t ou r en t erp rise bean, and t o op t ionally cont r ol t h e bean' s lif e cycle, v ia t he com ponent int erf ace. The cont ainer g en er at es t h e class t h at im plem en t s t hese m et h ods w hen t he bean is d ep loyed, but w hat is im p ort an t t o not e now is t h at t h ey are sim ply m et hods av ailab le t o ou r client .
The in t er f aces ar e g en er ic and int end ed t o pr ov id e a st an dar d com pon en t int er face m odel f or bot h ent it yn an d session beans. How ev erion , n ot all m et hod s m ay be av ailable t o bot h t y pes. For Lear about t he EJB 2 .0 specif icat exam ple, ev en t h ough av ailab le, t he getPrimaryKey m et hod s ar e applicable t o en t it y beans only . Dev At t em pt sEJBs t o call m etApp hods onion session beans w ill r esu lt in ex cep t ions. Figur e 2- 4 sh ows elop w itthhese St u dio licat Dev eloper t he rem ot e int er face m et hods and Figur e 2- 5 sh ows t he local int er face m et hods. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Figu r e 2 - 4 . Re m ot e in t e r fa ce m e t h ods ( j av a x.ej b.EJBO bje ct )
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. public interface javax.ejb.EJBObject extends java.rmiRemote { Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. public EJBHome getEJBHome() throws java.rmi.RemoteException; I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic public getPrimaryKey() java.rmi.RemoteException; concep t s andObject t h e ar ch it ect ur e. I n par t icuthrows lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed public void RemoteException, java.rmi.RemoveException; per sist ence ent it y remove() b ean s and throws m essagedr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. public Handle getHandle() throws java.rmi.RemoteException; I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session public boolean beans, an d m essag e- disIdentical(EJBObject r iv en beans using WebSphejbobject) er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At java.rmi.RemoteException; t he en d,throws w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. }
Figu r e 2 - 5 . Loca l i nt er f a ce m et hods ( ja v ax .e jb .EJBLocal O bje ct )
public interface javax.ejb.EJBLocalObject {
public EJBLocalHome getEJBLocalHome() throws EJBException; public Object getPrimaryKey() throws EJBException; public void remove() throws RemoveException, EJBException; public boolean isIdentical(EJBLocalObject ejblocalobject) • •
Table of Content s
throws EJBException;} I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Constructing the component interface: business methods Pub lish er: I BM
Pub Dat e: April 1 1, 20 03
As a bean p r ovider , w e m ust define t h e bu siness m et hods of ou r beans in ou r com p onent I SBN: 0- 73 84- 26 09 -1 int er face. Becau se it is an int erf ace, we are only defin in g t he m et hod. The bean class m ust Pages: 0 ual im plement at ion of t h ese m et hod s w it h t h e exact met h od sign at ur e as def in ed pr ov ide t h e 75 act in t his int er face ( ex cept for EJB and rem ot e ex cept ion s) . We w ill see lat er t h at ev en t h ough t he m et hods in t h e com p onent in t er f ace mu st corr elat e t o met h ods in t he b ean class, t h e bean class does n ot act ually im plement t his in t er f ace. Figur e 2- 6 is an ex amp le of a r emot e com p onent in t er f ace definit ion of business m et hods f or a fict it ious om ert he entEJB er pr2ise ean , icat andion Figur e 2- 7 is t he sam e ex am ple f or a local com pon en t Learcu n st about .0 bspecif int er face. Dev elop EJBs w it h St u dio App licat ion Dev eloper
Figu r e 2 - 6 . Re m ot e in t e r fa ce of cust om e r e nt i t y be a n Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans public interface Customer { ( EJB) ar ch it ect u re, an d d ep loymextends ent of su javax.ejb.EJBObject ch ap plicat ion s t o a WebSph er e App licat ion Serv er. public getCustomerId() throws java.rmi.RemoteException; Thr oug hout int t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. public String getName() throws java.rmi.RemoteException; I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic public setName(String concep t s andvoid t h e ar ch it ect ur e. I n parnewName) t icu lar w e throws point outjava.rmi.RemoteException; t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed } per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Figu r e 2 - 7 . Loca l i nt er f a ce of cu st om er en t it y be an im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. public interface Customer extends javax.ejb.EJBLocalObject { public int getCustomerId(); public String getName(); public void setName(String newName); }
EJB home interface: EJB home As EJBs are dist rib ut ed ob ject s, a f act ory ser vice is used t o cr eat e and find b ean inst ances. The hom e int er face p rov ides t h is ser vice, and is used by t h e EJB client t o g ain access t o t he b ean . I t defin es t he bean' s life- cycle m et hods, an d it pr ov ides for t he basic life- cy cle m anag em ent •capabilit ies of Table Content s t he ofb ean , such as cr eat e, r em ov e, and fin d. A messag e- dr iv en bean d oes not hav e •a h om e int erfI ndex ace, sin ce it is not inv oked dir ect ly b y a clien t . EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Recall t hat a clien t n ever, erLar act ually d irect access t oin aWeiss bean class inst ance. What t he client of By Ueli Wah li, Wout er Denay s Schu nk ,has Deborah Shaddon , Mart t he bean act u ally h as is an EJBObject, w hich is t he in t er cept or t o t he b ean inst an ce it self. Th is EJBObject is t he obj ect t hat act ually im plem ent s our com ponent int er face, w hich cont ain s our Pub lish er: I BM beans business met h ods. Pub Dat e: April 1 1, 20 03
The EjbHome I SBN: 0- 73 object 84- 26 09is -1 an ob ject t h at im plement s t he h ome in t erf ace. As in EJBObject, it is gener at ed f r75om Pages: 0 t he con t ain er t ools du ring d ep loy m ent , an d in clud es con t ainer- sp ecific code. At st ar t up t im e, t he EJB cont ainer inst an t iat es t he EJBHome ob ject s of t he deployed ent erp r ise beans and regist er s t he h ome in t h e nam ing ser v ice. An EJB client accesses t h e EJBHome ob ject s using JNDI ( Java Nam in g an d Direct or y I nt er face) . Ther e ar e t w o t y pes of h ome in t erf aces: a r em ot e h om e ( EJBHome) and a local hom e ( EJBLocalHome) in t erf ace. A par t icular user - defined hom e int er face m ay only im plem en t on e Lear n about t he EJB 2 .0 specif icat ion t y pe, eit her local or r emot e, alt h ough it is possib le t o hav e on e of each t y pe p er EJB ( Figur e 28) . Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o rWeeb2Sp e Applicat v er Figu - 8her . Di ff e r enion t t Ser yp es
of EJB hom e in t e r f ace s
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also We m ust oose t y pe dif of fer h om intapp erf in ace t or at exegies, t end for r ow n en t er ps rise e im plem entchfin der wmhich et h ods, en te m g st andousim p le client t h atbean u se thom he EJBs. At int er face, eit her EJBLocalHome or EJBHome. An d as in t h e comp onent in t erf ace, r em ot e int er face t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. m et hods m u st t hr ow a RemoteException, an d op t ionally any ap plicat ion except ions. Local int er face m et hods may on ly t hr ow op t ional ap plicat ion except ions. D ep en ding on t he t y pe of hom e int er face m et hod , add it ion al EJB ex cept ions m ay b e t hr ow n, su ch as cr eat e and find except ions.
Inherited home interface methods Alt hou gh w e do not cov er t hem in det ail in t his int r od uct ory discu ssion, t he f ollow ing ar e t he m et hods av ailable t o us f rom t he sup er class, d ep en ding on w hich in t er f ace t yp e w e choose. We w ill see lat er t h at t h e exist ence of t hese m et h ods h elps our EJB client s t o g ain im por t ant
inf orm at ion abou t ou r en t erp rise bean, and t o cont rol t he b ean 's life cy cle t hr oug h t he hom e int er face. The cont ain er generat es t h e class t hat im plem en t s t h ese m et hods wh en t h e bean is deploy ed, bu t w h at 's im por t an t t o not e n ow is t h at t h ey ar e m et hod s w hich ar e av ailable t o our client . The in t er f aces ar e g en er ic and int end ed t o pr ov id e a st an dar d h om e int erf ace m odel for bot h ent it y an d session beans. How ever , not all m et hods may be av ailab le t o b ot h t yp es. For exam ple, ev en t h ough av ailab le, t he remove(Object obj) m et hod is ap plicab le t o ent it y b ean s • Table of Content s only , because t he object p ar amet er is a pr im ar y k ey t hat is applicable t o en t it y beans only. • I ndex At t em pt s t o call t hese m et h ods on session beans w ill r esult in ex cep t ions. Figur e 2- 9 sh ows t he EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper r em ot e hom e int er face m et h ods an d Figur e 2- 1 0 sh ows t he r emot e hom e int er face m et hods. By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Figu r e 2 - 9 . Re m ot e hom e i nt er f ace ( j av ax .ej b.EJB Hom e) Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
public interface javax.ejb.EJBHome extends java.rmi.Remote { Pages: 75 0 public abstract void remove(Handle handle) throws java.rmi.RemoteException, RemoveException; public abstract void obj) Lear n about t he EJB 2 .0remove(Object specif icat ion throws RemoveException; Dev elop EJBsjava.rmi.RemoteException, w it h St u dio App licat ion Dev eloper public EJBMetaData getEJBMetaData() Deploy abstract EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio throws java.rmi.RemoteException; App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an dHomeHandle d ep loym ent getHomeHandle() of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. public abstract Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an throws java.rmi.RemoteException; un derly ing r elat ional dat ab ase. } I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or Figu r e 2imp - 1 0lem . Local h om e i nt er f a ce ( j av a x.ej b.EJBLoca lH om e ) successful ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also public interface im plem ent fin der m etjavax.ejb.EJBLocalHome h ods, dif fer en t m app in g st r{at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. public abstract void remove(Object obj) throws RemoveException, EJBException; }
Constructing the home interface: life-cycle methods As a bean p r ovider , w e m ust define t h e r eq uir ed life- cycle m et hods of our ent er pr ise b ean in our
hom e int er face. Ex act ly w hat t y pe of m et hods w e requ ir e, and t h eir st r uct u re of t he m et hod, is gov ern ed b y t he t y pe of bean an d r equ ir em en t s for how t h e EJB client s need t o locat e t he beans. The f ollow in g ar e t he t y pes of life- cycle m et hods t hat can be ad ded t o a hom e int er face, and som e guidelines on w hen t o use a par t icular t y pe. Cre at e m e t hods— Th e r et ur n v alue in t he hom e is t he bean's com pon en t in t er f ace. Th e Table of Content s s follow t h e conv en t ion of createXxxx, w her e Xxxx is an ar bit r ar y nam es of creat e m et hod • I ndex suf fix or em pt y . Beans m ay h ave zer o or mor e cr eat e m et h ods, depend in g on b ean t y pe. EJB 2 . 0Alt De ve lopm e nt w it h W ebSphe udio ica on ti onin Det vhe el oper hou gh discu ssed in m orr ee Stdet ailAppl lat er book , som e r ules for EJB cr eat e m et h ods e: li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss By Ueli ar Wah •
Pub lish er: -I BM St at eless
session b ean s m ust h ave on ly a single n o- ar gum ent create m et hod .
Pub Dat e: April 1 1, 20 03
at ef I SBN: -0- St 73 8426ul 09 session -1 Pages:
beans mu st hav e at least one createXxxx m et hod ( w it h or w it hou t ar gum ent s) , b ut can hav e as man y dist inct cr eat e m et h ods as r equir ed. 75 0 - Ent it y b ean s m ay hav e zer o or m or e creat e m et hod s. Having n o cr eat e m et hod on an en t it y bean imp lies t h at t he ent it y b ean cannot be cr eat ed b y t he clien t ( on ly fou nd t hr oug h a f in der m et hod) .
The b eh avior of wh at h appens on a cr eat e is also d ep en dent on t h e bean t y pe. For en t it y Lear n about EJB .0 specif iona d at abase. For session beans, it ju st m ean s creat ing beans, cr eat et he imp lies2 an inserticat int o t he bean in st an ce. Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Ne w EJBf or2 t.0 e jb Cr een att eof m h od f er eon nces App licat ion* Developer he -d ev elopm apet plicat iondif s based t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. I n EJB 2 . 0, t h e ejbCreate m et hod s can t ake t he for m of ejbCreateXxxx. Th is allow easier loading w hen ton he apar am le et ers aring e t he b utw it t he Thr oug hout t he sbook , wm e et prhod ov id over e exam ples based simp bank appsam licate,ion h an m et hods act diff erent ly . un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Find er m ar ewonly v alid ent it y bfuean as int t hey in d t he concep t s and t heet hods— ar ch it ectFind ur e.erI nmet parhod t icuslar e point outf or t he n ew ncts, ions r odf uced w it h t he per sist ent dat a in t h e dat ab ase t hat t he ent it y b ean r epr esent s. Each ent it y b ean hom eed EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag int er face m ust hav e a findByPrimaryKey m et hod t h at t ak es t he en t it y' s pr im ary key as an per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or ar gum ent . Addit ional cu st om find er s can be d ef ined, f or exam p le findAllCustomers. Th e successful imp lem ent at ions of EJBs. r et ur n v alue of a fin der is t he b ean 's com pon en t int er face or a collect ion ( repr esen t ing a collect ionin of com pon t intperlefaces) The n am e ion of find met hod follow t he ent ion of I n Part 2, we t r oduce t h en e sam bank .ing applicat andert hen imp slem ent en t it conv y beans, session findXxxx, w her is an arusing bit r arWebSph y suf fix .er e St u dio Ap plicat ion Dev elop er . We also beans, an d m essag e- e d rXxxx iv en beans im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At om e t hode s— e m et hod ar eplicat validion only ent it y beans. Hom e ion m etServ hod er. s ar e t he enHd, w ee m d escrib howHom t o deploy EJBs ap s t of or a WebSph er e App licat bu sin ess logic t h at is n ot specific t o an en t it y bean in st ance, but for ex am ple countCustomers. Th e nam e of a h om e m et h od can follow just about any n amin g conv en t ion, ex cept it m u st not st art w it h cr eat e, find , or r em ove. Hom e m et hods can r et ur n ju st abou t an y t y pe of ser ialized ob ject , and do not act ually generat e bean in st ances. Figur e 2- 1 1 is an ex amp le of a r emot e hom e int er face and Figur e 2- 1 2 is an ex amp le of a local hom e int er face d ef init ion for a fict it ious cust om er ent erp r ise b ean . Not ice t hat t he r emot e int er face m et hods t hr ow a RemoteException, b ut bot h t he local an d r em ot e int er faces t hr ow t h e CreateException an d FinderException, w her e app licable.
Figu r e 2 - 1 1 . Rem ot e h om e in t e r fa ce of cust om er en t i t y be an
public interface CustomerHome extends javax.ejb.EJBHome { public Customer findByPrimaryKey(CustomerKey primaryKey) throws javax.ejb.FinderException, java.rmi.RemoteException; • •
Table of Content s
public java.util.Collection findAllCustomers() I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
throws javax.ejb.FinderException, java.rmi.RemoteException;
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
public Customer create(int customerId, java.lang.String name) Pub lish er: I BM Pub Dat e: April 1javax.ejb.CreateException, 1, 20 03 throws
java.rmi.RemoteException;
I SBN: 0- 73 84- 26 09 -1
public Pages: 75 int 0 countCustomers() throws java.rmi.RemoteException; }
t he EJBh2 om .0 specif icat FiguLear r e 2n -about 1 2 . Local e i nt erion f a ce of cu st om e r e n t it y bea n Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er public interface CustomerLocalHome extends javax.ejb.EJBLocalHome { This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio CustomerLocal Apppublic licat ion Developer f or t hefindByPrimaryKey(CustomerKey d ev elopm en t of ap plicat ion s basedprimaryKey) on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. throws javax.ejb.FinderException; Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an public findAllCustomers() un derly ing r java.util.Collection elat ional dat ab ase. I n Part throws 1, we in tjavax.ejb.FinderException; r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he CustomerLocal create(int customerId, name) EJB public 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hancedjava.lang.String fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or throws javax.ejb.CreateException; successful imp lem ent at ions of EJBs. I n Part public 2, weint in t rcountCustomers(); oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At } t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
EJB bean class The b ean class con t ains all of t he act ual b usiness log ic of t he ent er pr ise bean, an d is t he act ual im plem ent at ion of y our b usiness log ic. Th er e is n o d ir ect clien t v iew of a bean class, as client s deal d ir ect ly w it h t he com ponent or hom e int er face, so t h e idea of local and r emot e view s do not app ly w hen const r uct ing t h is class. Ther e ar e t hr ee dif ferent t y pes of bean classes—ent it y , session, and m essage- dr iven beans—and t he bean class t hat y ou const r uct mu st im plement one of t hese sup er t y pes ( Figur e 2- 1 3) .
Figu r e 2 - 1 3 . Be an cla sses a n d t he ir su per t yp es
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
We m ust ch oose w hich t y pe of in t er f ace t o ext en d f or our ow n ent er pr ise bean class, eit h er EntityBean, SessionBean, or MessageDrivenBean. Ad dit ionally, if w e ar e const r uct ing a m essage- dr iv en bean, t hen we mu st also im plement t he MessageListener in t er f ace, w h ich is t he par t of t h e JMS sp ecificat ion t hat allow s u s t o regist er our class as a JMS m essag e list ener . Lear n about t he EJB 2 .0 specif icat ion
Inherited bean class methods Dev elop EJBs w it h interface St u dio Appcallback licat ion Dev eloper t o cov W eb her einApplicat iont his Serint v err od uct ory discu ssion, t he f ollow ing ar e t he Alt houDeploy gh w e EJBs do not erSp t hem det ail in m et hods t hat w e mu st im plem ent dependin g on w hich int er face t y pe w e choose. Becau se t he This Redbook ovidest odtet ion on hofow o efm fect ivelys is use WebSp udio beanI BM class is n ot exprposed heailed clienint ,for t hm e at pur pose t h tese et hod not t o g ivhere e ou rStclient App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans cont r ol of or in for m at ion ab out our b ean s, as w as t he case w it h t he com ponent and hom e (int EJB) ar chsuper it ect um re, d s, d ep loym ap plicat ion scont t o aainer WebSph Servour er. er face etan hod but r atent her of t o su pr ch ovide our EJB w it heraewApp ay tlicat o mion anage beans. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derlyming elatar ional datred ab ase. These et hrods e r efer t o as con t ainer callb ack m et hod s, and ar e par t of t he bean- cont ain er cont r act t h at w e d iscussed pr ev iou sly . Th e con t ainer does n ot do any t hin g t o aut om at ically Igener n Partat 1, we in tior r oduce EJBs m asetah ods. p art We of Jav a 2 pr Enov t eridperise ion ( en J2 EE) and basic e behav f or t hese m ust t heEd imitplem t at ion forcover t h eset he m et hod s in concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he our bean class. We discuss t his m or e in t he n ex t sect ion on const r uct ing t he bean. EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist entar it ye bspecif ean s ic and essageen beans. pr ovid icebeanguidelines f or The in tence er f aces to m t he t y pe ofdrbivean an d w hWe at isalso r equir ed teo best supppr ortact t he successful imprlem ions EJBs. cont ainer cont act ent d epaten dingof on t he t yp e of bean. Th ese ar e pr esen t ed in Figur e 2- 1 4 ( en t it y bean) , Figur e 2- 1 5 ( session bean) , and Figur e 2- 1 6 ( messag e- dr iv en bean) . I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Figu r eent2 -fin 1 der 4 . En y bea n en int tmeapp r f ace im plem m etthitods, dif fer in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
public interface EntityBean extends javax.ejb.EnterpriseBean { public void setEntityContext(EntityContext ctx); public void unsetEntityContext(); public void ejbLoad(); public void ejbStore();
public void ejbActivate(); public void ejbPassivate(); public void ejbRemove(); } •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Figu r e li,2 Wout - 1 5er. Denay Sessi ons Schu bea in t e rShaddon f ace , Mart in Weiss By Ueli Wah er, Lar nk n , Deborah Pub lish er: I BM Pub Dat e: April 1 1, 20 03
public interface SessionBean extends javax.ejb.EnterpriseBean { I SBN: 0- 73 84- 26 09 -1 Pages: 75 0 public void ejbActivate();
public void ejbPassivate(); public void ejbRemove(); }
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Figu r e 2 - 1 6 . M e ssa ge- dr i ve n bea n in t e r f ace This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. public interface MessageDrivenBean extends javax.ejb.EnterpriseBean { Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly public ing r void elat ional setMessageDrivenContext(MessageDrivenContext dat ab ase. ctx); I n Part 1, wevoid in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic public ejbRemove(); concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed } per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session Message methods beans, an dlistener m essag e-interface d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At Alt gh wnot en t er rise bean ru ction , msessagedr iv en er beans stion also exter. end t h e t hehou en d, e d really escrib eanhow t opdeploy EJBconst ap plicat t o a WebSph e Appmu licat Serv MessageListener in t er f ace t o allow t h e cont ainer t o regist er t he bean as a JMS m essage list en er . I t is really r eq uir ed as a hook t h at t h e cont ainer uses t o in vok e t h e ent erp r ise b ean ( Figur e 2- 1 7) .
Figu r e 2 - 1 7 . M e ssa ge li st en er in t e r fa ce
public interface MessageListener { public void onMessage(Message message);
}
Constructing the bean class: providing the implementation •We n ow discuss Tablehow of Content t he bean s class is t o be con st r uct ed. As a b ean pr ov ider, you m ust t he necessar y m et hod s in t he bean class. The t yp es of met h ods t hat m ust be •im plem ent all I ndex defin invetlopm he beean class are deriv f r Appl om ica t heti on f ollowin g f our sou rces: EJB 2 .ed 0 De nt w it h W ebSphe r e Sted udio De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Com p onen t i nt e rf ace : busin ess m et h ods— Recall t h at in our com pon en t int er f ace w e descr ibed how t h e comp onent int erf ace defined t he business met h ods of t he Pub ent Dat ere:prApril ise 1b1,ean 20 03 . The b ean class m ust pr ov ide t h e imp lem ent at ion of t he business met h ods defin in 84t he com I SBN:ed 0- 73 26 09 -1 pon en t int er face. As w e m ent ioned ear lier, you don 't act ually imp lem ent tPages: his class per se ( in t he Jav a sense) , bu t it is up t o you t o mak e sur e t h at each m et hod in 75 0 t he com pon en t in t er f ace h as a cor r espon ding m et hod in t h e bean class. These m et hods m ust m at ch exact ly t h e sig nat ur es in t he com ponent int er face, except t hat t he bean class m et hods m u st not t hr ow a RemoteException. I n t he case of ent it y b ean s, t hese may be simp le get t er/ set t er m et hods, or in t h e case of session beans, could be t ask - or ient ed m et hods such as transfer.
Pubdiscussion lish er: I BM ,
Lear t he H omne about int er fa ceEJB : li fe2 -.0cyspecif cl e micat et hion ods—Recall t hat in our hom e in t er f ace d iscussion, w e descr ibed how t he hom e in t er f ace d ef ines t he lif e- cy cle met h ods of t he bean, such as creat e Dev elop EJBs w it h St u dio App licat ion Dev eloper and f in d. Th e EJB con t ainer and t he EJB h om e obj ect wor k closely t o per for m t h e lif e- cy cle m anagem entt oofWt he bean. bean ion class Deploy EJBs eb Sp her eThe Applicat Sermv uerst pr ov id e im plem ent at ion s of t hese lif e- cy cle m et hods d ef ined in t h e hom e int er face so t h at t he cont ainer can do it s job . Un lik e t h e ponent intprerf ace mdetethailed ods, tinh for esembean m ett o hod s doively not mat exacthere ly t h St e udio This Icom BM Redbook ovides at ion class on h ow ef fect use ch WebSp signat re in t h e hom intder How , tplicat he f ollowin g conv ent : Jav aBeans App licat ion uDeveloper f or tehe evface. elopm en tev oferap ion s based on t hions e Entapply er pr ise ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. - Cre at e m e t hods—f or each createXxxx m et hod d ef ined in t he hom e int er face, a Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an corr espond in g ejbCreatXxxx m et hod an d ejbPostCreateXxxx m et hod m ust ex ist in un derly ing r elat ional dat ab ase. t he bean class. The ejbCreate is in vok ed by t he con t ainer pr ior t o t he b ean being creat andEJBs pr ep as ar es eat ion. Th eEdejbPostCreate in vokted t he I n Part 1, we in t red, oduce a pt he art bean of Javfor a 2crEn t er p rise it ion ( J2 EE) andiscover he by basic cont ainer d ir ect ly aft er t h e bean in st ance is creat ed, t o per for m an y follow up concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he m et hods, ust bprefov oride e, t he r et en ur ned t o t hfun e client . We discuss t his cy cleedin EJB 2. 0 sp ecif icat ion tj hat forr esult ex amisple, hanced ct ion alit y for cont ain erlif - me anag m or e det ail in su bseq uent ch apt er s. As t he cor r espon ding hom e cr eat e met h od per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or r et ur ns a com pon en t in t er f ace, t h e beans ejbCreate m et hod r et ur ns t h e t y pe of successful imp lem ent at ions of EJBs. pr im ary key ( or collect ion of pr im ary key s) . - fi in ndBy Pri mt h ae rysam Ke y— onlyapplicat f or ention it y band ean ts,hen whimp et her cor respon ding session I n Part 2, we t r oduce p le Valid bank ing lema ent en t it y beans, plem ent ion t h is met h od m ust be he bean is based w het h er beans, an d im m essag e- dat r iv en of beans using WebSph er ed ef Stined u dio in Apt plicat ion class Dev elop er . Weonalso it is a cont or fer beananaged y bean. A dev er musstt hpr idet he EJBs. At im plem ent fin der m et hainer ods,- dif en t m app in g stent r atitegies, and sim elop p le client atov u se plem ent at iont oofdeploy t h e findByPrimaryKey if it is aerbeananaged enter. it y . t he en d, w eim d escrib e how EJB ap plicat ion s t oonalyWebSph e Appm licat ion Serv Becau se t he cor respond ing hom e fin d m et hod r et ur ns a comp onent in t erf ace, t he bean' s f in d m et hod s r et u r n t h e t y pe of p rim ar y k ey ( or collect ion of p rim ar y k ey s) . - Find er m e t hods— For each find Xxx x m et hod in t he hom e int er face, a corr espond in g ejbFindXxxx m u st exist in t h e bean class ( bean- m an aged ent it ies only ) . Th e sam e r et ur n t y pe r ules app ly .
- H om e m e t hod s—f or each hom e int er f ace m et hod ( Xxxx) , a corr esp ondin g ejbHomeXxxx m et hod m ust ex ist in t he b ean class. Cont a ine r cal lba ck m e t hods— Th e fin al sour ce for m et hod s t o b e added int o t he b ean
class ar e t he cont ainer callback m et h ods. The EJB class m ust p rov ide t he im plem ent at ion of t he con t ainer callb ack m et hod s t hat ar e declar ed in t h e super class of t h e bean class, w hich is dr iv en by bean t yp e. For ex am ple, ejbStore an d ejbActivate ar e t yp es of cont ain er callb ack met h ods. A lot of t he t im e, t hese m et hods ar e im plem en t ed , bu t left blank . Wh en t o pr ovid e meanin gfu l im plement at ion s for t hese met h ods is discussed t hr ou ghout t h is book . Ot h er m et hods— I f t h is is a m essage- d riv en b ean , t hen t he b ean is r equir ed t o im plem ent Table of Content s t he onMessage m et hod , w hich pr ovides m eaning ful logic on w hat t o do w it h t he m essage • I ndex w hen it ar r iv es. Ot h er m et hod s could be add ed t o t he b ean class, f or ex amp le pr ivat e EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper helper m et hods. •
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss Pub lish er: I BM EJB deployment descriptor Pub Dat e: April 1 1, 20 03 I SBN: 0-ion 73 8426out 09 -1how b ean s ar e m anaged at r un t ime is not addr essed by t h e int erf aces an d The in for m at ab Pages: 75ssed 0 classes discu pr ev iously . Alt houg h we did discuss in gener al callb ack met h ods t h at ar e par t of t he in t erf aces an d ar e n ecessar y f or t he b ean - cont ainer cont r act f or m anag in g t hose beans, w e did not say any t hing ab out h ow t h is is act u ally man aged. Th is is t he p ur pose of t he deploy m en t d escrip t or.
A d ep loym ent descr ipt or is an XML d ocu ment descr ibin g t h e beans t hat com pr ise an applicat ion. I t cont ain sn st ru ct urt he al inf orm ion abou t h e beans, such as t he t yp e of bean and w hat in t er f aces Lear about EJB 2 .0at specif icattion and classes m ake up t he b ean , as w ell as any ex t ern al r esou rce d ep en dencies t h at t h e bean has. But a Dev d epelop loymEJBs ent descr also cont ain s inf orm at ion about how behav ior should be w it h ipt St or u dio App licat ion Dev eloper im plem ent ed at r u nt im e, so t hat t his b eh avior m ay b e cust om ized at r u nt im e independ en t of t h e EJBss t oh em W eb Spes. her They e Applicat v err unt im e at t rib ut es as how t he b ean int er act s bean Deploy comp onent selv descrion ibeSer su ch w it h secur it y, t r ansact ion , nam ing , and ot her serv ices of t he cont ainer . I t descr ibes, in p art , t he This et ailed in for at ionser onvices h ow at t o ref bean-I BM contRedbook ain er conpr t r ovides act for dhow t o app ly m t hese unfect t imively e. use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (The EJB)d ep ar loym ch it ect u re, an diptdor ep is loym ch ap plicat s t omaust WebSph er aged e Appin licat ent descr on eent of of t hesucom pon en t sion t hat b e pack anion EJBServ JARer. file. The EJB JAR f ile cont ain s all t h e EJB classes an d in t erf aces com pr isin g t he app licat ion, as w ell as Thr hout tent he book w eorprXML ov idfile e exam based a simp t he oug deploym descr, ipt t hat ples descr ibes ton h ose EJBs.le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. The f ollow in g XML d ocum en t show s som e of t he basic st ru ct u r e of t he d ep loy m ent d escr ipt or f or I n Part we inand t r oduce EJBs as a p art ofisJav a 2t he Encom t er pplet riseeEd ion ur ( J2 coverent t hedescr basic an en t it 1, y bean a session bean. This not stitr uct e EE) of a and deploym ipt or s and e aripch it ect e. Iwnillpar lar w eedpoint out t heenn tew fu nct odstuced w ited h t he (concep deploytm en t dt hescr t or deturails bet icu p resent in subsequ chapt erions s) ; itint isr ju int end to EJB 2. 0 sp icat ion pr ov e, ion for in ex am hanced fun ctipt ion dem onst r atecif e som e oft hat t he inf or id mat t he ple, d ep en loym ent descr oralit . y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful ions EJBs. Figur e 2- 1 8imp sh lem owsent anatent it y of bean d ep loym ent descr ipt or ex am ple and Figur e 2- 1 9 sh ows a session b ean d ep loy m ent descr ipt or ex amp le. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Figu r e 2 - 1 8 . En t it y bea n de ploy m en t descr i pt or im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Account itso.bank5.cmp.AccountLocalHome itso.bank5.cmp.AccountLocal itso.bank5.cmp.AccountBean
Container java.lang.String False 2.x • •
Table of Content s
Account I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
accountID Pub lish er: I BM
Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
Pages: 75 0
balance
...... Lear n about t he EJB 2 .0 specif icat ion accountID Dev elop EJBs w it h St u dio App licat ion Dev eloper
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er ejb/TransRecord This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) arEntity ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. itso.bank5.cmp.TransRecordLocalHome Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. itso.bank5.cmp.TransRecordLocal I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic
concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed ...... per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, Retrieve an d m essag e- d r iv en beansgold usingaccounts WebSph er.... e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. findGoldAccounts java.math.BigDecimal
select object(o) from Account o where o.balance > ?1
•
Table of Content s
Figu r e 2 - 1I ndex 9 . Sessi on bea n de ploy m en t descr i pt or • EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 Banking I SBN: 0- 73 84- 26 09 -1 Pages: 75 0 itso.bank5.session.BankingHome
itso.bank5.session.Banking itso.bank5.session.BankingBean Stateless Lear n about t he EJB 2 .0 specif icat ion Container Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o Wid="EJBLocalRef_1035497442092"> eb Sp her e Applicat ion Ser v er > = < < = < > ) , f or st r ings on ly = and < > I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also NOT im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he enBETWEEN d, w e d escrib ar it hm e how et ic-t ex o deploy pr ession EJB AND ap plicat arit h met ion sict oexaprWebSph ession er e App licat ion Serv er. I N: v alue I N ( v alue1, value2, . .. ) LI KE, using w ild car d char act ers _ an d % nu ll comp arison: I S NULL emp t y com par ison : collect ion- v alued- p at h- exp r I S EMPTY collect ion mem ber exp r ession : MEMBER OF collect ion - valu ed - pat h- ex pr fu nct ional ex pr essions: concat, substring, locate, length, abs, sqrt
Parameters To p ass par am et er s t o t he q uery st at em ent , t h e declar ed par am et er s of t he find er m et hod ar e accessib le in t h e qu er y st at em en t as a qu est ion m ark f ollowed by a nu mb er , such as, ?1, ?2, ?3 . The n um ber is t h e posit ion of t he declarat ion in t he f inder m et hod' s signat u r e. This is sim ilar t o t he par am et er hand linofg Content in a JDBC pr ep ared st at em ent . • Table s •
I ndex
For exam ple, m at ching f in der m et hod declar at ion and EJB QL st at em ent s are: EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
findCustomerByName(String lastName, String firstName); Pub lish er: I BM Pub Dat e: April 1 1, 20 03
SELECT object(x) FROM Customer x WHERE x.lastName = ?1 OR x.firstName = ?2 I SBN: 0- 73 84- 26 09 -1
Pages: 75 0
The t yp e of t he p aram et er in t he st at em en t is d ef ined b y t he d eclar at ion of t he cor respond en t par am et er in t h e find er m et h od sign at ur e. Par amet er s can b e pr im it iv e t y pes ( int) , w r app er classes ( Integer) or EJB object s. UsingLear a par et ert he is on ly 2allow ed inicat t he n am about EJB .0 specif ionWHERE clause w it hin a con dit ional ex pr ession t hat inv olves a sin gle- valued pat h exp r ession , such as x.lastName. Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
WebSphere extensions to EJB QL
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion WebSph er e Developer Ap plicat ionf or Serv t heerd ev pr ov elopm idesen ext t ensions of ap plicat t o ion EJBs QL based t o mak on teh eit Ent morereprspecific ise Jav aBeans and f lex ible. ( EJB) Alt houar ghchall it ect of ut re, he W aneb d Sp d ep her loym e ex ent t ensions of su charap e pplicat resen ion t ed s thoerae,WebSph t hey mer aye cov Apperlicat t opics ion Serv t hat er. w e hav e not y et discu ssed, su ch as r elat ionships an d n avig at ion . Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly Not e t h at ingy ou r elat are ional using datvend ab ase. or- sp ecific ex t en sion s. I f y ou hav e t o reuse t h is EJB in an ot her EJB 2. 0 com pliant cont ain er , y ou should not use t h ese ext en sions. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep4t-s3and Table sh ows t h e aarcom ch itpar ect ur ison e. Iof n tpar he tEJB icu lar 2 . 0w specificat e point out iont he an nd ew t hefuex nct t en ions sionint s rof odt uced he WebSp w it h here t he EJBer2. qu y 0lang sp ecif uage. icatWe ion sh t hat ow pr t he ov id usage e, forofext ham e relevan ple, en hanced t ex t en sion fun ct s ion w italit h ayshor for cont t ex amp ain erle. - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e-Tab d r iv en WebSph e St u dios Ap Dev elop le beans 4 -3 . using EJB Q L ex t er e nsion inplicat W e ion bSphe r e er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
I tem
• •
EJB 2 . 0 spe ci fi ca t i on
W eb Sphe r e qu er y
SELECT clau se
r eq uir ed
opt ion al
Delimit ed ident ifier s
no
y es
St r ing comp arisons
= an d < > only
= >
WebSph for st r ingercomp w it h ion in WDev HERE im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Scalar functions EJB QL supp ort s concat, substring, locate, length, abs, sqrt. Th ese f unct ions ar e v alid for all DBMS supp ort ed by WebSp her e. WebSph er e sup por t s in addit ion t y pe conv ersion fu nct ions ( char, decimal, bigint, double, float, integer, smallint, real, date, time, timestamp) , st r ing fu nct ions ( lcase, ucase, digits) , an d dat e/ t im e f unct ions ( days, year, month, day, hour, minute, second, microsecond) . Th ese fu nct ions ar e sup por t ed on ly or DB2.
ORDER BY The ORDER BY clau se sp ecifies an or dering of t h e obj ect s in t he result collect ion .
ORD ER BY [ ord er _element ,] * or der _elem ent • •
Table of Content s
or der _elem ent : : = { pat h - ex pr ession | in t eg er } [ ASC | D ESC ] I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss PubThe lish er: p at I BM h ex pr ession
m ust specify a sin gle v alu ed f ield t hat is a p r im it iv e t y pe ( int) or a
w Dat r apper ( Integer) . Pub e: Aprilt y1pe 1, 20 03 I SBN: 0- 73 84- 26 09 -1
ASC sp ecifies ascen ding or der and is t he default . DESC specif ies d escending or der . Pages: 75 0
integer r efer s t o a select ion ex pr ession in t h e SELECT clau se. Ex am ple:
Lear n about t he EJB 2 .0 specif icat ion SELECT OBJECT(a) FROM Account c ORDER BY a.accountID, a.balance DESC Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
SQL This I date/time BM Redbookexpressions pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans WebSph er eit ect w ituhre, a Dan B2 at aloym st orent e supp orch t s dat e/ t imion e com isons and pr ession , for exer. am ple: ( EJB) ar ch d d ep of su ap plicat s t o par a WebSph er eexApp licat ion Serv Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. t.transID > '1990-02-24' I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch-it ect DATE('3/15/2000') '12/31/1999' ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
Subqueries, aggregations, group by, and having clauses I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also A quer can in qdif uant ied edicat EXI STS and pr edicat I N pr es.t he A su bqu erAty imsub plem enty fin derbemused et h ods, ferifen t mprapp in g es, st r at egies, sim pes le or client s tedicat h at u se EJBs. should a how sin gle ent EJB in t hap e plicat SELECT t he en d,only w e specify d escrib e t oelem deploy ion sclause. t o a WebSph er e App licat ion Serv er. When a p at h ex pr ession appear s in a sub quer y, t he ident if icat ion v ar iab le of t he p at h ex pr ession m ust b e defin ed eit her in t he sub quer y, in on e of t he cont ain in g su bqu er ies, or in t h e out er q uery . A scalar subq uery is a subq uer y t hat ret u rn s one v alue. A scalar subq uer y can b e used in a b asic pr edicat e and in t he SELECT clause of a d yn amic q uery . Agg regat ion f unct ions ar e avg, max, min, sum an d count(*). Ag gr eg at ion f un ct ion s can on ly be used in sub select and HAVI NG clauses, b ut not in t h e t op- lev el SELECT clause. Ex am ples:
Accou nt s t h at h ave mor e t h an t he av er ag e balance:
SELECT OBJECT(e) FROM Account e WHERE e.balance > ( SELECT AVG(e1.balance) FROM Account e1 ) •
Table of Content s
•
I ndex
t ran of each accou : ica ti on De v el oper EJB 2 . 0Lar D egest ve lopm e ntsact w it hion W ebSphe r e St udio nt Appl By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
SELECT object(t) FROM TransRecord t WHERE t.transAmount = Pub lish er: I BM Pub Dat(e: SELECT April 1 1, 20MAX(u.transAmount) 03
FROM IN (t.theAccount.transrecords) u )
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
EXISTS predicate The EXI STS pr edicat e t est s for t he pr esence of cert ain ob ject s. Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper EX I STS ( su bselect ) | collect ion- v alu ed - pat h - ex pr ession Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App ionofDeveloper t heif dt he ev elopm en t of s based t h or e Ent pr ise Jav The licat r esult EXI STS isf or t r ue su bselect r etap urplicat ns at ion least one von alue t heerpat h ex pr aBeans ession (evalu EJB) at ares ch titoect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. a nonem pt y collect ion; ot her w ise t he r esu lt is false. Thr hout e pr ove, id eprexam simp app licat ion w it h an To noug eg at e ant he EXIbook STS, pwredicat eced eples it wbased it h t heon loga ical opleerbank at or ing NOT. un derly ing r elat ional dat ab ase. For exam ple, r et ur n cu st omer s t hat hav e at least one account w it h a larg e balance, or account s t ha I n Part in t rion oduce EJBs hav e no1,t r we ansact r ecord s: as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem entFROM at ionsCustomer of EJBs. c WHERE EXISTS SELECT object(c) I n Part 2, we in1t rFROM oduceIN t h e(c.accounts) sam p le bank inga applicat ion and t hen> imp lem ent ( SELECT WHERE a.balance 10000 ) en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. SELECT object(a) FROM Account a WHERE NOT EXISTS a.transrecords
Dependent value attributes Wit h a p at h ex pr ession you can access t he at t rib ut es of a d ep en dent value class:
SELECT object(c) FROM Customer c WHERE c.address.state = 'California'
This is on ly valid if t he Address ob ject is m app ed t o indiv idual colu mn s in a t able u sin g a comp oser ( " Using com posers" on p age 48 0) . •
Table of Content s
Inheritance • I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
The d at a m od el for cont ain er - m anag ed per sist ence does n ot cur r en t ly supp ort inher it ance. This is By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss w r it t en in t he EJB Sp ecificat ion Ver sion 2 .0 as a r est r ict ion of t h e EJB QL definit ion in Chap t er 11 .2 . 11. I t is not suppor t ed in a July 2 002 dr af t v ersion of t he EJB Specificat ion 2 . 1 eit her . Wit h lish er: I BM t hisPub r est r ict ion, en t it y obj ect s or value classes of d iff er ent t yp es can not be com par ed . Pub Dat e: April 1 1, 20 03
WebSph I SBN: er e 0-ex 73t84en 26 sion 09 -1 s t o EJB QL enab le us t o quer y t h e t yp e of an en t it y bean in an inh er it ance hier archy : Pages: 75 0 The IS OF TYPE p r ed icat e is u sed t o t est t he t y pe of an EJB r efer en ce. I t is sim ilar in fun ct ion t o t he Jav a instanceof op er at or . The ONLY op t ion can be used in addit ion t o sp ecify t hat t he r eference m ust be ex act ly t his t y pe Lear n about t he EJB 2 .0 specif icat ion and not a su bt y pe. Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs o Wiable eb SpI her e Applicat ident if icat ion-tvar S OF TYPE ( ion [ ONSer LY]v ert yp e ) * This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (To EJB) arrch u re, d ep loym entwof chan apext plicat o aarWebSph er ee App illust atiteect EJB QLan f ord inher it ance e su use endion edsht ier ch y ( Figur 4- 2 licat 9) . ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an r e dat 4- 2 . I nh er i t a nce hi er a r chy f or EJB Q L e xa m pl es un derly ing r Figu elat ional ab9ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Ex am ples: To r et r iev e all t h e t y pes fr om t he inh er it ance hier arch y:
SELECT object(account) FROM Account account WHERE account.balance > 100
Ret ur ns inst ances fr om Account, Checking, Savings an d FixedSavings w h er e t h e balance is m or e t han 10 0. To r et r iev e only Savings accoun t s:
• •
of ContentFROM s SELECT Table object(a) Account a WHERE a IS OF TYPE (Savings) I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Not e t h at t h is st at em ent r et u rn s inst ances of Savings an d FixedSavings, b ecause FixedSavings is a subclass of Savings.
Pub lish er: I BM Pub Dat e: April 1 1, 20 03
To r et r0-iev in st an ces of Savings w it h out t he sub t yp e: I SBN: 73 e 84-only 26 09 -1 Pages: 75 0
SELECT object(a) FROM Account a WHERE a IS OF TYPE (ONLY Savings)
Lear n about t he EJB 2 .0 specif icat ion
WebSphere Enterprise extensions Dev elop EJBs w it h St u dio App licat ion Dev eloper WebSph er e Ap plicat ion Serv er En t erp rise Ed it ion p rov ides m or e ext ensions t o EJB QL. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Dynamic API f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans App licat ionquery Developer ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. The d yn am ic EJB q uer y ser v ice p rov ides J2EE applicat ions a way t o search CMP ent it y beans using Thr oug hout t he , we e exam a simp bank app ion wor it hvan an ex t ension of tbook he EJB qupr erov y id lang uage.ples Th ebased quer yon can ret ur le n eit hering EJB r eflicat er ences alu es of un derly ing r elat ional dat ab ase. CMP and CMR f ields. I n Part in t r oduce ascalled a p artdyofnam Javic a 2q uery En t erser p rise it ion (: J2 EE) and cover t he basic The d yn1, amwe ic quer y API EJBs is also viceEd( DQS) concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed DQS ies t o bem cr eat ed dr anivd en ex ecu t ed We du r in g r upr ntovid im e.e Quer d oice notguidelines hav e t o be per sist enceallows ent it yquer b ean s and essagebeans. also best ies pr act f or st at ically p redef . Qu ies can inv olv e a n um ber of abst r act schema t y pes. successful imp lem ent in ated ions oferEJBs. DQS comp w it ht ht he EJBpQL 2 . 0 an t he WebSp t ension s. You can par e session dy nam ic I n Part 2, we in tlies r oduce e sam le bank ingd applicat ion here and tex hen imp lem ent en t it com y beans, quan er d iesmw it h t he at ion and invWebSph ocat ion er ofeaStpru ep st at em JDBC, b ut also you ar e beans, essag e- dpr r ivepar en beans using dioared Ap plicat ionent Devinelop er . We oper ingder onmt he st r act schem t y pes h eegies, und erand lay ing t it client y bean im plem entatfin et hab ods, dif fer en t maapp in g of st rt at simen p le s tset h at. u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Dependent value methods We d escrib ed depend en t v alue at t rib ut es as t hose at t rib ut es of dependent classes of an en t it y bean Depend en t v alue m et hods are t he m et hods of t hose classes. You can use any ser ializab le Java class t o be used in a CMP field. Wit h depend en t v alue m et hods you can addr ess t he f ields and m et h ods o a d ep en dent obj ect direct ly on t he EJB QL st at em en t :
SELECT object(e) FROM Customer e
WHERE e.address.distance('San Jose') > 10 AND e.address.zipcode = 95120
This q uery in vok es t he distance m et hod of t h e Address class:
•
Table of Content s
public double • I ndexdistance(String start_location) { ... } EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
EJB Pubbean lish er: Imethods BM Pub Dat e: April 1 1, 20 03
A p at h I SBN: ex pr 0ession can 73 84- 26 09 -1also n avig at e t o a m et hod of an ent it y bean. Th e m et h od m u st be d ef ined in eit her Pages: t he r em ot e or local bean int er face. You can not mix b ot h rem ot e and local m et hods in a sin gle 75 0 qu er y st at em en t : I f t he q uery con t ains r emot e met h ods, t he d yn am ic quer y m ust b e ex ecut ed u sin g t he qu er y r em ot e in t er f ace. Using t he q uery rem ot e int er face causes t he q uery ser vice t o act iv at e b ean s and cr eat e in st ances of t he r em ot e bean in t er f ace. Lear n about t he EJB 2 .0 specif icat ion Lik ewise, a q uer y st at em ent w it h local b ean m et hods m u st be ex ecut ed w it h t he q uery local Dev it h St u tdio ion Dev int erelop face.EJBs Thiswcauses he App q uerlicat y ser v ice t oeloper act ivat e beans and local int er face inst ances. Deploy eb Spm her Applicat The ch aract erEJBs ist icst oofWbean eteh ods ar e: ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat Metion hodDeveloper s m ust h ave f or nt he on-dv ev oidelopm r et uren n t yof pes apand plicat mion et hod s based ar gum onent t h se an Entderr et prur ise n Jav t y pes aBeans m ust b e pr im it iv an e t dy pes int)ent or of w rap perap t yplicat pes (ion Integer) . ( EJB) eit arher ch it ect u re, d ep (loym su ch s t o a WebSph er e App licat ion Serv er. Thr oug Dohout not tuse he book get m , w etehod pr ov s tid o eaccess exam ples CMPbased an d CMR on afields simpof le abank bean. ing app licat ion w it h an un derly ing r elat ional dat ab ase. I f a m et hod has ov er load ed d ef init ions, t he over loaded m et hod s m ust h ave d iffer ent num ber I n Part of 1, parwe aminett er r oduce s ( h avin EJBs g das if fer a ent p artt yp of es Javisa not 2 Enenou t er pgh) rise. Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he inpicat ut ion arg um t ov t o id a e, m et is NULL, is assum t h at t hyefor m et hodain reteru-rn a NULL EJB 2.I f0 any sp ecif t hatenpr forhod ex am ple, enithanced funed ct ion alit cont msanag ed v alue and t h e m et h od is n ot in vok ed. per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. IMultiple n Part 2, element we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session SELECT clauses beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At I n t he specif icat ion it is allow ed t o r et ur n a CMP or CMR field. The EJB Query ex t ension allow s t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. m ult iple elem ent s and ex pr essions in t h e SELECT clause:
SELECT e.name, e.salary+e.bonus as total_pay from EmpBean e
Quick tour on dynamic query Here are a few ex am ples of dy nam ic quer ies:
Em ploy ee I D an d m ax im um salary :
select e.empid, max(e.salary) from Employee e
• •
Table of Content s
Nam e, tI ot al p ay, and an EJB r efer en ce for all em ploy ees: ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli select Wah li, Wout er Denay er,e.salary+e.bonus Lar s Schu nk , Deborah Shaddon , Mart in Weiss e.name, as total_pay,
object(e) from Employee e
Pub lish er: I BM Pub Dat e: April 1 1, 20 03
I nv okin ob09 ject I SBN: 0-g 73an 84- 26 -1 m et hod ( print m u st be d ef ined on t h e add ress CMP field) : Pages: 75 0
select e.address.print() as address from Employee e
Num ber of em ploy ees in each depar t m ent : Lear n about t he EJB 2 .0 specif icat ion Dev elop e.dept.deptno EJBs w it h St u dio as Appdept, licat ioncount(*) Dev eloperas employee_count from Employee e select Deploy EJBs t o W eb Sp her e Applicat ion Ser v er group by e.dept.deptno This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio order by 2 App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase.
Dynamic query interface basics I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep and t h e qaruer ch ity ect e. I is n par t icu lar w ewpoint hee ninew nct nions The d ynt sam ic EJB serur v ice an EJB bean it h a out r emtot t er fu f ace am eintofr od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed com.ibm.websphere.ejbquery.Query an d a local int er face nam e of per sist ence ent it y b ean s and m essagedr iv en beans. We also pr ovid e best pr act ice guidelines f or com.ibm.websphere.ejbquery.QueryLocal. successful imp lem ent at ions of EJBs. I n Part 2, t we t r oduce t h e sam bank applicat ion and lemice ent fren t it ay beans, Use he rinemot e Query in t erpfle ace if y ing ou ar e accessing t h et hen qu erimp y serv om rem ot e session clien t or beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also y ou w an t t h e qu er y t o ret u rn r emot e EJB refer ences in t h e quer y r esult . im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he enThe d, QueryLocal w e d escrib e how deploy ap plicat WebSph e App in t er tf o ace mu st EJB be called w ition h ins tao tar ansact ionercont ex tlicat andion r etServ ur nser. a dem anddr iv en it erat or. Any EJB r ef erences r et u r ned w ill b e local refer ences. The m ain m et hod t o u se is executeQuery. A ref er ence t o t he Query ob ject m ust be ob t ain ed f r om t he QueryHome. A t r ansact ion cont ex t is r equir ed; if n one exist s, t h e EJB con t ainer st art s a t r ansact ion u pon calling executeQuery. Figur e 4- 3 0 sh ows an ex am ple of a r em ot e dy nam ic qu er y .
Figu r e 4 - 3 0 . D yn am i c q ue r y ex am pl e
import com.ibm.websphere.ejbquery.*; ...
try { •
Table of Content s
•
ndex String I query =
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
"select e.name , object(e) Employee By Ueli Wah li, Wout er Denay er, Lar s as Schuname nk , Deborah Shaddon , Martfrom in Weiss
e where e.salary < 50000";
Pub lish er: I BM Pub Dat e: April 1 1, 20 03
InitialContext ic =
new InitialContext();
I SBN: 0- 73 84- 26 09 -1 Pages: 75 obj 0 Object =
ic.lookup("com/ibm/websphere/ejbquery/Query");
QueryHome qh = (QueryHome) PortableRemoteObject.narrow(obj, QueryHome.class); Query qb = qh.create(); Lear n about t he EJB 2 .0 specif icat ion DevYou elop must EJBs w it h St u dio licat ion Dev eloper // specify a App maximum size of the query result set (99) Deploy EJBs t o Wit eb Sp her e Applicat ion Ser v er QueryIterator = qb.executeQuery(query, null, null ,0, 99 ); This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB)// ar ch it ecttuple u re, an contains d d ep loym ent of su and ch apan plicat ion s t o a object WebSphreference er e App licat ion Serv er. Each a name employee Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an while (it.hasNext() ) { un derly ing r elat ional dat ab ase. tuple I n Part 1, IQueryTuple we in t r oduce EJBs as =a p(IQueryTuple) art of Jav a 2 En it.next(); t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he s =t hat (String) EJB 2. 0 spString ecif icat ion pr ov id e, tuple.getObject(1); for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or "+s); successfulSystem.out.println(it.getFieldName(1)+" imp lem ent at ions of EJBs. e =t h(Employee) PortableRemoteObject. I n Part 2, Employee we in t r oduce e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also narrow( tuple.getObject(2), ); At im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le clientEmployee.class s t h at u se t he EJBs. t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. System.out.println(it.getFieldName(2)+" "+e.getPrimaryKey().toString()); } }
catch (QueryException qe) { System.out.println("Query Exception "+ qe.getMessage() );
}
Ot h er met h ods of t he dy nam ic quer y int er face ar e prepareQuery an d executePlan, w her e a st at ement is pr epar ed on ce and ex ecu t ed m u lt ip le t imes w it h dif ferent par am et er s:
String query = "select e.name from Employee e where e.salary < ?1"; QueryIterator it = null; •
Table of Content s
•
I ndex Integer[] parms = new Integer[1]; parms[0] = new Integer(0); EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli WahqueryPlan= li, Wout er Denayqb.prepareQuery(query, er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss String parms, null );
parms[0] new Integer(50000); Pub lish er: =I BM Pub Dat e: April 1 1, 20 03
it = qb.executePlan(queryPlan, parms, 0, 99); I SBN: 0- 73 84- 26 09 -1
Pages: 75 0
......
parms[0] = new Integer(60000); it = qb.executePlan(queryPlan, parms, 0, 99); Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Developing entity beans with advanced concepts I n t his sect ion, w e ex pan d t he ban kin g ap plicat ion t o descr ibe h ow w e m ight add t h e follow ing new f eat ur es t o ou r beans: • •
Table of Content s
Relat ionships I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
I nher it ance
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Cu st om quer y m et hods Pub lish er: I BM
EJB QLApril exam Pub Dat e: 1 1, ples 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Relational database mapping As in t he CMP p er sist en t f ields, t h e CMR r elat ion sh ip f ield s m ust also b e map ped t o an act ual dat ab ase elem en t at deploy ment t im e, so t hat t he con cr et e code can be g en er at ed. Th er efor e, each CMR f ield is m app ed t o a f oreign k ey on t h e t able t h at w e ar e r elat ing t o. Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
Expanded banking entity requirements
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er The f ollow in g ar e t he new f eat ur es t hat w e w ill add t o ou r bank ing sy st em : This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ewd dent it y be a nofAccoun Th is bean has t h e follow field ( EJB) Su ar pport ch it ect ufor re, nan ep loym ent su ch apt — plicat ion s to a WebSph er e ing Appper licatsist ionent Serv er.s defin ed : accountID ( pr im ar y k ey) , balance, interest, an d accountType. Th is bean m u st supp ortt he t hebook business etid h ods of deposit an donwithdrawal. Thr oug hout , w e prmov e exam ples based a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. Su pport for a n ew en t it y b ea n Tra nsRecor d— Th is bean has t h e follow ing per sist ent fields d efinined: transID ( praimp ar k ey) , atransType, transAmount. I n Part 1, we t r oduce EJBs as arty of Jav 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he d 1ecif :micat r elion at i onshi Account-TransRecord. EJB 2.Ad 0 sp t hat prp— ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or Ad d m :mlem re ent la t ion sh ip— Customer-Account. successful imp at ions of EJBs. Account bt y pe ts— d pt w subting yp es of Account, Checking d Savings, sing t hsession e I BM I n Part 2, we insu t r oduce h e Ad sam leobank applicat ion and t hen impan lem ent en t it y ubeans, extan ension of inher beans, d m essag e- d ritivance. en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At e ww ebu ne sse an dq y m etEJB h ods— W eion can exp loit rer elat ionsh ip sion and defin t he enNd, d si escrib how t ouer deploy ap plicat s tnoow a WebSph e App licat Serv er.e new bu sin ess ( com ponent ) and qu er y ( h om e) m et h ods. Usin g EJB QL w e can for m ulat e q uit e com plex quer ies t o fin d b ean s t hat sat isf y cer t ain r eq uir em ent s. For exam p le, w e can fin d accou nt s t hat h ave a lar ge balan ce ( we call t hose gold accou nt s) .
Entity bean relationship requirements The r elat ionships t hat w e su ppor t are sh own in Figur e 4- 3 1.
Figu r e 4 - 3 1 . Rel at ionsh ips of t he ba nk m odel
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Becau se of t hese relat ionships, ou r beans w ill sup por t t h e follow ing new m et hods: By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
er PubCust lish er:om I BM Pub Dat e: April 1 1, 20 03 I SBN: -0- CMR 73 84- 26 09 -1 accessor
met h ods—setAccounts, getAccounts
Pages: 75 0
- Busin ess m et hod s t o ex ploit r elat ionships—addAccount, removeAccount - Quer y m et h ods—findGoldCustomers, findHighInterest, findAllCustomers - Hom e m et hod —findAllCustomers ( just t he last n am es) Lear n about t he EJB 2 .0 specif icat ion Account Dev elop EJBs w it h St u dio App licat ion Dev eloper - CMR accessor met h ods—getCustomers, setCustomers, getTransrecords, DeploysetTransrecords EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d ets ailed for m at ion on h ow t o ef fect ively use WebSp here St udio - Busin ess m et hod t o ex in ploit r elat ionships—addCustomer, removeCustomer App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it-ect u re,ess an dmdetep loym ent of su ch ap plicat ion t o sa applicat WebSphion er eex App ion Serv er. Busin hod s—deposit, withdraw ( t hrs ow ceplicat t ion) Thr oug hout - t Quer he book pr ov id e exam ples basedfindGoldAccounts, on a simp le bank ing app licat ion w it h an y m, etwheods—findAllAccounts, findTransferAccounts, un derly ing findLargestAccount r elat ional dat ab ase. I n Part 1,nsRe we incor t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Tra d concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed CMR m et hmods—getTheAccount, per sist ence -ent it y access b ean s and essage- dr iv en beans.setTheAccount We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. - Cr eat e met hod t o exp loit r elat ionship s—create ( wit h account par am et er ) I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin derstructures m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At Entity bean t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. The f ollow in g d escr ibes w hat t he new ent it y beans of Account an d TransRecord w ou ld look lik e. Not all det ails are pr ovid ed ( such as all t he callback m et hods) , but you shou ld g et a f eel f or h ow t hese new r elat ionships can be ex posed . We conclud e w it h a client view t h at show s t he act ual usage of t hese n ew m et h ods lat er . For each bean ( new an d old ) , w e show t he follow ing : Com pon en t int er f ace Hom e int er face Bean class
Customer entity bean updates The Customer ent it y bean requ ir es som e sm all u pdat es.
Changing the Customer • Table of Content s local component interface •
I ndex
Figur 4-e ve 3 2lopm sh ows he com ponent int erAppl faceicawtieondef e oper for t he Customer ent it y bean. We kn ow EJB 2 .e 0 D e nt wt it h W ebSphe r e St udio Dein v el tBy hat Customer is relat ed t o Account t hr ough a man yt oman y r elat ion sh ip . We ar e r equir ed t o Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss defin e a local com ponent int er face because w e use t h is bean in a bidir ect ional relat ionship. Pub lish er: I BM
Figu r e 4 - 3 2 . Local com pon en t in t er f a ce of cu st om er en t i t y be an Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
public interface Customer extends javax.ejb.EJBLocalObject { //cmp field accessors the same as before
Lear n about t he EJB 2 .0 specif icat ion //new methods available because of relationships Dev elop EJBs w it h St u dio App licat ion Dev eloper public Collection(getAccounts(); Deploy EJBs t o W eb Sp her e Applicat ion Ser v er void addAccount(AccountLocal Thispublic I BM Redbook pr ovides d et ailed in for m at ionanAccount); on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans public void ( EJB) ar ch it ect u re,removeAccount(AccountLocal an d d ep loym ent of su ch ap plicatanAccount); ion s t o a WebSph er e App licat ion Serv er. } Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h eCustomer ar ch it ect urhome e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he Changing the interfaces EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or Figur e 4- 3 3 sh ows t he n ew hom e int er faces w e define f or t h e Customer ent it y bean. Som e successful imp lem ent at ions of EJBs. add it ion al f in der m et hods ar e d eclar ed t o dem onst rat e how w e can t r av er se r elat ionship s. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans,r e Figu an4 d -m3essag 3 . Local e- d r iv en h om beans e i nt using er fWebSph a ce of ercu e St stuom dio e Apr plicat e n t it iony Dev beaelop n er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. public interface CustomerLocalHome extends javax.ejb.EJBLocalHome { public void create(int customerID) throws CreateException; public CustomerLocal findByPrimaryKey(CustomerKey key) throws FinderException; public Vector getAllCustomers() throws FinderException;
public Collection getGoldCustomers() throws FinderException; } public interface CustomerHome extends javax.ejb.EJBHome { public void create(int customerID) •
Table of Content s
•
I ndex
throws CreateException, RemoteException;
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
public CustomerLocal findByPrimaryKey(CustomerKey key)
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
throws FinderException, RemoteException; Pub lish er: I BM
public Collection Pub Dat e: April 1 1, 20 03
findHighInterest(int interest)
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
throws FinderException, RemoteException;
}
Updating Customer bean class Lear n the about t he EJB 2 .0 specif icat ion it hn St App licat eloper Figur eDev 4- 3elop 4 shEJBs ows twhe ewu dio Customer b ion ean Dev class and t he n ew m et h ods t h at it su ppor t s. Th er e is a h om e m et h od an d t w o select m et hod s declar ed here as w ell. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Figu r e Redbook 4 - 3 4 . Cust om deet r ailed be anin for cla This I BM pr ovides mss at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. public abstract Customer javax.ejb.EntityBean { ing app licat ion w it h an Thr oug hout t he book , w e pr ovextends id e exam ples based on a simp le bank un derly ing r elat ional dat ab ase. //new methods only I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence entused it y b ean //fields fors and CMR m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. public Collection getAccounts(); I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essagsetAccounts(Collection e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also public void accounts); im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. //new business methods public void addAccount(AccountHome anAccount) { getAccounts().add(anAccount); } public void removeAccount(AccountHome anAccount) {
getAccounts().remove(anAccount); } //new select methods: public abstract Set ejbSelectAllCustomerNames() throws FinderException; •
Table of Content s
•
I ndex
public abstract Collection ejbSelectNumberOfAccounts(String) EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
throws FinderException;
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
//home method Pub lish er: I BM
public Vector ejbHomeGetAllCustomers() Pub Dat e: April 1 1, 20 03
throws FinderException {
I SBN: 0- 73 84- 26 09 -1
//implementation not shown
Pages: 75 0
}
//other lifecycle methods not shown, but they exist! Lear n about t he EJB 2 .0 specif icat ion }
Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Account entity bean App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (The EJB)accoun ar ch ittect u it re, d drep loym ent su ch ap plicat ent y ban ean epr esent s aofban k accoun t . ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase.
Building the Account local component interface
I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep s 3and e art ch ect urponent e. I n par icu lar w e point ew fu nct ent ionsit yintbean. r od uced h t he Figur e t45 sht hows heitcom intter face def in eout for t the he nAccount We wknit ow t hat EJB 2. 0 sp icat hat pr ov id an e, dforTransRecord, ex am ple, en hanced fun ct iont halit y for accessor cont ain ers.- m anag Account isecif relat edion t o tCustomer so w e ex pose e CMR We areed ence ent it y baean s and m onent essageiv ace en beans. Wew also bestin pr guidelines f or rper eq sist uir ed t o define local comp intdrerf becau se e useprt ovid his beean a act b idice irect ional rsuccessful elat ion sh ipimp . lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session Figu r e 4 - 3 5 . Accou nt local com pon en t in t er f a ce beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. public interface AccountLocal extends javax.ejb.EJBLocalObject {
public BigDecimal getBalance(); public void getBalance(BigDecimal balance); public int getInterest();
public void getInterest(int interest); public String getAccountType(); public void setAccountType(String accountType);
• •
Table of Content s
public Collection getCustomers(); I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
public void addCustomer(CustomerLocal customer);
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
public void removeCustomer(CustomerLocal customer); Pub lish er: I BM
public Collection Pub Dat e: April 1 1, 20 03
getTransrecords();
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
public BigDecimal deposit(BigDecimal amount); public BigDecimal withdrawal(BigDecimal amount); }
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
I n addit ion, we define t he bu sin ess m et hods of deposit an d withdraw. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Building Account interface App licat ionthe Developer f orlocal t he d home ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Figur e 4- 3 6 sh ows t he h ome in t er f ace we define f or t h e Account ent it y bean. Som e ad dit ional Thr ougm hout t he ar book w e ed pr ov e examstples a simp le bank ingionship app licat fin der et h ods e d ,eclar t oiddemon rat e based how won e can t r aver se r elat s.ion w it h an un derly ing r elat ional dat ab ase.
Figu - 3in 6 t.r oduce Accou nt as local hom e ai nt ertferace I n Partr e1, 4we EJBs a p art of Jav 2 En p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or public interface extends javax.ejb.EJBLocalHome { successful imp lem entAccountLocalHome at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also public void create(String CreateException; im plem ent fin der m et h ods, dif fer enaccountID) t m app in g st r at egies, and throws sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. public void create(String accountID, BigDecimal balance, int interest) throws CreateException; public AcountLocal findByPrimaryKey(String key) throws FinderException; public Collection findAllAccounts()
throws FinderException;
public Collection findGoldAccounts(BigDecimal balance) throws FinderException;
public Collection findTransferAccounts(int customerID, String accountID) throws FinderException; public AccountLocal findLargestAccount(int customerID) throws FinderException; •
Table of Content s
} •
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Building Pub lish er:the I BM Account bean class Pub Dat e: April 1 1, 20 03
Figur e I43 70-sh he-1n ew Accoun t b ean class an d t he n ew met hod s t hat it su ppor t s. SBN: 73ows 84- 26t09 Pages: 75 0
Figu r e 4 - 3 7 . Accou nt be an cl ass
public abstract AccountBean extends javax.ejb.EntityBean { Lear n about t he EJB 2 .0 specif icat ion //fields used for CMP Dev elop EJBs w it h St u dio App licat ion Dev eloper public abstract String getAccountID(); Deploy EJBs t o W eb Sp her e Applicat ion Ser v er public abstract void setAccountID(String accountID); This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Apppublic licat ion Developer or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans abstract fBigDecimal getBalance(); ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. public abstract void getBalance(BigDecimal balance); Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r abstract elat ional dat ab ase. public int getInterest(); I n Part 1, weabstract in t r oducevoid EJBs getInterest(int as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic public interest); concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB public 2. 0 sp ecif icat ion t hat pr ov idgetAccountType(); e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed abstract String per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful lem ent atvoid ions ofgetAccountType(String EJBs. publicimp abstract accountType); I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At //fields used for CMR t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. public abstract Collection getCustomers(); public abstract void setCustomers(Collection customers); public abstract Collection getTransrecords(); public abstract void setTransrecords();
//business methods:
public void addCustomer(CustomerLocal aCustomer) { getCustomers().add(aCustomer); } •
public void removeCustomer(CustomerLocal aCustomer) { Table of Content s
•
I ndex getCustomers().remove(aCustomer);
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli } Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
public Pub lish er: I BigDecimal BM
deposit(BigDecimal anAmount) {
Pub Dat e: April 1 1, 20 03
setBalance(getBalance().add(amount));
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
return getBalance();
} public BigDecimal withdraw(BigDecimal anAmount) Lear n about t he EJB 2 .0 specif icat ion throws InsufficientFundException { Dev EJBs w it h St u dio App licat ion Dev eloper ifelop (getBalance().compareTo(amount) == -1) Deploy EJBs t o W eb Sp her e Applicat ion Ser v er throw new InsufficientFundException("Not enough finds"); This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio else setBalance(getBalance().subtract(amount)); App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. return getBalance(); Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an } ing r elat ional dat ab ase. un derly I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he notexshown, but they fun exist! EJB //other 2. 0 sp eciflifecycle icat ion t hat methods pr ov id e, for am ple, en hanced ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or } successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h bean ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At TransRecord entity t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. The TransRecord ent it y bean repr esent s a t r ansact ion on a ban k accoun t .
Building the TransRecord local component interface Figur e 4- 3 8 sh ows t he com ponent int er face w e def in e for t he TransRecord ent it y bean.
Figu r e 4 - 3 8 . Tr a nsRe cor d loca l com pone nt i nt er f ace
public interface TransRecord extends javax.ejb.EJBLocalObject {
public String getTransType(); • •
public void setTransType(String transType); Table of Content s I ndex
public BigDecimal getTransAmount();
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss public void setTransAmount(BigDecimal transAmount); Pub lish er: I BM Pub Dat e: April 1 1, 20 03
public AccountLocal getTheAccount(); I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
public void setTheAccount(AccountLocal theAccount); }
Lear n about t he EJB 2 .0 specif icat ion
Building the TransRecord local home interface Dev elop EJBs w it h St u dio App licat ion Dev eloper Figur e 4- 3 9 sh ows t he h ome in t er f ace we define f or t h e TransRecord ent it y bean. An add it ional EJBs o W eb e Applicat ionion Serrecor v er d t o an accoun t . creat eDeploy m et hod is dteclar edSpt oher add a t r ansact This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Figu r eion 4 -Developer 3 9 . Tr a nsRe locaenl t hom e in tion e rsf ace App licat f or t hecor d evd elopm of ap plicat based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an public interface extends javax.ejb.EJBLocalHome { un derly ing r elat ionalTransRecordLocalHome dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB public 2. 0 sp ecif void icat ion create(Timestamp t hat pr ov id e, for ex transID) am ple, enthrows hanced CreateException; fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful publicimp void lem create(String ent at ions of EJBs.type, String amount, AccountLocal account) I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion andCreateException; t hen imp lem ent en t it y beans, session throws beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif ferfindByPrimaryKey(Timestamp en t m app in g st r at egies, and sim key) p le client s t h at u se t he EJBs. At public TransRecordLocal t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. throws FinderException; }
Building the TransRecord bean class Figur e 4- 4 0 t he new TransRecord b ean class and t he n ew m et h ods t h at it su ppor t s.
Figu r e 4 - 4 0 . Tr a nsRe cor d bea n cl ass
public abstract TransRecord extends javax.ejb.EntityBean { //fields used for CMP • •
Table of Content s
public abstract java.sql.Timestamp getTransID(); I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
public abstract void setTransID(java.sql.Timestamp transID);
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
public abstract String getTransType(); Pub lish er: I BM Pub Dat e: April 1 1, 20 03 public abstract
void setTransType(String transType);
I SBN: 0- 73 84- 26 09 -1
public abstract BigDecimal getTransAmount(); Pages: 75 0 public abstract void setTransAmount(BigDecimal transAmount);
//fields used Lear n about t hefor EJBCMR 2 .0 specif icat ion public abstract Dev elop EJBs w it hAccountLocal St u dio App licatgetTheAccount(); ion Dev eloper public void setTheAccount(AccountLocal theAccount); Deploy abstract EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect //showing how u re, ejbCreate an d d ep loym andent postCreate of su ch ap plicat now ion work s t owith a WebSph the relationships er e App licat ion Serv er. Thr oug hout String t he bookejbCreate(Timestamp , w e pr ov id e exam ples based on a ..., simp leAccountLocal bank ing app licat ion w it h an public transID, theAccount) un derly ing r elat ional dat ab ase. throws CreateException { I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep tsetTransID(transID); s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or setXxxxxx(.....); successful imp lem ent at ions of EJBs. return null; I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, } an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he public en d, w e void d escrib e how t o deploy EJB ap plicattransID, ion s t o a WebSph er e App licat ion Serv er. ejbPostCreate(Timestamp ..., AccountHome theAccount) throws CreateException { setTheAccount(theAccount); } //other lifecycle methods not shown, but they exist! }
Checking and Savings entity beans The Checking an d Savings b ean s ar e subclasses of Account .
•Building •
Table of Contentlocal s the Checking component interface I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Figur e 4- 4 1 sh ows t he com ponent int er face w e def in e for t he Checking ent it y bean. I t inclu des By UeliitWah li, nWout Denay Larinh s Schu Deborah Mart in Weiss only s ow at ter r ibu t e er, and er itnks, all ot h erShaddon met h,ods f rom t he Account b ean . Pubrlish Figu e er:4 -I BM 4 1 . Che ck in g loca l com pone nt i nt e r f ace Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
public interface CheckingLocal extends AccountLocal { public java.math.BigDecimal getOverdraft(); public void setOverdraft(java.math.BigDecimal newOverdraft); Lear n about t he EJB 2 .0 specif icat ion } Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Building the Checking local home interface This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans Figur 4 it 2 ect sh u ows hedhdome in t er f ace f or ion t h es Checking enter it ye bean. I t ion incluServ deser. only ( EJB) ear4-ch re, tan ep loym ent of we su chdefine ap plicat t o a WebSph App licat t he findByPrimaryKey m et hod an d a t ailored creat e m et hod . Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly Figu r eing4 -r elat 4 2 ional . Chedat ckab inase. g loca l hom e in t e r f ace I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed public interface CheckingLocalHome extends javax.ejb.EJBLocalHome { per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. public CheckingLocal findByPrimaryKey(String key) I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session throws javax.ejb.FinderException; beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At public CheckingLocal create(String accountID, BigDecimal balance, t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. int interest, BigDecimal overdraft) throws javax.ejb.CreateException; }
Building the Checking bean class Figur e 4- 4 3 sh ows t he b ean class f or t h e Checking ent it y bean. I t inclu des only t h e addit ion al accessor m et h ods, a t ailor ed ejbCreate m et hod , and an ov er w r it e for t he withdraw m et hod
t hat check s f or t h e over dr aft am ount .
Figu r e 4 - 4 3 . Che ck in g bea n cl ass
public abstract class CheckingBean extends AccountBean { • •
Table of Content s
// new accessor I ndex method
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
public abstract java.math.BigDecimal By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon getOverdraft(); , Mart in Weiss public abstract void setOverdraft(java.math.BigDecimal newOverdraft);
Pub lish er: I BM
Pub e: April 1 1, create 20 03 // Dat tailored
method
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0 public String ejbCreate(String accountID, BigDecimal balance,
int interest, BigDecimal overdraft) throws CreateException { super.ejbCreate(accountID, balance, interest); setAccountType("CHECKING"); Lear n about t he EJB 2 .0 specif icat ion setOverdraft(overdraft); Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er return null; This}I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ch it ect u re, withdraw an d d ep loym ent of to su chcheck ap plicat ionoverdraft s t o a WebSph er e App licat ion Serv er. //aroverwrite method for amount Thr oug hout BigDecimal t he book , w e withdraw(BigDecimal pr ov id e exam ples basedamount) on a simp le bank ing app licat ion w it h an public un derly ing r elat ional dat ab ase. throws InsufficientFundException { I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he if ( getBalance().add( getOverdraft() ).compareTo(amount) == -1) EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or throw new InsufficientFundException("Checking: Not enough funds"); successful imp lem ent at ions of EJBs. else setBalance( getBalance().subtract(amount) ); I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also return getBalance(); im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. } }
Savings interfaces and bean The im plem en t at ion of t he int er faces and bean class f or t h e Savings b ean w ould be sim ilar t o t he Checking b ean .
Deployment descriptor updates I n t his sect ion, w e out line t h e deploy m en t d escrip t or ent r ies for som e of t he ent it ies, r elat ion sh ip s, and fin ders t hat we hav e defined. This is a scaled - dow n v er sion of t h is descr ipt or . A f ull copy can be foun d in Ch apt er 11 , " Cont ainer - m anaged en t it y bean developm ent " on page 39 7. •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper Beans By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
We h ave alr eady seen t h e deploy ment descr ipt or en t r ies f or t he Customer, Account, an d TransRecord Pub lish er: I BMent it y beans in " En t it y bean definit ion in t he deploy m en t d escr ip t or " on p age 12 6. Pub Dat e: April 1 1, 20 03
The Checking an d Savings b ean ar e sim ilar t o t h e Account b ean . Not e t h at t h e CMP fields 73 84- of 26 09 -1 Account b ean an d t heir own f ields. includeI SBN: t he 0f ields t he Pages: 75 0
Relationships We h ave alr eady seen an exam ple of t h e Account-TransRecord 1 : m r elat ion sh ip. Figur e 4- 4 4 show s t h e Customer-Account d ep loym ent descr ipt or ent ry . Lear n about t he EJB 2 .0 specif icat ion
FiguDev r e elop 4 - 4 EJBs 4 . Cust e r -Accou nt Dev r e la t ion sh ip in t he de pl oym e nt w it h om St u dio App licat ion eloper de scr ipt or Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans
( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Customer-Account Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase.
I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep taccounts s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Many per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an dCustomer m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
accounts java.util.Collection
customers Many
• •
Table of Content s
Account I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: customers April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
java.util.Collection
Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o Wexamples eb Sp her e Applicat ion Ser v er Query language This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio I n t his sect ion, w e out line som e of t he quer ies im plem en t ed u sing EJB QL an d h ow t hey ar e App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans defin ed in t he d ep loym ent descr ipt or . ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Customer un derly ing rqueries elat ional dat ab ase. I n Part 1, we in t r oduce EJBs asdaetpail, art findGoldCustomers of Jav a 2 En t er p rise (Ed it ione (4J24EE) We d escrib e only one quer y in Figur 5) . and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Figu e 4 -ent 4 5it.y Cust r essagef in derdrm et hod t he enpr t act deice scrguidelines i pt or f or per sistrence b ean som ande m iv en beans.inWe alsode prploy ovid em best successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session
beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At gold EJB customers (large t he Retrieve en d, w e d escrib e how t o deploy ap plicat ion s t o a balance). WebSph er e App licat ion Serv er.
findGoldCustomers
java.math.BigDecimal
select distinct object(c) from Customer c, in(c.accounts) a where a is of type (Savings) and a.balance > ?1
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
This q uery st ar t s wit h a cu st omer inst an ce, f ollow s t he r elat ion sh ip t o accou nt s, t h en t est s t he t y pePub oflish account er: I BM , and comp ares t h e balance t o t he p ar amet er . The q uery illust r at es t he use of a r elatPub ionDat she:ipApril , using in(c.accounts), w her e accounts is t he CMR f ield for all r elat ed accou nt of a 1 1, 20 03 cust om er . Th e qu er y also show s t h e use of inh er it ance, using is of type (Savings), t o select I SBN: 0- 73 84- 26 09 -1 only sav ings account s. Pages: 75 0
The findHighInterest q uer y ret u rn s cust om er s t h at h ave account s w it h a high int er est r at e and at least one t r ansact ion r ecord of t y pe cr edit . This q uer y follow s t w o r elat ionships f r om cust om er t o account s t o t r ansact ion recor ds:
Lear n about t he EJB 2 .0 specif icat ion select distinct object(c) from Customer c, in(c.accounts) a, Dev elop EJBs w it h St u dio App licat ion Dev eloper in(a.transrecords) t Deploy EJBs t o W eb Sp her e Applicat ion Ser v er where a.interest > ?1 and t.transType = 'C' This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. The ejbSelectAllCustomerNames q uer y ret r ieves t h e collect ion of last n am es of all cu st om er s. Thr hout he book e pr ovm id et e exam based a simp le of bank ingf ields: app licat ion w it h an Thisoug is an extamp le of, awselect hod t hples at r et ur ns on a collect ion CMP un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep and t h e ar chfrom it ect urCustomer e. I n par t icu selectt so.lastName o lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. The ejbSelectNumberOfAccounts is a select m et hod t hat r et riev es all t he account num b er s f or Ione n Part cu st2, omwe er :in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. select a.accountID from Customer o, in(o.accounts) a where o.lastName = ?1
Account queries The findAllAcounts q uer y ret u rn s all accou nt s:
select object(o) from Account o
The findGoldAccounts q uer y ret u rn account s wit h a high balance: •
Table of Content s
•select object(o) I ndex from Account o where o.balance > ?1 EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
ThePub findLargestAccount q uer y ret u rn s t he accoun t w it h t he lar gest b alan ce for on e cust om er . lish er: I BM This q uery uses a subselect t o r et r iev e t he lar gest b alance, follow s t he r elat ionship t o cust om ers Pub Dat e: April 1 1, 20 03 t o ch eck f or t he given cust om er k ey, and t hen follow s t he relat ionship fr om t he cust om er t o all I SBN: 0- 73 84- 26 09 -1 accou nt s t o fin d t he accou nt w it h t h e lar gest balance. This m eans t hat t he Customer-Account Pages: r elat ion sh ip75is0 t r av ersed t wice.
select object(a) from Account a, in(a.customers) c where = 2?1 and a.balance = Learc.customerID n about t he EJB .0 specif icat ion Dev elop ( select EJBs w itmax(a1.balance) h St u dio App licat ion from Dev eloper in (c.accounts) a1 ) Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides dqet ailed at ion h ow t o ively here St udio The findTransferAccounts uer y retinufor rn m s all t heon accoun s tef o fect w hich a use cu st WebSp omer can t r ansfer App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans m oney fr om a giv en account . This qu er y also t rav erses t he Customer-Account r elat ionship (t w EJB) ch it ect re, an dt dt oepcust loym ent sudchf r om ap plicat s t ocust a WebSph App licats. ionFinally Serv er. ice, ar fr om t heu accoun om er sofan t he gion iven omer t o er alle account , t he or iginal account is discard ed f r om t he list t o only r et ur n ot her account s of t he cust om er . Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1,object(b) we in t r oduce EJBs as a p art Jav a 2 En t er p rise Ed ion ( J2 EE) and cover select from Account a,ofin(a.customers) c, itin(c.accounts) b t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat= pr ovand id e, for ex am ple, en=hanced fun ct ion alit y for cont ain er - m anag ed where c.customerID ?1 a.accountID ?2 per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful impa.accountID lem ent at ions of EJBs. b.accountID and I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plemqu ent der m et h ods, ferpen t er m app in g QL. st r atSuch egies,quand sim s t h atd uone se twhe These er yfinexam p les showdif t he ow of EJB er ies w perlee client n ot easily it hEJBs. t he At tim heplem en d,ent w at e dion escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. pr ov ided by EJB 1. 1.
Home method I n t he Customer class, w e can im plem en t a hom e m et hod t hat uses t he t w o select m et hods t o r et ur n t he list of cust om er s an d t heir accoun t s. The ejbHomeGetAllCustomers m et hod is im plem en t ed in t he Customer b ean t o r et u r n a vect or of cust om er in for m at ion. Fir st t he ejbSelectAllCustomerNames m et hod is called t o ret r ieve all t he nam es. For each nam e, t he ejbSelectSumberOfAccounts m et hod is called t o ret r ieve t he
accou nt num ber s of t he cu st omer . The in for m at ion is ret ur ned as an ar ray of st r ing s t hat is add ed t o t he r esu lt v ect or . The act u al im plem ent at ion of t h e met h od is show n in " Dev elopin g a hom e m et hod" on p age 46 3.
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Entity bean design and construction best practices I n t his exam p le, w e in t rod uced a cou ple of best p ract ices t hat w e w ant t o sum m ar ize in r elat ion t o t he ad van ced en t it y bean con cep t s: •
Table of Content s
U se of Ilndex oca l in t er fa ces— W e m odeled t he ent it y beans t o be u sed only w it hin t he JVM om here nt prwocesses, in trhe isSt case, ot her s.elNot EJB 2 . 0fr D e veot lopm it h W ebSphe udio Appl ica tib onean De v opere t hat t his is n ow a requ irem en t of ou r beans becau se of t he nav igabilit y in our r elat ionships. By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss •
Ta il ore d a cce ssor m e t hods f or m a ny - t o- m any re la t ion shi ps— To simp lif y client we su ggest y ou define t ailor ed add and r em ov e m et h ods t o add and delet e Pub Datances e: April of 1 1,m 20: 03 inst m r elat ion sh ip s ( f or ex am ple, addAccount f or t h e Customer b ean ) .
Pubpr lish er:am I BMm ing, ogr
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Client view of advanced entity bean concepts I n t his sect ion, w e look at t h e client v iew wh en d ealing w it h relat ionships, in herit an ce, an d cust om f in der s. •
Table of Content s
•
I ndex
Relationship programming EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
We h ave p r omot ed some relat ionship- b ased m et hods t o t he com pon en t in t er f ace an d added som e addit ional find er s t o t he hom e int er faces t hat pr ovid e us w it h add it ion al bu sin ess logic t h at er:pose I BM t o t he cust om er. w e Pub canlishex Pub Dat e: April 1 1, 20 03
Alt hou gh w e0- und I SBN: 73 84-er 26st 09and -1 t h at behin d t he scenes t h ese r elat ionships ex ist and t her e w ere com plex m appin gs t o est ab lish t hem , in realit y , t o t he EJB client s, t h ese CMR field s ju st look like Pages: 75 0 collect ions of ot h er EJBOjbect r efer ences. Let us look at client pr ogr am m in g f or a 1: m r elat ionship.
Traversing a "one" relationship Lear n about t he EJB 2 .0 specif icat ion WhenDev lookin elopg EJBs at t hew Account-TransRecord it h St u dio App licat ion Dev r elat eloper ionship fr om t h e TransRecord side, t h e theAccount is just anot her f ield ( act ually a CMR field) . Deploy EJBs t o W eb Sp her e Applicat ion Ser v er The CMR access m et h od of getTheAccount an d setTheAccount ar e n ow accessib le fr om t h e This I BM Redbook ovides et ailed in for m on wh eowdot on ef ively WebSp heret oStget udiot he TransRecord. I f w eprhav ea d TransRecord inat stion an ce, otfect r equir e tuse h e accountID App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans Account t hat t his t r ansact ion is r elat ed t o; w e j ust use t h e accessor m et h od of t he TransRecord: ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elataccount ional dat ab AccountLocal = ase. transrecord.getTheAccount(); I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Traversing a it"many" per sist ence ent y b ean s relationship and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. When lookin g at all t h e t r ansact ion r ecor ds for an account t hen t he accessor m et hod of t he Iaccou n Partnt2,retwe in t r oduce t h eion sam p lewbank ingt oapplicat lemion entt oenaccess t it y beans, r ieves a collect and e h ave it erat eion t hr and oug ht hen t h e imp collect each session beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also r elat ed t r ansact ion r ecor d : im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. AccountLocal account = accountHome.findByPrimaryKey(accountID); Collection coll = account.getTransrecords(); Iterator collit = coll.iterator(); while ( collit.hasNext() ) { TransRecordLocal tr = (TransRecordLocal)collit.next();
String timestamp = ((java.sql.Timestamp)tr.getPrimaryKey()).toString(); String creditOrDebit = tr.getTransType(); java.math.BigDecimal amount = tr.getTransAmount(); ... •
Table of Content s
•
I ndex
}
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Not e t h at y ou hav e t o cast t he it em s in a collect ion t o t he cor r ect t y pe. Pub lish er: I BM
Tr aver sin g in an y dir ect ion of an m: m relat ionship is t he sam e as t rav ersing in t he m any Pub Dat e: April 1 1, 20 03 dir ect ion of a 1: m r elat ion sh ip. A collect ion of r elat ed beans is ret u rn ed and it er at ion t h r ough t he I SBN: 0- 73 84- 26 09 -1 collect ion is requ ired. Pages: 75 0
Programming with a collection Here is a subset of t h e m et hods t hat y ou can u se on a Jav a Collection: Lear n about t he EJB 2 .0 specif icat ion size— Ret ur ns t h e nu mb er of elem en t s Dev elop EJBs w it h St u dio App licat ion Dev eloper isEmpty— Ret ur ns t r ue if t he collect ion has no elem ent s Deploy EJBs t o W eb Sp her e Applicat ion Ser v er iterator— Ret ur ns an it erat or t o g o t h rou gh t he collect ion This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f oran t he ev elopm en t ofall aptplicat ion en s based on t h e Ent er pr ise Jav aBeans toArray— Ret ur ns ardr ay con t aining h e elem ts ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. contains(Object o)— Ret ur ns t r ue if t he obj ect is cont ained in t h e collect ion Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase.
Inheritance I n Part 1, we in tprogramming r oduce EJBs as a p art
of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 0 sp ecif ion t hat pr ov id e, forstex amur ple, en hanced fun ct y fer for en cont - mbanag Pr ogr2.am min g icat ag ainst an inher it ance r uct e r equir es dealin g ion w italit h dif t t yain peserof ean sed t hat pere sist y rbiev ean iv en We also pr ovid e best pr act ice guidelines f or ar hanence dled ent or it r et eds and u sin gmtessagehe sam dr em et h beans. ods. successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session Testing the object type beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At Wit ance ehierar , one of t heapmplicat ost com rog r ammering t ask s ision t o Serv t est er. wh at k ind t he henan d, inher w e d itescrib how chy t o deploy EJB ion smton o apWebSph e App licat of obj ect is r et u rn ed f rom a r et r iev al. For exam ple, w hen w e r et r ieve an Account b y pr im ary key w e w ant t o kn ow if w e got a Checking or a Savings accoun t . The instanceOf con st r uct is used t o check t he obj ect - t yp e:
AccountLocal account = AccountLocalHome.findByPrimaryKey(...keyvalue...); if (account instanceOf CheckingLocal) { CheckingLocal checking = (CheckingLocal)account;
BigDecimal overdraft = checking.getOverdraft(); ... } •
Table of Content s
•When w e follow I ndex a r elat ion sh ip t o Account ( for ex amp le fr om Customer) , w e m ay also h ave t o EJB 2 . 0ig D at e ve e ntkind w it hof W ebSphe r e sStare udioret Appl ti on De v el oper inv est e lopm w hat account ur ica ned: By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM
CustomerLocal cust = CustomerLocalHome.findByPrimaryKey(...keyvalue...); Pub Dat e: April 1 1, 20 03
I SBN: 0- 73 84- 26= 09 -1 Collection coll cust.getAccounts(); Pages: 75 0
Iterator collit = coll.iterator(); while ( collit.hasNext() ) { AccountLocal account = collit.next(); Lear n about t he EJB 2 .0 specif icat ion if (account instanceOf CheckingLocal) { Dev elop EJBs w it h St u dio App licat ion Dev eloper CheckingLocal checking = (CheckingLocal)account; Deploy EJBs t o W eb Sp her e Applicat ion Ser v er ... This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App}licat ion Developer f or t instanceOf he d ev elopm en t of ap plicat else if (account Savings) { ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. SavingsLocal savings = (SavingsLocal)account; Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. ... I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic } concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed } per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an dobjects m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Creating im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At tWhen he en creat d, w eing d escrib e ect howwtitohin deploy o a itWebSph er eant App ione Serv er. rect t y pe an obj an inEJB heritap anplicat ce stion r ucts ut re, is imp ort t olicat cr eat t he cor of obj ect and t o in it ialize t h e super class f ields and t he sub class f ields. The b est w ay t o h andle t his is t o hav e t ailor ed create (ejbCreate) m et hod s for each t y pe of bean. For ex am ple, t he ejbCreate m et hod of t h e Checking b ean calls t he ejbCreate m et hod of t he Account b ean b ef or e set t ing it s ow n dat a:
public String ejbCreate(String accountID, BigDecimal balance, int interest, BigDecimal overdraft) throws ... {
super.ejbCreate(accountID, balance, interest); setOverdraft(overdraft); return null; } •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Not e t h at if all account s mu st be eit her Checking or Savings accoun t s, t hen t he sup er class By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss Account sh ould n ot h ave a pu blic create m et hod in t he h om e int erf ace. Only sub class beans can be creat ed by u sing t h e create m et hod on t he su bclass h om e int erf aces. Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Using custom finder methods Pages: 75 0
Cu st om find er met h ods r et ur n eit her a sin gle EJBObject or a collect ion of EJBObjects. Sing le EJBObject ar e h andled as in t he findByPrimaryKey m et hod . When r et r ievin g a collect ion, t h e pr ogr am m in g is id en t ical t o t r av ersing a man y r elat ionship . For exam ple, t h e findGoldCustomers m et hod r et ur ns a collect ion of Customer ob ject s: Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Collection coll = customerHome.findGoldCustomers(balance); Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Iterator collit = coll.iterator(); This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans while ( collit.hasNext() ) { ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. CustomerLocal cust = (CustomerLocal)collit.next(); Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. int customerID = ((CustomerKey)cust.getPrimaryKey()).getCustomerID(); I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic String = ch cust.getName(); concep t s andname t h e ar it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed ...ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or per sist successful imp lem ent at ions of EJBs. } I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Summary I n t his chapt er , w e cov er ed t he ad van ced con cept s of en t it y beans: relat ionships, in herit an ce, and f in der m et hods u sin g t he EJB qu er y lang uage. •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Chapter 5. Session beans This chap t er descr ibes t he con cept s of session beans, an d in t r oduces t he t wo t y pes of session beans: st at ef ul an d st at eless. We t hen ex pan d t h e r eq uir em ent s of t he online b ank ing ex am ple pr Ch apt 2, " EJB • esent ed in Table ofer Content s ar chit ect u r e and concep t s" on page 33 t o show how t o dev elop a session b ean . Finally, som e b est pr act ices for session bean dev elopm ent and t he client view of a • I ndex session b ean ar e d iscussed . EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By li, Wout er Denay er, Lar s Schu Deborah Shaddon Weiss At Ueli t heWah conclusion of t his chapt ernk , y, ou sh ould hav,eMart a ginood u nder st anding
of w hen session b ean s ar e app rop riat e ch oices for imp lem ent ing bu sin ess logic in y our EJB app licat ions. Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Introduction Session beans ar e bu sin ess logic b ean s t hat im plement t he t ask s or w or k flow of a syst em , and pr ov ide coor dinat ion of t hose act ivit ies bet w een ot h er beans. An ex am ple is a bank ing ser v ice t hat allow s for a t r ansfer b et w een accoun t s ( or account ent it y beans) . Som et im es session beans •act as a facad Table of Content s ent it y bean fr om it s client , as discu ssed in pr ev iou s chapt er s. e t hat hides an •Session beans I ndex of t en ar e seen as ex t en sion s t o t he client pr ogr am m ing m odel, in t h at t hey EJB . 0 D et h veelopm e nt s. w itOf h W ebSphe r e St udio Applar ica on De v pr ov2 ide object t en, session beans e tiused t oel oper m odel u se- case int eract ions of an app licat ion. Un er likDenay e enter, it yLar b ean s nk t hat r ep resen t concr dom ain con cept s, or noun s of a sy st em , By Ueli Wah li, Wout s Schu , Deborah Shaddon , Martet in eWeiss t he session beans can r epr esent t he v erb s, or pr ocesses, of t he syst em . Pub lish er: I BM
As w e saw in t he pr ev ious chap t er , en t it y beans allow u s t o obj ect ify sh ared dat a t hat is st ored Pub Dat e: April 1 1, 20 03 per sist ent ly , of t en in a d at abase. The cont ain er m an ages t he concur r ency of access t o t his 73 8426 09 shar edI SBN: d at a0-in a saf e -1 w ay. Session b ean s m ay also dir ect ly access d at a in a d at abase, t o Pages: 75 0p art icular t ask at han d, or m ay in t er act w it h ent it y b ean s t hat access t he dat a. com plet e t he Ch oosing t o access dat a using ent it y beans or session b ean s depends on t he cont ex t , and w e pr ov ide som e guid elines lat er on in t his book .
Lear n about t he EJB 2 .0 specif icat ion
* Ne w EJB 2 .0
Dev elop EJBs w it h St u dio App licat ion Dev eloper EJB2 .0 h as int r oduced no sign ificant ch anges t o t he session bean com pon en t m odel itDeploy self , w it h t het oex ion em ot e and local EJBs Wcept eb Sp herofe rApplicat ion Ser v erhom e and comp onent int erf aces, an d creat e m et hod signat u r es, w hich ar e applicable t o all ent erp rise b ean t y pes. Reader s This Ifam BMiliar Redbook w it h EJB pr ovides 1. 1 concept d et ailed s may in forch m oose at ion t on o skim h ow over t o ef fect t h is ively ch aptuse er . WebSp How ev er, heret he St udio App licat exam ionples Developer p resen t fed or ht he er edwevillelopm f orm en t he t of b asis ap plicat f or tion he sfutbased ur e discu on t ssion h e Enton er pr MDBs, ise Jav aBeans ( EJB)t rar ansact ch it ect ions, u re,and an dsecur d ep loym it y . ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Session bean concepts Session beans ar e non - per sist ent en t er p rise beans, in t hat t hey d o not sur viv e soft w ar e or har dw ar e cr ash es, as is t he case w it h ent it y beans. Ther e ar e t w o t y pes of session beans, st at eful session beans an d st at eless session beans. •
Table of Content s
•Session beans I ndex m ay coor dinat e t he w or k bet w een ent it y b ean s, and pr ovid e a cont ex t f or w hich EJB e ve lopm e nt wisit h W ebSphe r e St udio Applp ica Detvask el oper ent it2 y. 0 bDean access per for m ed. For exam le,ti on t he associat ed wit h t r ansf er r ing m oney bet w een t w o bank accoun t s—r epr esent ed by ent it y beans—can b e im plem ent ed in a session By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss bean. Such a t r ansfer session bean h as t o find t w o in st ances of t he accou nt en t it y bean ( by u sing t he accoun t I Ds) , an d t hen sub t r act a specified amou nt fr om one accoun t an d ad d t he sam e Pub lish er: I BM am ount t o t he ot h er accoun t . Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0 Session bean types
As w e m ent ioned, t here are t w o t y pes of session b ean s: st at eful and st at eless. St r uct ur ally, t hese bean t y pes m ay appear sim ilar , b ut w hat d ist in guishes t hem is h ow t h ey ar e int en ded t o be u sed , and w het her a con ver sat ion al st at e is m aint ain ed bet w een t h e bean and t he clien t of t he bean. St at ef ul session beans r epr esent a conv ersat ional st at e w it h a sin gle clien t , and ar e Lear n about t he EJB 2 .0 specif icat ion act ive for t h e life of t h at conv ersat ion. St at eless session b ean s ar e not dedicat ed t o a p art icular client Dev , and ar eEJBs int ended t oube sed across m ult ip le client s. elop w it h St dior eu App licat ion Dev eloper The sign ificant d iffter bether w een t h ese tion w o Ser t ypves Deploy EJBs o ence W eb Sp e Applicat er is in h ow t he con t ainer m anages t h eir life cycle. St at eless session beans m ay b e inst ance pooled and reused as need ed am ong m any dif ferIen t s. Conv ersely,d et st ailed at ef ulinsession beans arow e ntot inst anively ce p ooled , in t hat t heStcont This BMt clien Redbook pr ovides for m at ion on h o ef fect use WebSp here udioainer m ustlicat allocat e t h e sam ef or b ean ce f or mplicat et hodion inv ionon ont ht he eEnt bean. YouJav r aBeans App ion Developer t he inst d evan elopm en each t of ap s ocat based er pr ise r( espon sib iesuas bean developer ar esuinf enplicat ced by cycle r eq em ent s ion of t Serv he session EJB) ar chilit it ect re,aan d d ep loym ent of chluap iont sh et olifea WebSph eruir e App licat er. bean t y pe t h at y ou are developing . Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase.
Stateless session beans I n Part 1, we in t r oduce EJBs as a p art
of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he St at eless beans conple, ver sat st at ew h alit a client Th eyain arer e -pmooled EJB 2. 0 spsession ecif icat ion t hatd oprnot ov idme,aint forain ex am en ional hanced fun ct it ion y for. cont anag ed inst ances r eu sed by t h e cont ainer t o ser v ice m ult ip le client s. Their ident it y t o t h e client is t he f or per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines sam e regar d less of w hich b ean inst an ce is loan ed out f rom t he p ool t o ser vice t hat client . We successful imp lem ent at ions of EJBs. descr ibe t hese con cept s in m or e det ail b elow . I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, Non conversational statedif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. A st at eless session bean holds con ver sat ion s t hat span a single m et hod call, w hich is t he sam e as say in g t hey hav e no conv er sat ional st at e. Aft er each met h od call on a st at eless session bean, t he cont ain er releases t h e bean back int o t he pool and t he b ean inst ance b ecom es available so t hat it can ser ve a new r equ est . Th is bean has no client m em or y of t he pr ev ious m et hod inv ocat ion. Even if t he sam e client inv okes anot her m et hod on t his sam e EJBObject, t h er e is n o gu ar ant ee t hat t he ex act sam e bean in st ance w ill be u sed . When t he sam e client is inv ok in g anot her m et hod, a d if fer en t st at eless session bean inst ance m ay act ually serv ice t he n ew requ est . Therefor e, st at eless session beans ar e n ot allow ed t o st or e an y conv er sat ional st at e f r om m et hod t o m et hod .
For exam ple, if a client calls methodA on a st at eless session b ean , and t hen im mediat ely calls methodB on t h at sam e bean, t hose r eq uest s m ay be serv ed b y a d iff er ent bean in st an ce. A st at eless session bean d oes not m aint ain an y st at e on behalf of a clien t . This is im por t an t t o r em em ber, becau se one of t h e m ost comm on m ist ak es people m ak e w hen developing st at eless session b ean s is in t r ying t o incorr ect ly cache dat a t hat is reused bet w een m et h ods inv ocat ions.
Other member • Table variable of Content sstate •
I ndex
EJB 2 . 0 gh D e ve lopm e ntbeans w it h W may ebSphe r e St Appl ti on De vst el oper Alt hou session not st udio or e an y ica inst ance at e dat a t hat is specif ic t o a par t icular client , t he b ean is allow ed t o st ore ot her inst ance v ar iab le st at e bet w een m et hod inv ocat ion s, if By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss needed. Th e r ule her e is t hat t his d at a cann ot , or shou ld n ot , be d ep en dent on an y par t icular client . Pub lish er: I BM
e: m April 1, 20 ThisPub is Dat com on1wit h 03 dat a t h at m ight be n eeded t o in it ialize t he bean, or w it h d at a such as an I SBN: 073 8426 -1ion, w here t he conn ect ion cou ld b e used b et w een met hod inv ocat ions. open dat ab ase conn09 ect The t hing Pages:t o75r 0em em b er , h owev er , about open dat abase conn ect ions is t hat t hey m ust b e event u ally closed if t h e bean goes aw ay. Theref ore, t h e bean pr ovid er mu st r em em ber t o pr ovide t he means t o close t his connect ion w hen t h e bean is r em ov ed f rom mem or y, su ch as in t h e bean' s ejbRemove m et hod .
Lear n about t he EJB 2 .0 specif icat ion
Not e EJBs w it h St u dio App licat ion Dev eloper Dev elop We h aveEJBs n ot m ioned it ey et t o t hision p oint ut no EJB ( st at efu l, st at eless, message, Deploy t oent W eb Sp her Applicat Ser, vber ent it y ) m ay h ave st at ic v ar iables. So alt hou gh t he m em ber v ariable in it ializat ion m ay ies of t hpr e ovides same ddat b et ween inat st ion ances, ey t mu st ill be use r egular v ar here iables, not This Ibe BMcop Redbook etaailed in for m on ht how o efstfect ively WebSp St udio st ation ic vDeveloper ariab les. This se t heenuse at ic vion ar siab les in on t er fter w iterhprt he App licat f or tishebecau d ev elopm t ofofapstplicat based h eesEnt ise Jav aBeans ainer ab ilit p rop er ent ly mof anage heplicat EJBs.ion The specif icat descr h is, er. ( EJB)cont ar ch it ect's u re, anyd tdoep loym su ch tap s t oEJB a WebSph erion e App licatibes ion tServ and ot h er , r est rict ions on EJB behav ior t h at should be avoided. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Stateless session identity concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed All ancesent of itsty at session beans dr criv eat fr om tWe h e same ectprare id enguidelines t ical fr om ftor he per inst sist ence b eless ean s and m essageenedbeans. also prhom ovideeobj best act ice client per sp ect iv e, and t he con t ainer can u se an y av ailab le in st ance t o sat isfy a client r eq uest . successful imp lem ent at ions of EJBs. II fn aPart clien2,t hwe as inhand les t ot htew sam o d iff cop of a st ion at eless b ean cr eat t he session sam e t r oduce p er le ent bank ingiesapplicat and session t hen imp lem ent ened t it yfr om beans, hom e ob t he isIdentical m etusing hod ofWebSph t h e comerpeonent in tAp er fplicat ace alw s r et ur nertrue. beans, anject d m, essag e- d r iv en beans St u dio ionay Dev elop . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Instance pooling St at eless session beans ar e eff icient because t he cont ain er can use a small num ber of pooled inst ances t o ser ve a lar ge nu m ber of client s. Recall t hat ent it y beans w er e an ent erp r ise b ean t y pe t h at could be in st ance pooled. St at eless session b ean s m ay also be p ooled by t he cont ain er , an d r eused an d m an aged in a w ay sim ilar t o t hat of ent it y b ean s. I n st an ce pooling is a serv ice of t he cont ain er t hat allow s t he cont ainer t o r eu se bean in st an ces, as op posed t o creat ing new on es ev ery t ime a r eq uest f or a b ean is m ade. This is a per for m ance opt im izat ion t hat is done b y t he con t ainer. This allow s for a scalab le env iron ment , because t his p ool size can be in cr eased as n eeded, by eit her m anipu lat in g t he
cach e siz e of t he cont ain er , ad ding m ore mem or y, or m ore machin es. Rem emb er t hat EJB client s nev er deal dir ect ly w it h bean inst ances; t hey alw ays do so fr om t h e cont ainer ( t h rou gh t he EJBObject) . Th e cont ainer is f ree t o pool an d reuse b ean inst ances as needed and hide t h e im plem ent at ion specif ics fr om t h e client s.
• •
Not e
Table of Content s I ndex
EJB 2 .Session 0 D e ve lopm e nt w W ebSphe r e St udio Applex ica ti on ly De vr el bean initsth ance pooling is not plicit eqoper uir ed
by t he EJB specif icat ion . b ean inst an ce p oolin g.
By UeliWebSph Wah li, Wout er er e Ap Denay plicat er, Lar ions Schu Serv nker , Deborah supp orShaddon t s st at, eless Mart in session Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: session 0- 73 84- 26 09 -1bean Stateful Pages: 75 0
St at efu l session b ean s m aint ain con ver sat ional st at e w it h a client . Th ey m u st belong t o on e and only on e client , in f act , concur rent access t o t he sam e st at eful session b ean w ill r esult in except ions bein g t hr ow n b y t he con t ain er . They are not pooled inst ances t o be r eused by t he cont ainer , because t he con t ainer m ust k eep t h at one in st ance ser v icing t h e sam e client f or t h e lengt h of t he con ver sat ion . The con t ainer m ay st ill ch oose t o opt im ize access t o st at eful session beansLear t hr ough t h et he useEJB of passivat ion and n about 2 .0 specif icat ionact ivat ion . The id en t it y of indiv idual st at ef ul b ean s is not t he sam e t o t he clien t , ev en if t heir inst ance st at e is t he equiv alen t . We d escr ib e t hese Dev EJBsd et w it St u dio App licat ion Dev eloper concep t s elop in m ore ailh below. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Conversational state This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans A st at ef conv ersat ions t hat canion span m et hod A tion y pical ( EJB) arul chsession it ect u re,bean an d hd olds ep loym ent of su ch ap plicat s t omaany WebSph er e calls. App licat Serv er. app licat ion holds con ver sat ion s w it h client s at var ious lev els. A con ver sat ion is t he int er act ion bet een a client and ,an andples it can r epron esent oneleorbank m oring e bapp usinlicat ess ion p rocesses. Thr w oug hout t he book w eapplicat pr ov id eion, exam based a simp w it h an This int act ing ion of t enional r equir esabt hat un er derly r elat dat ase.conv er sat ional d at a b et w een t he client and t h e applicat ion sh ould be st or ed and accessed, wh en ev er needed. An exam p le of con ver sat ion al dat a is a shop ping car t n Part 1,ains we all in t rtoduce EJBs Javer a 2w ant En tsertporise Edase. it ion Th ( J2isEE) coverknt he basic tI hat cont he p rod uct sas t h aatpaart cuof st om p ur ch datand a is also ow n as concep t sd at and session a. t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed All h e ence m et hent od it calls t hats and com e om a sin gle ar eWe seralso v ed pr b yovid t heesam bean. Dur ing t his f or per tsist y b ean m fr essagedr iv enclient beans. beste pr act ice guidelines conv er sat ion, he bean holdsofcon ver sat ion al st at e f or t hat client . I n ad van ced cases, t he successful imptlem ent at ions EJBs. conv er sat ional st at e may con t ain op en r esou rces, such as open d at abase connect ions. The Iconv n Part 2, ional we inst t rat oduce e at sam bank ing applicat t hen impinst leman ent t it ys. beans, er sat e of at hst efulp le session b ean is st orion ed and in t he b ean ce en f ield For t hesession m ost beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also par t , t his dat a m ust b e ser ializ able ( m ore on t his lat er) . im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At Wit at w eful he contEJB ainer ust ion allocat sam e er bean inlicat st anion ce for each t he henstd, e dsession escrib e beans, how t o tdeploy apm plicat s t oeat he WebSph e App Serv er. met h od call. A new in st an ce is cr eat ed each t ime a client inv ok es create on t h e hom e int er face. Wit h st at eful beans, y ou can save dat a in t h e bean in st ance so t hat t his dat a w ill st ill be available on t he next m et h od call.
Other member variable state Ther e is no dist inct ion b et ween t he r ole of m em ber v ar iable t y pes, as t her e is w it h st at eless session b ean s. Any inst an ce m em ber v ariab le is par t of t h e conv er sat ional st at e, ev en if t his v ariab le is not direct ly depend en t on t he client . Th er efor e, such t hing s as open dat ab ase connect ions m ay also b e par t of t h e st at e of a st at ef ul session bean, as in t h e st at eless exam ple
abov e. The cav eat her e is t h at open dat ab ase con nect ion s ar e not ser ializab le. As w e w ill see lat er , it is t he bean p rov ider 's r esponsibilit y t o en su re t hat t he cont ainer can ser ialize it s m em b er v ar iables w hen it passivat es t he bean. This can b e achieved, f or inst an ce, b y mak ing t h ese f ields transient, an d/ or b y set t in g t h em t o n ull. I t is u p t o t he developer t o en su re t hat t he b ean st at e can be pr op er ly passivat ed, or pr epar ed f or ser ializat ion, as w ell as act iv at ed , t h at is, r eb uilding f rom ser ializat ion . •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Stateful session identity
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
All inst ances of st at eful session b ean s cr eat ed fr om t he sam e h ome object ar e u nique fr om t h e Pub lish er: I BM client per sp ect iv e. Pub Dat e: April 1 1, 20 03
I f a clien I SBN: t h as 0- 73hand 84- 26les 09 -1t o t w o d iff er ent st at ef ul session beans cr eat ed f rom t he sam e hom e obj ectPages: , t he isIdentical m et hod of t h e com p onent in t er f ace will alw ay s r et ur n false. 75 0
Activation and passivation En t it y beans p art icipat e in t h e act ivat ion and passivat ion life cy cles, w hen en t it y beans are Lear about t he EJB .0 specifed icat loaned outn of t he pool and2associat wion it h an EJBObject. Th e pur pose of act iv at ion an d passivat ion w as t o pr ep ar e t he ent it y bean t o be associat ed w it h t he act ual dat a, an d allow ed for elop ion EJBs it h bSt u dio any inDev it ializat of wt he ean t o App occulicat r. ion Dev eloper Deploy EJBs W eb Sp her ter ansit Applicat Seroccu v er r f or st at ef ul session beans, alt h ough t he The act iv at ion andt opassiv at ion ion sion also pu rp ose is sligh t ly diff erent fr om ent it y beans. Passiv at ing of session beans occu r s w hen t he This ovides dbean et ailed in for on h ow yt ostef ively udio st at eI BM of t hRedbook e st at ef ulprsession is sav edmtat o ion a secondar orfect age, so tuse hat WebSp t he conthere ainerStcan App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans conser v e or f r ee u p r esour ces. This g en er ally occur s if t he b ean has been idle f or some amou nt (ofEJB) ar ch ectpassivat u re, an dion d ep loym ent or ofy su ch ap plicat o a WebSph e App ion ed. ServI er. t im e. Aftiter , t he mem occu pied b y ion t hesbtean inst anceercan be licat r eclaim f t he bean is needed ag ain ( an d b ef or e it t im es out ) by t he or iginal client , t h en it is act iv at ed back Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an int o t he cont ain er ' s m em or y . The cont ain er may act iv at e and p assiv at e st at ef ul session beans as un derly ing r elat ional dat ab ase. necessar y t o m anag e it s r esou rces. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic The con t ain er can not passiv at e an inst ance if t hat inst ance is inv olv ed in a t r ansact ion. Th e concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he t r ansact ion h as t o com plet e f ir st . We descr ibe t r ansact ional consider at ions f or session b ean s in EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed " Tr ansact ion s" on p age 27 9. per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Not e im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en w e d escrib e how t o deploy ap plicat s tivat o aion WebSph er e Appion licat iont Serv Std, at eless session beans d o not EJB par t icipat e inion act and passivat , bu m uster. st ill im plem ent t he act iv at e and p assiv at e callb ack m et hod s.
Serialization We h ave alr eady d iscussed som e guid elines t o follow w hen det erm inin g if t h e st at ef ul session beans mem ber v ariab les can be p assiv at ed . We d escr ib e some addit ional scenar ios her e. I n ord er for a con t ainer t o man age eff icient ly a larg e nu mb er of bean in st an ces, it can t ak e an
inst ance ou t of m emor y an d st or e it in p er m anent st orag e. The cont ainer u ses ob ject ser ializat ion t o conv er t t he b ean st at e in t o a binar y st r eam or BLOB ( binar y lar ge obj ect ) . I t can t hen w r it e t he serialized d at a t o per m anent st or age. As we ment ion ed , t his is called passiv at ion. When t his b ean is inv ok ed ag ain , t he con t ainer creat es a n ew inst ance and init ializes it w it h t he dat a saved dur ing passivat ion . This is called act iv at ion. Th er efor e, if t he session bean con t ains a conv er sat ional st at e t hat mu st be pr eserv ed bet w een m et h od in vocat ions, t h e session b ean indicat es st at eful m anagem en t m ode. •
Table of Content s
•I t is t he beanI ndex pr ov id er ' s r espon sib ilit y t o not only ensur e t h at t h is dat a can be passiv at ed, b ut EJB 2 . 0 ed D e ve lopm e nt Let w it h ebSphe r e St Deefv ul el oper act ivat as w ell. 's Wuse as an exudio am Appl ple tica h eti on st at session
bean t hat has t o m aint ain an open datUeli By ab ase Wah li,con Wout nect er Denay ion . er, Most Lar slik Schu elynk , ,t hDeborah is connect Shaddon ion, Mart w asinob Weiss t ained w h en t h e bean was fir st cr eat ed, for inst ance, in ejbCreate. Befor e t h e bean is passiv at ed, t he dev elop er m u st m ake su re t he connect ion Pub lish er: Iis BMclosed ( w h et h er t he f ield is transient or not ) , and set t o n ull. I f t he bean is r eact iv at ed, t h e developer m ust reest ablish t h is open conn ect ion. The cont ain er p r ovides t h e Pub Dat e: April 1 1, 20 03 callb ack met h ods t o allow us t o do t he n ecessar y dest r uct ion an d in it ializat ion of passiv at ed I SBN: 0- 73 84- 26 09 -1 obj ect s. Pages: 75 0
Not all st at ef ul session beans act u ally end u p g et t in g passiv at ed, b ecause t h e con t ainer has not det er min ed t hat som e resour ce opt im izat ion is requ ired. Somet im es t hey ar e ju st dir ect ly r em ov ed w h en t h e client no lon ger needs t h em , such as w hen t he b ean t im es out . Th er efor e, t h e bean pr ov id er mu st r emem ber t o pr ovid e t he m eans t o close t h e connect ion w h en t h e bean is r em ov ed f r om m em or y ( as w e w ill see, in t h e ejbRemove m et hod ) . Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Choosing stateful or stateless session bean Deploy EJBs t o W eb Sp her e Applicat ion Ser v er The f ollow in g ar e som e gu idelin es t o f ollow wh en choosing bet w een st at efu l and st at eless This I BMb ean Redbook pr rovides d etion. ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio session s in you app licat App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Comparing stateless and stateful session beans
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. When desig ning an ent erp r ise ap plicat ion , m any t im es t he d ev eloper has t o m ake design decisions t y pe of session ould ThisEddecision ay and not cover alway st he b ebasic sim ple. I n Part 1, on wewinhtat r oduce EJBs as a pbean art ofshJav a 2beEnutsed. er p rise it ion ( J2m EE) The g oal of t h is sect ion is t o simp lif y t h e decisionm ak ing pr ocess. concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Table - 1 g ives m mar y com par isondrofiv st eless an d st at efu session beans. per sist5ence ent itay su b ean s and m essageenatbeans. We also prlovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Tab -1 .difSt uapp l v einrgsu at eland esssim sep ssi on sb tea im plem ent fin der m le et h 5 ods, ferat enef t m st rsatst egies, le client h atns u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. St a t e ful
Can r et ain st at e b et ween met hod inv ocat ions Can ser v ice business pr ocesses t hat sp an m ult iple m et hods or t r ansact ions Can be awar e of an y client hist or y
St a t e le ss
Can not ret ain st at e bet w een m et hods Ty pically u sed for sin gle r eq uest , sin gle m et hod inv ocat ion Anon ym ous met h od p rov ider , not aw ar e of any clien t h ist or y or st at e
The t yp e of session b ean t o use could b e det er mined by t ry ing t o answer t hese t wo qu est ions: Does t h e bean hav e t o k now t he st at e of t h e client t o perf or m business logic? Does t h e bean hav e t o m aint ain st at e bet w een m et hod calls? I f t he an sw er t o eit h er of t hese quest ions is y es, t hen y ou should consider using st at ef ul beans. •I f t he an sw erTable is n oof tContent o bot h squest ions, t h en using st at eless beans sh ould be f in e for you r •app licat ion. I ndex EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
How ev er , consid er t hese t w o p oin t s w hen you int end t o use st at eful beans: By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss PubThe lish er: I BM st at e
may be lost if a f ailur e occu rs ( for ex am ple, net w ork or syst em f ailu re, sy st em
Pub r eb Datoot e: April ) . 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
St at efu l EJBs can not easily be r eplicat ed , lead in g t o p oor per for m ance in a clu st er ed Pages: 75 0 env iron ment . Ch oose t o use st at efu l session beans car ef ully, and only w hen absolut ely necessary , b ecause t hey r equir e m uch m ore sy st em over head t o m aint ain. How ev er, if t he bu sin ess pr ob lem r eadily lends it self t o a st at ef ul session bean im p lem ent at ion, su ch as m aint ain in g a shopp ing car t , t hen cer t ainly consider t h em . Bu t k eep in m in d t hat t he d ev elop er mu st t ak e st eps t o help ensur e t h at n about t he EJB 2 .0such specif ion in g, using , and r em ov ing, are clear ly sp elled out , so t he lifLear e cycle of t hese beans, asicat cr eat t hat t he w ork flow can be ex pr essed t hr oug h t h e bean. Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Common usage
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App Developer or t he elopm t of apbeans plicat ion s based on session t h e Ent er pr ise an JavdaBeans The licat EJB ion specificat ion dfescr ib esd ev st at ef ul en session t o be r egu lar beans, st at eless (t oEJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion er. be m or e of a d er iv at iv e. We b elieve t his is only b ecause of t h e emp hasis on t he t erServ m session , w hich seems t o d en ot e st at e t hat is associat ed w it h a client , wh ich is in fact wh at st at ef ul session Thr ougare houtin tt he bookt o, w e present ov id .e St exam plessession based on a simp ingany appsession licat ion inw itt hhisan beans en ded r epr at eless b ean s do le notbank h ave sense un derly ing r elat ional dat ab ase. ( w e do not consid er met h od- level conv ersat ional st at e t r uly a session) . I n f act , som e believ e t hat t he phr ase st at eless session is in f act a cont rad ict ion. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep s and t h eearstch ectl ur e. I n par t icus lar w e tpoint outlart he n ew tfu nctmions uced w it het he We d o tnot believ atitefu session b ean t o be he regu beans h at ost int d evr od elopers hav EJB 2. 0 .spI fecif icat ion , t hat pr ov idsession e, for ex am ple, enear hanced alit y inan for cont ain er -mmost anag adop t ed any t hing st at eless beans app t o b efun t hectpion redom t choice, ly ed per sist ence s and dr ivten We alsot ed pr ovid e best pr actions ice guidelines f or because of tent heirit ystbatean eless nat m ur essagee in su ppor ingbeans. ser v iceor ien sy st em in t eract ( not t o successful impof lem entan at ions EJBs. m en t ion ease use d lowofover head) . Th er efor e, w e believ e t h at m ost of t he t im e, developer s w ill choose t o im plem en t st at eless session beans t h at ex pr ess t he r eu sable, dist rib ut ed, Icom n Part 2, we in t r oduce t h e sam p leser bank ingofapplicat ionlicat andions. t henStimp ent en t itbeans y beans, ponent - b ased, businesslev el vices t heir app at eflem ul session cer tsession ain ly beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also hav e a place in a desig n, b ut sh ould be used mor e conserv at iv ely . I n t he end , t her e is no cookieim fin der m e, et hand ods,wdif en t mm app p le client s t h atcan u sebe t heexEJBs. At cutplem t er gent uidelin e her hatferr eally at tin ergs st isr at w hegies, et h er and t he bsim usiness p r oblem pr essed talong he en td, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. he lin es of on e m odel or t h e ot her .
Session bean type structure Recall fr om Ch apt er 2, " EJB ar chit ect u r e and concep t s" on page 33, t hat session beans im plem ent t he javax.ejb.SessionBean in t er f ace. Giv en t hat w e kn ow t hat w e hav e t w o t y pes of session b ean s, w e can ex t en d t he concept ual m odel t o in clud e t hese t y pes ( Figur e 5- 1 ) .
Figu r e 5 - 1 . Se ssion be a n t yp e st r uct ur e
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
The d ist in ct ion s ar e m ore log ical t han phy sical. As you can see, st at eful and st at eless session Pub lish er: I BM beans st ill im plem en t t h e same su pert y pe. Th e act ual t y pe of session bean is declar ed in t h e Pub m Daten e: tApril 1 1, 20t or. 03 The ent ry of t he deploy m en t d escr ip t or m ay be eit her deploy d escrip I SBN: 073 8426 09 -1 Stateful or Stateless. Ot her t han t h e act ual im plement at ion specifics depend in g on t h e bean 75 sical 0 t y pe, tPages: h e phy st r u ct u re of st at ef ul an d st at eless session beans are alm ost t he sam e.
Callback methods Figur e 5- 2 sh ows t he callback m et h ods t h at session bean classes m u st im plem ent becau se t hey Lear about t he EJBin2 t.0 ext end t h en SessionBean er fspecif ace. icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
Figu r e 5 - 2 . Se ssion be a n cal lba ck m et h ods Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t ofjavax.ejb.EnterpriseBean ap plicat ion s based on t h e Ent{er pr ise Jav aBeans public interface SessionBean extends ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. public void setSessionContext(SessionContext ctx); Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r void elat ional dat ab ase. public ejbActivate(); I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic public void ejbPassivate(); concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed public void ejbRemove(); per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. } I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At Regar dless of w het her t he t yp e of session b ean is st at efu l or st at eless, t he bean class m ust t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. im plem ent t hese m et h ods b ecause t h ese ar e w hat t he con t ainer uses t o act ually m anag e t he bean. The bean m ust also im plem en t t h e life- cy cle m et h ods on t h e hom e int er face, specif ically , create an d remove. Th ese m et hods define t he basis of our bean- cont ainer cont r act .
ejbCreate A session bean m ust hav e at least one ejbCreate m et hod . For st at eless session b ean s, t his is a single, no- ar gu m en t m et hod. A st at efu l session b ean , on t he ot her hand , m ay hav e m ult iple ejbCreate m et hod s t hat each def in e a u niqu e w ay t o in it ialize t h e conv er sat ional st at e of t he session bean.
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Bean-container contract Under st anding w hat y ou as a bean dev eloper w ou ld hav e t o do t o im plem en t a meanin gfu l session b ean m eans u nder st anding t h e callback an d lif e- cy cle m et hod s of t he b ean , and h ow t his bean- cont ain er con t r act is enfor ced. As w e m ent ioned, t his cont r act is d iff er ent depending on t he •t y pe of session Table of Content s ul or st at eless. Th is sect ion descr ibes t he basic con cept s of t he b ean bean: st at ef •cont ainer cont I ndex r act , such as inst ance p oolin g f or st at eless session beans, and act ivat ion EJB 2 . 0 D eion ve lopm h Wsession ebSphe r beans. e St udio Th Appl passivat f or estntatwefit ul e ica lif eti on cy De clev el ofoper each session bean t yp e is discussed. By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
For t he pur poses of t his d iscussion , EJBObject w ill b e used t o r ef er gener ically t o t he obj ect t hat im plem ent s t he com ponent int er face, r eg ard less of w het her it is act ually a r em ot e EJBObject or Pub lish er: I BM a local EJBLocalObject. Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0 States of a session bean
St at efu l and st at eless session beans h ave d iffer ent st at es t h at d ef ine t h eir lif e in t h e cont ainer . These ar e descr ibed below . Lear n about t he EJB 2 .0 specif icat ion
States of a stateless session bean
Dev elop EJBs w it h St u dio App licat ion Dev eloper St at eless session beans h ave t wo- st at es: no st at e an d r ead y pool ( Figur e 5- 3 ) . Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Figu r e pr5ovides - 3 . Stdat e t r an simt iatons f or a st e leively ss session anSt udio This I BM Redbook et ailed in for ion on h ow to a eftfect use WebSpbe here App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Essen , if t h e ar bean ance eenw ecr point eat ed,out t h en at e. int Once t he cont er concept ially t s and ch it in ectstur e. I nhas parntot icublar t heitnis ewin funo nctstions r od uced w it ain h t he decides eeds st at eless session b ean , it ple, m oven eshanced t o t he rfun eadctyion st at e.yTh beanain w er ill -rem ain ed in EJB 2. 0 it spnecif icataion t hat pr ov id e, for ex am alit fore cont m anag tper hissist r eady at eit unt t he contm ainer d ecides it nbeans. o long er it .ovid e best pr act ice guidelines f or encestent y b ilean s and essagedr iv en Weneed alsos pr successful imp lem ent at ions of EJBs. Mov ing bet w een pooled and r ead y is w hat happens w hen t h e cont ainer is lever aging it s inst an ce pooling m ech Thet hf act t hat b eaning in applicat t he p oolion is j and ust at hen beanimp t hat notenassociat ed wsession it h an I n Part 2, we anism. in t r oduce e sam p lea bank lemisent t it y beans, EJBObject note-mean it isusing n ot aWebSph bean in er st ance. I t isApa plicat r eadyion - and - welop ait inerg . pooled beans, an d dmoes essag d r iv ent hat beans e St u dio Dev We also inst ance, as itmliv in t he r eady st at e, int hgestcon ainer wand ill en sur e itclient associat es uwse it ht he an EJBs. At im plem entand fin der etes h ods, dif fer en t m app r attegies, sim p le s t h at EJBObject en a client est s ser vices of t heion b ean t he en d, w ewdhescrib e how requ t o deploy EJB ap plicat s t o. a WebSph er e App licat ion Serv er.
Not e As w e w ill see, a client calls create an d remove m et hod s on st at eless session b ean s, bu t it is really up t o t he cont ain er t o m anag e t he lif e cy cle of t he b ean . Calling creat e fr om a client is j ust a w ay t o t ell t h e cont ainer t o allocat e on e fr om t h e pool, t he act ual creat ion of t he b ean inst an ce m ay h ave alr eady occur r ed . A clien t calling t he remove m et hod on a st at eless session bean d oes not h ave any im pact , b ecause it is st ill u p t o t he con t ainer w hen t o r emov e t h e bean fr om t h e pool. Wit h st at eless session beans,
client calls t o cr eat e w ill act u ally cr eat e new inst an ces of EJBObject ( t he obj ect w hich im plem ent s our r emot e int erf ace) , and a remove call w ill r emov e an EJBObject inst ance, not t he act u al session b ean inst an ce it self.
• •
The con t ain er w ill ret ain t he n um ber of inst ances r eq uir ed t o ser v ice t h e cur rent client w or kload ( w it hou t exceeding some con t ainer m axim um ) . Th is w ork load is not m easu red in t er m s of how man y act ual clien t s t h er e m ay be, b ecause clien t s only n eed t he session bean for t he lengt h of a m et h od in vocat ion. The act ual num ber is st at eless Table of Content s session b ean s r eq uir ed in a p ool is usually m uch, mu ch sm aller t h an t he act ual num ber I ndex of act ive client s.
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
These st at t r ansit ions ar e im por t ant , becau se each st at e chang e w ill t r ig ger t he con t ainer t o Pub lish er:e I BM inv ok e a callback m et h od ap pr opr iat e t o t hat st at e chang e. As a st at eless session bean Pub Dat e: April 1 1, 20 03 dev elop er , y ou hav e t o u nder st and t hese t r ansit ions t o kn ow how you shou ld ap ply appr op riat e I SBN: 0- 73 84- 26 09 -1 logic in t hese callb ack m et hod s for you r bean. Pages: 75 0
Let us discuss t he pur poses of each callb ack met h od f or st at eless session beans: set SessionCont e x t — Called by t he cont ainer w hen t he bean is ad ded t o t h e ready pool, it should be u sed t o cap t ur e a r efer ence t o t h e SessionContext f or t h e bean. Figur e 5- 4 show t h e m ost onspecif for m icat of tion his m et hod. Lear ns about t he comm EJB 2 .0 Dev elop h St u dio App eloper Figu r e EJBs 5 - 4w . itSe ssion belicat a n ion se tDev Sessi onCont e x t
m e t h od
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio public void setSessionContext(javax.ejb.SessionContext ctx){ App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. mySessionCtx = ctc; Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly}ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he a ticat e—ion Th is m et odidis h en e con t ainer any for instcont anceain ofert h- m e st at eless EJB 2.e0j bCre sp ecif t hat prhov e, called for ex w am ple,t hen hanced funcreat ct iones alit anag ed session b ean . Recall t hat a st at eless session bean m ust h ave on ly a single n oar gum ent f or per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines create mlem et hod ot her ent er pr ise bean t yp es, t h is m et h od is not called w h en t h e successful imp ent.atUnlike ions oft he EJBs. client inv ok es t he creat e m et hod of t h e hom e obj ect . Rat her , t he con t ainer calls t his once w hen a new in st ance added t o t he pool. e client ustlem st ill inven okt iteyt he h om session e I n Part 2, we in t r oduce t h eissam p le bank ingr eady applicat ionThand t hen m imp ent beans, m et eat e, ev erbeans , in orusing der t oWebSph g et a refer t o Ap t heplicat EJBObject. beans, anhod d m cr essag e- how d r iv en er e ence St u dio ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At hod iseused o one- EJB t im eap init ializat he bean.erThis m licat ight ion include t he enThis d, wm e et d escrib how ttoo ddeploy plicat ionion s t oofa t WebSph e App Serv such er. act ivit ies as init ializat ion of any r esou rces used b y t he bean, such as dat ab ase con nect ion s, t hat ar e safe t o be shar ed amon g diff er ent m et h od in vocat ions of t he client ( Figur e 5- 5 ) .
Figu r e 5 - 5 . St at e le ss se ssion be a n ej bCr e at e m et hod
public void ejbCreate() throws CreateExceptions{ //do some initialization, if any
}
• •
e j bR em ov e— Called by t he cont ainer w hen a b ean inst an ce is n o long er need ed in t he pool. Like ejbCreate, ejbRemove is not in vok ed w h en a client calls t h e remove m et hod on t he hom e obj ect . Rat her , it is called w hen t he con t ainer decid es t o r emov e it fr om t h e pool, such as w hen t he cont ainer is cleaning up some unu sed inst ances t o p reser v e r esou rces. Table of Content s Theor et ically, any r esou rces t h at w ere obt ained in t h e create m et hod shou ld b e cleaned up I ndex her e.
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM
Not e
Pub Dat e: April 1 1, 20 03 I SBN:The 0- 73con 84- 26 09 -1 t ain er
is not guar ant eed t o alw ay s call t he ejbRemove m et hod . For if t h e ser v er it self cr ashes, ejbRemove w ou ld n ot b e called. Th e pr oblem , t hen, is t hat or phan ed open r esour ce connect ions m ight not be ab le t o be closed p rop er ly .
Pages:exam 75 0 ple,
e j bPa ssiv a t e— Alt h ough it mu st be im plem en t ed, it is not called in t h e st at eless session Lear n lifabout bean e cy cle. t he EJB 2 .0 specif icat ion e j bAelop Dev ct i va EJBs t e —wAlt it hhSt ough u dioitApp mulicat st beion imDev plem eloper en t ed, it is not called in t h e st at eless session bean lif e cy cle. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio States of a stateful session bean App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. St at efu l session b ean s hav e t h ree- st at es: no st at e, in use, an d passiv at ed ( Figur e 5- 6 ) . Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing Figu r elat ional ase. r e 5dat - 6ab . St at e t r an si t i ons f or a st a t e fu l se ssi on b ea n I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. I f t he b ean inst ance h as not been cr eat ed , t hen it is in no st at e. Once a clien t d ecides it n eeds a st at eful session b ean , it w ill call t he hom e obj ect ' s create m et hod , w hich will m ov e t h e bean t o t he I n Use st at e. The bean w ill rem ain in t h is ready st at e as long as t h e client is m ain t aining a conv er sat ion w it h t h e bean. While t h e bean is in u se, it m ay per iod ically be passiv at ed. Mov ing bet w een t h e I n Use and Passiv at ed st at es is w h at h appens w h en t h e con t ainer is lev er aging it s passivat ion and act iv at ion m ech anism . Th is happ en s w hen t he cont ain er has t o con ser v e som e r esour ces. These st at e t r ansit ions ar e im por t ant , becau se each st at e chang e w ill t r ig ger t he con t ainer t o
inv ok e a callback m et h od ap pr opr iat e t o t hat st at e chang e. As a dev elop er you hav e t o un derst an d t h ese t r an sit ions t o k now if y ou should apply ap pr opr iat e logic in t hese callb ack m et hods f or y our b ean . Let us discuss t he pur poses of each callb ack met h od f or st at eless session beans:
• •
set SessionCont e x t — Called by t he cont ainer w hen t he bean is f irst cr eat ed and just of Content befor e itTable m oves t o I n sUse, it should be used t o cap t ur e a refer ence t o t he SessionContext I ndex for t he b ean . The p ur pose is t he sam e as wit h st at eless session beans.
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
j bCre a t e— Th iser,mLar etshSchu od is h en a client vok es By Ueli eWah li, Wout er Denay nk ,called DeborahwShaddon , Mart in in Weiss
t he create m et hod on a session beans hom e int er face. There mu st be at least one ejbCreate m et hod f or each st at efu l session b ean , alt hou gh you can hav e m ult iple create m et hod s w it h diff er ent sig nat ur es or Pub lish er: I BM dif fer en t suf fix es, such as ejbCreateXXX, as w ell as d iff er ent met hod ar gu ment s. Un lik e Pub Dat 03 ent ite:y April b ean1 1, s, 20 t here is no ejbPostCreate m et hod . I SBN: 0- 73 84- 26 09 -1
This Pages:m75 et0hod is used t o d o one- t im e init ializat ion of t he bean. This m ight include such act ivit ies as init ializat ion of any r esou rces used b y t he bean, such as dat ab ase con nect ion s, t hat ar e safe t o be shar ed amon g diff er ent m et h od in vocat ions of t he client , as w it h st at eless session beans. How ev er, a st at eless session bean is now cr eat ed on behalf of t he client , so any in it ial con ver sat ional st at e in it ializat ion should also be d one her e as w ell. The f ollow in g is a com mon exam p le of an ejbCreate m et hod w her e t he client is in it ializing Lear n about t he EJB 2 .0 specif icat ion t he st at e of t he b ean ( Figur e 5- 7 ) . Dev elop EJBs w it h St u dio App licat ion Dev eloper
Figu r e 5 - 7 . St at e f ul sessi on be an e jbCr ea t e m e t h od Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans public void ejbCreateCart(String userName) throws CreateExceptions{ ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. this.userName = userName; Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. shoppingCartID = //do some randomization for a uniqueID, for instance.. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep}t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. e j bR em ov e— Th is m et h od is called w h en a client in vok es t he remove m et hod on a session beans int er face. Session b eaning s hav e t im eout s associat ed lem w it hent t hem t hat det erm ine I n Part 2, wehom in t e r oduce t h e sam p le bank applicat ion and t hen imp en t it y beans, session w hen beans bebeans ev ict ed fr omWebSph t he cont AnyAp r esour h at elop w er er e .acquir ed b y beans, an d idle m essag e- dcan r iv en using erainer e St u. dio plicat ces ion tDev We also t heent bean, such opendif dat conninect ions, shou ldand b e sim cleaned up her is im plem fin der m etas h ods, ferab enase t m app g st r at egies, p le client s t he.atCalling u se t heremove EJBs. At sign o t he econt t hat t he can ion n ow ovion ed Serv f romer. t he t he enad, w eald tescrib howain t oerdeploy EJBbean ap plicat s tbe o acleaned WebSphup er eand Appr em licat cont ainer 's m em or y. You shou ld ensur e t h at y our log ic guar ant ees t h at remove w ill b e called at t he nat ur al comp let ion of a u ser 's session of act ivit y , an d do n ot r ely on t he cont ainer t o do t he clean up for you .
Not e The con t ain er is not guar ant eed t o alw ay s call t he ejbRemove m et hod . For exam ple, if t h e ser v er it self cr ashes, ejbRemove w ou ld n ot b e called. Ad dit ionally, if t he b ean sim ply t im es out an d is in p assiv at ed st at e, ejbRemove is not called.
This shou ld b e consid er ed w hen d esign in g EJBs w hen using r esour ces.
• •
e j bPa ssiv a t e— Th is m et h od is called w h en t h e con t ainer decides t o p assiv at e t he bean. When passivat ing , it will ser ialize t h e bean t o som e second ary st or ag e and evict it fr om m em or y . I t is called j ust b ef or e t he ser ializat ion occu rs, g iving t h e developer t h e opp ort unit y t o ensur e t hat t he st at e of t he bean can b e ser ialized . The client h as no con t r ol Table of Content s ov er wh en t h is occu rs, an d t he cont ainer u ses som e int er nal caching algor it hm t o decide I ndex w hich beans ar e t he best candidat es f or passivat ion .
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
e j bA ct i va t e — Th is m et h od is called w h en a client calls a m et h od on t h e bean t hat is cur r en t ly passiv at ed. ejbActivate is act u ally called j ust aft er t he bean h as been Pub lish er: I BM deser ialized f rom secondar y st or age, an d g ives t h e developer t h e oppor t u nit y t o reinit ialize Pub Dat e: April 1 1, 20 03 any d at a n eeded t o con t in ue t h e oper at ions of t h e bean. I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Life-cycle sequence of session beans I n t he pr ev iou s sect ion w e descr ibed t he r esp onsibilit ies of each callback m et hod, and how t hey ar e used in t r ansit ions of b ean s bet w een t he st at es. •We ex t en d t hat Table of Content discu ssion st o d escrib e t he act ual seq uence of ev ent s t h at occur w h en a session •bean is creat Ied, ndexu sed, or r em ov ed. We also h ave t o consider t h e passiv at e an d act iv at e life EJB 2 . 0 as D e ve e nt w h W tebSphe r e how St udio ica tiainer on De vact el oper cycles w lopm ell. We witant o show t hAppl e cont ually m anag es t he beans based on eit her cont ainer or client based ev ent s, and t h e or der in w h ich t hose act iv it ies occur . St at eless an d By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss st at eful session b ean s con for m t o a com plet ely diff er ent set of con t ainer- bean cont r act ru les, so t hese w ill be d iscussed ind ep en dent ly . Pub lish er: I BM
Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
Stateless session bean life cycle Pages: 75 0 We d iscuss t h e lif e- cy cle sequ en ce of a st at eless session bean in t er ms of t he f ollow ing int er act ion s: Cre at i ng/ r em ov in g a n st a t e le ss se ssi on be an— Th is is ent ir ely a con t ainer Lear n about r espon sib ilit yt.he EJB 2 .0 specif icat ion Dev elopa EJBs h St u dio App ionThDev eloper U si ng st at ewleitss session belicat an — is is w hen a client inv ok es a m et hod on a bean obt ain ed t h rou gh t he h ome object . Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Life cycle—creating/removing a stateless App licat ion Developer f or t he d ev elopm en t of apsession plicat ion s bean based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Figur e 5- 8 sh ows t he sequence of cont ainer ev en t s w hen a cont ainer cr eat es, an d su bseq uent ly Thr hout book , wsession e pr ov idb eean exam based on a simp le bank ing app licat ion w it h an destoug r oys, t h et he st at eless instples an ce. un derly ing r elat ional dat ab ase.
Figu r ein5t r-oduce 8 . CrEJBs e at e/ li fae2 cycle of aEdst a te leEE) ss and session bebasic an I n Part 1, we as raepm artov ofeJav En t er p rise it ion ( J2 cover t he concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
The ab ove in t eract ions are b et ween t he cont ain er an d t he b ean . The b ean pr ov ider m ust ensur e t hat t he appr opr iat e logic is in clu ded in t he callback m et hods. 1 . Cont a ine r de ci de s t o cr ea t e be a n— Th e cont ainer d ecides, m ayb e on st ar t u p, or m ay be w hen it has t o allocat e m ore beans t o t he p ool, t o cr eat e some bean inst an ces. Gener ally , an init ial pool of beans is cr eat ed w hen t he serv er f ir st st ar t s u p.
2 . ne w I nst an ce — Th e cont ainer f irst cr eat es a n ew in st ance of t he b ean class. 3 . set SessionCont e x t — Cont ainer set s t he session cont ex t on t hat bean. 4 . e j bCre a t e— Allow s f or init ializat ion, if any , of t h e bean t o occur .
• •
5 . e j bR em ov e— Th e cont ainer n ow decid es t o ev ict t he b ean f rom mem or y t o con ser ve r esour ces, allow ing f or cleanu p of t he beans resour ces t o occur . Table of Content s I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Life cycle—using a stateless session bean
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Figur e 5- 9 sh ows t he sequence of client event s w hen a client has t o use a st at eless session b ean . Pub lish er: I BM Pub Dat e: April 1 1, 20 03
Figu r e 5 - 9 . Usi ng a st at el ess sessi on bea n
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er The ab ove in t eract ions are b et ween t he cont ain er an d t he b ean . The b ean pr ov ider m ust ensur e This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio t hat t he appr opr iat e logic is in clu ded. We assum e t h at ne w I nst an ce , se t Ent i t yCont e xt , App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans e j bCre a t e h ave alr eady b een called by t h e cont ainer , so t h ey ar e n ot r eally p ar t of t h is ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. int er act ion . Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un1derly elatClient ional calls dat abt he ase.create m et hod on t he hom e int er face, w hich causes t he con t ainer . creing at er — t o associat e a b ean inst ance f r om t he p ool w it h t he clien t . I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep h e ce ar — ch itTh ect e. I n par t icu lareat wed e point out t he n ew fu nct ions int r od uced w it h t he 2 . net swand I nstt an e ur EJBObject is cr . EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per y bTh ean s and m essageen beans. We also pr ovid e best pr act ice guidelines f or 3 .sist e jence bO bjent e ctit— e EJBObject is retdr urivned t o t he clien t. successful imp lem ent at ions of EJBs. 4 . Busin ess m e t hod— Th e client calls t he bu sin ess m et hod ( s) on t he b ean t h rou gh t he I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session EJBObject. beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im5plem ent fin der h ods,The difEJBObject fer en t m app in g st egies, le client at u EJBs. At . Busin ess mm e tet hod— calls t hreatmet h odand on tsim h e pinst ance sint ht he p se ool.t he The t he enEJBObject d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. is only associat ed w it h inst ance f or t he leng t h of t he b usin ess m et hod . When w e descr ibed t he ent it y bean lif e cy cle, w e did not act u ally sh ow t his im plied in t eract ion. What is im por t ant t o n ot e, how ever , is t h at t h e associat ion bet w een t he EJBObject an d t he b ean inst ance is m aint ained only f or t he lif e of t his m et hod .
Stateful session bean life cycle We d iscuss t h e lif e- cy cle sequ en ce of a st at efu l session b ean in t erm s of t h e follow ing int er act ion s:
Cre at i ng a n st a t ef ul session be a n— Th is is a clien t - init iat ed act iv it y t hat is inv ok ed w it h a call t o t he hom e obj ect , an d in volv es creat ing a new st at eful session bean. Find ing a n ex ist i ng st a t e ful se ssi on b ea n— Th is inv olves how an ex ist in g session bean is r eassociat ed w it h a clien t . I t is r eally a clien t int er act ion, w hich w e descr ibe b riefly below . • •
Table ofaContent s ssion be a n— Th is is w hen a client inv ok es a m et hod on an ex ist ing U si ng an d st t e ful se bean. I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
P assiv at i ng/ a ct iv a t ing a st a t ef ul session be a n— Th is is w hen t he cont ainer d ecides t o passivat e and t hen re- act ivat e t he session bean.
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
PubRe lishm er:ovi I BM ng
a st at e fu l se ssi on bea n— Th is is also a clien t - init iat ed act iv it y t hat is inv ok ed r emov e m et hod .
Pub 03 w Dat it h e:a April call1t1,o 20 t he
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Life cycle—creating a stateful session bean Figur e 5- 1 0 sh ows t he sequence of client event s w hen a client creat es a new st at eful session bean. Lear n about t he EJB 2 .0 specif icat ion
Figu r e 5 - 1 0 . Cr ea t i ng a st at ef ul session be an
Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en The abd, ove w eindt eract escribions e how aret ob et deploy ween EJB t he cont ap plicat ain er ionan s dt ot he a WebSph b ean . The er e bApp eanlicat pr ov ion ider Serv m ust er. ensur e t hat t he appr opr iat e logic is in clu ded. 1 . cre at e — Client calls t he create m et hod on t he hom e int er face, w hich causes t he con t ainer t o begin building t h e bean in st ance and EJBObject. 2 . ne w I nst an ce — A new inst an ce of t he st at eful session b ean is cr eat ed. 3 . set SessionCont e x t — Set s t he session con t ex t . 4 . e j bCre a t eX X X — Th e bean inst ance' s cor respond ing ejbCreateXXX m et hod is called , w hich m ay init ialize t he st at e of t he session bean.
5 . ne w I nst an ce — Th e EJBObject is cr eat ed . 6 . e j bO bj e ct — Th e EJBObject is ret ur ned t o t he clien t .
Life cycle—finding an existing stateful session bean •
Table of Content s
A st at ef ul session bean r em ains act ive unt il it is eit her r em ov ed b y t he clien t , or it t im ed out in • I ndex t he con t ainer. A client m ay need t o gain access t o an ex ist in g st at ef ul session bean, f or exam p le, EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper bet w een m ult iple ser vlet in vocat ions w h er e t h e EJBObject h as gon e out of scope. Th er efor e, By Ueli li, sWout Lar s tSchu nk , Deborah , Mart in session Weiss t her e Wah exist a werayDenay in wer,h ich o find an existShaddon ing st at ef ul bean t hat a clien t h as been w or king w it h, but does not cur r en t ly hav e a r efer en ce t o it s EJBObject. To do t his, t he client m ust ave r eference t o a h andle of t h e bean. Pubhlish er: a I BM Pub Dat e: April 1 1, 20 03
En t it y beans h ave find er m et hod s in wh ich t o locat e ex ist in g en t it y beans. The id en t it y of a I SBN: 0- 73 84- 26 09 -1 st at eful session b ean , how ever , is not dict at ed by a pr imar y k ey, or some ot her un iq ue ident if ier . Pages: 75 0 Ther ef or e, t hey ar e not fou nd t hr oug h f inder m et hods. Rat her , st at eful session beans are associat ed wit h a client , and t he clien t can gain access t o it s session beans t hr ough a hand le t o t he com pon en t in t er f ace. Th is hand le can be ser ialized and used t o r ef ind t he b ean at a lat er t im e. Figur e 5- 1 1 sh ows t he sequence of client event s w hen a client needs t o find an exist ing st at eful Lear n about EJB t2o.0t hat specif icat ion session b ean b y a thhe andle bean. Dev elop EJBs w it h St u dio App licat ion Dev eloper
Figu r e 5 - 1 1 . Fin din g a n e xi st ing st a t e fu l se ssion b ea n
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or The ab ove imp in t eract ions are bof et ween successful lem ent at ions EJBs. t he cont ain er an d t he b ean . The b ean pr ov ider m ust ensur e t hat t he appr opr iat e logic is in clu ded. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also . geent t H afin nd le — Thheods, client geten s tthm e app hanin dle obj ect t hat and can sim b e sav f ors exam le tinhe EJBs. At im1plem der m et dif fer g st r at egies, p le ed, client t h at upse HttpSession. A new Handle ob ject is cr eat ed . t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. 2 . ge t EJBOb j e ct — Th e getEJBObject m et hod is called on t he handle. 3 . fi nds be an— Th e cont ainer r eassociat es t he EJBObject w it h t h e client . 4 . e j bO bj e ct — Th e EJBObject is ret ur ned t o t he clien t .
Life cycle—using a stateful session bean Figur e 5- 1 2 sh ows t he sequence of client event s w hen a client has t o use a st at ef ul session bean.
Figu r e 5 - 1 2 . Usin g a st a t e f ul sessi on bea n
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
TherPub e lish r eally isn' t m uch int er act ion her e w or t h ment ion in g, j ust t he execut ion of bu siness er: I BM m et hod. Pub Dat e: April 1 1, 20 03
I SBN: 0- 73 84- 26 09 -1
1 . Busin Pages: ess 75 0 m e t hod— Th e client inv okes a business m et hod on t he EJBObject. 2 . Busin ess m e t hod— Th e EJBObject d elegat es t o t he bean inst an ce, p ossibly u pdat ing it s st at e as it goes along.
Life cycle—passivating/activating a stateful session bean Lear n about t he EJB 2 .0 specif icat ion elop EJBs w itah st Stat u dio licat ion Dev PassivDev at ion occur s on ef ulApp session bean weloper hen t he cont ainer h as t o r elease som e cont ainer r esour ces. Act iv at ion occur s w h en a client in vok es an oper at ion on a bean t hat is p assiv at ed. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Each of t hese is a cont ain er - m anag ed act ivit y . This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Figur e 5- 1 3 sh ows t he sequence of cont ainer or client ev ent s w hen t he con t ainer has t o App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans passivat e or act iv at e a st at eful session bean. ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Figu r et he 5 -book 1 3 . , Pa ateion nd based act iv at ofleabank st aing t e fu l licat se ssion ea n Thr oug hout w e ssiv pr ov id examaples on ion a simp app ion w it hb an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
1 . P assiv at i on t ri gge r— Based on som e caching algor it hm , t h e con t ainer det er mines t h at t he bean is a candidat e for p assiv at ion. 2 . e j bPa ssiv a t e— Th e ejbPassivate m et hod is called on t he bean, t o pr ep ar e it t o be ser ialized . 3 . St a t e i s w ri t t en t o se conda ry st ora ge — Th e st at e of t he bean is w r it t en t o som e
3. second ar y st orag e, su ch as a dat ab ase or file dir ect or y . 4 . Busin ess m e t hod— Th e client inv okes a business m et hod on t he bean. 5 . St a t e i s re bui lt fr om second ar y st or ag e— Th e st at e of t he bean is r ebu ilt . 6 . e j bA ct i va t e — Th e ejbActivate m et hod is called t o reinit ialize any n on- serializable st at e dat a t hat is necessar y. •
Table of Content s
• 7.
I ndex Busin ess m e t hod— Th e act ual b usiness met hod on t h e bean in st an ce is ex ecu t ed.
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Life cycle—removing a stateful session bean Pub lish er: I BM
Figur e Dat 5- 1e:4April sh ows t he Pub 1 1, 20 03 sequence of client event s w hen a client asks t o r emov e a st at ef ul session bean. Addit , t09 he-1 con t ainer m ay also decid e t o r em ove a bean w hen it s t imeout exp ires, I SBN: ionally 0- 73 84- 26 eit her fr om a passiv at ed or r ead y st at e. When t his occur s, t he remove m et hod is n ot act ually Pages: 75 0 called.
Figu r e 5 - 1 4 . Rem ov in g a st a t e fu l se ssi on b ea n
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also 1 . re m ove —The client in it iat es a r em ov e, eit h er by callin g t he EJBObject remove m et hod , im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At or b y calling t h e removeHandle on t h e EJBHome. Bot h m et hods pr oduce t h e same r esult . t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. 2 . e j bR em ov e— Th e ejbRemove m et hod is called , giv ing t he b ean one last ch ance t o cleanup any op en r esou r ces. 3 . t i m e out — St at ef ul session beans m ay also b e r em ov ed b y t he cont ain er w hen t h ey t ime out , eit h er fr om t h e passiv at ed or r ead y st at e. When t hese st at ef ul session beans t im e ou t in passiv at ed st at e, t heir ejbRemove m et hod is n ot called . The d ev eloper sh ould help t o ensur e t hat a u se- case seq uence t erm inat es w it h a call t o t he remove m et hod of t h e session bean.
Developing a session bean Tools such as Applicat ion Dev eloper pr ov ide pow er ful t ooling t o assist w it h t he dev elop ment of session b ean s. Developing w it hin t he Applicat ion d ev eloper t ool is d iscussed in det ail in t he second p art of t his b ook. •
Table of Content s
•This sect ion bI ndex ring s t oget h er all t he concept s d iscussed in t his chap t er , an d sh ow s how t he act ual EJB 2 . 0 Db e ve lopm e nt w it hconst W ebSphe e St Appl ica ti onwDe oper session ean s can be r uctred . udio We cont in ue it hv elt he ap plicat ion t h at w as p resen t ed in Ch apt er 2, " EJB ar chit ect u r e and concep t s" on page 33, wh By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss er e w e id en t if ied t h e t w o cand idat e session b ean s: Banking an d Reports. Pub lish er: I BM Pub Dat e: April 1 1, 20 03
Building a73session bean I SBN: 084- 26 09 -1 Pages: 75 0
When w e bu ild a session bean w e ar e concer ned w it h t he bean class, t h e hom e and com ponent int er faces, and t he d ep loy m ent descr ipt or : Be an cla ss— Th e bean class m ust con t ain t he bean callb ack m et hod s, imp lem ent at ion for t he com pon en t in t er f ace m et hod s ( bu siness) , and any requ ir ed hom e life- cycle m et hods. Lear n about t he 2 .0t hese specifcallback icat ion m et hods is diff er ent dependin g on w het h er it is a The im plem en t atEJB ion of st at eful or st at eless session bean. Dev elop EJBs w it h St u dio App licat ion Dev eloper H om e int er fa ce — D ef ines t he lif e- cy cle met hod s of t he bean. Ther e ar e no find er or ot her Deploy EJBs t o W eb Sp her e Applicat ion Ser v er hom e int er face m et hod s of session b ean s, just cr eat e and r emov e. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Com p onen t i nt e rf ace — D ef ines t he client view of t he bean, an d t h e corr espond in g App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans bu sin ess m et hods. ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. D e ploy m en t de scr ip t or— Th e XML deploy m en t d escrip t or for t he b ean . Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we inonline Expanded t r oduce banking EJBs as a p art requirements of Jav a 2 En t er p rise Ed it ion
( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he 2. 0ban sp ecif t hat wpreov id e, ibed for ex ple, enat hanced fun ct ion alit y for cont ain - matanag IEJB n our kin gicat exion am ple, descr t wam o candid e session b ean s: Banking, as aer st elessed per sist ent it y b ean andb m essagedrem iv en beans. We also e best icesession guidelines facad e ence session bean for sour ank in g sy st , and Reports, as pr a ovid pot ent ial stpratact ef ul beanf or successful ent ing at ions EJBs. candid at e. imp Thelem f ollow is aofrev iew of t he cr it er ia t h at w e u sed t o help u s classify t h ese as session b ean s: I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem finss dersemssi et on h ods, fer en appkintghat st r w ateegies, s t som h at u t he EJBs. At St ent a t e le beadif ns— Tht emban descr and ibe hsim as p t olep client er f orm e se b usiness t he enprd,ocesses w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.b ank is a such as t ran sf err ing balan ces an d op en in g new accou nt s. Ther efor e, t he good candid at e for a session bean, w hich r ep resen t t h e ser v ices t h at t h e bank pr ovid es. This can be st at eless, b ecause each f unct ion r ep resent s a dist inct serv ice or act ivit y t o t he client , and t her ef or e r eq uir es n o conv ersat ional st at e in or d er t o be im p lem ent ed. St a t e ful se ssi on b ea ns— W e assu me t hat t he ban k can b e st at eless because w e do not need con ver sat ion al st at e in t his sim ple exam ple. How ever , f or dem onst rat ion p ur poses only , w e t h in k t hat t he pr ocess of r eq uest in g r epor t s could be a conv er sat ional p r ocess, w her e t he p rocess for requ est ing a repor t w ill be a sequ en ce of m enu s t hat in t ot al w ill be used t o gener at e a set of repor t s.
Banking stateless session bean This m ain pur p ose of t h e Banking b ean is t o pr ov ide a f acade lay er of our sy st em , and h id es t he det ails of t he en t it y bean in t eract ions fr om t he EJB client s ( Figur e 5- 1 5) , an d also t o allow us t o exp ress t he cor e b usin ess f un ct ion s of our sy st em . Th is session bean w ill ult im at ely su ppor t su ch bu sin ess f unct ions as quer yin g cust om er dat a fr om t he ban k, get t ing a p art icular cust om er 's balance, w it hd raw al fr om a cu st om er 's accoun t , deposit ing t o a cust om er ' s accou nt , an d •t r ansfer r in g bet Table of Content s w een cu st omer s' accoun t s. •
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Figu r e 5 - 1 5 . Ba nk i ng se ssi on bea n ap pli cat ion e xa m ple
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Reports stateful session bean Lear n about t he EJB 2 .0 specif icat ion The Reports b ean w e d escr ib ed as b ein g a candidat e for a st at ef ul session bean because w e EJBssom w iteh conv St u dio App licatst ion w ant Dev t o melop aint ain er sat ional at eDev w iteloper h t he client as r ep ort s are requ est ed and pr odu ced. We r ecogn ize t hat t his is a slig ht ly cont r ived exam ple, an d t h at t here ar e lot s of ot her Deploy EJBs t o W eb Sp her e Applicat ion Ser v er w ay s t o g en er at e r epor t s an d m aint ain t h e requ est st at e bet w een int er act ion s, but t his is only an exam Som e r epor t b usiness f un ctinion w eonw h ant porively t includ creat ing list sStof all This I ple. BM Redbook pr ovides d et ailed forsmt hat at ion owt ot osup ef fect usee WebSp here udio cust om er s, gold cust om er s, account s, g old account s, balances, et c. App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. The Reports session bean is r ep resen t ed in Figur e 5- 1 6. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an r e 5dat - 1ab 6 ase. . Repor t s se ssion b ea n app licat ion ex a m ple un derly ing Figu r elat ional I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Stateful beans and state transitions St at efu l beans are of t en an ex t en sion of t h e client , and are not com plet ely decou pled f r om t heir client s as w it h st at eless session beans. W e can not just define a bu nch of generic, un r elat ed m et hods w it h out kn owin g t h eir relat ionship or t he ord er in w hich t hey ar e supp osed t o be execut ed. Th e bu siness m et hods, g en er ally, hav e no independent m eaning except in t h e con t ext of t he ord er in w hich t hey ar e t o be execut ed, wh ich is dict at ed by t h e cont r act t he bean h as w it h it s clien t . For exam ple, y ou cou ld n ot call t h e m et h od called listLargestAccount u nless t he currentCustomer h as alr ead y been set , because t he listLargestAccount m et hod d ep en ds on
t he st at e of t he session bean, t h at is currentCustomer, t o hav e been set . St at efu l session b ean s ar e m ost of t en m odeled in t erm s of st at e t ran sit ion diag ram s, w her e each st at e chang e t o is det er m ined by a p r ev ious t r ansit ion fr om . A par t icu lar st at e cannot usually be r eached out of seq uence. I t is up t o t he b ean dev elop er t o ensur e t h at t h e bean is in t h e pr oper st at e in or der t o ex ecut e t he cur r ent m et hod an d t r an sit ion t o t he nex t st at e. •
Table of Content s
Additional requirements • I ndex EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
The f ollow in g ar e som e exp anded r eq uir em ent s an d ch oices t hat w e hav e m ade f or t h ese sim ple By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss session b ean s. Pub lish er: I BM e: April 1 1, 20 03 UsePubofDatremote interfaces I SBN: 0- 73 84- 26 09 -1
Pages: 75 0
We ex pect t hese beans t o be used by client s. Ther efor e, w e w ill define on ly r em ot e h ome and com ponent int erf ace m et h ods f or our beans.
Component interface methods Lear n about t he EJB 2 .0 specif icat ion We h ave chosen t o u se a r em ot e in t er f ace for ou r session b ean s, becau se t hese are coar segr ained prwocesses t hatApp w illlicat be ion execut fr om ou t sid e of t he EJB cont ainer . Devbusiness elop EJBs it h St u dio Deved eloper For exam ple EJBs p ur poses w eewApplicat ill sh ow ion howSer t ov im Deploy t o W only eb Sp, her er plem en t t h e follow ing m et hods on our com ponent int erf aces: This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans nkitinect g—deposit, withdraw, ( EJB) Ba ar ch u re, an d d ep loym ent transfer of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Rehout por tts—listLargestAccount, setCurrentAccount Thr oug he book , w e pr ov id e examsetCurrentCustomer, ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. IBuilding n Part 1, wethe in t r remote oduce EJBscomponent as a p art of Javinterface a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed The r em ot e int er face r epr esent s t h e bu siness m et hods t hat we ex pose t o our clien t . per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful Figur e 5- 1 7imp sh lem owsent t heatrions emotofe EJBs. com p onent in t er f ace of t he Banking st at eless session bean. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session Figu 7 . Rem oten e beans com pone nt in t eerr ef ace f orApBplicat an kion in g stelop a t eerle. We ss session beans,r e an5 d -m1essag e- d r iv using WebSph St u dio Dev also im plem be an ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
public interface Banking extends javax.ejb.EJBObject { public BigDecimal deposit(String accountID, BigDecimal amount) throws FinderException, RemoteException; public BigDecimal withdraw(String accountID, BigDecimal amount) throws FinderException, InsufficientFundException, RemoteException;
public BigDecimal transfer(String accountID1, String accountID2, BigDecimal amount) throws FinderException, InsufficientFundException, RemoteException; ...... •
Table of Content s
• }
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Not ice t hat w e ext end ed EJBObject in st ead of EJBLocalObject—as w it h our en t it y Pub lish er: I BM beans—because t h is is a r emot e com pon en t in t er f ace. Also, w e m ust en su re t hat t he par am et er s April 1 1, of 20 03 andPub r etDat ur ne: values each m et hod ar e v alid Jav a RMI - I I OP t y pes. I SBN: 0- 73 84- 26 09 -1
Figur ePages: 5- 1 875 sh0 ows t he r emot e com p onent in t er f ace of t he Reports st at ef ul session bean.
Figu r e 5 - 1 8 . Rem ot e com pone nt in t e r f ace f or Re por t s st a t ef u l se ssion be an Lear n about t he EJB 2 .0 specif icat ion public interface Reports extends javax.ejb.EJBObject { Dev elop EJBs w it h St u dio App licat ion Dev eloper public setCurrentCustomer(int custId) throws RemoteException; Deploy EJBs t o W eb Sp her e Applicat ion Ser v er public setCurrentAccount(String accountID) throws RemoteException; This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans public String[] listLargestAccount() ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. throws RemoteException, ReportInconsistentException; Thr oug hout t he FinderException, book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. ...... I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic } concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. Becau se t hese are r em ot e int er faces, we mu st t h row RemoteException in our met h ods. Add it ion2, ally , winet rar e p rop ingp le the FinderException t o t ht hen e client as w ell en ast it cuy stbeans, om I n Part we oduce t hagat e sam bank ing applicat ion and imp, lem ent session app licatan iond ex cept ions ( not gh mion oney) beans, m essag e- d rInsufficientFundException iv en beans using WebSph er e St u dioenou Ap plicat Devand elop er . We also ReportInconsistentException ( cut st account notand setsim ) . We t ions m ore im plem ent fin der m et h ods, dif fer en mom apperinor g st r at egies, p ledescrib client s et hex atcep u se t he in EJBs. At det er 8, e" Ad dit ional concept sact ions, cep t ions,ersecur y " on e 277 t he ail en in d, Ch w eapt d escrib how t o deploy EJBs:apt ran plicat ion s t o ex a WebSph e Appitlicat ionpag Serv er. .
Building the remote home interface The r em ot e hom e int er face r epr esent s t he life- cy cle m et hods of our b ean for client s t o cr eat e a new in st an ce of our bean, as w it h st at ef ul session beans, or t o locat e an ex ist in g bean in st an ce in t he p ool, as w it h st at eless session beans. Fr om t h e client persp ect iv e, each appear t o be creat ing a bean. Figur e 5- 1 9 sh ows t he r emot e hom e int er face of t he Banking st at eless session bean.
Figu r e 5 - 1 9 . Rem ot e h om e in t e r fa ce of Ba nk i ng st at el ess sessi on bea n
public interface BankingHome extends javax.ejb.EJBHome { public Banking create() throws CreateException, RemoteException; •
Table of Content s
} •
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Figur e 5- 2 0 sh ows t he r emot e hom e int er face of t he Reports st at ef ul session bean. Pub lish er: I BM Pub Dat e: April 1 1, 20 03 Figu r e 5 - 2 0 . Rem ot e h om e in t e r fa ce of Repor t s st at ef ul session be an I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
public interface ReportsHome extends javax.ejb.EJBHome { public Banking create() Lear throws n about CreateException, t he EJB 2 .0 specif icat RemoteException; ion Dev elop EJBs w itcreateByAccount(int h St u dio App licat ion DevcustomerID, eloper public Banking String accountID) Deploy EJBs t o W eb Sp her e Applicat ion Ser v er throws CreateException, RemoteException; This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio } App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr book w eEJBHome pr ov id e exam ples on a simp le bank ion w it h an Not oug ice thout hat wt he e ext end, ed in st ead of based EJBLocalHome—as w it hing ourapp en licat t it y beans—because un derly ing r elat ional dat ab ase. t his is a r em ot e hom e int er face. IFor n Part we in t rboduce a pon art Jav Enum t erent p rise Ed it ionm (et J2hod EE), and cover basic t he 1, Banking ean wEJBs e d efas ined ly of one n ao-2arg create w hich is allt he t hat w e ar e concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he r eq uir ed t o, and can , defin e for a st at eless session bean. This rest r ict ion on st at eless session EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed beans en su res t h at ev er y st at eless session bean in st ance is t he sam e as ev er y ot h er in st ance of per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or t he sam e t y pe ( as defined as being cr eat ed fr om t he sam e hom e obj ect ) . successful imp lem ent at ions of EJBs. For t he Reports st at ef ul b ean w e show t wo dif fer en t f orm s of t h e create m et hod t h at allow u s I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session t o cr eat e t he st at e of t he b ean dif fer en t w ay s. We m ust not cr eat e any f inder m et hod s w it h ou r beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also beans, b ecause session beans d o not supp ort fin der m et h ods. The arg um en t t yp es of t h e creat e im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At m et hods m u st be v alid Java RMI - I I OP t y pes. t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Becau se t his is a rem ot e int er face, w e m ust t hr ow RemoteException in our met h ods. Add it ion ally , t h e CreateException m u st be t hr own t h at ind icat es an er ror on t he creat e. We descr ibe ex cep t ions in m ore d et ail in " EJB except ion hand lin g" on p age 30 4.
Building the bean class The session bean class im plem en t s t he b usiness met hod s of t he bean as defined in ou r com ponent int erf ace, and imp lem ent s t h e callb ack met hod s used by t he con t ainer as def in ed in our hom e int er face.
So f ar , t he only sig nificant dif ference w e saw in t h e con st ru ct ion of st at eless and st at efu l session beans is in h ow w e defin e create m et hod s in t he hom e in t er f ace. Ot her t han t h at , ever yt hing has been pr et t y m uch t he sam e. This w ill n o long er h old t ru e as w e m ove ont o t he const r uct ion of t he bean class it self. Th e dif fer en ces ar e so b ig t h at w e w ill cov er each in it s own sect ion . •
Table of Content s
Stateless session bean class • I ndex EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Figur 5- 2li,1Wout sh ows a skelet of nkt he st at eless session By UelieWah er Denay er, Lar on s Schu , Deborah Shaddon , Mart inbean Weiss wit h t h e create an d callback m et hod im plem ent at ion s, and an im p lem ent at ion of one b usin ess m et hod , deposit. Pub lish er: I BM
Figu e e:5 -April 2 11.1,Ba nk i ng be an i m ple m en t a t i on ( e xt r a ct ) PubrDat 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
public class BankingBean implements javax.ejb.SessionBean { private SessionContext mySessionCtx = null; private AccountLocalHome accountHome Lear n about t he EJB 2 .0 specif icat ion
= null; // entity home fields
private TransRecordLocalHome trecordHome = null; Dev elop EJBs w it h St u dio App licat ion Dev eloper private CustomerLocalHome customerHome = null; Deploy EJBs t o W eb Sp her e Applicat ion Ser v er //getHomes is a pr convience initialization method This I BM Redbook ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans protected ( EJB) ar ch it ect uvoid re, an getHomes() d d ep loym entthrows of su ch EJBException ap plicat ion s t o { a WebSph er e App licat ion Serv er. try { Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. InitialContext initCtx = new InitialContext(); I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and accountHome t h e ar ch it ect ur = e. (AccountLocalHome)initCtx.lookup I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. ("java:comp/env/ejb/Account"); We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. ... I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m(NamingException essag e- d r iv en beansex) using } catch { WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w eex.printStackTrace(); d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. throw new EJBException("Error looking up home: "+ex.getMessage()); } } public void ejbCreate() throws CreateException { getHomes();
} public void ejbRemove() { } //nothing to cleanup here, but this is where it would go public void ejbPassivate() { } • •
Table of Content s
public void ejbActivate() { } I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
public SessionContext getSessionContext() {
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
return mySessionCtx; Pub lish er: I BM
} Dat e: April 1 1, 20 03 Pub I SBN: 0- 73 84- 26 09 -1
public void setSessionContext(SessionContext ctx) { Pages: 75 0
mySessionCtx = ctx; } public BigDecimal deposit(String accountID, BigDecimal amount) Lear n about t he EJB 2 .0 specif icat ion throws FinderException { Dev elop EJBs w it h St u dio App licat ion Dev eloper try { Deploy EJBs t o W eb Sp her e Applicat ion Ser v er AccountLocal accountHome.findByPrimaryKey(accountID); This I BM Redbook pr ovidesaccount d et ailed =in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans amount, ( EJB) ar ch itTransRecordLocal ect u re, an d d ep loymtr ent=oftrecordHome.create("C", su ch ap plicat ion s t o a WebSph er e Appaccount); licat ion Serv er. account.deposit(amount); Thr oug houtreturn t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. } catch (FinderException ex) { I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic throw new concep t s and t h e ar ch itFinderException("Account ect ur e. I n par t icu lar w e point "+accountID+" out t he n ew fu nctnot ionsfound"); int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed } catch ex) per sist ence ent it y(CreateException b ean s and m essagedr iv{en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. throw new EJBException("Account "+accountID+" Transrecordfailed"); I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, } an catch d m essag e- d r iv en beansex) using (EJBException { WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w ethrow d escrib e how t o deploy EJB ap plicat ion s t o a WebSph e App"+accountID); licat ion Serv er. new EJBException("Account deposit failederfor } }
ejbCreate method A st at eless session bean can h ave on ly one ejbCreate m et hod , w hich mu st r et ur n void an d
cont ain no arg um en t s. When a cont ain er in vok es ejbCreate at session bean in st an ce cr eat ion t im e, w e t ak e t h e opp ort unit y t o get t he hom e in t er f ace ob ject s of t he entity b ean s used by our session bean. This w ay , w e can do t he JNDI look up on ce f or all in st an ces of t h is bean. Th is is an ex am ple of a com mon act ivit y in st at eless session bean cr eat e m et hods. •
Table of Content s
Callback methods • I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
We h ave left t he ejbActivate an d ejbPassivate m et hod s blank , since t h ey d o n ot af fect t h e By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss st at eless session bean b eh avior . Ad dit ionally, since w e h ave no r esour ces t o clean up w hen t he bean is r em ov ed f rom m em or y, w e hav e also left t he ejbRemove m et hod b lan k. I n t h e Pub lish er: I BM setSessionContext, w e m ade su re t hat we main t ained a local ref er ence t o our cont ex t ob ject . Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0 Business method
We h ave chosen t o show only t he de posit m et hod f or t his b ean . I n t his m et hod, we find t he Account ent it y bean, cr eat e a TransRecord in st an ce, t hen u se t he account t o u pdat e t he b alan ce based on t h e am ount passed as a par am et er . As y ou can see, no client st at e is at t emp t ed t o be saved in t his m et hod. Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
Stateful session bean class
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Figur e 5- 2 2 an d Figur e 5- 2 3 sh ow a sk elet on of t he Reports st at ef ul session bean, an d som e This pr ovides ailed in for m atack ion mon ows.t o ef fect ively use WebSp here St udio w or kI BM t hatRedbook w e choose t o d o ind et t he ot her callb ethhod App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Figu r e 5 - 2 2 . Repor t s bea n im p le m en t a t ion ( pa r t 1 )
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. public implements { and cover t he basic I n Part 1,class we in tReportsBean r oduce EJBs as a p art of Javjavax.ejb.SessionBean a 2 En t er p rise Ed it ion ( J2 EE) concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB private 2. 0 sp ecifSessionContext icat ion t hat pr ov idmySessionCtx e, for ex am ple,=ennull; hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or // home ofions ourof entity successful impfields lem ent at EJBs. beans private accountHome = null; I n Part 2, we transient in t r oduce t hAccountLocalHome e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also private CustomerHome custRemoteHome null; im plem ent fintransient der m et h ods, dif fer en t m app in g st r at egies, and= sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. private transient CustomerLocalHome customerHome = null; // stateful fields private int currentCustomer = 0; private String currentAccount = null; ......
//getHomes is a convience initialization method protected void getHomes() throws EJBException { // same as for stateless session bean } •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Figu r e li,5 Wout - 2 3er. Denay Repor ts bea im p le m en, Mart t a tinion By Ueli Wah er, Lar s Schu nk ,nDeborah Shaddon Weiss( pa r t 2 ) Pub lish er: I BM Pub Dat e: April 1 1, 20 03
...continued...
I SBN: 0- 73 84- 26 09 -1
Pages: 75 0 public Reports ejbCreate() throws CreateException {
getHomes(); } Lear n about t he ejbCreateByAccount(int EJB 2 .0 specif icat ion public Reports customerID, String accountID) Dev elop EJBs w it h St uthrows dio App CreateException licat ion Dev eloper { Deploy EJBs t o W eb Sp her e Applicat ion Ser v er getHomes(); This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio setCurrentCustomer(customerID); App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. setCurrentAccount(accountID); Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an } ing r elat ional dat ab ase. un derly public ejbRemove() I n Part 1, wevoid in t r oduce EJBs as a{p } art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he void ejbPassivate() EJB public 2. 0 sp ecif icat ion t hat pr ov id e, for{ ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or accountHome = ions null; customerHome = null; custRemoteHome = null; successful imp lem ent at of EJBs. } 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session I n Part beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also public void ejbActivate() im plem ent fin der m et h ods, dif fer en{t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. getHomes(); } public SessionContext getSessionContext() { return mySessionCtx; } public void setSessionContext(SessionContext ctx) { mySessionCtx = ctx; }
public void setCurrentCustomer(int custId) {
currentCustomer = custId; } public void setCurrentAccount(String accountID) { currentAccount = accountID; • •
Table of Content s
}
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
public String[] listLargestAccount()
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
throws FinderException, ReportInconsistentException { Pub lish er: I BM Pub Dat April 1 1, 20 03 ife: (currentCustomer
== 0) throw new ReportInconsistentException("..");
I SBN: 0- 73 84- 26 09 -1
try 75 {0 Pages: AccountLocal account = accountHome.findLargestAccount(currentCustomer); if (account == null) { Lear n about t he EJB 2 .0 specif icat ion return String[] "No Dev elop EJBs w it hnew St u dio App licat{ion Devlargest eloper account", "", "" }; } EJBs elset o{ W eb Sp her e Applicat ion Ser v er Deploy return new String[] { (String)account.getPrimaryKey(), This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans account.getBalance().toString() };er. ( EJB) ar ch it ect u re,account.getAccountType(), an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv Thr oug hout} t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. } catch (FinderException ex) { I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch itFinderException("LargestAccount ect ur e. I n par t icu lar w e point out t he nerror"+ex.getMessage()); ew fu nct ions int r od uced w it h t he throw new EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or } successful imp lem ent at ions of EJBs. } I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, ..... an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t}he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
ejbCreate methods A st at ef ul session bean m u st hav e at least one ejbCreate m et hod , bu t can hav e m ult iple un ique creat e m et hod s for each w ay in w h ich y ou wan t t o inst ant iat e y our session b ean 's st at e. Each ejbCreate m et hod m ust cor r esp ond t o a create m et hod in t he b ean h ome in t er f ace. Th e r et u r n t y pe of a cr eat e m et hod on a st at efu l session b ean m ust alw ay s b e t he com ponent int er face bean class ( in ou r case, Reports) , b ecause it is a r emot e bean. I f t h is wer e a local b ean , w e w ould
r et ur n a ReportsLocal cor r espon ding t o an EJBLocalObject. Not e t hat t here is no ejbPostCreate m et hod in a session bean, as t here is in an ent it y b ean . As w it h our st at eless session bean, we use t he cr eat e t o also in it ialize inst an ces t o hom e ent it y bean obj ect s f or lat er use. This ex amp le sh ow s t hat t he st at e of t he b ean can also be init ialized t hr ou gh t he create m et hod s, as in t he case of t he createByAccount(int customerID, String accountID) m et hod . Each m et h od set s t h e cur rent st at e of t h e repor t s diff er ent ly . Which one t o call is up t o t he clien t . •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Callback methods
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
I m plem ent ing m eaning ful callback m et hod logic is im por t ant t o pr operly m aint ain ing t he Pub lish er: I BM st abilit y of st at eful session beans: Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
The ejbPassivate m et hod shou ld ensur e t h at t h e st at e of t he b ean can be p assiv at ed , or Pages: 75 0 ser ialized . Add it ion ally , t h e ejbActivate sh ould ensu re t hat t he bean st at e is r econ st it u t ed . I n our case, we defined t he r efer en ces t o our ent it y bean h om e obj ect s as b ein g t ran sient . This m ean s t hat t his dat a w ill n ot b e sav ed wh en t h e bean is passiv at ed. W e are requ ired t o set t h ese n llabout EJB 2 .0 specif ion . When t he bean is r econst ru ct ed , w e m ust g et t he fields Lear t o nu in t het he ejbPassivate micat et hod hom e int er face ob ject s once again . Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o m Wet ebhod Sp her e Applicat v er t he r esour ces u sed by t h e bean are cleaned The ejbRemove shou ld m ak eion surSer e t hat up . This is m ost im por t an t f or open resour ces su ch as d at abase connect ions. Som et im es This Idev BM elop Redbook pr set ovides d ett ailed at ion on hgow o ef fect ively use WebSp here St er s w ill f ields o nu llint oforhm elp t r igger arbtage collect ion, but t his r eally is udio not App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans r eq uir ed . ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase.
Not e
I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h et hat ar chejbRemove it ect ur e. I nispar t icu lar ant w e eed point t he n ewf orfu inst nct ions intifr od h t he Recall not g uar t o out be called, ance, t heuced b eanw it has EJB 2. 0 spaecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er m anag ed t im eout . per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2,eloper we in st r ar oduce t hioned e samfrp om le bank applicat ion ion andt yt hen imp lem ent enin t it tyhe beans, session Dev e caut d oining g any f in alizat pe b usiness log ic ejbRemove beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also m et hod, su ch as an up dat e t o save d at a, b ecause t h is met h od w ill m ost lik ely ex ecut e in an im plem fin der tm et h ods, enext t m fapp in g st r atbegies, sim pain le er client s t h at t he EJBs. unent sp ecified r ansact iondif al fer cont or session ean s wand it h cont - m anag edutse r ansact ions At t he en( CMT) d, w e. dSo, escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. alt hou gh t he sp ecificat ion say s t h at y ou can access r esou rce m anag er s in ejbRemove ( and ejbCreate f or t h at m at t er ) , y ou mu st not m ake assum pt ion s abou t how t he con t ainer w ould hand le t he t r ansact ional con t ex t ( su ch t h at r ollback or com mit m ight not be g uar ant eed if t h e r em ov e fails) . Put t his t yp e of sav e logic in t o y our own b usin ess m et hod t o b e called befor e a call t o ejbRemove. This u nspecif ied t r ansact ional cont ex t is also t ru e for ejbCreate, b ut as long as t he ejbCreate on ly r ead s init ializat ion d at a t h at is n ot par t of a t r ansact ion, y ou should be OK.
Business method
We h ave chosen t o show t hr ee m et h ods of t h e repor t s bean t o show t h at t he t hir d m et h od ( listLargestAccounts) is called b ased on t he st at e t hat wou ld h ave alr eady b een set in t he fir st m et hod :
• •
We f irst call t he setCurrentCustomer m et hod , w hich set s t h e cur rent cust om er ( alt ern at ively , w e could hav e called t he ejbCreate(int customerID, String accountID) m et hod t o d o t h e sam e t hing) . Table of Content s
Next , wIendex sh ow t h e listLargestAccounts m et hod . This m et hod r et ur ns a String[] of t he This sam e logic could hav e heor ically been in a Shaddon st at eless session Ueli tWah li, et Wout er Denay er,im Larpslem Schuent nk , ed Deborah , Mart in Weiss bean, becau se w e cou ld h ave passed t he customerId t o t h e listLargestAccount m et hod . But t his is j ust a dem onst r at ion t hat m Iet hod 's ex ecu t ion is d ep en dent on an ot her m et hod, or st at e, hav ing been set . Pubone lish er: BM
EJB 2 . 0accou D e ve lopm e nt ber w it h, W udio Appl icattihat on De elom operer . nt num t yebSphe pe, anrde St b alance for cuvst By
Pub Dat e: April 1 1, 20 03
The m et hod checks t hat t he cur r en t cust om er has b een set and t hr ow s an app licat ion I SBN: 073 8426 09. -1 except ion if not Pages: 75 0
Transactions and stateful session beans We h ave st ar t ed a discussion abou t t r an sact ions, ev en t h ough w e k eep saying t h at t r ansact ions ar e cover ed in " Tr ansact ion s" on p age 27 9. Bu t t h er e ar e som e fu ndam ent al asp ect s of t h e Lear n about t he EJB 2 .0 specif icat ion t r ansact ional seman t ics t hat you w ant t o con sider w h en dev elop in g you r st at ef ul session beans t hat do not r eq uire kn owin g ev er y t hin g ab out t ran sact ions yet : Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy t o W debiscussed Sp her e tApplicat ion Ser vter We h aveEJBs alr eady he unspecified r an sact ional con t ex t of t he ejbCreate an d ejbRemove m et hod s. This w ill inf lu en ce w hat t yp es of act iv it ies y ou can, or sh ould, d o in This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio t hese callb ack met h ods. App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Anot h er issue t o consider is t hat t he session bean inst ance's conv ersat ional st at e is not t r ansact ional, and m ay hav e t o m anually r eset it s st at e if a r ollback occu rr ed. So, for Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an inst ance, if a b ean had u pdat ed it s ow n con ver sat ion al st at e, and t h en t r ied t o do a un derly ing r elat ional dat ab ase. t r ansact ional d at abase up dat e, and t hat dat abase upd at e failed, it w ould be up t o t he b ean dev ensurEJBs e t hatast he at eofofJav t he isprrise esetEd pritoperly h enand t h iscover occurtrhe ed.basic By I n Part 1,elop we er in ttrooduce a pstart a 2b ean En t er ion ( J2wEE) im plem ent in g t h e SessionSynchronization in t er f ace, st at efu l session beans can be concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he if ied hen occur , an d ple, can en t akhanced e t he n ecessar st eps r eset e. ed EJB 2.not 0 sp ecifwicat iont hese t hat ev pr ent ov ids e, for ex am fun ct ionyalit y fort ocont ainiters-st mat anag Dev eloper s m ust en su re t hat t he pr op er log ic is imp lem ent ed. See " Session sy nchr on izat fion per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines or int er face" on p age 30 0 for m or e inf orm at ion . successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Deployment descriptor im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Figur e 5- 2 4 sh ows t he d ep loy m ent d escr ip t or ent r ies for t he Banking st at eless session bean. For br evit y , w e d o n ot show t he Reports d ep loym ent descr ipt or .
Figu r e 5 - 2 4 . D epl oym e nt de scr ipt or of a st a t e le ss session be an
Banking
mysessionpackage.BankingHome mysessionpackage.Banking mysessionpackage.BankingBean Stateless •
Table of Content s
Container
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
ejb/Account Pub lish er: I BM
Entity Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
myentitypackage.AccountLocalHome
Pages: 75 0
myentitypackage.AccountLocal
...
Lear n about t he EJB 2 .0 specif icat ion
Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er The in g ar e som e im por t ant point sm regar dinon g thhis m en t descrip t or: here St udio This fIollow BM Redbook pr ovides d et ailed in for at ion ow deploy t o ef fect ively use WebSp App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) d— d ep ent su chif ap plicat ioniss st t oataefu WebSph ereless. e App licat ion Serv er. ont y pe Thloym is det ermofines t his bean l or st at Thr oug t he, book , w ov id e exam w ples le bank it h an w e < hout hom e> < rem otee pr >— Because e arbased e definon in ga tsimp his bean t o ing u se app remlicat ot e ion int erwfaces, un derly ing r elat ional dat ab ase. m ust d ef ine t h e hom e int er face class as ( vs. ) , an d t he com ponent int er face as ( vs. ) . Recall t h at a bean cou ld h ave b ot h t yp es if n eeded. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e.WI en hav par teicu lar ed w e tpoint out as t hecon n ew fu nctmions int wion it h t he — defin his bean t aineranag edr od t r uced ansact EJB 2.( CMT) 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er m anag ed . The alt er nat iv e is bean- m anag ed t r ansact ion. per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful EJBs. < ej b-imp r ef lem > —ent Th at is ions is anofopt ional en t ry t hat allow s u s t o define a local ref er ence t o anot her EJB. By definin g local r efer en ces, t he EJB can be r et r iev ed t hr ough t h e local JNDI I n Part 2, espace we in t r oduce e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session nam for t hist hbean. beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem entisfin et hr ods, app in st reratence egies, p le client s t h at u se t he EJBs. At This w der h y inmou codedif w fer e wen ertemable t ogref t heand EJBsim u sing t he en"java:comp/env/ejb/Account". d, w e d escrib e how t o deploy EJBI fap plicat ion s t o a WebSph App licat ione Serv er. ch t h e w e did not do t his, t hen er w ee w ould hav t o sear cell or ser v er JNDI nam espace for t he deploy m en t n ame of t his bean. This allow s us t o creat e a logical bind ing t o an associat ed EJB' s JNDI n ame t hat can r emain const ant for t he life of ou r EJB, an d allow s u s t o n ot b e depend en t up on t he r eal JNDI n am e t hat t he associat ed bean m ay h ave act u ally been deploy ed u nder . For exam ple, t h e Banking b ean can alw ay s r efer t o t he Account b ean u nder t h e ejb/Account n am e. How ev er , at deploy m en t t im e, a r u nt im e bind in g is mad e t hat link s t he ejb/Account t o t h e real n am e t hat t he bean is d ep loyed und er . Now w e do not hav e t o chan ge our bean' s view of t h ings based on som e deploy m en t set t ing.
Session bean design and construction best practices I n our exam p le we in t r oduced a couple of b est pr act ices t hat w e w ant t o su mm ar ize, and int r odu ced a f ew add it ion al it ems for y ou t o consider . These ar e guid elines, r at her t h an r ules, so let y our ex perience g uide y ou in t h ese m at t er s. •
Table of Content s
•
I ndex
oe n steore e rsat irona l st aAppl t e ica i n tist t e vleelss se ssi on EJB 2 . 0DD veot lopm nt wconv it h W ebSphe e St udio ona De oper By
b ea ns— Th is is pr et t y m uch t he defin it ion of a st at eless session b ean , but it bears r epeat ing . A m et hods ex ecu t ion sh ould Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss not r ely on any pr ev ious client or con ver sat ional st at e h aving b een set in or der t o execut e. PubUlish seer:e jI BM b Cr ea t e
t o ini t i al iz e non- con ve rsa t ion al st a t el ess v a ria bl es— Tak e ad van t age init ialize resour ces t hat ar e u sed t hr oug hout t he lif e of t he st at eless
Pub e: April 1, 20 03 ofDat t his m et1hod to
session I SBN: 0- 73 b ean 84- 26 . 09 -1 Pages: 75 0
Re le a se op en re source s on st a t e le ss ej bRe m ove — Use t he ejbRemove m et hod t o clean up any open r esour ces acqu ir ed by a st at eless session b ean . D o n ot i m p le m ent log ic i n st a t e le ss be an e j bP a ssiv a t e a nd ej bAct iv a t e— St at eless beans are n ot act iv at ed or passiv at ed, so t his behav ior is not called. Lear n tabout t he 2 .0otspecif icat ion D o se t r an si enEJB t a nd he r nonse ri a liz a bl e fi el ds f or p assiv at ion— Be su re t hat you clean up and r econ st it ut e y our st at e dat a p r operly in t he ejbPassivate an d ejbActivate Dev h St u dio Appb licat ion Dev eloper m et elop hods EJBs f or stwatitefu l session ean s. Deploy e Applicat v er it is ex t r a w or k , w e t hin k t hat t he st at ef ul V a li dat EJBs e st attoe W i nebstSp a ther e ful b ea ns—ion AltSer h ough bean should check it s st at e t o ensur e t h at t h e bean is in a v alid st at e in or der t o ex ecut e This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio t he cu rr ent oper at ion —for ex am ple, check ing t hat t he customerID is not n ull b ef ore t r ying App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans t o list t he lar gest account s. Th is w ay you d o not hav e t o rely solely on t h e client ' s ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. int er act ion s ( w hich mig ht n ot b e k now n) t o d et er m in e t he im pact of a chan ge on t he syst em t.he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Thr oug hout un derly ing r elat ional dat ab ase. Ensur e you r st a t e ful b ea n use ca se log ic cal ls ej bRe m ove — You shou ld ensur e t h at y our logic ant ees ov of e wJav ill abe2 called t heEdnitation ur al com of a tuser' s I n Part 1, we in tgr uar oduce EJBst hat as ar em p art En t er pat rise ( J2 EE)plet andion cover he basic session of act iv it y. Do n ot r ely on t he cont ain er t o do t he cleanup f or y ou. concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed W at ch out for un spe cif ie d t ra nsact i onal cont e x t m e t hods—Met hod s such as per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or ejbCreate an d ejbRemove m ay ex ecu t e in an u nspecif ied t r ansact ional cont ex t , and so successful imp lem ent at ions of EJBs. behav ior t h at m ight hav e t o be t r ansact ional sh ould b e av oid ed , such as dat ab ase u pdat es. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session Be a w a r e t o m a nua ll y synchr oni ze se ssion st a t e w it h t ra nsa ct i ons— Be awar e t hat beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also y our b ean could m ove t o an unst ab le st at e if a t r ansact ion it w as execut ing f ailed, an d you im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At do not t ak e measur es t o set t he st at e back t o somet h in g saf e, by b eing not if ied of t h e t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. SessionSynchronization ev ent s. D o n ot cal l a session be an f rom m ul t ip le cli en t s— Ensur e t h at y our log ic will p r ohibit ( even fr om t he sam e client ) t he st at ef ul session bean b ein g called f r om t w o d if fer ent client s ( as d ef ined as t w o sep ar at e t hr eads) . Re e nt ra ncy — Ensur e t h at y our b ean s do not call each ot her 's m et hod s in cy cles. Reent r ant beans are p r ohibit ed. Ex ce pt i on h an dli ng- - Cer t ain except ions cau se t he bean t o b e evict ed f r om m em or y . Ke e p t h e be an s re la t iv e ly shor t l iv ed — Alt h ough session beans can sp an a client 's
conv er sat ion, t his conv er sat ion sh ould not last for ever ( or ev en v ery lon g, say , a wor k day ) . Th e longer t h e bean in st ance lives, t he m or e chan ces it has of som et h ing bad hap pening t o it ( such as t he cont ain er rem ov in g it ) . Tr y t o k eep t h e client conv er sat ional st at e f in it e and m anageable, f or exam ple, t h e len gt h of a t r an sact ion of som e sor t . I f necessar y, br eak b ig, long - r unn in g session beans in t o m ult iple, sm aller liv ed on es t hat ar e easier t o m anage. Coar se - g ra ine d vs. f in e- gra in ed obj ect s— Session b ean s sh ould be coar se- gr ained Table of Content s bu sin ess pr ocesses, su ch as d ep osit and w it hd raw al. Tr y t o k eep fine- gr ained act iv it ies • I ndex hidd en —t hat is, do not ex pose all t h e m et h ods of an ent it y b ean t h rou gh a session bean EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper w r apper . •
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Se ssi on f a ca de — Th e EJB session f acad e pat t er n pr ov id es a st able, high - lev el gat ew ay t o serI BM ver - side com ponent s. An EJB session f acade hid es t he ent it y b ean int er faces t o t he Pubt he lish er: client s. All access t o ent it y beans m ust g o t h rou gh m et hods of a session b ean . I n EJB 2 .0 Pub Dat e: April 1 1, 20 03 ent it y b ean s ar e defined w it h local int er f aces only . Session beans ar e d ef ined wit h r emot e I SBN: 0- 73 84- 26 09 -1 and possibly local int er faces. Because all access is t h r ough session b ean m et hods, 75 0 tPages: r ansact ion m an agement is w it hin t he session bean and all ent it y accesses are perf or med w it hin t he sam e t r ansact ion. See " Using a session f acad e t o en t it y beans" on p age 26 2 for m or e inf orm at ion . Cachi ng of EJB h om e s— W e hav e seen one w ay t h at w e ar e caching EJB hom es in our session b ean t cr em et hspecif ods. Alt Lear n about heeat EJB 2 .0 icathou ion gh t his is m or e eff icient t han no caching alt er nat iv es, w e ar e st ill doing t h e looku ps and t h e cach e for ev er y b ean inst ance. Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Client view of a session bean This sect ion d escrib es m or e of t h e client view f or t he Banking an d Reports EJBs t hat ext en ds t he basic client ex am ple in " EJB client v iew " on p age 56 . The ex am ples ar e based on r em ot e hom e an d com pon en t int er faces. •
Table of Content s
•We h ig hlight I w ndex her e t he clien t v iew chang es depending on w het her t h e bean is st at ef ul or EJB . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper st at2eless. By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Looking Pub lish er: Iup BM the bean home Pub Dat e: April 1 1, 20 03
Figur e I52 50-sh he-1f inding of a hom e int er face of a session bean. SBN: 73ows 84- 26t09 Pages: 75 0
Figu r e 5 - 2 5 . Cli en t l oca l l ook up of hom e i nt e r f ace for a r em ot e se ssion be an
Lear n about tctx he EJB 2 .0 specif icat ion InitialContext = new InitialContext(); DevobjHome elop EJBs=wctx.lookup("itso/BankingJNDIName"); it h St u dio App licat ion Dev eloper Object Deploy EJBs t o W eb Sp her Applicat ion Ser v er BankingHome bankingHome = e(BankingHome)javax.rmi.PortableRemoteObject.narrow This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio (objHome, BankingHome.class ); App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Becau se we use a r em ot e int er face, w e are requ ired t o call PortableRemoteObject.narrow on un derly ing r elat ional dat ab ase. t he object t hat is r et ur ned fr om t he InitialContext lookup m et hod . Rem em b er , t h e JNDI nam e is1,specified at deploy m as en tat im e and n ot t he u al Ed deploy enEE) t descrip t or file. I n Part we in t r oduce EJBs p art of Jav a 2in En t eract p rise it ion m ( J2 and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he We n ow hav e a valid hom e obj ect in w h ich t o find r emot e com pon en t in t er f aces of our bean. EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
Client view of the session bean life cycle
I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, d m essag d r ivten beans WebSph e cle St uint dioerAp er ibed . We also We n owandiscuss t hee-clien v iew for using each of t he lif eercy actplicat ion s ion t hatDev w eelop descr im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At pr eviou sly . t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Creating and using a stateless session bean Figur e 5- 2 6 d em on st r at es an EJB client creat ing a Banking ob ject . Becau se t his is a st at eless session b ean , cr eat e does not act u ally cr eat e t h e bean in st an ce, b ut rat her allocat es on e fr om t he in st ance pool.
Figu r e 5 - 2 6 . Cr ea t i ng a st at el ess se ssi on b ea n
try { Banking bank = bankingHome.create(); String accountID = "101-1001"; BigDecimal amt = new BigDecimal(99.00); • •
Table of Content s I ndex bank.deposit(accountID, amt)
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
} (RemoteException ce) { By catch Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah
Shaddon , Mart in Weiss
// series problem using the bean
Pub lish er: I BM
Pub Dat e: April 1 1, 20 03
} catch (OtherException ce) { I SBN: 0- 73 84- 26 09 -1 75 0 problem using the bean? //Pages: other
}
Lear n about t he EJB 2 .0 specif icat ion Creating and using a stateful session bean Dev elop EJBs w it h St u dio App licat ion Dev eloper Figur e 5- 2 7 d em on st r at es an EJB client creat ing a Reports ob ject . Becau se t his is a st at ef ul session b ean ,EJBs cr eatt oe W act allyher creeat es a new inst an ce. I n t his ex am ple, w e are usin g one of Deploy ebu Sp Applicat ion bean Ser v er t he alt er nat e for m s of t he create m et hod of t h e bean. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat t he daevst elopm ap plicat ion based on t h e Ent er pr ise Jav aBeans Figu r eion 5 -Developer 2 7 . Cr eaf or t i ng at efen ult of session besan ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. try { I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic intt scustomerID 123; concep and t h e ar ch=it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed String acountID "123-4567"; per sist ence ent it y b ean=s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. Reports reports = reportsHome.create(customerID, accountID); I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session // use bean beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he reports.listLargestAccount(); en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. reports.listGoldAccounts(); ...... } catch (CreateException ce) { // series problem creating the bean }
Finding an existing stateful session bean We m ent ioned t hat an ex ist ing st at ef ul session bean can b e ret r ieved if we hav e a h andle t o t he bean. This is don e, for inst an ce, w h en a ser vlet st or es t he handle of t he st at eful session b ean in HttpSession b et ween ser vlet in vocat ions, an d su bseq uent ly has t o r et r iev e t he h andle in t h e nex t serv let inv ocat ion. Th e hand le can b e ser ializ ed t o any secon dar y locat ion ; t h e ser v let • Table of Content s session is t he m ost com m on b ecause serv let s ar e a com m on client of EJBs. •
I ndex
EJB . 0 D esh veow lopmhow e nt wt o it hstWor ebSphe e St udio v elsession oper We 2f irst e t h e rhand le tAppl o t hica e ti ston at De eful
bean. Figur e 5- 2 8 d em on st r at es an EJB client et r ievin g a er, hand t o nk a , st at ef ulShaddon session bean, and st orin g t h is in an HttpSession. By Ueli Wah li,r Wout er Denay Lar sle Schu Deborah , Mart in Weiss Pubrlish Figu e er:5 -I BM 2 8 . Cr ea t i ng an st a t e fu l se ssi on b ea n ha nd le Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
try { ...//already retrieved bean and httpSession... Handle rptHandle = reports.getHandle(); Lear n about t he EJB 2 .0 specif icat ion httpSession.setAttribute("MyReportsHandle", rptHandle); Dev elop EJBs w it h St u dio App licat ion Dev eloper } Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Now w e ion w anDeveloper t t o find our ist ding st at ef ul beanion using t he on h andle. Figur 5- 2Jav 9 aBeans App licat f or ex t he ev elopm ensession t of ap plicat s based t h e Ent er pre ise dem r atites EJB r et r iev ingof t su hech h andle f r om an d licat u sinion g it Serv t o f ind ( EJB)onst ar ch ectan u re, anclien d d ept loym ent ap plicat iontshe t o HttpSession, a WebSph er e App er. an exist ing st at eful session b ean . Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly Figu r eing5 -r elat 2 9 ional . Retdat r ieabviase. ng a st at ef ul session be an f r om a h an dle I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed try { per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. ...//httpSession... I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session rptHandle=(Handle) httpSession.getAttribute("MyReportsHandle"); beans, Handle an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At reports (Reports) javax.rmi.PortableRemoteObject.narrow t he en d,Reports w e d escrib e how t=o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. (rptHandle.getEJBObject(), Reports.class ); // use bean ... }
We shou ld n ot e t hat y ou cann ot u se t his t echniqu e w it h st at eless session beans ( nor w ould it
m ake any sense t o t r y t o, becau se a call t o t he h ome create m et hod d oes essen t ially t h e same t hin g) .
Removing a stateful session bean Figur e 5- 3 0 d em on st r at es an EJB client r em ov ing t he Reports st at ef ul session bean. Because it •r eally d oes n Table of tContent ot m at er if w se call remove on a st at eless session b ean ( b ecause all it w ould do is •clean up t h e IEJBObject, ndex n ot t h e bean it self) , w e do n ot show it h er e. EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah er,ov Lar sin Schu nk , st Deborah Shaddon Marton in Weiss Figu r e li,5 Wout - 3 0er. Denay Rem ga a t e fu l se,ssi b ea n Pub lish er: I BM Pub Dat e: April 1 1, 20 03
try { I SBN:
0- 73 84- 26 09 -1
Pages: 75 0
reports.remove(); } catch (RemoveException re) { // handle the remove exception..probably more serious }
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This r em ove w ill act ually cause t h e st at ef ul session bean t o b e r em ov ed f r om m em or y , and any fin al Iclean up of t heprbovides ean could occur in . for m at ion on h ow t o ef fect ively use WebSp here St udio This BM Redbook d et ailed App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Session EJB client exception handling
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r ld elat ab ch ase.and hand le ex cept ions. Becau se w e use t he rem ot e int er face, our Client s shou exional plicitdat ly cat client s m ust cat ch t he RemoteException on m any of t hese bean int er act ions. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t sally and it ect e. I n par t icu lar wife tpoint out heoblem n ew fu int r odbean' ucedswcreate it h t he Add it ion , wt heewar illch g et a ur CreateException h er e is a tpr innct t heions session EJB 2. 0 sp icat ion t hat pr ov id ex am ple, enhod hanced funexcept ct ion alit y for er - m m et hod, orecif RemoveException f r e, omfor a remove m et . These ions ar econt b otain h con sidanag er eded per sist ence entel it yexb cep eantsions, and as m essagedr iv eny beans. alsoion pr ovid ice guidelines f or app licat ion- lev w ould be an cust om We except t hat eabest b eanprdact ev elop er m ay successful imp lem tent of aEJBs. opt ion ally choose o t at hrions ow on m et h ods clause. EJB ex cep t ion h andling st r at egies are discu ssed in " EJB except ion hand lin g" on p age 30 4. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Summary This chap t er descr ibed bot h st at ef ul and st at eless session b ean s, t heir cont r act w it h t he cont ainer , and how t o b uild one. How t o build each bean using Applicat ion Developer is discussed in d et ail in Par t 2 of t his r ed book . •
Table of Content s
•We r evisit t he I ndex session b ean s discussed her e in f ut u re ch apt ers in t h e cont ext of except ions, EJB 2 . 0 D eions, ve lopm e nt secur w it h W r e St udio Appl ica ti on De v el oper t r ansact and itebSphe y. By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Chapter 6. Message-driven beans This chap t er descr ibes a n ew feat ur e av ailable in EJB 2. 0: messag e- dr iv en beans. We st ar t w it hTable t h e of con cept s of JMS- based messaging in g en er al, t h en d escrib e t he concept s of • Content s m essage- dr iv en beans, an d finally out line t he b ank in g ex am ple ext end ed w it h a m essag e- d riv en • I ndex bean. EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Introduction Messag e- d riv en b ean s ( MD B) ar e an ex cit ing new f eat u re of t he EJB 2. 0 specif icat ion . They open up a wh ole new r ealm of applicat ion ar chit ect ur al and int eg rat ion alt ern at ives n ot p rev iously av ailab le in J2 EE. This chapt er d escr ib es t he concept s an d st r uct u re of m essag e- d riv en b ean s •and MD B devTable of Content s int r odu ces som e new int egr at ion pat t er ns an d best pr act ices w hen elop ment , and •y ou ar e consider I ndexing ut ilizing MDBs in y our design. EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM
* Ne w EJB 2 .0 - M e ssa ge -dr i ve n be an s
Pub Dat e: April 1 1, 20 03
I SBN: 0-e73d84Messag riv26 en09b-1ean s ar e new in EJB 2. 0, an d, t herefor e, ar e new t o WebSph er e Version Pages: 755. 0
Dev eloper s w ere able t o ach iev e MDB- lik e fu nct ionalit y in Web Spher e Version 4 En t er p rise Ed it ion w it h m essage beans. Th ese m essage beans w ere a specialized class of st at eless session beans, and r an as a cust om ser vice of WebSph er e Ent erp rise, such t hat t hey w er e associat ed an d regist ered as app licat ion ser v er ser vices, and alt houg h pack in an er especif n ot associat Lear aged n about t heEAR, EJB 2w.0 icat ion ed w it h t he r unt im e cont ex t of a par t icular app licat ion or EAR. Alt houg h t hese messag e beans w er e like MDBs, t hey w ere sim ply a conv en ienEJBs t w ayw it t oh imp JMS ener in WebSph er e Ver sion 4. They could n ot Dev elop St u lem dio ent App alicat ionlist Dev eloper com plet ely benef it f rom t he m essagin g ser vices pr ovid ed by t he serv er, not ably , m essageEJBs b eant os W t hat lost connect ionsion t o Ser t heir Deploy eb Sp her e Applicat v erJMS dest inat ions could not gr acef ully r ecov er, and t he ent ir e app licat ion ser v er pr ocess had t o be r est art ed. Because t h ese This Iar BM e now Redbook man ageable pr ovidesJ2EE d et ailed ser vices in forof m at t he ion apon plicat h owiont oserv ef fect er,ively t his use t y peWebSp of r ecover hereyStcan udio App licat nowion beDeveloper det ect ed an f ord thhe andled d ev elopm mor eensaf t of elyapinplicat WebSph ion s er based e Veron siont h5. e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase.
Asynchronous messaging
I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep s and h eessag ar ch itinect ur descr e. I n ibed par t icu e hod pointofout n ew nctwions od uced w t het Asyn ch tron oust m g is as lar a mwet comt he m un icatfu ion her eint mressages ariteh sen EJB 2. 0 sp t hata pr ov id e, is forsent ex am hanced ct ion y ch forr onous cont ain - m anag onew ay , tecif hat icat is, ion w here r equest , b ple, ut noenr eply is r fun eceiv ed .alit Syn mer essag in g ed is per sist ence it y est b ean s and m essagedr iv en beans.gWe also best pr act ice f or analogou s t oent requ / r eply . Asyn ch ron ous m essagin is on e wpr ayovid t o ecom m unicat e b guidelines et w een successful imporlem ent at ions EJBs. s w it hin t he applicat ions. app licat ions, bet w een comofp onent IWe n Part weeady in t r oduce samlicat p le ion bank ing applicat ion m and t hen imp enion t it yEJBs beans, session h ave2,alr seen t ht hate app client s may com un icat ew it hlem appent licat t hr ough beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also RMI / I I OP. I nt er - app licat ion comm u nicat ion can be achieved any n um ber of w ay s: RMI , im plem ent finCORBA, der m et Web h ods,ser difvices/ fer en tSOAP, m app asyn in g stch r at egies, siminpg, le am client s tot h at se t he EJBs. At COM/ DCOM, ron ous mand essag ong h erus. t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Asyn ch ron ous m essag in g can be an alt er nat iv e comm un icat ion m ech anism for in vok ing EJBs, w her e m essage p rod ucer s ( t h e EJB client s) and m essage consum ers ( t he m essag e- d riv en b ean s) com mu nicat e via a t h ird - par t y const r uct , t h e m essage- or ient ed m iddlew are ( MOM) pr ov id er , such as WebSpher e MQ. I n t h is w ay, t he MOM in t er cept s all m essag es b et ween pr oducer and consum ers, such t hat t hey ar e decoupled pr ocesses.
JMS and the role of the JMS provider Jav a Message Serv ice ( JMS) is a Jav a API for accessing m essage- or ien t ed m iddlew ar e ( MOM) ser v ices. All EJB 2. 0 comp lian t ap plicat ion serv ers mu st supp or t asy nchr onous messag ing as a
m et hod of com m un icat ion based on t h e JMS pr ogr am min g int er f ace. JMS pr ov ides a com m on w ay f or Jav a pr ogr am s ( clien t s and J2EE applicat ions) t o creat e, sen d, r eceiv e, and r ead asyn ch r onous r equest s, as JMS m essages. Lik e any of t he J2EE API s, h ow ev er, JMS is j ust an ab st r act ion of a com mon an d p ort able pr ogr am m ing m odel. I t does n ot act ually pr ov ide t h e r unt im e im plement at ion or under lyin g ser v ice of t he m essagin g, such as w ould be p rov ided by t h e MOM. Theref ore, t o be EJB 2. 0 com pliant , ap plicat ion ser v ers mu st also pr ov ide f or a n ew t yp e of ser v ice: JMS m essagin g • Table of Content s ser v er. •
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
* Ne w EJB 2 .0 - I n t e r na l JM S se r ve r
Pub lish er: I BM
Pub Dat e: April 1 1, 20 03
To b e EJB 2. 0 ( and r eally , J2EE 1. 3) com pliant , t he ap plicat ion serv ers mu st pr ov ide 0- 73 84- 26 09 -1 t wI SBN: o t hings: Pages: 75 0
Sup por t f or MDBs for an aut omat ic asy nchr onou s m essaging pr ogr am m ing m odel. Pr ovid e an im p lem ent at ion of a JMS m essaging ser v er. This now means t hat t he JMS ser v er ab ilit ies ar e bu ilt int o t he ap plicat ion serv er, and t her ef or e can b e by t MDB and - MDBased solu t ions alike. Lear nused about he EJB 2 .0non specif icatbion Dev elop EJBs w it h St u dio App licat ion Dev eloper WebSph er e Ap plicat Serv er Version 5 ion pr ovid Deploy EJBs t o ion W eb Sp her e Applicat Seres v eran int er nal JMS m essag in g ser ver as p ar t of t he base WebSph er e pr od uct t o m eet t he secon d r equir em en t . The in t er nal JMS ser ver is essent iallyRedbook a ligh t wpr eig ht im plem en t atinion of at Web e MQ t hat for WebSp t h e exchan of This I BM ovides d et ailed for m ion Spher on h ow t o ef fectallows ively use here ge St udio m essages w een ap plicat iondsev welopm it hin t en het sam e plicat WebSph e inst an hou it isJav p ossible App licat ionbet Developer f or t he of ap ion serbased once. t h eAltEnt ergh pr ise aBeanst o r( em ot ely d umessag intloym o t heent WebSph er ap e em bedded he qu euServ e m anager EJB) ar chsen it ect re, an desd ep of su ch plicat ion s tm o essaging a WebSphserv er eice, Apptlicat ion er. of t he ser vice cannot com mu nicat e ( sen d) messag es out side of WebSp here ( or, bet w een queue Thr m anager oug hout s w it t he hinbook WebSph , w e er pre) ov. idTher e exam efor ples e, t he based serv er onitaself simp h as le lim bank it ed ingabilit appylicat for ion ex tw ern it halan un egr int derly ating ion.r elat Luckional ily , WebSp dat ab ase. here also allow s f or WebSp here MQ ( or ot her gener ic pr ov id er s) t o be used in st ead of t he in t ern al JMS ser ver as t he JMS pr ov ider w hen a high er qualit y of serv ice is I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic exp ect ed. concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. The in t0ersp n al ecif JMS icat pr ionovider t hat pr is ov n ot id e, in tfor en ded ex am t ople, be aenfuhanced ll- blow n fun MOM ct ionor alitayvfor iable cont opt ain ion er f- or m anag ed per er ent sist prence ise ap ent plicat it y bion eanint s egr andatmion essage( EAI )dr . Iiv t en simbeans. ply p r ovides We also t h eprJMS ovid ser e best ver as pr act requ iceirguidelines ed by t h e f or successful specif icat ion imp , and lem allow ent at ions s for of som EJBs. e cr eat iv e app licat ion- lev el com m un icat ion solut ion alt er nat iv es. I t is ex pect ed t h at m ost ap plicat ion s w ill requ ire act u al WebSph er e MQ f or t r ue ap plicat ion Iint n er Part 2, we operab ilitin y .t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Message-driven bean introduction
Messag e- d riv en b ean s ar e used f or t h e pr ocessing of asy nchr onou s JMS messages w it hin J2EEbased applicat ions. The ar e inv ok ed b y t he cont ain er on t h e ar riv al of a m essage. I n t his w ay , t hey can be t h ough t of as an ot her int er act ion m echanism for inv oking EJBs, but unlik e session and ent it y b ean s, t he cont ain er is responsible f or inv okin g t hem w hen a m essage is r eceiv ed , not a client ( or anot h er b ean ) . Alt hou gh MDBs ar e som et imes said t o not hav e a t r ue client , t h e client act ually is t he orig in al pr odu cer of t h e m essage. We u se t his idea of MDB clien t t h rou ghou t t his ch apt er. The m ost im p ort an t p rop er t ies of m essage- dr iv en beans are:
They hav e n o ident it y t o t h e client , but ex ecut e anon ym ou sly . They ar e com plet ely m anag ed by t he EJB cont ainer . They ar e not exp osed dir ect ly t o t he EJB clien t . They ar e st at eless, in t hat t hey do not main t ain an y st at e on behalf of a clien t . •
Table of Content s
They m Iust ndexim p lem ent javax.jms.MessageListener in add it ion t o javax.ejb.MessageDrivenBean. EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper •
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
They hav e n o hom e or com pon en t in t er f aces.
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Java messaging service concepts Jav a Messaging Ser v ice ( JMS) is a Jav a API f or accessing m essage- orient ed m iddlew are syst em s. JMS is t o m essaging w hat JDBC is t o dat ab ase access, in t hat it ab st ract s aw ay t h e v en dor - specif ic asp ect s of t h e m essaging pr ov ider , so t hat t hey can be accessed in a v endor •neut r al way . Table of Content s •
I ndex
EJB 2 . 0 really D e ve lopm w itEJBs h W ebSphe e St in udio v eliv oper MDBs ar ee nt ju st t hat rare vokAppl ed ica b ytiton heDearr al of JMS messag es. Because JMS is t he back bone of MDBs, it is im por t an t t o und er st and t h e fun dam ent al concept s of JMS. JMS is a By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss pow er ful m essaging st r uct ur e t hat can be used by any t yp e of EJB, or any ot h er Java class or app licat ion, not just MDBs. Pub lish er: I BM
Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
JMSPages: messaging models 75 0 Ther e ar e t w o t y pes of JMS m essaging m odels: poin t - t o- point and p ublish- and - su bscr ibe.
Point-to-point (P2P) Lear n about t he EJB 2 .0 specif icat ion I n point - t o- p oint , m essag es ar e sent via queues. Messag es are p ut ont o t he q ueues by t h e Dev elop EJBs w( it h St u dio licat Dev eloper m essage p rod ucers t he clien t s)App . The mion essage con su m er is r esponsible f or pu lling t h e m essage fr om t he qu eu e. Point - t o- p oin t is t yp ically used w h en a given m essage m ust be p rocessed Deploy EJBs oW her econsum Applicaterion v erw ay , t h er e is on ly one consum er of t he given ( r eceived) only on tce byebaSp g iven . I nSer t his m essage. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Publish-and-subscribe (pub/sub)
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an ing an r elat ional dat Iun n derly pub lishd- subscr ib e,abmase. essages ar e sent t h rou gh t opics. Messages ar e pu blished t o t opics by t he message pr odu cer s. The m essag es m ay b e r eceived by an y con su m er s t h at sub scr ibe t o t he I n Part 1,ic. weI nintthr is oduce as a p art of b Jav 2 Ened, t er porrise Ed it ion (, J2 and t he basic giv en t op w ay,EJBs a m essage m ay e raeceiv pr ocessed byEE) m ult ip lecover con su mer s. concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or Message-driven and messaging models successful imp lem ent atbeans ions of EJBs. IMessag n Part e2,dwe riv en in t b r oduce ean s can t h e be sam eitpher le bank qu eu ing e or applicat t opic m ion essag and et hen consum imp er lem s. ent Theen cht it oice y beans, of session beans, m essaging an d m model essagr eally e- d r ivdepends en beansonusing w hatWebSph is supp osed er e Stt uo dio happ Apen plicat t o tion he m Dev essage elop erit. self, We also su ch as imhet w plem herent it is finint der ended m et htods, o b e dif consum fer en ted m app by m inore g st tr han at egies, one consum and simer p leorclient n ot .sAdd t h atit ion u seally t he , qEJBs. ueuesAt t hee en ar t y pically d, w e dassociat escrib e ed how w itt h o deploy a singleEJB p rod apucer plicat . The ion sbehav t o a WebSph ior of any er emod Appellicat canion beServ sim ulat er. ed by t he ot her by ad opt ing cr eat iv e ( an d som et im es abu siv e) p at t ern s. I n our opinion, P2P is t he m ost com mon f or int er - applicat ion com m un icat ion, w here t he pr odu cer s and consum er s ar e k now n en t it ies. I n t his b ook, we use point - t o- point for all t he m essag in g ex am ples.
JMS components App licat ions in t er act wit h t h e m essaging ser v ice t hr ough t h e JMS API . Th e app licat ion ser v er consist s of t he f ollow ing h ig h- level com ponent s pr ov id ing t his serv ice ( Figur e 6- 1 ) .
Figu r e 6 - 1 . I n t e r na l JM S pr ovi der im p le me n t at ion
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0 JMS provider
A JMS pr ov ider is a base m essaging syst em and r elat ed Jav a classes t h at im plem en t t h e JMS API . The r esour ces of t h e JMS pr ovid er ar e accessed t hr ough JMS con nect ion f act ory and dest inat ion obj ect s. The pr ovider m ay eit her b e t he int er nal JMS p rov ider ( as depict ed abov e) pr ov ided w it h WebSph er e, or m ay b e an ex t er nal p rov ider such as WebSph er e MQ. Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
JMS server
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er The JMS f unct ions of t he JMS pr ov id er are accessed by t h e JMS ser ver w it hin t he applicat ion This I BMThis Redbook prver ovides d et ailed in ffor ion on ht how t o ef fect ser v er. JMS ser is r esponsible or m mat anaging e follow ingively ser v use ices:WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Que ue m a na ge r— Respon sib le for p rov iding t h e qu eu e ser v ice w hen using point - t o- point Thr oug hout messag t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an based ing. un derly ing r elat ional dat ab ase. Brok e r— Respon sib le for p rov iding t h e pu blish / su bscr ibe ser vice w hen using pub / sub I n Part 1, wemessag in t r oduce based ing. EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or JMS administered objects successful imp lem ent at ions of EJBs. I n JMS b er of ob ject ar e pcreat ed by e Web Spher e adm or ent andenstt or in a session Part a2, num we in t r oduce t h es sam le bank ingt happlicat ion and t heninist impr at lem it yedbeans, dir ect oran y . dThe Java eNam g and Dirusing ect or yWebSph I nt er face ) nam espace u sed efer en ces beans, m essag d r ivinen beans er e( JNDI St u dio Ap plicat ion is Dev elopt oerh. old We r also tim o plem JMS ad er m edetob ject s,difen in ginset necessary forpconn ect ing o and t he At entmin finist der h ods, fercap en su t mlat app g stt ing r at segies, and sim le client s t htat u se tusing he EJBs. qu esd,and/ t opics of messaging Theion JMS inist er ed object s ar ion e connect ion t heeuen w e or d escrib e how t o deploy syst EJBem ap s. plicat s t oadm a WebSph er e App licat Serv er. fact or ies and dest inat ions.
JMS connection factories A conn ect ion fact or y is u sed t o cr eat e connect ions w it h t he JMS pr ov ider for a specif ic JMS queue or t op ic dest in at ion. Each connect ion fact or y encapsulat es t he conf igur at ion par am et er s n eeded t o cr eat e a con nect ion t o a JMS dest inat ion. St or ing t h e connect ion d et ails in JNDI m ak es t he app licat ion connect ion code vend or independ en t . Ther e ar e t w o t y pes of JMS connect ion f act or y:
Que ue Conne ct ionFa ct or y— Encapsulat es t he set t ing s necessar y t o conn ect t o a queuebased messag ing sy st em. Topi cConne ct ion Fa ct ory — Encapsulat es t he set t ing s necessar y t o conn ect t o a t opicbased messag ing sy st em.
JMS destinations • Table of Content s •
I ndex
A JMS ides a specif ic endp f orDemv el essages. Ther e ar e t w o t y pes of JMS EJB 2 . 0 dest D e ve inat lopmion e nt pr w itov h W ebSphe r e St udio Apploin ica tit on oper dest inat ion t y pes: By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss PubQue lish er: ueI BM s—
A JMS dest in at ion u sed for P2P.
Pub Dat e: April 1 1, 20 03
Topi dest in at ion u sed for pub / su b. I SBN:cs— 0- 73 A 84-JMS 26 09 -1 Pages: 75 0
Example JMS interaction To sh ow how t hese JMS ob ject s ar e used b y a r egular Jav a class, w e int r odu ce a simp le point - t opoint exam p le, w h er e a sim ple t ex t m essag e is sen t t o a q ueue by a JMS m essage pr od ucer or he by EJBa 2JMS .0 specif icat ion client Lear and nisabout r eceivt ed message list en er . Dev elop EJBs w it h St u dio App licat ion Dev eloper
JMS Deploy message producer EJBs t o W eb Sp her e Applicat ion Ser v er Figur 6- 2Redbook sh ow samp le Java a ion m essage t o t ao JMS eu e.use WebSp here St udio This IeBM pr ovides d etcode ailedt oinsend for m at on h ow ef fectqu ively App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (Figu EJB) rarech6it-ect re,m anple d d ep loym ente of su che ap ion s t o a WebSph er e App licat ion Serv er. 2 .u Si JM Sm ssag prplicat oducer Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. ... I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he theicat JNDI to ple, do en JNDI lookups EJB //Get 2. 0 sp ecif ion tInitial hat pr ov idContext e, for ex am hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or InitialContext initCtx = new InitialContext(); successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also //Get QueueConnectionFactory nameand sim p le client s t h at u se t he EJBs. At im plem ent the fin der m et h ods, dif fer en t m app inby g stJNDI r at egies, t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. QueueConnectionFactory qcf = (QueueConnectionFactory) initCtx.lookup("java:comp/env/jms/ItsoMDBConnectionFactoryRef");
//Create a connection QueueConnection conn = qcf.createQueueConnection();
//create a JMS session QueueSession session = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
• •
Table of Content s
//get the queue used to send the message by JNDI name I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Queue queue = (Queue) lookup("java:comp/env/jms/ITSOMdbQueueRef");
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM
//send a message Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
QueueSender sender = session.createSender(queue); Pages: 75 0 TextMessage msg = new TextMessage("Test Message"); sender.send(msg); Lear n about t he EJB 2 .0 specif icat ion sender.close(); Dev elop EJBs w it h St u dio App licat ion Dev eloper session.close(); Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Thisconn.close(); I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. ... Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep The codt sinand g sequ t h eenarcechisit ect f airurlye.stIrnaigh part tfor icuwlar ar d. w eTh point e conout nectt he ionnfew act ory fu nct isions lookint edr od u p,uced a connect w it h t he ion EJB is r et2.riev 0 sp edecif , and icatition is tuhat sedprt oovobt id e, ainfora ex session. am ple,Th enehanced sessionfun is uctsed ion alit t o ycr for eat econt a sender ain er - m obanag ject ed on a pereu qu sist e,ence and ent t hen it yt he b ean m essage s and misessagesent . dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. Sever al ot h er JMS obj ect s are in t r oduced in t h is ex am ple t h at ar e descr ibed as f ollow s: I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Que Conne o tcreat e ain g livst e rconn ect ion t o sim t hepJMS m essaging sertvhe ice, an d At im plem entuefin der mct etion— h ods, Used dif fer ten m app at egies, and le client s t h at u se EJBs. used t o cr eat e QueueSessions. I splicat an alog he conn er ecteion ection in Serv JD BC. t he enard,e w e d escrib e how t o deploy EJB ap ionous s t ot oa tWebSph Appobj licat er.I n a pu b/ sub m odel, a TopicConnection w ou ld b e used. Que ue Se ssi on— Used t o creat e sender ob ject s and m essages. Can only b e used b y one t hr ead at a t im e. I n a pub/ su b m od el, a TopicSession w ou ld b e used. Que ue Se nd er — Used t o sen d t he m essage on t h e queue. I n pu b/ sub, t his w ould be a TopicPublisher. Te xt M essag e—A sim ple t ex t m essage t yp e. Ot her t y pes ar e ObjectMessage, BytesMessage, StreamMessage, an d MapMessage. Each m essage t yp e ex t end s t he Message super class.
I n t his exam p le, a sim ple t ex t messag e is sen t on a queue. A good pr act ice t o emp loy is t o close t hese ob ject s w hen you ar e don e using t hem , as y ou w ould for JD BC t y pe object s.
JMS message listener The ex am ple in Figur e 6- 2 sh ows a JMS client . A simp le JMS q ueue m essage con su m er ex am ple •is show n in Figur Tableeof6-Content s r odu ce a f ew addit ional imp ort ant JMS ob ject s. 3 t o int •
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Figu r e 6 - 3 . Si m ple JM S m e ssag e consum e r By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM
import javax.jms.*; Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
public class AListener implements MessageListener { Pages: 75 0
public void onMessage(Message aMsg) { try { Lear n about t he EJB 2 .0 specif icat ion TextMessage msg = (TextMessage) aMsg; Dev elop EJBs w it h St u dio App licat ion Dev eloper System.out.println("Got Message: " + msg.getText()); Deploy EJBs t o W eb Sp her e Applicat ion Ser v er } Redbook catch (Exception { in for m at ion on h ow t o ef fect ively use WebSp here St udio This I BM pr ovides d ete) ailed App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ecte.printStackTrace(); u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. } Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. } I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic // tas and method that sets listener.. concep t h e ar ch it ect ur e.up I n the par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed public void per sist ence ent it y runIt() b ean s and{ m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. // Lookup connection factories, queue, session, same as in producer I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, ... an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d,QueueReceiver w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. rec = session.createReceiver(queue); rec.setMessageListener(this); rec.close(); ... } ...
}
All st and ard m essage list ener s ( our MDBs included) m ust im plem en t t h e javax.jms.MessageListener in t er f ace, so t hat t he ob ject can be r eg ist er ed as a JMS list ener . I n t his exam p le, w e r egist er our selv es as a list en er . For an MD B, t he con t ainer w ould regist er t he bean as a list en er . Wh en a JMS m essag e is r eceived, t he cont ainer ( ou r selves in t his • Table of Content s exam ple) t rig gers t he onMessage m et hod , w hich is requ ired by t h e MessageListener in t er f ace. •
I ndex
EJB 2 . 0alD eot vehlopm e nt w it hect W ebSphe r e t St udio Appl Deam v el oper Sever er JMS obj s are in r oduced inicat hti on is ex ple : : By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Que ue Re cei ve r— Th e obj ect used t o get m essag es fr om a queue. I n pub / sub, t his w ould be a TopicSubscriber.
Pub lish er: I BM
Pub Dat e: April 1 1, 20 03 I SBN: 73Li 84-st 26en 09 -1 Me ssa0-ge er — I n t er f ace t hat declar es t he onMessage m et hod . Pages: 75 0
The ex am ple in Figur e 6- 3 sh ows t he r equir ement s for h avin g a dist inct MessageListener obj ect . There is anot her w ay t o r eceive a m essage f r om a q ueue, r at her t han r egist er ing as a MessageListener. Receiv ing obj ect s may be don e by j ust cr eat ing a r efer en ce t o a QueueReceiver ob ject d ir ect ly, and blocking un t il a messag e is r eceived ( Figur e 6- 4 ) .
FiguLear r e 6n -about 4 . Qu euEJB e Re ve ricat i nst t he 2 .0cei specif ion e ad of M e ssa geList en e r Dev elop EJBs w it h St u dio App licat ion Dev eloper ... Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio QueueReceiver rec = session.createReceiver(queue, selector); App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. TextMessage inMessage = rec.receive(15000); Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an rec.close(); un derly ing r elat ional dat ab ase. ... I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or I n t his exam p le, t h e receive(timeout) m et hod is called . I t list en s on t he q ueue f or t he successful imp lem ent at ions of EJBs. m essage u nt il t h e t im eou t h as elap sed. The select or is used t o q ualify w hich m essage is t o be eceived. select tor h e next m essage r eceiv ed t het hen q ueue t h eent oneenpt rocessed. Ir n Part 2, Wit we hinno t r oduce h e, tsam p le bank ing applicat ioninand impislem it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At tMessage he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. What w e sen d as m essages is r eally t h e hear t of t h e m essaging syst em . We h ope t hat t h e m essage h as some meanin gfu l dat a w it h wh ich t h e receiv ing m essage can per for m it s w or k . The m essage is r eally t he cont r act bet w een t he m essag e pr oducer and t h e m essage consum er , and is gener ally agr eed upon in adv ance, in t he sam e w ay t hat m et hod par am et er s ar e t he con t ract bet w een collabor at ing classes. Th er efor e, m essaging - based sy st em s ar e n ot com plet ely decoupled pr ocesses, in t hat t hey m ust st ill hav e a f und ament al und er st and ing, an d agr eem ent of, w hat is t o b e sent . A m essag e is gener ally d ef ined in t er m s of t hr ee t hing s: header , pr oper t ies, and pay load :
The h ead er of a m essage defines t h e met ad at a d escrib in g t he m essage, such as t h e rou t ing inf orm at ion and cor relat ionI Ds of t h e m essage. The p r opert ies def in e some addit ional dat a about t he m essag e t hat m ay be m anip ulat ed. The p ayload , or m essage body , cont ains t he act ual business- sp ecific d at a t h at t h e m essage consum er is int er est ed in . •
Table of Content s
I n JMS, m essages ar e def in ed accord ing t o t h e t y pe of pay load t hat t hey ar e able t o t r anspor t . • I ndex Ther e ar e six messag e pay load t yp es in JMS: EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Me ssa ge — Th is is a m essag e t hat has n o pay load, just a header ( an d ot her pr oper t ies) . Pub lish er: I BM
Te xt M essag e— Th is is usually a Java st rin g or ot her simp le t ex t t y pe, such as XML. Th is t y pe of m essage pr ovid es a looser coup lin g bet w een t he t echnolog ies of t h e m essage 0- 73 84- 26 09 -1 prI SBN: odu cer an d con su mer . The con su mer is r esponsible f or const ru ct in g som et hing Pages: 75 0 ul f r om t he t ex t of t he m essage. m ean ingf
Pub Dat e: April 1 1, 20 03
Obj e ct M essag e— A ser ialized Jav a ob ject . This is an easy m odel t o u se, b ecause y ou can r ely on y our ex ist in g ob ject m odels, and is of t en used w hen MDBs ar e used f or int r aapp licat ion comm un icat ion . The m essag e pr oducer and consum er ar e now t ight ly coup led t o bot h t he t echn olog y and v er sion of t he m essage, so u se cau t ion w h en choosing obj ect m essages. Lear n about t he EJB 2 .0 specif icat ion Ma e ssa ge— hash ap, ion t h isDev is aeloper set of n ame- v alue pair s. This is easier t han DevpM elop EJBs w it Like h St uadio Appmlicat eit her t ext or ob ject m essages, because built - in set t er s/ get t er s can b e used t o m anipulat e tDeploy he dat a. EJBs t o W eb Sp her e Applicat ion Ser v er This IBy BMt esMe Redbook ssa ge pr ovides — St ream d et ailed of r aw in for d atma at b ion yt es.onThis h owcan t o ef befect u sed ively t o use t ranWebSp sf er m ore herecom St udio plex App licat st r ion uct ur Developer es. f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. St r ea m Me ssa ge — St ream of p rim it ive Java t y pes. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Message-driven bean concepts You should now h ave a good u nder st an ding of t he var ious JMS obj ect s and how t hey int eract for sen ding and r eceivin g m essag es. Th is sect ion pr ovid es som e det ail on t h e st ru ct ur e of m essagedr iv en beans. •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Extending the JMS model to MDBs
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
As m ent ioned abov e, message- dr iven beans sit on t h e back bone of JMS. The ex amp le in Pub lish I BM int er act ion " on p age 22 7 sh ow s a JMS client ( m essage p rod ucer) int er act in g w it h " Ex am pleer:JMS Pub Dat e: April 1e 1, ser 20 03 t he JMS m essag v ice. Each of t he JMS ob ject s m ust be in t eract ed w it h in or der for t he I SBN: 073 8426 -1 m anaged by t h e client . m essage t o be sent ,09and Pages: 75 0
MDBs on t he ot her h and ar e not nearly as diff icu lt t o build as a st an dar d JMS clien t ab ove. MD Bs m ust im plem en t t h e MessageListener in t er f ace, an d t he cor respond ing onMessage m et hod , bu t t he con t ainer act ually per for m s t he ot her int er act ion s abov e for t he JMS list ener f or MD Bs on behalf of t h e bean. Ther ef or e, MDBs n orm ally do not hav e t o in t er act w it h QueueConnectionFactory, Lear n about t he EJB 2an .0dspecif icat ion QueueConnection, Queue, QueueReceiver ob ject s dir ect ly . An except ion t o t his is w hen t he MDB is also a m essag e pr odu cer , as in t he case of pseudo- sy nchr onou s m essaging w her e t he Dev elop EJBs w it h St u dio App licat ion Dev eloper MDB sen ds back a r ep ly . Th is scenar io, am ong ot her s, is descr ibed lat er in t h is ch apt er . The MDB w ouldDeploy hav e t o havteo kn ledg e eofApplicat t he JMS m Ser essage EJBs W ow eb Sp her ion v er t yp e, how ever , in ord er t o p rop er ly han dle t he message. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Alt ghion forDeveloper a sim p le MDB t hed ev client is en nott of pr ogr am mion at ically m anag t hese s, aBeans t hey ar e Apphou licat f or t he elopm ap plicat s based on ting h e Ent er probject ise Jav ill being albeit t he conent t ainof er su . The associat ions of b ean wer it h pr oper t ies d one (stEJB) ar ch used, it ect u re, an dbdy ep loym ch ap plicat ion t o taheWebSph e App licat ionisServ er.b y a com binat ion of deploy ment descr ipt or en t r ies an d ot her WebSpher e adm inist r at ion deploym ent tThr askoug s t hat ially , bind bean w it hples t hebased appr opr QueueConnectionFactory Queue. houtessent t he book w e prt he ov id e exam oniat a esimp le bank ing app licat ion wan it hdan un derly ing r elat ional dat ab ase. Lik e t he ex am ple abov e, w hen a m essage is receiv ed, t h e cont ainer k now s h ow t o in vok e t h e app rop riat e MDB in st an ce, anas d ita calls t heJav onMessage et hod I t iscover possib le basic t hat I n Part 1, we in t r oduce EJBs p art of a 2 En t er pmrise Ed itof iont h(eJ2bean. EE) and t he m ult iple inst an ces of MD Bs are ex ecu t ing t he onMessage at any given t im e, but for queueconcep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it hbased t he pr ocessing ( point t o- point ) , ov w eid ar guar eed ten hat a m essag is only EJB 2. 0 sp ecif icat -ion t hat pr e, efor ex ant am ple, hanced fun cteion alit y pr forocessed cont ainbery- a m sing anagleed bean. per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. IMessage-driven n Part 2, we in t r oducebeans t h e samstructure p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At Recall t hat m essage- dr iven beans im plem en t t h e javax.ejb.MessageDrivenBean in t er f ace. t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Messag e- d riv en b ean s m ust also im plem en t t h e javax.jms.MessageListener in t er f ace. Th is hier archy is sh ow n in Figur e 6- 5 .
Figu r e 6 - 5 . M essa ge -d r iv en be an i nt er f a ces
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss lish er: I BM NoPub home or component interfaces Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26e 09hom -1 MDBs do n ot r equir e or com p onent in t er f aces since t her e is no dir ect client int er act ion w it h 75 0t h at h ome met hod s ar e used t o locat e t he b ean s, and t he only ent it y t h at does t h is MDBs.Pages: Recall is t he con t ainer, and t he cont ain er alr eady h as suff icient k now led ge t o locat e t he MD B. Recall t hat com ponent int er faces are in t en ded t o ex pose business logic t o client s. Alt hou gh t he MDB m ight im plem ent business k now led ge w hen t he onMessage m et hod is called , t he bean it self d oes not exp ose t his in any w ay t o ex t er nal clien t s.
Lear n about t he EJB 2 .0 specif icat ion
Callback methods Dev elop EJBs w it h St u dio App licat ion
Dev eloper
Giv enDeploy t h at t hEJBs er e art oe W n oebhom e ore Applicat com pon ion en t Ser int er f aces, t he callback and cont ain er - m anag ed Sp her v er m ech anism of t he b ean s is gr eat ly sim plif ied. Figur e 6- 6 sh ows t he callback m et h ods t h at m essagedr iv en bean classes ust im because plem enuse t t h eWebSp here St udio This I BM Redbook pr ovides d etmailed inplement for m at ion on h owt hey t o efim fect ively javax.ejb.MessageDrivenBean t er f ace: App licat ion Developer f or t he d ev in elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Figu r e 6 - 6 . M essa ge -d r iv en be an cal lba ck me t h ods
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic public MessageDrivenBean extends { concep t sinterface and t h e ar ch it ect ur e. I n par t icu lar w e pointjavax.ejb.EnterpriseBean out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed public void ctx); per sist ence ent it y setMessageDrivenContext(MessageDrivenContext b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. public void ejbRemove(); I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session } beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Alt hou gh not t ru ly cont ainer callb ack met h ods, t he m essag e bean mu st also im plement t he ejbCreate m et hod . This is so t he cont ain er can act ually creat e t he bean inst ance. App licat ion Developer also gener at es a getMessageDrivenContext m et hod . Nor mally t h e ejbCreate w ou ld b e r eq uired t hr oug h an associat ion w it h a h ome in t erf ace, b ut in t h e case of MDBs, t he obj ect creat ion is imp lied by t h e cont ainer , so n o hom e is r equir ed. Figur e 6- 7 sh ows t he ad dit ional m et hod s of a m essage- dr iv en b ean .
Figu r e 6 - 7 . Add it i ona l m essa ge -d r iv en be an m e t h ods
... public MessageDrivenContext getMessageDrivenContext()... public void ejbCreate()... •
Table of Content s
} •
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Bean-container contract Pub lish er: I BM Pub Dat e: April 1 1, 20 03
To u nder st and w hat I SBN: 0- 73 8426 09you -1 as a bean developer w ould hav e t o do t o dev elop a meaning fu l m essagedr iv en bean m ean s un derst and ing t he callback and life- cycle m et hods of t he bean, and Pages: 75 0 how t his bean- con t ainer cont r act is enf orced.
States of a MDB Ther eLear ar e nt wabout o st attes an2MDB ( Figur 6- 8 ) . I t eit h er does not ex ist ( n o st at e) , or it is in t he he of EJB .0 specif icateion m et hod- r eady p ool st at e. The m et hod - ready pool of m essage- dr iv en b ean s is sim ilar t o t hat of eloppool EJBsofwst it at h eless St u diosession App licat ion Dev eloper t he inDev st ance beans. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Figu r e 6 - 8 . St at e s of a m e ssag e- dr iv e n bea n
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. Pooling is a of t he cont t hat het er cont ainer r eu( J2 se EE) bean in st an ces, I n Part 1, weserv in t ice r oduce EJBs asain a er p art of allow Jav a s2 tEn p rise Ed titoion and cover t heasbasic opp osed t o cr eat ing new ones ev er y t im e a requ est for a bean is m ade. This is a perf or w man concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced it h ce t he opt im izat ion t hat is done by t h e con t ainer. This allow s for a scalab le en vir onm ent , b ecause h is EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ted pool size can be in cr eased as needed, b y eit her addin g m or e m emor y , or m or e m achines, based per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or on t he m essag e load t hat m igh t b e exp ect ed. successful imp lem ent at ions of EJBs. Essen , if in t hter oduce bean in n ot ing b eenapplicat cr eat ed, en itt hen is inimp no lem st atent e. Once cont ain er I n Partt ially 2, we t hste ance sam phas le bank iont hand en t it yt he beans, session decides it n eeds an MD B, it m ov es t o t h e m et hod ready pooled st at e. Th e bean w ill rem ain in beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also t h is m hodent ready st atm e et unt il t hedif cont aint er decides noegies, lon ger needs imet plem fin der h ods, fer en m app in g stitr at and sim pitle. client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. These st at e t r ansit ions ar e im por t ant , becau se each st at e chang e w ill t r ig ger t he con t ainer t o inv ok e a callback m et h od ap pr opr iat e t o t hat st at e chang e. As a dev elop er you hav e t o un derst an d t h ese t r an sit ions t o k now if y ou should apply ap pr opr iat e logic in t hese callb ack m et hods f or y our b ean . The f ollow in g ar e t he pu rp oses of each callback m et hod: set M e ssa geD r iv en Con t ex t — Called by t he cont ainer w hen t he bean is f irst added t o t he pool, it should be u sed t o cap t ur e a r efer en ce t o t h e MessageDrivenContext f or t h e bean. Used m ost oft en for one- t im e init ializat ion w it h t he b en ef it of MessageDrivenContext. Figur e 6- 9 sh ows t he m ost com m on f or m of t his m et hod .
Figu r e 6 - 9 . M essa ge -d r iv en be an se t M e ssag eD r iv en Con t ex t m et hod
public void setMessageDrivenContext(javax.ejb.MessageDrivenContext ctx){ myMessageDrivenCtx = ctx;
• •
Table of Content s
}
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
j bR Pubelish er:em I BMov e—Called
w h en t h e con t ainer m oves t h e bean fr om t h e m et h od ready p ool back t o t he n o st at e. Any cleanup oper at ions could be p er f orm ed in t h is m et h od. Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Additional life cycle of a message-driven bean Since t her e is no dir ect client int er act ion w it h an MDB, as t h er e is in t he case of t h e ot her ent er pr ise b ean s, t he life cy cle of t he b ean is not dict at ed by t h e find ing or locat in g requ ir ed by a client . I t s life cy cle is d ict at ed comp let ely by t he int er act ion w it h t he con t ainer. There are, how evLear er , nsom e imtpor ant 2ot.0her act ivit about he tEJB specif icaties iont h at occur besides t he calling of t he callback m et hods descr ibed abov e, w hen t ran sit ion in g b et ween st at es ( Figur e 6- 1 0) . Dev elop EJBs w it h St u dio App licat ion Dev eloper
Figu r e 6 - 1 0 . Li f e cycle of a m e ssag e- dr i ve n bea n
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Let us describ e t he life- cy cle sequence f or m essage- dr iv en beans: 1 . Cla ss. ne w I nst an ce — Th e cont ainer d ecides t o mov e som e beans t o t he pool, bu t m ust fir st cr eat e an in st ance of t he class. Web Spher e set s t he m inim um pool coun t f or MDBs t o be 1 ; t h er efor e, t h is act ion w ill occu r as t h e ser v er st ar t s u p, an d as new beans are r eq uir ed t o pr ocess t h e incomin g m essag es. 2 . set M e ssa geD r iv en Con t ex t — Th e MessageDrivenContext is now av ailable. 3 . e j bCre a t e—Alt hou gh t her e is no hom e int er face, an ejbCreate is st ill called t h at w ill allow t he bean t o per for m a one- t im e init ializat ion . The bean w ill h ave access t o t he
MessageDrivenContext if r equir ed. 4 . onM essag e— W hen a m essage is r eceiv ed by t he cont ain er , it deleg at es t he pr ocessing t o an inst ance in t he m et hod r ead y pool and ex ecu t es t he onMessage m et hod . An MDB inst ance can only execut e ag ainst on e message at a t im e. Alt houg h t his bean is st ill t echnically in t h e pool, a bean t hat is in t h e m id dle of execut ing it s onMessage m et hod is un available t o pr ocess ot h er messages unt il it is com plet e. •
Table of Content s 5 . e xe cut e / del e ga t e busin ess l ogic— Th e m essage- dr iv en bean m ust p er f orm som e I ndex bu sin ess logic. Ty pically , it is not recom mend ed t h at t h e bu siness logic it self b e EJB 2 . 0im D plem e ve lopm nt win it ht he W ebSphe r e St udio Appl ica ti el oper or any ot h er m et hod of t h e bean. A enteed onMessage m et hod ofont hDe e vbean, By Ueli best Wah li,pWout racter iceDenay is t hat er, Lar t he s Schu MDB nk , Deborah behav eShaddon just as, Mart a pinassWeiss t h r ough t o anot her st at eless session bean t hat act ually imp lem ent s t he bu sin ess logic. •
Pub lish er: I BM
6 . e j bR em ov e— W hen t he cont ainer d ecides t hat it n o long er requ ires t h e bean, it w ill Pub Dat e: April 1 1, 20 03 execut e t h e ejbRemove m et hod an d m ov e it t o a n o st at e. I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Developing a message-driven bean Tools such as Applicat ion Dev eloper pr ov ide pow er ful t ooling t o assist w it h t he dev elop ment of MDBs. D ev eloping w it hin Applicat ion D ev elop er w ill be discussed in gr eat det ail in Ch apt er 16 , " Messag e- d r iv en bean d ev elopm en t " on pag e 56 3. •
Table of Content s
•This sect ion w I ndex ill br ing t og et h er all t he concept s d iscussed in t his chap t er, and sh ow how t he EJB 2 .0 b De ve lopm e nt wbe it h const W ebSphe St udio ti onw De act ual ean s could r uctr eed. We Appl con ica t inue itvheltoper he applicat ion t h at h as been p resent ed so far , and ex t end t he t ran sf er act ivit y t o be t hat w h ich is inv ok ed asyn ch r onously. By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss Pub lish er: I BM
Building message-driven beans Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
When Pages: w e bu75ild 0 a messag e- dr iv en bean, w e ar e concern ed w it h buildin g on ly t he b ean class and t he deploym ent descr ipt or . Be an cla ss— Th e bean class m ust con t ain t he callb ack met h ods as w ell as an im plem ent at ion of t h e onMessage m et hod . Lear n about 2 .0 specif ion deploy m en t d escrip t or for t he b ean . D e ploy m en tt he deEJB scr ip t or— Th icat e XML elop w it h Stlogic? u dio App Dev What Dev abou t t hEJBs e bu siness As dlicat escrion ib ed abeloper ove, b ur dening t he b ean class w it h t he bu sin ess logic is n ot r ecom m ended. A bet t er pr act ice is t o d elegat e t his t o anot her class, pr ef er ab ly a Deploy EJBsbean. t o W eb herwe ay Applicat Ser v er st at eless session I nSp t his , t he stion at eless session b ean w ill hav e t he responsibilit y of pr ov iding t he act u al im plem ent at ion of t h e business logic, and t he MDB's responsibilit y w ill be t o This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio simp ly act as a deleg at or for t his bu sin ess logic. App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (Ther EJB)efar u re, an d irdeept he loym ent ing of sut ochbeapbu plicat ion t o taofWebSph er elet App licat ion Serv er. orch e, itwect ew ill requ follow ilt as a spar t h e comp e MDB b ean dev elop ment cycle: Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. St a t e le ss se ssi on busin ess l og ic b ea n— Th e bean t hat t he MDB d elegat es t o t hat I n Part weent in tsr oduce as a p artlogic. of JavThis a 2 is Enm t erorperise ion ( J2 EE) cover im1, plem t he actEJBs ual business of aEdgituideline t hanand a ru le. t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or Expanded banking successful imp online lem ent at ions of EJBs.requirements IWe n Part we t in r oduce h e ban samkin p leg bank ing leman entexam en t itple y beans, session cont2, in ue o tex pan d t he ex am pleapplicat used t hion us fand ar t ot hen in t r imp oduce of MDBs. beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Recall in our ban g exdif am built strat p rise sbean Banking im plemt hat ent fin der m et kin h ods, ferple, en t wmeapp in ga st ateless egies,session and simenp tleer client t h at called u se t he EJBs. At t hat he en per d,for w emds escrib t he cor e ehow b usin t o ess deploy f un ct EJB ion ap s of plicat our ion on lin s t eo bank a WebSph ing syst er eem App , such licat ion as deposit Serv er., w it hdr aw al, an d t r an sf er , am on g ot hers. Th is Banking b ean p rov ides a facade lay er of our syst em , and hid es t he det ails of t he en t it y bean int eract ions fr om t h e EJB client s ( Figur e 6- 1 1) .
Figu r e 6 - 1 1 . Ba nk i ng ap pli ca t ion e xa m ple
•
Table of Content s
•
I ndex
The2transfer meet t h e Banking ean ica is ticur ent called syn ch ron ously by t h e EJB client , in EJB . 0 D e ve lopm nt hod w it h of W ebSphe r e St udiob Appl on rDe v ellyoper tBy his case a ser vlet , w h ich t r ansfer s fu nds for a cu st omer b et w een accoun t s. For som e r eason , w e Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss decide t hat w e w ant t his f un ct ion t o be pr ov ided asy nchr onou sly ( t his is cont r ived, but st ick w it h us) over JMS as opp osed t o in vok ing t he Banking.transfer m et hod d ir ect ly . Pub lish er: I BM Pub Dat ex e: April 1 1, 20 03 ing bu sin ess logic t o anot h er inv ocat ion m ech anism is a com m on f unct ion Act ually, posing exist of MDBs. I SBN: Th0-is73can 84- 26 of09t en -1 p rov ide t he basis f or ser v ice- or ien t ed ar chit ect u r es, w her e t he session beans Pages: are p75 ur0e r ep r esent at ions of b usiness ser vices, but can b e inv oked by an yt hing t hat can p ut m essages ont o a queue ( or t opic) , such as non- Java based sy st em s.
We ex pand t h e online ban kin g ex am ple and int r odu ce a new Transfer MDB ( Figur e 6- 1 2) .
Figu r e 6 - 1 2 . Ba nk i ng ap pli ca t ion e xa m ple w i t h M DB Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. We can 2, seewet hin att rnoduce ow ourt hBanking can inv ok ed nchrt hen onously an dent asyen nchr I n Part e sam p leEJB bank ingbe applicat ionsyand imp lem t it yonously beans, . session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At tDesigning he en d, w e d escrib how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. the emessage payload We int rod uced a new int er act ion p at t er n for our Banking b ean t h rou gh t he use of t he m essag edr iv en bean const r uct , bu t w e ar e not act ually chang ing t he st r uct ur e of t h e transfer m et hod . Ther ef or e, w e hav e t o ensur e t hat t he inf orm at ion r eq uir ed t o ex ecut e t h is t ran sact ion is par t of t he message pay load. W e hav e t o d et er m ine ex act ly w h at our m essage pay load, or st ru ct ur e, has t o look like. At a m inim um , w e hav e t o b e able t o pass t he accoun t s for w hich w e n eed t o per for m t h e t r ansfer , and t h e am ount of t he t r an sf er . We h ave a choice of six d iffer ent messag e pay loads t o choose fr om . I n t his ex amp le, w e ch oose t o use t he ObjectMessage st r uct u re m or e f or sim p licit y t h an an yt h ing else. We d esign a special TransferDataObject t o be used as t h e act ual ob ject pay load for our m essage t hat con t ains
t hr ee f ields: fromAccount, toAccount, an d amount. Perh aps a m ore ex t ensible ser v ice- or ien t ed d esign m ight lead us t o d esign an XML- b ased m essage, an d u se t he TextMessage con st r uct . This w ould r eq uire us t o par se t his m essage on t he con su mer side. Alt er nat iv ely , using t he MapMessage m igh t also h ave been an opt ion, becau se w e could easily m ap t hese t hr ee field s int o nam e/ value pair s of pr imit iv e t y pes. And fin ally , t h e StreamMessage cou ld hav e b een chosen if w e could guar ant ee t h e or der of t he p rim it iv es t hat w ere p assed. I n t h e en d, t h e ObjectMessage w as ch osen for it s sim plicit y only . •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
What about a reply? By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss Asyn ch ron ous m essag in g is generally a fir e- an d- f org et t y pe act iv it y, wh er e n o r eply t o t he Pub lish er: I BM consum er is r eq uir ed or ex pect ed. How ev er, pseu do- syn ch r onous r equest / r eply t y pe seman t ics Pub Dat e: April 1 1, 20 03 ar e comm on and can be incor por at ed in t o a m essaging int er act ion if r eq uir ed , alt houg h t his is SBN: 073 iat 84- e 26choice 09 -1 not an I appr opr f or long- r un ning t r ansact ion cy cles. I n t his w ay , t he messag ePages: 75 0 can p rocess t he m essage, an d t hen lev er age messag ing it self as a JMS m essag e consum er MDB pr odu cer t o sen d b ack a r eply. The or iginal message pr odu cer m ust list en for a m essage r equest t hat is being sen t b ack fr om t h e con su m er , so is now also a JMS m essag e consum er . I t is m ost likely t he case t h at on e queue is used f or t he send, and anot h er queue is u sed for t he r eply . Dif ferent int er act ion scen arios and pat t er ns ar e discussed lat er in t his chapt er , as alt er nat iv es t o t hose m ent ioned so f ar. Lear n about t he EJB 2 .0 specif icat ion I n our case, we wan t t o k now t h e st at us of t h e t r ansfer . We ex t end our scen ario t o includ e t he r ep ly Dev as par t of t he wint act Figur e 61 3) . eloper elop EJBs it herSt u ion dio (App licat ion Dev Deploy EJBs t oFigu W eb Sp v erest / r eher 6 -e1Applicat 3 . M Dion B rSer e qu
r ep ly sem a nt ics
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session Ev en t houg h t h e MDB client and t he Transfer MDB ar e b ot h messag e consum er s, t h eir b eh avior beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also is dif fer en t . The MDB is a polling consum er , t hat is, it is act iv ely, and r ep eat edly , list en in g for im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At m essages t h at it can w or k on. The MDB client in t his exam ple is a block in g con su mer , essent ially t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. block in g for a m essage, t hat is, it is ask ing for a par t icular m essage t o be r eceiv ed on t h e reply qu eu e.
Building the MDB bean class The MDB is p er hap s t he sim plest bean t o act u ally build, p ar t icu lar ly if y ou d elegat e t he pr ocessing t o an ot her bu sin ess logic b ean , as is t h e case wit h our bank ing exam ple. Th e bulk of t he wor k is d eciding w hat behav ior t o imp lem ent in t h e onMessage m et hod , and how t o r ecover fr om er r ors. Because w e also r equir e a r eply in our scenar io, w e ar e put t in g t he r eply logic in t he MDB it self . Ther ef or e, t he MD B is pr im arily concer ned w it h t he ex t r act ion of t h e m essage,
for w ar ding t o t he st at eless session bean, handling t h e r eq uest and r eply, and any er ror han dling. The skelet on of our Transfer MDB is show n in Figur e 6- 1 4.
Figu r e 6 - 1 4 . M e ssa ge- dr i ve n bea n ske le t on cod in g
• Table of Content s import javax.ejb.*; •
I ndex
EJB 2 . 0 D ejavax.jms.*; import ve lopm e nt w it h W ebSphe r e
St udio Appl ica ti on De v el oper
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
public class Transfer implements MessageDrivenBean, MessageListener { Pub lish er: I BM
private MessageDrivenContext msgCtx;
Pub Dat e: April 1 1, 20 03 I SBN: 073 84- 26 09 -1 public void ejbCreate() { Pages: 75 0
//initialize the bean to find the BankingHome, not shown here... } public void setMessageDrivenContext(MessageDrivenContext ctx) { Lear n about t he EJB 2 .0 specif icat ion msgCtx = ctx; Dev elop EJBs w it h St u dio App licat ion Dev eloper } Deploy EJBs t o W eb Sp her e Applicat ion Ser v er public void ejbRemove() { This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App}licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. public void onMessage(javax.jms.Message msg){ Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly trying{ r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic String selector = msg.getJMSCorrelationID(); concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed ObjectMessage objectMsg = (ObjectMessage) msg; per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. TransferDataObject transferMsg = I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session (TransferDataObject) objectMsg.getObject(); beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At String fromAccount = transferMsg.getFromAccount(); t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. String toAccount = transferMsg.getToAccount(); BigDecimal amount = transferMsg.getAmount(); Banking bank = bankingHome.create(); BigDecimal balance = bank.transfer(fromAccount, toAccount, amount); sendResponseMessage(selector, response); } catch (Exception e) {
e.printStackTrace(); sendResponseMessage(selector, "ERROR: " + e.getMessage()); } • •
Table of Content s I ndex
private sendResponseMessage(String selector, String text) {
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
By Ueli Wah li, Wout er Denayconnect er, Lar s Schuto nk , Deborah Shaddon , Mart in Weiss //this will the reply queue, and
//not Pub lish er: I BM shown
send the message.
here in detail - will be described in development chapter
Pub Dat e: April 1 1, 20 03
} }
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
We h ave left out some det ails, such as t h e act ual look up t o t he JNDI for t h e Banking EJB an d t he r ep ly queue. Th ese ar e discussed in det ail in Ch apt er 16 , " Message- dr iven bean developm ent " Lear563 n about t he EJB 2 .0 specif icat ion on page . Dev elop EJBs w it h St u dio App licat ion Dev eloper
Deployment Deploy EJBsdescriptor t o W eb Sp her e Applicat ion Ser v er This ovides d et forip mtat h ow fect ively use WebSp here St udio FigurIeBM 6- 1Redbook 5 sh ows pr t he d ep loy mailed ent d in escr orion enton r ies for ttoheefTransfer MDB. App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Figu r e 6 - 1 5 . D epl oym e nt de scr ipt or f or a m e ssag e- dr iv e n bea n
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase.
I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB Transfer 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or itso.bank5.mdb.TransferMDB successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session Bean|Container beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At
t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. javax.jms.Queue
I f w e w ant t his EJB t o h ave ref er ences t o t h e Banking session EJB ( ) , an d r eply r esour ce dest inat ion ( ) , t h en w e could opt ion ally d ef ine t h ese ent r ies her e as w ell, so t hat w e w ould not hav e t o h ard cod e t hese v alu es. Th is is not sh ow n h er e f or br ev it y.
Transaction type The t ran sact ion t y pe can be set as Bean or Container:
• •
Wit h t he Bean t ran sact ion t y pe, t he MDB it self can st ar t an d com m it ( or roll b ack ) Content s can also not imp lem ent t ran sact ional behav ior and leave t r an sact ion t r ansactTable ions.ofThe b ean I ndex m anagem ent t o t h e session b ean t hat is called.
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Wit h t he Container t ran sact ion t y pe, t he con t ainer m anages t r ansact ions an d b y defau lt st ar t s a t ran sact ion w hen t he onMessage m et hod is inv ok ed .
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish I BM par t icip at es in a t r ansact ion , t he consequ en ce is t hat t he JMS inpu t m essage is When an er: MDB Pub Dat e: 1 1, 20 03 and is comm it t ed ( r em ov ed ) or r ecover ed . I n case of an abend ( f or par t of t he tApril r ansact ion I SBN: 073 8426 09 exam ple, an except ion-1 in t he back end ) t h e inpu t m essage is recov er ed an d placed back on t he qu eu ePages: again. 75 0This can lead t o a loop b ecau se t he sam e message is pr ocessed again.
The list ener por t can be defined w it h a r et r y coun t t h at w ill st op t he sam e m essage fr om being deliver ed ind ef init ely. The MDB it self can quer y t he r et r ies u sin g t he m et hod call:
Lear n about t he EJB 2 .0 specif icat ion message.getStringProperty("JMSXDeliveryCount") Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Using t h ese t echniq ues, an MDB can be p rev en t ed f r om pr ocessing t he sam e m essage f or ev er. See also Be aw ar eprofovides poison mailed essagines" onatpion ageon 2 47. This I BM "Redbook d et for m h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
MDB client programming Ther e is no MDB client pr ogr am min g, per se, as clien t s m ay not int er act dir ect ly w it h t he MDB. How ev er , t he MDB clien t is r eally just a JMS m essage p rod ucer w hose ult im at e dest inat ion is t he MDB. We hav e alr ead y seen an ex am ple of a sim ple JMS m essage p rod ucer ( send er ) in t he JMS •concep t s sectTable ion. of Content s •
I ndex
EJB 2 . 0 D eclient ve lopmtehat nt woperat it h W ebSphe udiodoAppl icach ti on De v el oper An MDB es in rae St pseu syn r onous r equest / r ep ly m ode, how ever , m ust also im plem ent QueueReceiver log ic in or der t o r eceive t he r ep ly, and also som e m essag e select ion By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss t o en su re t hat t he ap pr opr iat e r ep ly is bein g receiv ed. MD Bs ar e desig ned t o execut e in a m u lt it hr eaded man ner ; t h er efor e, m ult iple r eply m essages can be pr odu ced at t he sam e t im e. There Pub lish er: I BM is not h ing t o g uar ant ee t he aff in it y or or der of r eply m essages t o t he MD B client exam p le t hat we Pub Dat e: April 1 1, 20 03 descr ibed abov e. Ther ef ore, it is our r esponsibilit y t o en su re t hat we get t h e pr oper r eply back 0- 73 e 84-t hat 26 09 w -1 e sen t . for t heI SBN: m essag Pages: 75 0
Correlation ID I n our exam p le, w e lev er age t he JMS m essage cor r elat ion I D f or t his r eason. Th e cor r elat ion I D is gu aran t eed not be chan ged by a JMS p rov ider , sin ce a m essage is act u ally sent , and t her ef or e, Lear n about t he EJB 2 .0 specif icat ion w e can use t his f ield t o cor r elat e t w o dist inct , b ut relat ed, JMS m essages t o each ot her . We u se t his t oDev corelop relatEJBs e t h ewrequ est m essage wion it h tDev he reloper eply m essage, such t hat t he MDB client k now s it h St u dio App licat how t o select t he app r opr iat e reply m essag e fr om t he r ep ly queue. Th er e ar e sever al t echniqu es for setDeploy t in g t h EJBs e corrt elat I D. r exam ple, wveerlet t h e client set it b ased on som e r and om o Wion eb Sp herI ne ou Applicat ion Ser nu mb er , an d t hen use t he sam e I D t o select t h e r ep ly message. This clien t snipp et is show n in Figur 6- 1Redbook 6. This IeBM pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (Figu EJB) rarech6it-ect an dt in d ep entr el of at su ch 1 6u .re,Set g loym a cor ionapIplicat D ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. selector = "CORR:TF"+ (new I n Part 1, we in t r oduce EJBs as ajava.util.Random()).nextInt(999999); p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he outMessage.setJMSCorrelationID(selector); EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. IMessage n Part 2, wetype in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At An b e ane asy nchr s frEJB on t -ap enplicat d for ion m any hod s ofert he samlicat e session bean. t he MDB en d, cou w e ld d escrib how t o onou deploy s t o met a WebSph e App ion Serv er. For exam ple, an MD B could not only fr ont - en d t he transfer m et hod , bu t also deposit an d withdraw. On e w ay t o p ass in for m at ion fr om a client t o an MDB is by set t ing a p r opert y ( Figur e 6- 1 7) . Based on t he pr opert y valu e, t he MBD can ex t r act dif fer en t p ayload obj ect s an d in vok e t h e corr ect m et hod of t he session bean.
Figu r e 6 - 1 7 . Set t in g a m essa ge pr ope r t y
outMessage.setStringProperty("BUSINESSMETHOD", "transfer");
Timeout Anot h er im p licat ion of doing t his t yp e of requ est / r eply is t h at t h er e is n o gu ar ant ee t hat t he • Table of Content s r ep ly m essage w ill be receiv ed b ack in a reasonable am ou nt of t im e. Th is is not t o say t hat t her e • ndex is no guar an tI eed deliver y, just t hat t he t im e of d eliv er y cann ot be guar ant eed. I t is t he client 's EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper r espon sib ilit y t o set an app rop riat e t im eout for t he app licat ion, but t oo lon g of a t im eou t m igh t By li, Wout Denay er, on Lar san Schu nk , Deborah Weiss havUeli e aWah negat iv eer imp act applicat ion'sShaddon perf or, Mart manince, b ecau se t he client essent ially blocks un t il t he messag e is r eceived. I f t h e t im eou t is t oo long, t hen an alt er nat iv e m essaging int er act ion at t er n m ight hav e t o be used. Pub lish er: p I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Message-driven bean interaction patterns This sect ion d escrib es som e com mon int er act ion p at t ern s for m essage- dr iven beans. We h ave alr ead y int r oduced t w o: asy nchr onous fir e- an d- f org et , and pseudo- sy nchr onous requ est / r eply in t he ex am ples abov e. •
Table of Content s
•We ex pand on I ndex t hese, and discu ss som e v ar iat ions of t hese t hem es t hat m ay help y ou t o ch oose EJB 2 . 0 Dop e veriat lopm it h Wf or ebSphe udio Appl on De vion el oper an appr e epntatwt ern y ourr emStessagin g ica aptiplicat . We t h en describ e som e com m on b est pr act ices as well as som e pit f alls t o av oid w hen d esign g MDBs. By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in in Weiss Pub lish er: I BM
Message interactions Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
I n t hisPages: sect ion, 75 0 w e list som e com m on messag ing sy st em int er act ion s. We ar e sur e t hat t his is n ot a com plet e list , and t hat m any m or e int er act ion p at t ern s can b e discov er ed based on y our own int er act ion r equir ement s.
Fire-and-forget Lear n about t he EJB 2 .0 specif icat ion This is a scenar io in w hich a m essage p r oducer sen ds ( or p ublishes) a messag e, but does not elop EJBs it h St, uadio App licat ion w ait fDev or , or ever g etw back r eply . This p at Dev t ern eloper m ight be best used f or such sy st ems as r em ot e loggin g, w her e w e do not r eq uire a r esp onse t o b e sen t b ack , or b r oadcast - t yp e m essages. This Deploy EJBs t o W eb Sp her e Applicat ion Ser v er is not t o say t hat t he m essage p r oducer does not care wh at h appens t o t h e m essage, or t h at tThis her eI BM is not som e qualit y - of-dser vice associat is tmo essage, ju stuse t hatWebSp in t hishere caseStt udio he MDB Redbook pr ovides et ailed in for m ated ionwit onh ht how ef fect ively client t r ust s t h e MDB receiv er t o hand le it or com pensat e in som e w ay if it can not be han dled. App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Fire-and-forward un derly ing r elat ional dat ab ase. This is a v ariat ion on t he f ire- and - for get scenar io ab ove, in t hat t he client d oes not requ ire a I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic r espon se. How ev er , t h e m essage consum er is r esp onsible for pr ocessin g t he m essage, an d t hen concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he for w ar ding t h is message ont o some ot her pr ocess. Th is cou ld b e t o an ot her qu eu e, or anot h er EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed syst em . This is a comm on scenar io t hat one m ight fin d in w or kf low - based sy st em s, or ot h er h ubper sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or based sy st em s, an d is a feat ur e of t h e WebSph er e MQ I nt egr at or Br ok er and I BM WebSp her e MQ successful imp lem ent at ions of EJBs. Wor k flow p rod uct s. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, difrequest/reply fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At Pseudo-synchronous t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. This is t he v ariat ion t hat w e hav e u sed in ou r exam ple. Th e idea is t h at t h e r eq uest and r ep ly of t he message ar e int end ed t o be pr ocessed w it h in a sin gle int er act ion cy cle. This in t er act ion say s t hat t he message pr odu cer sends a m essage, t h en b lock s t he reply . Fr om a m essag e pr oducer 's per sp ect iv e, t he im plicat ion is t hat t he r eply m ight not be r eceiv ed back in a r easonable t im e fr am e, and it mu st hav e a means in w hich t o ensur e t hat t he app rop r iat e reply is r eceiv ed ( t h rou gh m essage cor relat ion) . Ther e ar e some cr eat ive alt er nat iv es t o t h is t hat could be im plem ent ed by t h e consum er so t hat m essage r eceipt block in g is not r equ ir ed, such as h aving a dedicat ed m essage list ener t hat polls for t he m essag e and t r igger s an ev ent back t o t he w ait ing client . Regar dless of h ow eff icient t he un derly ing im plem ent at ion of t his w ait ing can be m ade, t h e client is st ill w ait ing for a m essage,
and w ill t im e out if it is not r eceived in a r easonab le t im e fr am e.
Asynchronous request/reply Of t en r eplies are st ill r equir ed, j ust n ot as par t of t h is par t icu lar int er act ion . I n t his w ay, t he client can fir e t he r equest , b ut d oes not for get ab out it . As in t he p seud o- sy nch ron ous exam p le •abov e, t he client Table ult of Content imat elys r eceiv es som e r eply back , bu t it does not happ en w it hin t he •par t icular int Ier ndex act ion. EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Lat er , Wah t h eli,client ay go back t o nk see if a r eply has b een r eceived, or alt er n at ely , t h e r ep ly may b e By Ueli Wout erm Denay er, Lar s Schu , Deborah Shaddon , Mart in Weiss sen t b y m ean s of som e n ot ificat ion. Th is is com m on in w or k flow - based sy st ems, as well as lon gr unn ing act iv it ies w here t he clien t cannot w ait f or t h e reply in r eal t im e. An ex am ple is w her e a Pub lish er: I BM client subm it s a r eq uest t o p r oduce a repor t t hat t ak es a long t im e t o p rod uce. When t he r epor t Pub Dat e: April 1 1, 20 03 is com plet ed , a m essag e is sent back t o t he or igin at or. I SBN: 0- 73 84- 26 09 -1 75 0io, t h e r ep ly m essage m ust som ehow st ill b e cor r elat ed w it h t he or iginal r eq uest I n t hisPages: scenar m essage, so t he client k now s w hich repor t r eq uest is comp let e. Giv en t hat t he r equest an d r eply ar e par t of t w o dif fer en t int er act ions, t he cor r elat ion m ust b e m aint ained som ew her e else, m ayb e in an HTTP session or a dat ab ase, such t hat t he cor r ect r ep ly can be quer ied lat er.
Lear n and aboutsubscribe t he EJB 2 .0 specif icat ion Publish patterns Dev elop EJBs w it h St u dio App licat ion Dev eloper I n t his chapt er , w e h ave f ocused pr im ar ily on p oin t - t o- poin t b ased m essaging int er act ion s and pat t erDeploy ns. Recall t htat p ub/ sub s for t ion he pu blishing of a m essage t o a t opic b y a m essage EJBs oW eb Sp herallow e Applicat Ser v er pr odu cer , of w hich mu lt iple consum er s m ay h ave su bscr ibed t o t h at t opic. Theref ore, t h er e m ay be y Redbook con sum erpr s of a m essage. ThismIan BM ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans I( tEJB) is n ot f air t o uclassif b/loym sub as com elyplicat diff er ent dom ain of iner t eract ion pat t erServ ns. er. ar ch it ect re, anyd pu d ep enta of su plet ch ap ion s to a WebSph e App licat ion How ev er , it is im por t an t t o und er st and t h at t h er e ar e m ult iple m essag e con su m er s t h at v ar y fr om t he or iginal in t er, act pat ns inples some way .on a simp le bank ing app licat ion w it h an Thr oug hout t he book w eion pr ov id teerexam based un derly ing r elat ional dat ab ase. Any , or all, of t hese m essag in g con su mer s f or pub / sub cou ld p art icipat e in f ire- and - for get or fir and-1,for w ard ased EJBs int eract as of abov w hatEdhapp s EE) aft erand t h ecover m essage is sent is I n ePart we in t r b oduce as ions a p art Jave, a 2because En t er p rise it ion en ( J2 t he basic not t he con cer n of t he m essage p r oducer. concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Pseu dosy nchr r equ est / m r eply is a dr litiv t leenb it m or e We diff icult h pue b/ sub,prbecau pseu do- f or per sist ence entonous it y b ean s and essagebeans. also w pritovid best act iceseguidelines syn ch r onous assum es a single r eply f rom a single consu mer , bu t it is possib le t hat ev en t houg h successful imp lem ent at ions of EJBs. t her e ar e m ult iple m essage con su mer s, a sing le reply is alw ay s sen t b ack t hr ough som e sort of back - en2, d orchest at ion. t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session I n Part we in t r roduce beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Asyn ch ron r equest r ep lydif cou oreincom on in t heand pu b/ sub odel.s Itnh at t h is case, he im plem ent ous fin der m et h/ ods, ferldenbtemmapp g stm r at egies, sim p lemclient u se t he tEJBs. At single r equest couldebe b r oadcast t oEJB t he ap m ult ipleion m sessag con su mereres,App each const r uct ing t he en d, w e d escrib how t o deploy plicat t o a eWebSph licat ion Serv er. par t of a w h ole reply m essag e. The ind iv idu al r ep lies could be sent back, or some ot her back - en d or ch est r at ion could be used t o send a single r eply back . The id ea t hat t his d oes not happen at t he sam e t im e as t he or iginal m essag e pub lish st ill holds t ru e.
Message-driven bean best practices We h ave seen t h r oughou t t his book som e gu idelin es an d best pr act ices f or ent it y and session bean developm ent . We cont inue t his t h em e w it h an over view of som e of t he m essag e- d r iv en bean best pr act ices, as w ell as som e com mon m ist akes t o av oid , in t he f orm of do or do not : •
Table of Content s
•
I ndex
oe d eg at ss log icudio t o an otica hetironha er — EJB 2 . 0DD veel lopm e nte wbitusine h W ebSphe r e St Appl Dendl v el oper By
I n our ex am ple, w e show t h at t h e MDB' s r esp onsib ilit y is t o r eceiv e t he m essag e, but t he h andling of it is delegat ed t o Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss anot h er pr ocess, in our case a st at efu l session b ean . This b est pr act ice h old s t r ue in any int er act ion p at t er n m en t ioned abov e. Pub lish er: I BM Pub e: April 1er 1, 20 D Dat o consid m03 a k ing
t he MD B a r eq ue st / r ep ly cont r oll er — W e cou ld h ave t aken t his delegat I SBN: 0-ion 73 84a 26 st09 ep-1 f ur t her , and d elegat ed t he r eply han dling t o an ot her com ponent t o d o as wPages: ell, t o 75 m 0 ak e t he sy st em a bit m ore object - or ien t ed . Par t icu larly w hen considering r eq uest / reply as an in t er act ion pat t er n, consider incor por at ing r eq uest / reply m essag in g sem an t ics in y our m odel ( sim ilar t o w h at is com m only done in serv let s/ JSPs) , and m ake t he MDB m or e of t h e con t r oller of t h e int er act ion .
D o n ot a t t e m p t t o m a i nt a in st at e w i t h MD Bs— MDBs do not hav e t he not ion of client st at e, and ar e designed t o b e st at eless pr ocesses. W e recom mend n ot at t em pt ing t o do Lear n about t he EJB 2 .0 specif icat ion m ult iple MD B int er act ions t hat ar e som eh ow depend en t on a conv er sat ion al st at e w it h t he MDB client in an asy w ay, ch eloper t h at t h e MDB is responsible f or m anagin g t he Dev elop EJBs w it h Stnchr u dioonous App licat ionsu Dev st at e. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Defin it ely d o not at t em pt t o use st at efu l session beans f rom w it h in t he MDBs. I f m ult iple int er act ion s m ustdbeteailed coor dininfor at m edat , tion h enon it hshou up ively t o t he clien t t o coord in at e t h ese This IMDB BM Redbook pr ovides ow tld o befefect use WebSp here St udio int er ion s, and fnot r elyd ev onelopm t he MDB t o ap useplicat somion e sts at e syston emt ht oe Ent do so. e p ooled App licat ionactDeveloper or t he en t of based er prMDBs ise JavaraBeans ances, so tan herdedis guar hatapt plicat he sam ance er we ill App ex ecut t h eServ nexter. ( EJB) inst ar ch it ect u re, epno loym entanoft ee su tch ione sMDB t o a inst WebSph licateion m essage t hat y ou sen d, so n o af finit y t o a st at ef ul session could b e pr oper ly m ain t ained. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an D oing n ot ha ional ve a m essag e proce ss dep en d on a not he r m essag e be ing pr oce sse d— un derly r elat dat ab ase. Qu eu in g, b y definit ion , imp lies a fir st - in/ f ir st - out ( FI FO) pr ocessing m odel. Alt houg h a m essage beforEJBs e anot m essage is 2g uar y itt his odel be cover consum edbasic in a I n Part 1, we insent t r oduce asher a p art of Jav a En tant er peed rise bEd ion m ( J2 EE) t oand t he FI FO or der, t her e is no guar ant ee t h at t h e pr ocessing of t he m essag e finishes in any concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he t icular orion der .t hat Therprefov orid e,e,if for t here is som cy cton d er ,- m t hanag e client EJB 2.par 0 sp ecif icat ex am ple, eendepend hancedenfun iont he alitm y essage for contor ain ed m ust h andle t his t hr oug h som e ot h er means, such as r equest / r ep ly , w h er e t h e second per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or m essage is n ent ot sent untofil EJBs. t he f irst one has su ccessf ully comp let ed. This r espon sib ilit y could successful imp lem at ions be of fload ed t o t he m essag e con su m er s t o check an d wait , but t his cr eat es an d ep en dency I n Part w it2, hinwet he in tconsum r oduce er t h et hsam at isp le d ifbank ficulting to m applicat anag e.ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also D oent w afin t ch e of dif t he e inpa I t is possib ov ersbur den you r EJBs. At im plem dert hmeetsihzods, ferm enessag t m app g yl stoa r atd— egies, and sim plelet oclient t h at u se t he sy steem m essages h atplicat ar e ion j usts ttoo for it er t o ehand le efion ficien t ly.er. Th e t he enmd,essaging w e d escrib howw it t oh deploy EJBtap o abig WebSph App licat Serv m essaging serv er ( w het her int er nal or ext er nal) is anot her sy st em r esour ce t hat now m ust be m on it ored as anot h er applicat ion r esour ce. Ap pr opr iat e considerat ion of t he size of t h e m essage w ill inf luence y our choice of w het her t o incorp orat e m essaging in t he sy st em . D o consid er usi ng X ML- ba se d m essage s f or int e r- ap pl icat i on int e gr at i on—XML is com mon ly used as a m essaging st r uct ur e t hat allow s for a m or e por t able int er - app licat ion int egr at ion m odel. Alt houg h it does add som e ov er head t o t h e m essage p ayload size, and does m ost lik ely r equir e b ot h t he MDB client s and MDBs t o now deal w it h XML par ser s, it is fast becoming a st andar d for in t er op er abilit y . The SOAP- based m odel of m essagin g u sed in Web ser vices is av ailab le for JMS and WebSp here MQ ( Su ppor t Pac MA0R for W eb Spher e MQ 5. 3, Febr uar y 2 003 ) .
D o n ot use f or r e al l ong- runn in g t r an sa ct ions— Alt h ough w e ind icat ed t hat asyn ch r onous r equest / r ep ly m ig ht be app r opr iat e w hen t he r eq uest is t oo long ru nnin g f or t he MDB client t o w ait f or , w e ar e not saying t h e MDBs t h em selves should be used f or longr unn ing t r ansact ion s. This m ight be b et t er left t o a bat ch cy cle, or som e ot her act iv it y t hat br eaks t he w or k int o smaller execut able com pon en t s. The MD B can be t h e launching poin t for coor d in at ing t he act iv it ies of t h e comp let e lon g- r un ning t r ansact ion . Be a w a r e of poison m e ssa ge s— Because MDBs are EJBs, t hey h ave m any of t he Table of Content s capabilit ies available t o ent er pr ise b ean s, su ch as t ran sact ional su ppor t . Alt h ough w e h ave • I ndex not y et discu ssed t r ansact ion s, w e hav e seen t hat t he d ep loym ent descr ipt or set t ing for t he EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper MDB can d en ot e a t ran sact ion t y pe of cont ainer . Wh at t h is means is t hat if t he MD B has a By Ueli pr Wah li, Woutper Denay er, Lar nk , Deborah , Mart in Weiss oblem rocessing t hseSchu message ( t h Shaddon e onMessage m et hod t h row s an ex cept ion ) , t he cont ainer w ill at t em pt t o r oll b ack t he t r ansact ion au t om at ically . Rollback in JMS means t o t h eI BM m essage b ack on t h e queue. Of cou rse, t h e con t ainer w ill now see t h is messag e on Pubpu lisht er: t he queue, an20 d 03 w ill d eliv er it t o t he MD B again, w hich could cause it t o fail again. I f t h is Pub Dat e: April 1 1, mI SBN: essage is som ehow f und am en t ally unpr ocessable, t h is poison m essag e m ig ht con t inually 0- 73 84- 26 09 -1 be d eliv ered and r olled back indef in it ely u nless you han dle it . •
Pages: 75 0
I f you ar e lever aging WebSph er e MQ, a redeliver coun t can be set on a messag e t o pr ev ent t his f or b eing r edeliv ered aft er a nu mb er of at t em p t s, at w hich point it can go t o a deadlet t er queue. Ot herw ise, you m ay h ave t o han dle t he pr oblem s w it h t h is message by m anually p ut t ing it ont o y our ow n d ead let t er qu eu e or log, t o av oid t h is r ecur sive p rob lem . See " Tr Lear ansact n about ion s tand he EJB m essage2 .0 specif dr ivicat en ion b ean s" on p age 29 8 for m or e inf orm at ion on t r ansact ion han dling w it h MDBs. Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Summary I n t his chapt er , w e ex plor ed MDBs fr om a con cept ual v iew and also pr ov id ed g uidelin es for t h eir usage. •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Chapter 7. EJB clients I n t his chapt er , w e d escrib e EJB client s. We p r ovide a list a client t yp es an d pr ogr am min g t echniqu es, includ ing t he u se of r elat ionships, inh er it ance, and f in der m et hods. •
Table of Content s
We t h en p rov ide g uidelin es for clien t d esign t hr ou gh t he u se of t he m odel- view - cont r oller • I ndex pat t er n, access beans, and t h e session f acad e pat t er n. EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah Denay Schu nk design , Deboraht ech Shaddon , Mart in Finally, wli, e Wout comerpar e t her, e Lar difsferent niques.
Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Client types Ther e ar e sev eral diff er ent t y pes of client s t hat access EJBs in a WebSp here Applicat ion Ser ver Version 5 and t he J2 EE pr ogr am m in g en vir onm ent . •
Table of Content s
•
I ndex
Session beans EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
A session bean can b e looked at as bein g a client of t he en t it y beans it accesses. I n most cases t hese beans ru n in t he sam e EJB cont ainer an d can use local int er faces. Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Servlets Pages: 75 0
Serv let s can in t er act w it h EJBs t hr oug h r em ot e or local int er faces. A sugg est ed appr oach is t hat ser v let s only int er act w it h session beans, an d t he session beans int er act w it h ent it y beans. To access session beans, t he ser vlet can use eit her r em ot e or local in t erf aces; for p ort ab ilit y , r em ot e in t er f aces ar e sug gest ed. Lear n about t he EJB specif ionent at ion w here t he u ser f ron t end is in HTML and JSPs, The ser vlet is a t y pical t hin2-.0 client imicat p lem w hich in vok e t h e ser v let s. Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Model-view-controller
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App ion Developer f or tthhe enh t HTML of ap plicat ionser s based e Entis ertpr Jav aBeans The licat p r ogr amm ing m odel at disevuelopm sed w it pages, v let s, on an dt hJSPs heise m odelv iew (cont EJB)r oller ar ch (itMVC) ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv . MVC is a pr ogr am m in g par adigm t hat w as fir st int r odu ced by Sm allt alk.er. I t is a design pat t er n t hat aim s at sep arat in g t he bu sin ess logic f r om int er act ion and p resen t at ion log ic. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an ing rWeb elat ional dat ab ase. Iun n derly a t y pical applicat ion, a user int eract s w it h a Web sit e using a b r owser . The u ser m ak es a r eq uest by follow in g a link or sub m it t ing a f orm . Th e app licat ion, aft er pr ocessing t he requ est , n Part we in t r oduce EJBs a peart of Jav t er p rise ( J2 EE) and cover he basic rI et ur ns 1, a response t o t he useras. Th r espon seais2 pEn r esent ed tEd o tit h ion e user in HTML. A t yptical ex am ple concep ar ch I nctpar icu lar w einf point out intctr od w it hI tDhe is w hent saand u sert ht e r ies t oitgect et ur pre. odu or t account orm at iont he by nuew sin gfutnct h e ions pr odu oruced account 2. 0ivsp ecif rEJB espect ely . icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful ions EJBs. The MVC mimp od ellem enent for at ces t h eofapp licat ion t o b e split int o t hr ee m ain p art s: t he m odel, t h e v iew , and t h e cont r oller ( Figur e 7- 1 ) . I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Figu r e 7 - 1 . M VC pr ogr a m m in g pa r ad igm im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Mod el
The m od el en cap su lat es all t he b usin ess log ic and ru les and does t he b usiness pr ocessing. I t is usually im plem en t ed by JavaBeans or EJBs.
V ie wLear n about The vt he iewEJB uses 2 .0 t he specif result icat ofion t he bu sin ess pr ocessing and const r uct s t h e r espon se t hat is pr esen t ed t o t he u ser . I t is usually imp lem ent ed by JSPs. Dev elop EJBs w it h St u dio App licat ion Dev eloper Cont rol le r The con t roller m anages an d cont rols all t he int er act ion b et w een t he user and t h e app licat ion. Usually it is a ser let tvhat Deploy EJBs to W eb Sp her e ,Applicat ionv Ser er r eceives t h e user requ est and passes all t he inpu t p aram et er s t o t h e m odel t hat does t h e act ual w or k . Fin ally , a JSP is called t o This I BM Redbook r et ur nprt ovides he out pdut et.ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (This EJB)aparprchoach it ect g u iv re,esan d ep loym apexam plicatple: ion s t o a WebSph er e App licat ion Serv er. usd man y advent antof agsu es,chf or Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing elat ional ab ase. It p rovr ides clear dat separ at ion of t he b usiness log ic fr om p resen t at ion and int eract ion logic. I n Part wesint ht reoduce as t aierp art of ect Javur a es 2 En t er p rise Ed it ionlog ( J2 cover t he logic basicar e I t 1, allow use ofEJBs t hr eear chit w here b usiness ic EE) and and pr esent at ion concep t s andont hdif e ar ur e. larent w esecur pointit out t he n ew fu nct ions int r od uced w it h t he placed ferchenit tect serv er sI nwpar it h tdicu iff er y m odels applied. EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed I tence enabent lesitryole separ at ion in a developm t eam , walso h er eprt hovid e weork of pr a pag e desig ner per sist b ean s and m essagedr iv en ent beans. We best act ice guidelines f or sm ootimp hly lem int egr w itof h EJBs. t he w or k of a b usiness developer . successful entat ates ions When ingin t his pr ogr ing odel ing in Web applicat ions, t he imp vehicle t hatenist itused f or car ry ing I n Partapp 2, ly we r oduce t hamm e sam p lembank applicat ion and t hen lem ent y beans, session dat a or an objd ect s fr ome-tdhreivser let t o using t h e JSP is oft ener tehe or er HttpSession. beans, m essag en vbeans WebSph StHttpServletRequest u dio Ap plicat ion Dev elop . We also The r equ estfin object ain s dif allfer t he forapp m atinion a Weband br ow ser Web w henAt im plem ent der mcont et h ods, enin t m g stt rhat at egies, sim p lesends clienttso t ah at u seserv t heerEJBs. a u ser r equ s a resour on EJB t he ap serv er. ion s t o a WebSph er e App licat ion Serv er. t he en d, w eest d escrib e howcet o( URL) deploy plicat
Application clients Wit h WebSph er e App licat ion Serv er Version 5, ap plicat ion clien t s now con sist of t he follow ing m odels: Act iv e X a ppl ica t ion cli ent — Th e Act iveX ap plicat ion clien t m odel u ses t h e Jav a Nat iv e I nt er face ( JNI ) ar chit ect ur e t o pr ogr amm at ically access t he Java Vir t u al Machine ( JVM) . Ther ef or e t he JVM ex ist s in t he sam e p rocess sp ace as t he Act iv eX app licat ion ( Visu al Basic,
VBScrip t , or ASP) and r em ains at t ach ed t o t he p rocess unt il t hat pr ocess t er min at es. Ap ple t a pp li ca t i on cli e nt — I n t he applet ap plicat ion clien t m odel, a Java applet is emb ed ded in an HTML docum ent r esiding on a r em ot e client m ach ine fr om t he WebSp here App licat ion Ser v er. Wit h t his t y pe of clien t , t he user accesses an en t erp rise bean in t he WebSph er e Ap plicat ion Serv er t h rou gh t he Jav a applet in t he HTML docum en t . J2 EE a ppl ica t ion cl ie nt — Th e J2EE ap plicat ion clien t is a Jav a app licat ion pr ogr am t h at Table of Content s accesses ent er pr ise beans, JDBC dat abases, an d Java Message Ser v ice m essag e queues. • I ndex The J2EE app licat ion client pr ogr am r un s on client machin es. Th is pr ogr am f ollow s t h e EJB 2 . 0sam D e ve it ham W ebSphe e St udio ica ti on el oper e lopm Javae nt pr w ogr m ing rmod el asAppl ot her JavDe a vpr ogr am s; how ever , t h e J2EE ap plicat ion By Ueli client Wah li, Wout depend er Denay s oner,t he Lar sap Schu plicat nk , Deborah ion client Shaddon r unt, im Mart eint oWeiss config ur e it s execut ion env ir onm ent , and uses t he Java Namin g an d Direct or y I nt erf ace ( JNDI ) n am e sp ace t o access r esou r ces. •
Pub lish er: I BM
P lug ga ble a pp li ca t ion cli en t an d t hi n ap pli ca t i on cl ie nt — Th e plug gable an d t hin app licat ion client s pr ovide a light weigh t Jav a clien t p rog r amm ing m odel. These client s are I SBN: 0- 73 84- 26 09 -1 best suit ed t o sit uat ion s w her e a Jav a clien t ap plicat ion ex ist s b ut t he ap plicat ion needs Pages: cem 75 0 ent s t o use ent er pr ise b ean s, or w her e t he clien t ap plicat ion r equir es a t h in ner, enhan m or e lig ht w eight en vir onm ent t han t he one of fer ed by t he J2EE applicat ion clien t . Th e dif fer en ce b et ween t he t h in ap plicat ion client and t h e plug gable ap plicat ion client is t h at t h e t hin app licat ion client includes a Java Virt ual Machin e ( JVM) , an d t he plu ggab le applicat ion client r eq uir es a JVM be p rov ided by t h e user .
Pub Dat e: April 1 1, 20 03
The J2EE app licat ion amicat m inion g mod el pr ovid es t he benef it s of t he J2 EE plat f orm for Lear n about t he client EJB 2 pr .0 ogr specif t he Java client applicat ion. Th e J2EE ap plicat ion clien t of fer s t he ab ilit y t o seamlessly d ev elop, assemDev ble,elop deploy EJBsan wdit hlau Stnch u dioa App client licat app ionlicat Dev ion. eloper The t ooling pr ov ided w it h t he Web Spher e plat f or m suppor t s t he seamless int egr at ion of t hese st ag es t o h elp t he d ev eloper creat e a client Deploy oW her. e Applicat ion Ser v er app licat ion frEJBs om sttar t teb o fSp in ish This d et licat ailedion in for m atand ion on h owing t o t ef St udio WhenI BM y ouRedbook d ev elop pr a ovides client app using adher o tfect he ively J2EE use plat WebSp for m , yhere ou can m ov e App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans t he client applicat ion code fr om one J2EE plat f or m im plem ent at ion t o anot her . The client (app EJB) arion ch itpack ect u re, d drequ ep loym of su ch ap plicat s t oJ2 a EE WebSph er e deploym App licat ion licat agean can ir e ent r edeploy ment usingion each plat f orm ent Serv t ool,er. bu t t he cod e t hat comp rises t he client ap plicat ion d oes n ot chan ge. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r ion elat clien ionalt dat ase.su pplies a cont ain er t hat pr ovid es access t o syst em serv ices for The ap plicat r unab t ime t he client applicat ion code. Th e client app licat ion cod e m ust cont ain a main m et hod. The I n Part in t r oduce a pt art Javm a et 2 hod En t erafptrise Ed it ionir (onm J2 EE) cover t and he basic app licat1, ionwe client r unt imEJBs e inv as ok es his of main er t he env en tand init ializes r uns concep t s Java and tVirt he u aralchMachine it ect ur e. cod In e par t icuinat lar w e point out t he n ew fu nct ions int r od uced w it h t he un t il t he t erm es. EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent y ballow ean ssand essagedr iv en beans. also pr ovid e best pr act icedefined guidelines f or The J2EE plat f oritm t he m app licat ion client t o useWe nickn ames or sh ort nam es, w it hin imp lemion ent at ions of ent EJBs. tsuccessful he client applicat d ep loym descr ipt or . These deploy m en t d escrip t or s ident ify ent er pr ise beans or local resour ces ( JDBC, JMS, Jav a Mail and URL API s) f or simp lif ied r esolu t ion t h r ough It he n Part in .t rThis oduce t hplified e sam presolut le bank ingt oapplicat and bt hen lem entanend t local it y beans, use 2, of we JNDI sim ion t h e ention erp rise ean imp r ef erence r esousession rce beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We r ef erence also elimin at es ch anges t o t he clien t ap plicat ion code, w hen t he u nder lyin g obalso ject or im plemce ent m et hesods, difoves fer ent ot m in en g st at egies, and sim p lechang clientes s th at u se r esour eitfin herder chang or m a app d if fer t rser ver . When t hese occur , t ht he e EJBs. At tapp he licat en d,ion w eclient d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. ent " on can r equir e r edeploym ent ( for det ails see Ch apt er 17 , " Client dev elopm pag e 587 ) . The ap plicat ion clien t also p rov ides init ializat ion of t he r un t im e env ir onm ent f or t h e client app licat ion. The deploy m en t d escr ip t or def in es t his un ique init ializat ion for each client app licat ion. The app licat ion client r unt im e also p rov ides supp ort f or secur it y aut hent icat ion t o t he en t er pr ise beans an d local r esour ces. The ap plicat ion clien t u ses t h e RMI - I I OP p rot ocol. Using t his pr ot ocol enab les t he client app licat ion t o access ent er pr ise bean ref er ences and t o u se CORBA ser v ices pr ov ided b y t he J2EE plat f or m imp lem ent at ion. Use of t h e RMI - I I OP p rot ocol and t he accessibilit y of CORBA ser v ices assist user s in dev elopin g a client app licat ion t hat requ ires access t o bot h ent er pr ise bean
r ef erences and CORBA ob ject r eferences. When y ou com bine t he J2 EE and CORBA env iron ment s or pr ogr am m ing mod els in one client app licat ion, you m ust under st and t he d iff er ences bet w een t he t wo pr ogr am m in g mod els t o use and m anag e each app rop riat ely .
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
How to access EJBs The b asic st eps for accessing EJBs f r om any t yp e of client are:
• •
Table of Content s
Ret r iev eI ndex t h e hom e int er face
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Find or creat e an EJB inst ance
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
I nv oke t he m et hods of t he EJB inst ance Pub lish er: I BM Pub Dat e: April 1 1, 20 03 0- 73 84- 26 09 -1 HomeI SBN: interface Pages: 75 0
Ther e ar e basically t w o w ays t o r et riev e a h ome in t erf ace: Access w it h f ull y qu al if ie d b ea n hom e na m e— You access a bean usin g it s JNDI n ame. This is on ly possib le for a r em ot e h ome in t erf ace and should only be used f or t hin client s w herne about no J2EE is av ailable. Lear t hecont EJBainer 2 .0 specif icat ion The JND I nam e is st or ed in an ex t ension of t he deploy m en t d escrip t or ( ibm-ejb-jar-bnd.xmi) . Dev elop EJBs w it h St u dio App licat ion Dev eloper Access w it h a n EJB r ef er en ce — You access a bean usin g an EJB r efer en ce t hat is defined in t he d EJBs ep loy tm escr ipteorApplicat an d t hion at pSer ointvsert o t he EJB. The con t ain er is r espon sible f or Deploy o ent W ebd Sp her t r anslat ing t his r efer en ce t o t h e r eal JNDI nam e of t h e comp onent at r un t ime. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio EJBion r efDeveloper er ences decouple applicat om t h eion JNDI set t ings e deploy App licat f or t he dt heve elopm en ion t of frap plicat s based on tin h et hEnt er pr isement Jav aBeans ipt oru. re, EJBanr efer enloym ces rent equir a ch J2EE cont ainer are t he only w aylicat t o ion access ( EJB) descr ar ch it ect d d ep ofesu ap plicat ion s tan o ad WebSph er e App Servlocal er. hom e int er faces, bu t r eferences ar e also t he su ggest ed w ay f or r emot e hom e int er faces. An Thr oug hout book w eher pr ov e exam based onence; a simp le bank w it h an EJB r ef terhe ence is, eit a ridem ot e orples a local ref er one n am eing canapp notlicat be ion bot h. un derly ing r elat ional dat ab ase. JNDI n am es m u st be assigned by t h e deploy er lat est wh en an ent er pr ise ap plicat ion is inst alled I n oPart we in ion t r oduce EJBs int an 1, applicat ser ver . as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he Not y using access beans t h for e r et of en t hehanced h ome fun in t erf acealit can be cont generat o t he EJBe2.t h0 at spbecif icat ion t hat pr ov id e, exr ieval am ple, ct ion y for ain ered- mint anag ed fact or yence access ands is t r anspar ent dr t oivt he . We also pr ovid e best pr act ice guidelines f or per sist entbitean y b ean and m essageen client beans. successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session Basics programming principles beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At We howe w e can r it e a EJB sim ple EJB clien can finderand u se ent er pr ise er. beans in t he w enilld,f irwst e dsee escrib how to w deploy ap plicat ion s tt ot haatWebSph e App licat ion Serv an EJB con t ainer. Ther e ar e cer t ain st ep s a clien t h as t o f ollow befor e accessing ent erp rise b ean s on an EJB serv er. These st ep s ar e pr esent ed her e.
Obtain an initial naming context When y ou ar e w r it ing an EJB client , y ou ar e respon sible f or cr eat in g a JNDI InitialContext t hat w ill be h ooked t o t he nam ing serv ice ru nnin g in t he applicat ion ser ver .
I n t he sim plest for m w e g et t he in it ial con t ext using it s const r uct or :
javax.naming.InitialContext initialContext = new javax.naming.InitialContext(); •
Table of Content s
•
I ndex
EJB 0 D eple ve lopm e ntiswappr it h W opr ebSphe udior unn Applin icagti on oper This2 .sim f orm iat er ewSthen in tDe h ev elsame
JVM or in t h e sam e m achine as t he By Ueli and EJBs Wah li, t he Wout t ar erget Denay is er, a WebSp Lar s Schuhere nk , Deborah Applicat Shaddon ion Ser , Mart ver in Weiss . When const r uct ing an in it ial con t ext , you can pass t w o p aram et er s: Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73ion, 84- 26host 09 -1 n ame, an d p or t of t h e nam ing ser v ice ( PROVIDER_URL) The locat Pages: 75 0
The n am e of t he init ial con t ex t fact or y ( INITIAL_CONTEXT_FACTORY) This in for m at ion is p rov ided t o t h e const r uct or of t he InitialContext as a java.util.Properties. Her e is an ex am ple: Lear n about t he EJB 2 .0 specif icat ion java.util.Properties properties = new java.util.Properties(); Dev elop EJBs w it h St u dio App licat ion Dev eloper properties.put(javax.naming.Context.PROVIDER_URL, "iiop:///"); Deploy EJBs t o W eb Sp her e Applicat ion Ser v er properties.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep"com.ibm.websphere.naming.WsnInitialContextFactory"); loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. javax.naming.InitialContext = a simp le bank ing app licat ion w it h an Thr oug hout t he book , w e pr ov id e initialContext exam ples based on un derly ing r elat ional dat ab ase. new javax.naming.InitialContext(properties); I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Not ice ence t hat w e use he st ing m iiop:/// f or t h ebeans. PROVIDER_URL p rovid opert . Th is st rin g ind icat es t of or per sist ent it y btean s rand essage- dr iv en We also pr e ybest pr act ice guidelines tsuccessful he ru nt im eimp env ir on ment t o find t he nam ing ser vice at t he st an dar d p or t on t he local m achine. lem ent at ions of EJBs. The g en er al for m of t he PROVIDER_URL st r ing is iiop://hostname:port/. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session The v alue he second ropbeans er t y , INITIAL_CONTEXT_FACTORY, is t h eion class am eerof t he also n amin g beans, an dofmt essag e- d r ivpen using WebSph er e St u dio Ap plicat Devnelop . We ser v ice ent fact fin or y. Web Spher ioninSer Ver sionand 5 , use im plem derFor m et h ods, dif eferApplicat en t m app g stvrer at egies, sim p le client s t h at u se t he EJBs. At com.ibm.websphere.naming.WsnInitialContextFactory. t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Looking up an EJB home from the initial context Using t h e init ial nam ing cont ex t , w e can ret r ieve t he EJB hom e of t h e en t it y bean w e w ant t o access, for ex am ple, t he Customer ent it y bean. For a r em ot e hom e int er face w e can use t he global JND I nam e or an EJB r eference. For a local hom e int er face w e m u st use a local r efer en ce.
Using a global JNDI name The cod in g sequ en ce f or a rem ot e hom e in t er f ace u sin g a global JNDI n ame is:
Object objHome = initialContext.lookup("ejb/itsobank/Customer"); •
Table of Content s
CustomerHome customerHome = (CustomerHome) • I ndex EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
javax.rmi.PortableRemoteObject.narrow(objHome,CustomerHome.class);
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM
Not ice Pub Dat t hee:JND AprilI 1 nam 1, 20 03 e of t h e cu st omer ent it y bean, ejb/itsobank/Customer. Th is is t he n ame w e defIin e in0-t73 he84-deploy SBN: 26 09 -1 m en t d escr ip t or w hen w e cr eat ed t he Customer b ean . Pages: 75 0
I n t he secon d line we use t he u t ilit y class PortableRemoteObject t o nar r ow t h e r ef er ence t h at w as obt ain ed f rom t he InitialContext. Th is can be seen as eq uiv alent t o cast ing ; it t akes a gener ic RMI - I I OP r efer en ce and ret u rn s an inst ance of t he p r oper class.
Using ann EJB Lear aboutreference t he EJB 2 .0 specif icat ion Dev EJBs w itf h dio ot App licateion eloper The cod inelop g sequ en ce or St a urem e hom in tDev er f ace u sin g an EJB r eference is: Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BMobjHome Redbook =prinitialContext.lookup("java:comp/env/ejb/CustomerR"); ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Object App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (CustomerHome EJB) ar ch it ect ucustomerHome re, an d d ep loym=ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. (CustomerHome) Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an javax.rmi.PortableRemoteObject.narrow(objHome,CustomerHome.class); un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t h eenarcechfitorect e. I nh om paret icu w e point t he n ew fu nct ions The codt sinand g sequ a ur local intlar erf ace using out an EJB r efer ence is: int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. Object objHome = initialContext.lookup("java.comp/env/ejb/CustomerL"); I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- dcustomerLocalHome r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also CustomerLocalHome = (CustomerLocalHome)objHome; im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. When lookin g f or a ref er ence y ou u se t he pr efix java:comp/env/. Th e r ef er ence nam e ( ejb/CustomerL) is defined by t ooling , such as Applicat ion D ev eloper in t he deploy m en t descr ipt or ed it or and st or ed in an ex t ension f ile. EJB r efer en ces ar e au t omat ically defin ed w hen y ou u se con t ainer- man aged r elat ionships in App licat ion Developer . Ot h er refer ences, f or exam ple f r om a session EJB t o an ent it y EJB, ar e defin ed m anu ally . Not e t h at w it h local r ef er ences sim ple class cast in g can be u sed t o con ver t t h e looku p result obj ect int o a local hom e int er face.
Using the EJB home Wit h t he EJB h ome, w e can creat e n ew inst ances, f ind, and r em ov e ent it y beans. Not e t hat m ost m et hods can t hr ow a FinderException an d a RemoteException; t hese hav e t o b e hand led in a try/catch b lock .
•
Cr eat ingTable a n ew of Content cu st omer s b ean :
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Customer customer = customerHome.create(customerID);
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
customer.setTitle("Mr."); Pub lish er: I BM Pub Dat e: April 1 1, 20 03 customer.setFirstName("Ueli"); I SBN: 0- 73 84- 26 09 -1
...... Pages: 75 0
Here we use one of t h e available con st r uct or s of t h e Customer b ean t h at on ly t akes t h e customerID. Ot her pr oper t ies are set u sin g set t er m et hods. Lear n about t he EJB 2 .0 specif icat ion Finding a cu st om er b ean : Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Customer customer = customerHome.findByPrimaryKey(new CustomerKey(id)); This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. The findByPrimaryKey m et hod t ak es a p aram et er of t y pe CustomerKey t hat we in it ialize w it h t he anbook I D en byidteheexam userples as an in putonpar am etle er .bank ing app licat ion w it h an Thr oug hout t he , wt er e ed pr ov based a simp un derly ing r elat ional dat ab ase. Cu st om find er met h ods ar e also inv ok ed fr om t he hom e: I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed java.util.Collection specialCustomers = customerHome.findHighInterest(x); per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session Rem g a cust ean : using WebSph er e St u dio Ap plicat ion Dev elop er . We also beans, an ovin d m essag e- dom r iv er en bbeans im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. customerHome.remove(customer);
You can also u se t he javax.ejb.Handle of t he cust om er as a p aram et er. This m et hod r esult s t o t h e delet ion of t h e cor r esp onding r ow in t he cu st om er t ab le.
Not e
The remove m et hod is n ot def in ed in t he hom e int er face. I t is inh er it ed f r om t he EJBHome su perclass.
Manipulating EJB references •
Table of Content s
Aft er acq uirin g t he cust om er bean ob ject , w e can in vok e an y of t he m et hod s available in t he • I ndex com ponent int erf ace, for ex am ple, w e can g et or set t he cu st omer p rop er t ies. We can also EJB 2 . 0 D e ve lopm e nt wmitet h W ebSphe e StEJB udio ob Appl ica.ti on De v el oper inv ok e t he remove hod on trhe ject By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM
customer.getName(); Pub Dat e: April 1 1, 20 03
I SBN: 0- 73 84- 26 09 -1 customer.setLastName("Newname"); Pages: 75 0
customer.remove();
Following relationships Lear n about t he EJB 2 .0 specif icat ion Wit h cont Dev elop ainerEJBs - m anaged w it h Strelat u dioionships, App licat ion y ouDev caneloper in vok e m et hods in an ent it y b ean t hat r et riev e one or m ult iple inst ances of a r elat ed ent it y b ean . Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Multiplicity one App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. I f you t r av er se a 1: 1 or 1: m relat ionship t o t he one side, t h en t h e r esult is a single ob ject . For single jectt he r et riev ing y simp le.based For exon ama ple, r ieving e t he t ob Thr ougob hout bookal, , wcod e pr ov is id ever exam ples simpt oler et bank appaccoun licat ion w itject h anfr om a tun r ansact ion rrelat ecord : dat ab ase. derly ing ional I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he Account EJB 2. 0 spaccount ecif icat ion= ttransrecord.getTheAccount(); hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. IMultiplicity n Part 2, we many in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At I f you t r av er se a 1: m or m : m relat ionship t o t he m any side t hen t he r esu lt is a Collection or a t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Set. You hav e t o it er at e t hr ough t h e collect ion ( or set ) result t o g et t he r elat ed ent it y bean inst ances. For ex am ple, t o list all t he t r ansact ion recor ds of an accou nt :
Account account = accountHome.findByPrimaryKey("101-1001"); Collection coll = account.getTransrecords(); Iterator collit = coll.iterator(); while ( collit.hasNext() ) {
TransRecordLocal tr = (TransRecordLocal)collit.next(); System.out.println ( ((java.sql.Timestamp)tr.getPrimaryKey()).toString() +" "+tr.getTransType()+" "+tr.getTransAmount().toString() ); } •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
NotUeli e tWah h atli,an Iterator only one w ay t o Shaddon access, tMart he inmem ber s of a collect ion; t he Collection By Wout er Denay er,isLar s Schu nk , Deborah Weiss and Set classes p rov ide a nu mb er of met hod s. Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 75 t0 a nt I Pages: m por
A collect ion r esult of a r elat ionship ret r iev al m u st be accessed w it h in on e t r ansact ion . You cannot r et r ieve in st an ces at a t im e u sin g d iffer ent t ran sact ions. Th er efor e it is good pr act ice t o p rov ide a session b ean m et hod t hat it erat es t h r ough a r elat ionship and collect s t h e infor m at ion int o a r esu lt Jav aBean . Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
Finder methods Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Finder This I BM m et Redbook hods retpr urovides n eit herd et sin ailed gle obj in for ectmsatorion valu ones h ow or collect t o ef fect ions ively of use obj ect WebSp s or valu herees. St To udio rApp et riev licat e ion all tDeveloper he r esult s fof or at he mudltev i- object elopm en f inder t of ap m et plicat hods ionuses s based cod inon g sim t h e ilar Ent er t o pr r elat ise ionships: Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Collection coll = dat accountHome.findGoldAccounts(aBalance); un derly ing r elat ional ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Iterator collit = coll.iterator(); concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0(spcollit.hasNext() ecif icat ion t hat pr ov ) id e, while { for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of= EJBs. AccountLocal account (AccountLocal)collit1.next(); I n Part 2, we process in t r oducethe t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session //..... account beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im } plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
The sam e t r ansact ional p rin cip les ap ply t o f inder m et hod s as for relat ionships.
Inheritance I nher it ance of ent it y b ean s t ouches t h e cod ing in t w o w ays: Sub class ent it y b ean s inher it t h e m et h ods of t h e super class. From a Checking b ean
( subclass of Account) , y ou can f ollow all t he relat ionships defined for t he accou nt . There is no special codin g r equir ed:
Checking checking = checkingHome.findByPrimaryKey("xxx-xxxx"); Collection coll = checking.getTransrecords(); • •
Table of Content s
Iterator collit = coll.iterator(); I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
while ( collit.hasNext() ) {
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
TransRecordLocal tr = (TransRecordLocal)collit.next(); Pub lish er: I BM
process Pub Dat// e: April 1 1, 20 03transaction
record
I SBN: 0- 73 84- 26 09 -1
}
Pages: 75 0
When follow ing r elat ionsh ip s fr om an ot her ent it y b ean t o t he sup er class or w hen usin g a cust om f in der m et hod on t he sup er class, t h e r esult collect ion cont ain s inst ances of any of t he su bclasses. Lear n about t he EJB 2 .0 specif icat ion For exam ple, w hen r et r ievin g account s for a cust om er wh en u sing t h e findGoldAccounts Dev EJBs h St u diocont Appain licat Dev eloper cust elop om f in der ,wtithe r esult s ion Checking an d Savings in st an ces. You can ch eck t h e t y pe of inst ance using t h is cod e seq uence: Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Collection coll = accountHome.findGoldAccounts(currentBalance); App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Iterator collit = coll1.iterator(); Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an while ( collit.hasNext() ) { un derly ing r elat ional dat ab ase. account I n Part 1,AccountLocal we in t r oduce EJBs as a p=art(AccountLocal)collit.next(); of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he BigDecimal balance shared EJB 2. 0 sp ecif icat ion t hat pr ov id=e, account.getBalance(); for ex am ple, en hanced fun// ct ion alit y forattribute cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or (account instanceof successfulifimp lem ent at ions of EJBs. CheckingLocal) { checking I n Part 2, weCheckingLocal in t r oduce t h e sam p le bank = ing(CheckingLocal)account; applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also // checking im plem ent fin derprocess m et h ods, dif fer en t instance m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. BigDecimal over = checking.getOverdraft(); } else if (account instanceof SavingsLocal) { SavingsLocal savings = (SavingsLocal)account; // process savings instance BigDecimal minimum = savings.getMinAmount(); } else {
// process generic account instance } }
•
Table of Content s
•
I ndex
Simple EJB client application
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Figur e 7- 2 sh ows a simp le m ain ap plicat ion t hat uses t he basic cod e t o f ind t he h om e, cr eat e, andPub r etlish riev Customer ent it y beans, and delet e a b ean . er: eI BM Pub Dat e: April 1 1, 20 03
Figu rI SBN: e 7 -0-273. 84Si26m09ple EJB cli en t app li cat ion -1 Pages: 75 0
package itso.bank5.simpleapp; import itso.bank5.cmp.*; Lear n about t he EJB 2 .0 specif icat ion import javax.transaction.UserTransaction; Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er public class SimpleApp { This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Apppublic licat ion Developer f or tmain(java.lang.String[] he d ev elopm en t of ap plicat ion s based static void args) { on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. UserTransaction tx = null; Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly try ing { r elat ional dat ab ase. I n Part javax.naming.InitialContext 1, we in t r oduce EJBs as a p art of initialContext Jav a 2 En t er p rise Ed = it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov idjavax.naming.InitialContext(); e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed new per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful lem ent at ions of EJBs. tx imp = (UserTransaction)initialContext.lookup("jta/usertransaction"); I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session Object objHome = initialContext.lookup("ejb/itsobank/Customer"); beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At CustomerHome customerHome = (CustomerHome) t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. javax.rmi.PortableRemoteObject.narrow(objHome,CustomerHome.class); tx.begin(); Customer cust2 = customerHome.create(201); System.out.println("Customer 201 created"); cust2.setFirstName("John"); cust2.setLastName("Smith");
System.out.println("Customer 201: name="+cust2.getName()); cust2.setLastName("Keller"); System.out.println("Customer 201: name="+cust2.getName()); tx.commit(); • •
Table of Content s I ndex tx.begin();
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah Customer li, Wout er Denay cust1 er, Lar = s customerHome.findByPrimaryKey(new Schu nk , Deborah Shaddon , Mart in Weiss
System.out.println("Customer Pub lish er: I BM
CustomerKey(101));
101: name="+cust1.getName());
Pub Dat e: April 1 1, 20 03
String[] accounts = cust1.getAccountNumbers();
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
if (accounts != null) { for (int i=0; i< accounts.length; i++ ) { System.out.println(" - Account: " + accounts[i]);
Lear n }about t he EJB 2 .0 specif icat ion Dev } elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er cust2.remove(); This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio tx.commit(); App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. System.out.println("Customer 201 removed"); Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an } catch(Exception ex) un derly ing r elat ional dat ab ase.{ I n Part ex.printStackTrace(); 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he { icat tx.rollback(); } for catch(Exception e) {} EJB 2. 0try sp ecif ion t hat pr ov id e, ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or } successful imp lem ent at ions of EJBs. } 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session I n Part beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also } im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. I t also f ollows a r elat ion sh ip t o list t he relat ed accoun t s. The getAccountNumbers m et hod is a m et hod on t he r emot e in t erf ace t hat uses t he cont ain er - m anag ed r elat ion sh ip t o get t he account nu mb er s.
Not e Becau se we did not d ef ine a r em ot e int er face f or t h e Account b ean , w e cannot exp lore
t he accoun t at t ribu t es in a r em ot e clien t .
I n a r em ot e client , w e can use t h e UserTransaction t o execut e m u lt ip le rem ot e calls w it hin one t r ansact ion. A t r ansact ion obj ect is r et r ieved fr om t he init ial cont ex t u sin g t he JNDI n am e of jta/usertransaction: •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt wtx it h W r e St udio Appl ica ti on De v el oper UserTransaction = ebSphe (UserTransaction)initialContext. By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
lookup("jta/usertransaction"); Pub lish er: I BM
tx.begin();
Pub Dat e: April 1 1, 20 03
// starts a transaction
I SBN: 0- 73 84- 26 09 -1
......
Pages: 75 0
tx.commit();
// ends a transaction
Programming restriction Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
I m por t a nt
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat or trhe ev selopm en t ofbap plicat ion s based onv tar he er pr Jav aBeans Theion EJBDeveloper specificatfion estdrict a session ean t o declar e st at ic iabEnt les. Thise e r eason is ( EJB)t hat ar chstitat ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Servher. ic v ariab les ( class v ariab les) ar e m aint ained w it hin t he cur r ent JVM. I f anot er EJB of t he sam e t y pe u ses t h e same st at ic field inside an ot her cont ainer r un ning in a Thr oug book , wteh pr id evalue examof ples simp ing ionefor w ite,h w ane difhout fer entthe m achine, enov t he t hebased st at ic on v araiab le wleillbank be dif ferapp en tlicat . Ther un derly ing r elat ional dat ab ase. ar e for ced t o use inst ance v ar iables for caching t h e hom e obj ect . I n Part 1, we in t r oduce EJBs p art of a ilit 2 En t er p rise it ion ( J2cache EE) and When using an ex t er nal as JavaaBean asJav a ut y class, w eEd can t h en t h ecover hom et he in basic a concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he st at ic v ariab le. Th er efor e, t he hom e is r et rieved by t he ut ilit y b ean only on ce in each EJB 2. 0 spand ecif icat t hat pr ov id e,inst forances ex am use ple, ten ion alitI ny our for cont ain er - m anag ed JVM all tion he facade b ean h ehanced same r fun eferctence. ex am ple, each per sist ence ent it y b ean s and m essagedr iv en beans. We also pr ovid e best pr act ice guidelines f or bean inst ance has t o r et riev e a separ at e r ef erence t h e fir st t im e t h e client inv okes a successful imp lem ent at ions of EJBs. m et hod. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Using access beans Access beans are an I BM ex t ension t o t h e EJB pr ogr amm ing m odel t o m ake client pr ogr am min g simp ler an d t o im pr ov e perf or m ance: •
Table of Content s
The codIendex t o access t h e hom e int er face can be g en er at ed in t o a fact or y class. Th e hom e face r et aut omat ically asica soon asvael oper clien t cr eat es or find s a b ean inst ance. EJB 2 . 0int D er e ve lopmis e nt w riev it h Wed ebSphe r e St udio Appl ti on De •
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
A d at a class can cache t h e at t r ibut es of an ent it y b ean f or fast access w it hin a client . All or a su bset of en t it y bean at t r ibut es ar e r et r iev ed an d cached at t h e client w hen t he access Pubbean lish er: is I BM creat ed or at t he f ir st at t rib ut e access. The client decides w hen dat a t hat has b een Pub Dat e: ed Aprilis1 1, 03 onized w it h t he EJB con t ain er . This t ech niqu e sim plif ies t r ansact ion chang sy20 nchr mI SBN: anagem 0- 73 84ent26by 09 -1 t he client . Pages: 75 0
The d at a class access bean is a d at a t r ansf er ob ject t h at is m ov ed b et w een t he client and t h e EJB cont ainer . The fact or y and dat a class ar e g en er at ed by App licat ion Developer . Ther e is also an older st yle of an access b ean called copy h elper t hat is generat ed by I BM VisualAge f or Jav a ( and op t ionally by App licat ion Dev eloper ) . Cop y helper f unct ions ar e sim ilar t o t he com binat ion of dat a classes and fact or y . Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
IDeploy m porEJBs t a ntt o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio We d o not cover t h e det ails of access beans in t h is ch apt er. Refer t o Ch apt er 15 , " EJB App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans access beans" on p age 5 51 for concept u al det ails abou t t h e diff erent access beans an d ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. how t o cr eat e access beans in App licat ion Developer . Refer t o " GUI client using access beans" age ,63w4e for anid exam p leples of abased clienton t hat usesleanbank access bean. Thr oug hout ton he pbook pr ov e exam a simp ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Using a session facade to entity beans We h ave seen h ow client s can access ent er pr ise beans dir ect ly or by u sin g access b ean s. We saw t hat using access beans sim plifies t h e client cod e and accelerat es it s ex ecu t ion. •The t rad e- of fTable of p Content of sim licit y sand speed is f r eedom . Using access beans lim it s t he f lex ibilit y of t h e •design and r est I ndex r ict s t he d ev eloper fr om u sing ot h er t echn iques. I n t his sect ion, w e sh ow how EJB EJB 2 . 0 app D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ead. ti on De v el oper client licat ions can use session beans inst By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Facade beans Pub lish er:session I BM Pub Dat e: April 1 1, 20 03
A com m on appr oach I SBN: 0- 73 8426 09w -1h en designin g EJB app licat ions w it h ent it y and session beans is t o u se a facad ePages: in f r ont of ent it y beans t h at p rot ect s t h e per sist ent dat a lay er and cont r ols all t h e client 75 0 access. Such a f acad e can b e built using a st at eless session bean t hat pr ovid es: Hom e int er face—create m et hod of t h e session b ean Com pon en t int er f ace—Business m et h ods f or t he client Lear n about t he EJB 2 .0 specif icat ion Dev elop - Busin EJBsess w it h m St et hod u dios App of en licat t it yion beans, Dev eloper f or exam ple t he deposit m et hod of t h e Account bean is m ade av ailab le as a deposit m et hod of t h e session b ean . Deploy EJBs t o W eb Sp her e Applicat ion Ser v er - New business met h ods t h at com bine ent it y bean m et hod s, for ex amp le a transfer This I BM Redbook d et ailed in for ionokon ow t o ef fect use WebSp here m et hodprinovides t h e session b ean t hm atatinv es hwithdraw anively d deposit m et hod s inSttudio wo App licat ion ent Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans it y b ean s. ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. - New business met h ods t h at w r ap en t it y bean find er m et hod s and f orm at t he Thr oug hout r tethe book w eit yprbov id es exam ples based simp le tbank ing app licat ion w it h an riev ed ,ent ean int o usable dat a on for at he clien . un derly ing r elat ional dat ab ase. The ad van t ages of using f acad e session b ean s ar e: I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he er sist ce tmod el ov is id hidden he client . En t itfun y beans ar ey vfor iewcont ed as al- ed EJB 2.The 0 sppecif icaten ion hat pr e, for fr exom amtple, en hanced ct ion alit aingener er - m anag puence rp ose and ar e not dr v isible t o client licat per sist entdat it yabsou eanrces s and m essageiv en beans. Weapp also prions. ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. Session beans can t ie t oget her m ult ip le dat a sou rces and act as t he single p oint of ent ry t o t he2,business dat a lay . p le bank ing applicat ion and t hen imp lem ent en t it y beans, session I n Part we in t r oduce t h eersam beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Cent of tdif ranfer sact and y is delegat ed pt o h e session im plem entr alized fin der cont m et rol h ods, en ions t m app in gsecur st r atitegies, and sim le tclient s t h at fuacad se t e helayer EJBs.anAtd m oree ef ficien ly for t he app ion. t he enmd,anaged w e d escrib how t o tdeploy EJB aplicat plicat ion s t o a WebSph er e App licat ion Serv er. Figur e 7- 3 illust r at es how session beans can act as a f acad e layer t o ent it y beans in f ron t of client s.
Figu r e 7 - 3 . Se ssion f acad e t o e nt it y b ea ns
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM
Facade session design Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
The f acade session bean can be easily built usin g t he EJB dev elop ment t ools in Applicat ion Pages: 75 0 Dev eloper . We r ecomm end t hat st at eless session beans be used , because t hey ar e light w eigh t and pr ov ide b et t er p er f orm ance. For a session bean t o serv e as a facade t o ent it y b ean s, it shou ld h ave t hese k ind of m em b er s: obj e — Th ar2e.0private f ield LearHnom about t heese EJB specif icat ions t hat cach e t he h om e of t he ent it y b ean s( s) t hat t he session b ean w ill serv e ( inst an ce v ar iable) . Caching t h e hom e obj ect r ef er ence im pr ov es it hTh Steu session dio App licat Devldeloper tDev he elop perf orEJBs manwce. b eanion shou also h ave a getObjHome m et hod t h at p er for m s a lazy init ializat ion an d ret ur n s t he h ome object . Deploy EJBs t o W eb Sp her e Applicat ion Ser v er En t it y dat a is not cach ed ; st at eless session beans do not carr y an y volat ile d at a and an y This Iinst BM ance Redbook pr ovides d et ailed m at ion est on .h ow t o ef fect ively use WebSp here St udio can ser v e a clien t f or aingfor iven requ App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) cre ar ch epis loym ent of su chhod ap plicat s tao cr a eat WebSph e App ion Serv atiteect Obujre, e ct an — dThdis a public m et t h at ion calls e m eter h od on licat t h e hom e of er. the ent it y b ean , such as objHome.create(...). Su ch m et hods mak e ent it y b ean cr eat ion Thr oug het obook w e pr. ov id e exam based on a ion simp le bank appit licat ion wisit h an e t he avhout ailabtle t h e ,client Howev er, inples m any ap plicat s creat ion ing of ent y beans insid un derly ing r elat ional dat ab ase. bu sin ess m et hods of t he session bean. I n Part 1, we t rs— oduce EJBs a p artmofetJav Encalls t er p rise Ed it ion ( J2 EE)ofand he basic fi ndO bj einct Th is is a as public hoda t2h at a f inder m et hod t he cover ent it y t bean. Th e concep t s and e ar it ect urs e. I npack par taged icu larinwt oe apoint t he n ew nctt oions odned uced it h client t he . r esult in gt hent it ychobject ar e dat aout t r ansfer objfu ect b e int r etrur t o wt he EJB 2.The 0 spclient ecif icat id e,t he forent exitam hanced fun ct ion alit y for cont ainit er m anag ed s shion ouldt hat n evpr erovsee y bple, eanen it self . In m any app licat ions, ent y -bean f inder per sist entin it side y b ean essagedr ivs en We also pr ovid e best pr act ice guidelines f or arence e used t hes and b usinmess m et hod of beans. t he session bean. successful imp lem ent at ions of EJBs. bu si ne ssMe t hod — Th is is a public m et hod t h at p rov ides t he real ap plicat ion f unct ion t o I n Part we in. tTh r oduce t h e sam p le bank ingcrapplicat ionf inder and tm hen imp lem en teit en y beans, session t he2,client e bu siness m et hods u se eat e and et hods t o rent et riev t it y bean beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also inst ances, and get t er and set t er met h ods t o m anipulat e ent it y bean d at a. im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Using an access bean for the session facade We can comb in e t he session f acad e m odel w it h access beans by usin g a Jav a w r app er f or t h e session b ean . How ever , t his ap pr oach is q uest ion able and t h er e is h ard ly a r eason t o w rap a session b ean int o an access bean. St at eless session b ean s do not car r y any d at a an d t herefor e t her e is not hing t o cache in a d at a class.
Client comparison Aft er p r esent ing t he diff er ent w ays of accessing ent er pr ise b ean s fr om a client app licat ion ( a ser v let in our ex amp les) , let 's now see t he ad van t ages and d isadv ant ages of using t hem . •
Table of Content s
•
I ndex
Direct access EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
This is t he st r aig ht f orw ar d w ay for a clien t t o access an en t er pr ise bean. The hom e of t h e bean should be cached in a static v ar iable and b e init ialized in t he init m et hod of t h e ser v let . This Pubgood lish er: Iper BM for m ance. Howev er , t he r esu lt of each m et hod inv ocat ion p er for m ed by t he client giv es Pub Dat e: April is a rem ot e call.1 1, Ev20 en03if t he client is a serv let r unn ing in t he sam e applicat ion ser ver as t he EJB I SBN: 073 8426 09 -1 cont ainer ( sam e JVM) , all t he m et hod calls are rou t ed t h rou gh t he TCP/ I P st ack. Of cour se, t h is has anPages: im pact 75 0 on t he per for m ance of t he applicat ion.
Pros and cons The ad van t ages of using t h is appr oach ar e: Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs Fast t o cod e w it h St u dio App licat ion Dev eloper Deploy o Weeb Spr aher e Applicat No n eedEJBs t o crt eat ext com pon en t sion Ser v er This INo BMex Redbook ovides d eton ailed for m at ow t o ef fect ively use WebSp here St udio t r a load pr gener at ed t he inclient orion seron v erh side App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans er uprre, isean bean imloym p lement entof at ion not m odified ( EJB) The ar chent it ect d d ep su chis ap plicat ion s t o a WebSph er e App licat ion Serv er. The d isad vant he t ages ar,e:w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Thr oug hout book un derly ing r elat ional dat ab ase. Poor p er in fort rmoduce ance EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic I n Part 1, we concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he esicat EJBion pr ogr t h eexclient code EJB 2.Requ 0 sp ir ecif t hatam prm oving id e,infor am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or The client d ev eloper has t o k now h ow t o use EJBs successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an dbeans m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Access im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At tThis he en w e tderescrib t o deploy plicat Th iones hom t o a eWebSph er einApp ion Serv er.an d f or is d, a bet w ay et ohow access en t erpEJB rise ap beans. is cached t h elicat access bean, dat a classes t he p rop er t ies of t he bean ar e cached as w ell. Th is imp rov es t he perf or man ce by elim inat in g m an y rem ot e calls. Howev er , t he dev elop er sh ould alw ay s be car efu l w hen usin g t he local cach e of t he b ean pr op er t ies, becau se t he bean' s d at a m ay hav e b een upd at ed in t he m ean t im e by an ot her app licat ion. The n um ber of t h e rem ot e m et hod calls over t h e net w or k is decr eased gr eat ly, as t he access beans perf or m all t he get or set m et hod s in a single call. For ex am ple, if w e assu me t hat a cust om er bean h as 10 pr oper t ies, ret r iev ing t hem g en er at es only on e r em ot e m et hod call inst ead of 1 0. How ev er , t h is requ ires t h e m odificat ion of t he ent er pr ise bean im plem en t at ion, because get t er / set t er m et hods for t he dat a class ar e ad ded t o t he bean.
Pros and cons The ad van t ages of using access beans are: Very good perf or man ce • •
of Content s Sim ple Table t o use I ndex
EJB 2 . 0The D e ve lopm ednt it h W ebSphe e St udio ti on Deann v el oper client evweloper uses rEJBs in Appl t he ica sam em er as
Jav aBean s
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
The d isad van t ages ar e: Pub lish er: I BM
Pub April pon 1 1, 20 ExDat t r ae: com en03t s
hav e t o be cr eat ed
I SBN: 0- 73 84- 26 09 -1
The ent Pages: 75er 0 pr ise bean im p lem ent at ion is m odified Rest r ict iv e desig n t hat does not allow use of ot her m odels You hav e t o use access beans f or all t h e beans in an applicat ion
Lear n about t he EJB 2 .0 specif icat ion
Facade beans
Dev elop EJBs w it h St u dio App licat ion Dev eloper Facad e beans ar e an alt er nat ive appr oach t o access b ean s. I t is app licable w hen accessin g ent it y Deploy t o W eb her e Applicat ion v ersession bean t hat cach es t he hom e an d beans. Th e enEJBs t it y bean is Sp accessed v ia a st at Ser eless pr ov ides access t o all t he p rop er t ies of t he b ean in a sing le met hod . This also d oes not requ ire This I BMificat Redbook et ailedasinrfor m ated ionw hon ow t o access ef fect ively use WebSp here St udio t he mod ion of pr t hovides e beandclass, equir enh using beans. App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (The EJB)p er arfch it ect d dver ep loym enther of e, su ch ap plicat ion s er t o of a WebSph er.it ed . orm an ucere, is an also y good sin ce t he nu mb t he r em er ot e App m et hlicat od ion callsServ is lim Act ually, all t he met hod calls bet w een t he facade b ean and t h e en t it y bean ar e m ade locally Thr ouggh hout book w e, pr ov idbeot exam bank ing app licat ion w it h an ( t h rou t het he local p ip, es) since h of ples t hembased ar e inon t h ae simp samelecon t ainer. un derly ing r elat ional dat ab ase. How ev er , t he f acade app r oach r equir es t he cr eat ion of a session bean for each set of en t it y I n Partwe 1, access we in t r oduce as ion. a p art Jav a ires 2 Enmt er it ionent ( J2tEE) t her esou basic beans in on e EJBs applicat Thof is requ orperise devEd elopm im e and and cover syst em rces concep s and e arserv ch iter, ect becau ur e. I nsepar t icu we point outint gheinn tew nct ions .int r od uced w it on t he tap plicatt hion mor e lar b ean s ar e r unn h e fu con t ainer Addit ion ally , ht htehe EJB 2.code 0 sp ecif t hatple pras ov id ex amaccess ple, enbeans, hancedb ecause fun ct ionaalit y for cont aininst er - ance m anag client is nicat ot ion as sim w e, henforusing session bean m ed ust perusist it ymbet ean s and be sedence for ent each hod call.m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. How ev er , t he cod e here can be sim plified by u sing an ext er nal Java class ( a Jav aBean ) t h at Ir n 2, an wed in t r oduce e sam p leerbank ion andinst t hen impThis lem ent en t it y tbeans, session et Part riev es caches t h et hhom e int face ing in aapplicat class var iable ead. incr eases he beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also per for m ance and enables code r euse, sim plif ying t h e dev elopm ent of all t he facade b ean s t hat im ent fin et h dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At ar eplem needed f order t h emen t itods, y beans. t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Pros and cons The ad van t ages of using f acad e beans ar e: Very good perf or man ce Op en design t o ot h er mod els The ent er pr ise bean im p lem ent at ion is not m odified
The d isad van t ages ar e: Ex t r a d ev elopm en t t im e Mor e load in t he EJB cont ainer •
Requ ir es EJB pr ogr am m ing in t h e client code Table of Content s
•
I ndex We b elieve t hat usin g facade session beans t o en t it y beans is a bet t er ar chit ect ur e appr oach t han EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper using access beans. Th is w ay w e hav e bet t er cont r ol ov er t he persist en ce dat a lay er and w e By Ueli Wahall li, Wout Denay Lar s Schu , Deborah Shaddon , Mart m anage t he ersecu r iter, y and t he nk t ran sact ion issues in ina Weiss m or e eff icient and or ganized man ner . Pub lish er: I BM Pub Dat e: April 1 1, 20 of 03 approaches Recapitulation I SBN: 0- 73 84- 26 09 -1 0 on st r at es clien t access t o ent it y beans: Figur ePages: 7- 4 d75em
Figu r e 7 - 4 . Cl ie nt com pa r ison
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, d ly m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Diran ect im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he enThr d, oug we h d escrib e howb ean t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. an access Thr oug h a session f acade bean The client side in t h e figu re repr esen t s t he ap plicat ion serv er w her e t he ser vlet eng ine r esides. On t he ot her han d, t h e ser v er side repr esen t s t he ap plicat ion serv er w her e t he EJB cont ain er r esides. Alt hou gh t hese t w o com ponent s sh ould be in t he sam e app licat ion ser v er for per for m ance r easons, h er e w e assum e a mor e g en er al case t hat can occur wh en , f or ex am ple, a fir ew all is placed b et w een t h e pr esent at ion an d b usiness log ic for secur it y r easons.
Not es This com par ison ap plies also wh en t h e ser v let en gine r esid es in t h e same app licat ion ser v er wit h t h e EJB con t ainer. As w e m en t ioned b ef or e, all m et h od calls bet w een a serv let an d an EJB ar e r out ed t hr ough TCP/ I P. • •
The access bean show n in Figur e 7- 4 is a copy h elper b ean t h at encapsulat es t he Table of Content s RMI - I I OP access. W hen u sin g a dat a class, t he access bean it self is sh ipped ndex betI w een clien t and EJB cont ainer .
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
hou t heer,clien t is show n as Shaddon a ser v let , itincould By Ueli Wah li,Alt Wout er gh Denay Lar s Schu nk , Deborah , Mart Weiss
also be a st an d- alone client , f or
exam ple r u nning in a J2 EE client cont ainer . Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
WhatPages: client 75 0 type to chose? Use t he gu idelin es g iven in Table 7 - 1 w h en deciding how t o d esign y our clien t ap plicat ion .
Lear n about t he EJB 2 .0 specif icat ion
Tab le 7 -1 . W ha t cl ie nt t y pe t o choose
Dev elop EJBs w it h St u dio App licat ion Dev eloper Cli ent t y peEJBs t o W eb Sp Y es, he n ion Ser v er Deploy her ew Applicat
N o, w h en
D ir ect Sim ple EJB m odel is used En t it y bean con t ains m any p r opert ies This a cceI BM ss Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer Perf or man f or ce t heis dnot ev elopm cr it ical en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Access Sim ple and f ast dev elopm ent is Mor e com plex desig n m odel is r equ ir ed beoug a nshout t hedesirab Thr book , le w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. The ap plicat ion serv er has lim it ed Clust er ed env iron m en t w it h m any syst em r esou rces licat v er s isand u sed I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er app p rise Edion it ionser ( J2 EE) cover t he basic concep t s and t hJav e ar ect er ur e. parfor t icuaccessing lar w e point out t he n ew fu nct ions int r od uced w it h t he a ch w ritapp is Iunsed EJB 2. 0 sp ecif icat ion t hat prsov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed session b ean per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or Faca de imp lem Scalabilit y is cr ical Client has t o access session b ean s successful ent at ions ofitEJBs. be a ns en design is applied Dev elopm t im e is I n Part 2, we inOp t r oduce t h e sam p le bank ing applicat ion and t henent imp lem entcrit enical t it y beans, session beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Bet t er over all per for m ance is im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At r eq uir ed t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Home factory pattern I n t he EJB pr ogr am m ing m ode, client s requ ir e access t o t he hom e int er face of EJBs b ef or e accessin g t he EJBs. Th er efor e, ever y client is f aced w it h codin g t he r et riev al of hom e int er faces t hr ou gh t he n amin g serv ice. •
Table of Content s
•The id ea of t he I ndex h ome fact or y is t o cent r alize t his cod e and w rit e it only on ce. EJB clien t s t hen use EJB . 0 D eevefact lopm h W ebSphe St udioofAppl ti on De v el oper t he 2hom oreyntt w o itaccess t he rhe ome anyicaEJB. By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Benefits Pub lish er: I BM Pub Dat e: April 1 1, 20 03
The h om e fact y 26 pat er n pr ov id es t he f ollow ing b en ef it s: I SBN: 0- 73or 8409t-1 Pages: 75 0
I nsulat e EJB client s fr om n amin g ser vice comp lex it y . Cache n am in g con t ex t cr eat ion and EJB hom e looku p t o achieve b et t er perf or man ce. Pr ovid e an in t erf ace for cr eat in g rem ot e and local EJB hom es w it hout specif yin g t he m et hod Lear nt oabout t he 2 .0 specif icat ion used r et riev e tEJB he concr et e class at r unt im e. elopisEJBs it h St diohom Appelicat eloper This pDev at t ern also wk now n uas f action ory Dev or hom e caching. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Motivation This I BM Redbook
pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) earinv Befor chok it ect in gu re, an an EJB's d d ep b usiness loym entmet of hod su ch , aapclient plicatmu ionst s tcreat o a WebSph e or finderan e App EJBlicat objion ect Serv for t her. at bean. To cr eat e or find an inst ance of a bean's EJB object , t h e client m ust : Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. Locat e an d cr eat e an EJB hom e obj ect for t h at b ean . I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Use t he EJB hom e ect ob ur ject eat e or in stout ance he fu b ean 's EJB . concep t s and t h e ar ch it e. tIon cr par t icu larf ind w e an point t heofntew nct ions intobj r odect uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed JNDI isence usedent t o itfind t h esEJB e obj ect byennam e. We alralso eadyprdovid escrib ed t he basic ogr am min g per sist y b ean andhom m essagedr iv beans. We e best pr act ice pr guidelines f or tsuccessful echniqu es imp for lem cr eat in g an init ial con t ex t ( see " Ob t ain an init ial n am in g con t ex t " on p age 25 4) ent at ions of EJBs. and r et riev in g t he h ome ( see " Look ing up an EJB hom e f rom t he init ial cont ex t " on p age 25 5) . I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session Get t ing an access t o EJB esbeans is comusing plex , WebSph r eq uir eserinet er rocess com m unDev icatelop ion , er and is dif fer en t beans, d m essag e- dhom r iv en St-updio Ap plicat ion . We also for plem rem ent ot e an local Im plem in ginaghom f act or yand simsim plifies t he p sr ocess. im fin dder m ethhomes. ods, dif fer en t ent m app st r ateegies, p le client t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Applicability Use t he hom e f act ory pat t er n w h en : The r esult of EJB h omes look ups sh ould be cach ed f or per for m ance r easons. A client shou ld b e config ur able t o access diff er ent set s of classes, im plement in g an EJB w it h t he sam e EJB hom e JND I nam e.
The client sh ould b e independ en t of h ow t he EJB h om es ar e r et r iev ed ( n ar row met h od or cast ) . The f ir st bu llet h as pr ov en t o be a m andat or y r equir ement in any r eal- lif e applicat ion.
Structure •
Table of Content s
• The st r uct u reI ndex of t he hom e f act ory pat t er n is show n in Figur e 7- 5 : EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Figu r e 7 - 5 . Hom e f act or y pat t e r n st r uct ur e
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2.A0sing sp ecif le in icat st ion ancet hat of HomeFactory pr ov id e, for exisam cr eat ple,edenat hanced r unt imfun e. ct This ion HomeFactory alit y for cont ain cr er eat -m esanag EJB ed per sist ence ean sinand m essagedr ivmen beans. alsop le, pr ovid e ean bests pr ice guidelines hom es ent as itdyefbined an ex t er nalized ann er , f orWe exam by m of act r esour ce bu ndlesf or successful imp lem ent at ions of EJBs. XML files. I n Part HomeFactory 2, we in t r oduce u ses t thhe e sam m etpadat le bank a st or ingedapplicat in t h is ion ex t er and nalized t hen m imp ann lem er ent for en inst t itan y beans, t iat in g session and beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also r et ur ning t he appr opr iat e EJB hom e class. im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Consequences The h om e fact or y pat t er n has t h e follow ing benefit s: I t pr om ot es consist ency am ong client s— W hen n am in g con t ex t and EJB hom es are accessed in t he sam e w ay, it is easy t o m od if y and / or enhan ce t he level of serv ice pr ov id ed b y t he fact or y . The su ppor t of n ew k ind s of EJB hom es is t ran sp arent — I f specif ied ex t er n ally , EJB hom es and t h eir concr et e classes can b e added and m ade av ailable w it hout eff ort .
I t isolat es t he EJB h om e act u al classes— Th e fact or y encapsulat es and as such isolat es client s fr om act ual EJB hom e concret e classes, as long as t h ose con cr et e classes ar e not needed f or u sag e ( t hat is, none of t heir sp ecific m et hod s ar e called) .
Implementation •
Table of Content s
A p ur e im plem ent at ion of t h e hom e fact or y w it h ext er nalized m et adat a in an XML f ile is quit e • I ndex com plex . EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Singleton factory Pub lish er: I BM Pub Dat e: April 1 1, 20 03
A sing let on f act or y inst an ce en su res t h at on ly one inst ance of t h e fact or y class is cr eat ed, and I SBN: 0- 73 84-ance 26 09 -1 t hat t his one inst is accessible t o all client s. Client s get access t o t his inst an ce b y calling t h e Pages: 75 0 st at ic getSingleton m et hod , r at her t h an b y const ru ct in g t he inst an ce t hemselves. Ther ef or e, t he con st r u ct or s ar e private ( be sur e t o declar e at least one pr iv at e const r uct or; ot her w ise, a defau lt p ublic const r uct or is aut omat ically gener at ed ) . The sk elet on code of t h e HomeFactory class is show n in Figur e 7- 6 . EJB FiguLear r e 7n -about 6 . Fat he ct or y 2a.0s specif si ngicat le tion on Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er public class HomeFactory { This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio static HomeFactory singleton null; Appprivate licat ion Developer f or t he d ev elopm en t of ap = plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. private HomeFactory() { Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derlysuper(); ing r elat ional dat ab ase. } 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic I n Part concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he static getSingleton() { EJB public 2. 0 sp ecif icat ion HomeFactory t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or if imp (singleton == null) successful lem ent at ions of EJBs.singleton = new HomeFactory(); I n Part return 2, we in tsingleton; r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem } ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. ... }
XML configuration data Figur e 7- 7 sh ows t he st r uct ur e of an XML file t h at h old s conf ig ur at ion dat a of EJB h omes. Alt ern at ives t o XML ar e en vir onm ent pr oper t ies, r esou rce b und les, and p rop er t y f iles.
Figu r e 7 - 7 . I n f or m a t i on in XM L f or m a t
java.naming.factory.initial= •
Table of Content s
•
I ndex
com.ibm.websphere.naming.WsnInitialContextFactory
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss java.naming.provider.url=iiop://NameServer:900
Pub lish er: I BM
Pub Dat e: April 1 1, 20 03
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
ejb/itsobank/Banking Lear n itso.bank5.session.BankingHome about t he EJB 2 .0 specif icat ion javax.rmi.PortableRemoteObject.narrow Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase.
Externalize I n Part 1, we information in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. sp ecif ionant hat ov id e,uses for ex enJNDI hanced ct ion y for cont ainect er - m ay anag ed The p r0opert iesicat t h at EJBprclient t o am in itple, ialize andfun find analit EJB hom e obj v ary per sist ence ent itery im b ean s and mion essagebeans. also pr ovid e best act bet ice wguidelines across EJB serv plem ent at s. To dr m iv aken e an en t erWe pr ise bean m ore p or tpr able een EJB f or successful imp ent lemat ent at ions EJBs. ser v er imp lem ions, w e of h ave t o ex t er nalize t h ese p rop er t ies in en vir onm ent var iables, XML files, pr oper t ies files, or resour ce bun dles, rat h er t han har dcode t hem int o t he EJB client code. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, m essag r iv en er e St u dio Apnam plicat Dev elopfer Wer also On e of an t hed issues w eed face is tbeans h e EJBusing hom eWebSph fu lly qualified class e ion t o be used or. nar owin g t he im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At r esult of t he look up . Figur e 7- 8 sh ows how t o r ead XML conf ig ur at ion dat a. t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Figu r e 7 - 8 . Load ing e xt er n al iz ed in f or m a t i on ( e xt r act )
private static ConfigurationData configuration = null;
public static ConfigurationData getConfigurationData() { if (configuration != null) return configuration;
configuration = new ConfigurationData(); // obtaining an input stream and a DOM parser ...... Document document = parser.readStream(inputStream);
•
Table of Content s
•
I ndex
NodeList nodeList = document.getElementsByTagName("entity-bean");
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
By Ueli Wahlength li, Wout er = Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss int nodeList.getLength();
for Pub lish(int er: I BM i
= 0; i < length; i++) {
Pub Dat e: April 1 1, 20 03
EnterpriseBeanMetaData enterpriseBeanMetaData =
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
new EnterpriseBeanMetaData();
enterpriseBeanMetaData.readElement((Element) nodeList.item(i)); Class homeClass = enterpriseBeanMetaData.getHomeClass(); Lear n about t he EJB 2 .0 specif icat ion ConfigurationData.setEnterpriseBeanMetaData(homeClass, Dev elop enterpriseBeanMetaData); EJBs w it h St u dio App licat ion Dev eloper }
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio configuration; Appreturn licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. } Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. ICaching n Part 1, we in t r oduce EJBs as a pEJB art ofhomes Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic of initial context and concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed The in it ial con t ext is a sing le object , w her eas EJB hom es are cached in a hash t able w it h t he per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or JNDI n am es as key s. The p oin t is t h at w e can t ak e adv ant ag e of t he m et a in for m at ion st or ed in successful imp lem ent at ions of EJBs. t he XML file t hat specif ies t he EJB im plem ent at ion classes for w rit ing a full generic v er sion of t he lookupHome . t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session I n Part 2, weminett rhod oduce beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Figur e 7- 9 sh ows t he sk elet on infor m at ion of t he caching in t he lookupHome m et hod . im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Figu r e 7 - 9 . Na m in g con t e xt a nd EJB h om e ca chi ng
private static Hashtable homeCache = new Hashtable(); private static InitialContext initialContext = null; ... public static EJBHome lookupHome(Class aClass)
throws RemoteException { try { EJBHome home = (EJBHome) homeCache.get(aClass); if (home != null) { •
Table of Content s
return home; I ndex
•
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
}
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
ConfigurationData aConfigurationData = getConfigurationData(); Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
EnterpriseBeanMetaData enterpriseBeanMetaData = Pages: 75 0 aConfigurationData.getEnterpriseBeanMetaData(aClass);
ifLear (initialContext null)icat initialContext = new n about t he EJB 2== .0 specif ion InitialContext(enterpriseBeanMetaData.getJndiProperties()); Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er ThisString I BM Redbook pr ovides = d etenterpriseBeanMetaData.getJndiHomeName(); ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio lookupString App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d=d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Object anObject initialContext.lookup(lookupString); Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. Method narrowMethod = enterpriseBeanMetaData.getNarrowMethod(); I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep s and t h e ar ch it ect e. I n par if t(narrowMethod ==urnull) { t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence y b ean s andanObject; m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or homeent = it(EJBHome) successful imp lem ent at ions of EJBs. } else { I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Object[] parameters = new Object[] {anObject, aClass}; im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. home = (EJBHome) narrowMethod.invoke(null, parameters); } if (home == null) { return null; } homeCache.put(aClass, home);
return home; } catch (Exception ex) { ex.printStackTrace(); return null; •
Table of Content s
•
I ndex
}
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Concurrency Pub lish er: I BM
Pub Dat e: April 1 1, 20 03
Mu lt ip le client t hr eads m ay use t h e fact or y at t he sam e t im e. We assu me t hat it is bet t er t o let SBN: 0- 73 84- 26 09 -1 client s I en gage in look ing up t he sam e EJB hom e concur r en t ly, and t hen upd at e t he sam e ent r y Pages: m ult iple t im75 es0 in t he hash t able ( w it h t h e sam e v alue, as ex pect ed ) , r at her t h an serializing access t o a m et hod or a resour ce ot her t han t h e hash t able sy nchr onized put m et hod .
Known uses Lear n about t he EJB 2 .0 specif icat ion This w or k w as b ased on t he Fr eeside dem o ap plicat ion ( WebSp here Ap plicat ion Ser ver sam ple app licat ion) onwt he e fact orlicat y dev for t he I TSO bank ing app licat ion in t he r ed book Dev elopand EJBs it h hStom u dio App ioneloped Dev eloper En t er p rise JavaBeans Dev elopm ent Using VisualAge for Jav a , SG2 4- 5 429. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides a d etmodern ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Sample application: home factory App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. For t his r edbook , w e im plem en t ed a hom e f act or y w it h out caching . Becau se t he WebSp here App licathout ion Ser er pr ,ovwid caching of hples omes au t omat ionlevel caching no Thr oug t hev book e es pr ov id e exam based on aically simp, leapplicat bank ing app licat ion w it hisan longer r equir ed. un derly ing r elat ional dat ab ase. II n ion, n EJBEJBs pr ogr in g of t echn est tEd heituion se (of efercover en ces tthe o look n addit Part 1, wemod in t rer oduce asam am p art Jav aiques 2 En sugg t er p rise J2EJB EE) rand basicup hom e int er faces. The h ome fact or y could be exp anded t o sup por t EJB r efer en ces, bu t caching concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he becom appr opr iattehat because m ult r efer enen ceshanced can point t he EJB 2. 0esspinecif icat ion pr ov id e, foriple ex am ple, fun ctt oion alitsam y fore EJB. cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
Design I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- dst r iv using WebSph er eenStt uadio Ap plicat ion Dev elop er . We also Based on t he reasons aten ed,beans w e decided t o im plem m odern h ome f act or y class t h at : im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Pr ovid es access t hr ough EJB r ef erences only Ret ur ns t he local or r em ot e hom e int er face ( EJBLocalHome or EJBHome) Caches t h e init ial cont ex t b ut n ot EJB h omes Figur e 7- 1 0 sh ows an ex t r act of t he getHome m et hod of t h e HomeFactory class of our sam ple app licat ion.
Figu r e 7 - 1 0 . Sam pl e ap pli ca t ion h om e fa ct or y
public Object getHome(String ejbRef) throws NamingException { Object nsObject = initialContext.lookup (new StringBuffer("java:comp/env/").append(ejbRef).toString()); •
Table of instanceof Content s if (nsObject EJBLocalHome) {
•
I ndex
EJB 2 . 0 DSystem.out.println("ejbRef e ve lopm e nt w it h W ebSphe r e St udio Appl " +icaejbRef ti on De v el+oper "
is a local reference.");
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
return nsObject; Pub lish er: I BM
} else {
Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84-ejbHome 26 09 -1 EJBHome
= (EJBHome) javax.rmi.PortableRemoteObject.narrow
Pages: 75 0
( (org.omg.CORBA.Object)nsObject, EJBHome.class ); System.out.println("ejbRef " + ejbRef + " is a remote reference."); return ejbHome; Lear n about t he EJB 2 .0 specif icat ion } Dev elop EJBs w it h St u dio App licat ion Dev eloper } Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans Client ( EJB) arusage ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. A client Thr oug hout can tuse he book t h is cod , w e pr t o ov access id e exam a r em ples ot ebased h om eon intaerf simp ace:le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic try { t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he concep EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed CustomerHome = dr(CustomerHome) per sist ence ent it y bcustRemoteHome ean s and m essageiv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. HomeFactory.singleton().getHome("ejb/CustomerRemote"); I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an (NamingException d m essag e- d r iv en beans WebSph er e St u dio Ap plicat ion Dev elop er . We also } catch ex) using { im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he ex.printStackTrace(); en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. throw new EJBException("Error looking up homes: "+ex.getMessage()); }
A client can use t h is cod e t o access a local hom e int er face:
try { AccountLocalHome accountHome = (AccountLocalHome) HomeFactory.singleton().getHome("ejb/Account"); } catch (NamingException ex) { • •
Table of Content s
ex.printStackTrace(); I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
throw new EJBException("Error looking up homes: "+ex.getMessage());
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
} Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
Details Pages:
75 0
See " I m plem ent ing t h e hom e f act or y " on p age 39 4 for d et ails on t h e hom e fact or y d ev elop ed f or t his r edb ook.
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Summary I n t his chapt er , w e p resen t ed t h e possible w ays a clien t app licat ion can access an d use ent er pr ise b ean s. We descr ibed all t he st eps t hat hav e t o b e follow ed an d in clu ded sm all pr ogr am m ing ex am ples. •
Table of Content s
•We p rov ided I a ndex shor t discussion on access b ean s and t he session f acad e pat t er n, an d we EJB 2 . 0 D e vew lopm w it h W St udio ica tior ony De v el oper concluded it h eantdescr iptebSphe ion ofr et he homAppl e fact pat t er n and an imp lem ent at ion of t he p at t ern . By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Chapter 8. Additional concepts: transactions, exceptions, security •I n t his chapt er Table of w Content s our d iscussion of EJBs by in t rod ucing t he im por t an t concept s of , we r ap up •t r ansact ions,I ndex except ion hand lin g, an d secur it y. EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
All of t h ese ar e v ast t opics in t heir ow n rig ht , an d cou ld w ar r ant t heir ow n chap t ers. How ever , By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss w e simp ly int r oduce y ou t o t h e m ain ar eas her e t o rou nd out you r un derst and ing of EJBs. lish er: BMm at ion, r efer t o t he EJB specificat ion as well as ot her Redbook s. For Pub m ore in Ifor Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Introduction I n t his chapt er , w e int rod uce t he f ollowin g t opics: Tra nsa Table ct i ons— of Content W e descr s ibe h ow t r ansact ions ar e defin ed in t he cont ex t of J2EE and how t r ansactI ndex ions ar e m anaged ar ound ent er pr ise beans. We describe t he t r ansact ion at t r ib ut es t erp erise an d rtehe eff ect t hey e von com mit , rollback , concur r en cy , an d EJB 2 . 0for D een ve lopm nt wbeans, it h W ebSphe St udio Appl ica tihav on De el oper locking . We t h en d escrib e w ays of st ar t ing and end in g t ran sact ions by ent er pr ise b ean s, t he By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss cont ainer , and bean- man aged t ran sact ions. Finally , w e pr ov ide gu id elines f or t r ansact ion m anagem ent . • •
Pub lish er: I BM Pub 20 03 ExDat cee:ptApril i on1 1, h an dli ng—
Except ions are t he cust om ar y w ay in Java t o ind icat e t o a calling mI SBN: et hod 0- 73 t hat 84- 26 an09 abnor -1 m al condit ion has occur r ed. Wh en a met hod encoun t er s an abn or mal condit Pages: ion 75 0 ( an except ion con dit ion) t hat it cann ot han dle it self , it may t hr ow an except ion. Ex cep t ions ar e t h row n at man y dif ferent levels in lar g e applicat ions. We int rod uce t he cust om ary wisdom in d et er m in ing how t o han dle t hr ow n except ions in , and fr om , EJBs. Se curi t y— D ist r ibu t ed ap plicat ion s accessed t hr oug h t he I nt er net f ace t he p ossibilit y of m alicious at t acks. Th is em ph asizes secu rit y ar r ang em ent s an d t he set t ing up of a secu r it y syst em . A secur it y sy st em giv es us t h e pow er t o det er m ine w ho can an d will access Lear n about t he EJB 2 .0 specif icat ion r esour ces such as applicat ions, ser v let s, EJBs, and Web p ages. I t also enab les u s t o d ef ine tDev he elop securEJBs it y policies est App ab lish cont ol ofeloper r esou rces. I n t his chapt er , w e discuss secur ing w it h Stt uo dio licat ionr Dev EJBs and t he p rocedur es inv olved in it .
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Ad dit i ona l de ploy m en t i nfor m at i on— W e hav e alr ead y int r oduced deploym ent ipt or en tpr r ies in t his ook. in I nfor t hm is at sect e p troesent f in al use sumWebSp m ary of deploym This Idescr BM Redbook ovides d etbailed ionion, on hwow ef fectaively here St udioent descr or en t r iesf or f ort he t hedEJBs w it henr esour t ran ion assem , and it yaBeans assem b ly . App licat ionipt Developer ev elopm t of apces, plicat ionsact s based on t hbly e Ent er prsecur ise Jav ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Transactions A t ran sact ion is t he ex ecu t ion of a set of act iv it ies as one un it - of - w or k. Th is unit - of - w or k is a set of act ivit ies t hat r elat e t o each ot her and m ust b e comp let ed t oget her . I f any of t h ese act iv it ies fail, t he ent ir e un it - of- w or k m ust b e un done. •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
A transaction example: banking transfer
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
We ex t en d our ban kin g t ran sf er ex am ple in t he cont ex t of t ran sact ions. Let 's say t h at w e w ant t o Pub lishm er:oney I BM f r om one account t o t he ot h er , as we do in t he transfer m et hod of t h e bank ing t r ansfer Pub Dat e: April 1 1, 20 session b ean . The act03iv it ies t hat mu st be perf or med ar e: I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Wit hd raw t he m on ey f r om one account . Cr eat e a t ran sact ion r ecor d for t h at account . Deposit t he m oney t o anot her account . Lear he EJB .0 specif ionaccount . Cr eatneabout a t rant sact ion 2r ecor d foricat t h at Devsact elopion EJBs w itdhisStim u dio ion Dev The t ran r ecor p ortApp an tlicat b ecause t h eeloper balance of an accou nt is ver ified based on all t he r ecor ded t r ansact ion s. I f t h er e w er e a t r an sact ion t hat had not been r ecor d ed , t hen t he account Deploy EJBs o Wcon eb Sp v er high er or low er , d ep en ding on w het her t he balance w ou ld n ot t be sisther ente. Applicat I t w ouldion beSer eit her t r ansact ion w as a w it hdr aw al or a deposit respect iv ely. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat or each t he daccount ev elopmaft ener t ofw ap on ing t h emon Ent er Javimp aBeans Updat in gion t h Developer e balance ffor it hplicat dr aw ion in g sorbased deposit eyprisise also ort ant (for EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv t he success of ou r oper at ion. A mon ey t r ansfer fr om on e accou nt t o an ot her w ould noter. be com plet e if an y of t hese act ivit ies failed. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un ingpose r elattional datehow ab ase.a m oney t r ansfer is d one t o t he second accoun t , w it hou t act ually Letderly us sup hat som w it hdr aw ing t h e mon ey fr om t h e fir st account . I n t h at case, t he f irst accoun t w ou ld hav e t h e Isam n Part 1, we inand t r oduce EJBs asaccou a p art 2 En t er p rise Edmon it ioney( J2 he basic e balance t h e second nt of an Jav in craeased one. So, w EE) ou ldand n ot cover h ave tcom e out concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he fr om an account , but w ould hav e been deposit ed t o anot her account . The b ank w ould lose EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed m oney. per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful ent at iv ions EJBs. Ther ef or e, imp all tlem hese act it iesofhav e t o be com plet ed t oget h er . Only t hen is a m oney t r ansf er considered successf ul. Th is act ion is k now n as com m it . Th is is w hat m ak es t he set of t hese Iact n Part 2, awe in-t of r oduce e samep8le1bank ivit ies unit - w orkt h( Figur ) . ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At Figu e 8 - 1 .EJB Trap anplicat sf erionms one y t r a nsact i on t he en d, w e d escrib e how t or deploy t o a WebSph er e App licat ion Serv er.
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
lish er: I BM en s if any of t h ese act ivit ies do not succeed? This w ill cau se all t he ot her But Pub w hat happ comPub pletDat ede: act April ivit 1 1,ies 20 03 t o be un done. Th is act ion is k now n as r ollback . . I SBN: 0- 73 84- 26 09 -1
Pr oblems lik e t h is are not u nusual in r eal life. Sy st em s t hat ar e r un ning such business Pages: 75 0 app licat ions are u su ally pr et t y comp lex an d d ist r ibu t ed on sev er al m ach ines ( m ult i- t ier ar ch it ect u re) . Because no sy st em or n et w or k can reach 100 % av ailab ilit y , it is likely t hat at som e point a n et w or k conn ect ion or even a m achine b ecom es unav ailable. I f t his hap pens in t he m iddle of a t r ansact ion , w e w ould be in t r ouble. Apar t fr om av ailabilit y , t here is also a concur r en t d at a access asp ect . Su ch act ivit ies as r eading Learing n about t he EJB 2 .0 specif icat ion or u pdat an account balance usually r equir e access t o an und er ly in g d at abase w her e all t h e accou nt dat a is st or ed. I n our im p lem ent at ion, t he m et hod t hat w it hdr aw s m oney f rom an elop EJBs wtitohsee St uifdio licat ion accouDev nt fir st checks t h eApp balance is Dev n ot eloper ex ceeded by t he amou nt t o t r ansf er . Wh at w ou ld hap pen if , du rin g t he t r ansfer t r an sact ion, anot her ap plicat ion w ould at t em pt t o r educe t he Deploy EJBs t o W eb Sp her e Applicat ion Ser v er balance of t h e fir st account , aft er t h e w it hdr aw met hod h ad alr eady com mit t ed ? This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio The t ran sact ion syst em it self sav es us her e, becau se it gu ar ant ees t he f our m ain pr opert ies of a App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans t r ansact ion: at om icit y, con sist ency, isolat ion , an d du rab ilit y ( ACI D) . ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ACID properties ofabaase. transaction I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic A t ran sact ion has t h e follow ing fou r pr oper t ies: concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist entA it yt ran b ean s and must essagedr eiv en pr all. ovidEv e best pr ivit act yicein guidelines Atence om i c— sact ion m ex ecut combeans. plet elyWe or also not at er y act a unit - of - f or successful lem ateions of EJBs. w or k imp m ust exent ecut su ccessf ully. I f an y act ivit y f ails, t he ent ir e t r ansact ion is ab ort ed and all t h e dat a ch anges ar e r olled back . I f all act ivit ies execut e w it h out an er ror , t he I n Part 2, we ion in t r com oduce t hes e sam le dat bank applicat and t r ansact plet and pall a ing chang es areion com m itt then ed. imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent finen dert — mA et ht rods, difion fer en m app g ster at sim p leent client s tith at u selet t he Consist ansact m tust not inleav a egies, sy st emand inconsist aft er comp es.EJBs. At t he enCon d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. sist ency r ef er s t o t he in t egr it y of t he u nder lying d at a st or e. For ex am ple, in our m oney t r ansfer m et hod , w e should not hav e a n eg at ive balance in an account af t er t he t r ansf er is don e. Not ice t h at consist ency sh ould usually be enf or ced by t h e dev eloper . I sola t e d— All t ran sact ion m ust b e allowed t o ex ecu t e w it hout int er fer en ce fr om ot her pr ocesses or t r ansact ions. Any in t er m ediat e st at es ar e t r anspar en t t o ot her t r an sact ions, allow in g mu lt ip le t r ansact ion s t o ex ecu t e ser ially. D ur abl e — All dat a ch anges com mit t ed du rin g a t r ansact ion m ust be w r it t en t o a p er sist en t dat a st or e and should sur viv e har dw ar e or sof t w are failur es. I f a failur e occu r s, t he dat a can be r ecov ered by u sing t r ansact ional log s.
Java Transaction Service (JTS) Tr ansact ion s in EJBs r ely on t he t r ansact ion API s p rov ided as par t of t he J2 EE sp ecificat ion. JTS defin es a low - lev el API t h at is m eant t o b e used b y t he ap plicat ion serv er pr ov ider. JTS is t h e Jav a im plem en t at ion of t h e OMG CORBA Object Tr ansact ion Ser v ice ( OTS) . This API is not meant t o be used by EJB dev eloper s, because it is r at her com plex. •
Table of Content s
EJB dev eloper s should r at her use t h e Jav a Tr ansact ion API ( JTA) . JTA pr ovid es a pr ogr am m ing • I ndex m odel t h at dev elop er s m ay lever age f or exp licit t r ansact ion dem ar cat ion . EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Object Transaction Service Pub lish er: I BM April 1 1, 20 03 OTSPub is Dat an e:opt ional CORBA ser vice t h at p rov ides t r ansact ion sup por t in volv ing one or mor e I SBN: 073 8426 09 -1 of int erf aces t hat t he t r ansact ion man ager, t he r esou r ce m an ager, and par t ies. I t has a nu m ber t he t ran sact75 ion Pages: 0 al obj ect s u se t o collabor at e.
On e of t he m aj or adv ant ag es of OTS is t h at it has t r ansact ion con t ex t pr opagat ion bu ilt in. I t allow s pr opagat in g t ran sact ion cont ext s on mu lt iple ser v er s as lon g as t hey ar e using RMI - I I OP; t his f eat ur e is w hat m akes d ist r ibut ed t r ansact ion p ossible. I f t her e ar e con cu r rent t r ansact ion s and one applicat ion acqu ires a lock of t he dat a, all t he ot her Lear n about t he EJB 2 .0 specif icat ion app licat ions hav e t o w ait unt il t he lock is r eleased . Dev elop EJBs w it h St u dio App licat ion Dev eloper To im pr ov e per for m ance, t r ansact ion s use t w o main t y pes of dat a locks: r ead lock s an d w r it e lock s. Read locks ar e n on- exclusiv e. That is, an y num b er of t ran sact ions can acquir e t h e lock Deploy EJBs t o W eb Sp her e Applicat ion Ser v er concur r en t ly. Writ e lock s ar e exclusive; only one t ran sact ion can h old a w rit e lock at a t im e. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (Transaction EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. support in J2EE Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an A J2EE app licat ion ser v er mak es t r ansact ion s v er y easy t o u se becau se it hides t h e comp lex it y of un derly ing r elat ional dat ab ase. dist r ibut ed t r ansact ion s fr om t he dev elop er . I t is imp ort ant t o un derst an d t he basic t er min olog y of he par icipan in t r ansact iona m anag ent I n tPart 1, twe in t rt soduce EJBs as p art ofem Jav a 2: En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2.Re 0 source sp ecif icat ovent id e,stfor hanced fun ctorion y for cont ain m anag ed a —ion An yt hat per pr sist oreex onam w ple, hich en you can r ead w alit r it e. I t could be er a -dat ab ase, per sist ence ent it y b ean s and m essagedr iv en beans. We also pr ovid e best pr act ice guidelines f or JMS queue, or a JCA connect or. successful imp lem ent at ions of EJBs. Re source m an ag er — Respon sib le for m an aging a r esou r ce. Ty pically , r esour ce man agers I n Part in tional r oduce h e samor p lem bank ingprapplicat ar e2,awe r elat d att abase essage ovider ion p rodand uct .t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent finctder m letobj h ods, enponent t m app in g olv st red at egies, simion p le. Iclient t h at up se At Tra nsa i ona e ct dif — fer Com inv in a t rand ansact n ours exam le, tthe h e EJBs. ban king t he ensession d, w e dbescrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. ean is a t r ansact ion al obj ect . Tra nsa ct i on m a na ge r— Com ponent or syst em r esp onsible for man aging t he t r ansact ion al oper at ions of t r ansact ional com pon en t s. WebSpher e is a t ran sact ion m anager . Wh en an app licat ion uses m ore t han one r esou rce m anag er in on e t r ansact ion , w e need an ext er nal t r ansact ion m an ager, wh ich WebSp here can be. A t yp ical ex am ple is r eading fr om a m essage q ueue an d w r it ing t o a dat ab ase in on e t r ansact ion . This is a dist ribu t ed or global t r ansact ion.
XA protocol
XA is t he st and ard int er face bet w een a t r ansact ion m anag er and a r esou rce m anag er . Th e t r ansact ion m an ager coor d in at es a d ist r ibut ed t r ansact ion. I t t yp ically uses t he XA pr ot ocol t o int er act w it h t he d at abase back ends. Th e dat ab ases h as t o u nder st and t he XA pr ot ocol for dist r ibut ed t r ansact ion s. Ther e is a r esour ce man ager for each par t icip ant in t h e dist r ibut ed t r ansact ion , and t h e resour ce m anager is t he com mu nicat ion p oin t f or t h e t r ansact ion m anag er . To su ppor t t he XA pr ot ocol, a nu mb er of ex t r a r em ot e pr oced ur al calls mu st be sent t o t he dat abase bot h befor e an d aft er a • Table of Content s giv en conn ect ion is used. •
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Two-phase commit
Pub lish er: I BM Resour ce man ager s t hat w ant t o par t icipat e in a t wo- phase com mit hav e t o im p lem ent t he XA Dat e: April 1en 1, su 20 03 pr otPub ocol, w hich res t h at t h e r esult of a t r ansact ion is con sist ent acr oss all r esou r ce I SBN: 073 8426 -1 in t he t r an sact ion. I t is used on ly in d ist r ibu t ed t r ansact ions. The m anager s par t icipat09 ing pr ot ocol Pages: op er 75 at 0 es in dist in ct ph ases t o ult im at ely com m it or abor t a t r ansact ion :
P ha se on e—evalu at es t he st at u s of each r esour ce m anager . The t r ansact ion m anager check s w it h each local r esou rce m anag er wh et her t hey ar e r ead y t o com m it t h e t r ansact ion. Each r esou rce m anag er respond s t hat t hey ar e r eady or n ot . A t ran sact ion can com mit only w hen all par t icipat ing r esour ce m anager s agr ee du rin g t his ph ase on e. This Lear n about t he EJB 2 .0 specif icat ion ph ase is called t he pr epar e p hase. Dev elop EJBs w it h St u dio App licat ion Dev eloper P ha se t w o—concludes t h e t r ansact ion . Based on t he response f r om each r esour ce m anager , t h et ot rW ansact anag er ion in st Ser ru ctvserall r esour ce m anager s t o com m it t h e Deploy EJBs eb Sp ion her emApplicat t r ansact ion if all ag ree or t o r oll back t h e t r ansact ion if at least on e disagr ees. Th is phase is t he comprmovides it p hase. This Icalled BM Redbook d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Loca ve rional susdat glab obal un derly ing lr elat ase. t r an sa ct ions i n W ebSph er e When an inapplicat uses oneofr esou d ur a t Ed r ansact f or and exam p le wh I n Part 1, we t r oduceion EJBs ason a ly p art Jav a rce 2 En t ering p rise it ion ion, ( J2 EE) cover t heenbasic w r it in g t o t w o t ables in t he sam e dat abase, t h en it s r esou rce m anag er can p er for concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced mw itt he h t he r ole t r ansact m anag ere, . Th is am called t r ansact opalit t imyizat anain d er is - m anag ed EJB 2. 0 spofecif icat ionion t hat pr ov id foris ex ple, local en hanced funion ct ion forion cont t r anspar ent t h e sapplicat ion. Tr ansact in t his engag e inpr a act oneaseper sist ence ent it yt ob ean and m essagedr iv enions beans. Wescenar also prioovid e best icephguidelines f or com mit ( 1PC) t r ansact ion. successful imp lem ent at ions of EJBs. Global ion is t r ansact ion t ing hat applicat uses an ion ex t and er nalt hen r esour m ent an ager. I n Part 2, wet r ansact in t r oduce t h ea sam p le bank impcelem en t it yA beans, session dist r ibut t r ansact a t r ansact ov er a t ierAp deploy enDev t w itelop h sev al also beans, an d med essag e- d r ivion en is beans using ion WebSph er m e ult St ui-dio plicatm ion er er . We t r ansact arteticipan datinab JCA con it hin im plem ent finion derp m h ods,t s—su dif ferch en tasmaapp g ase st r ator egies, andnect simion p le—w client s tthhe at sam u se et he EJBs. At t r d, ansact A global sact ion m anager r equir t o m anage distlicat rib ution ed Serv er. t he en w e dion. escrib e how tt ran o deploy EJB ap plicatision s t o aedWebSph er e App t r ansact ions, and w ill coord inat e t h e upd at es acr oss m ult iple r esour ces. This is called a t w o- ph ase- com m it ( 2PC) . The r esour ce man ager can on ly par t icip at e in a dist r ibut ed t r ansact ion if it su ppor t s t he XA p rot ocol. I f you w ant y our r esou rces t o be par t of global 2PC t r ansact ion s, t hen you hav e t o ensur e t hat t he resour ces y ou defin e in WebSp her e associat e t o XA- com pliant dr iv er s. For exam ple: D B2 UD B ( JD BC) : COM. i bm .d b2 . j d bc. DB2 Conn ect i onPool D at a Sou rce—1PC
COM. i bm .d b2 . j d bc. DB2 X AD at a Sou rce—2PC Refer t o t he developm ent ch apt ers in Par t 2 f or m or e inf orm at ion on con figu rin g XAcom pliant JMS and JD BC r esou r ces.
•
Table of Content s EJB transaction demarcation • I ndex EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Det erm inin g w h en a t ran sact ion begins and en ds is called t r ansact ion demar cat ion. Bean By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss pr ov iders can choose t o eit h er con t rol w hen t ran sact ions begin an d en d p rog r amm at ically, b y using bean- m an aged t r an sact ion ( BMT) demar cat ion, or can delegat e t h is t o t h e cont ainer Pubgh lish con er: I tBM t hr ou ainer- m an aged t ran sact ion dem arcat ion ( CMT) . Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0 Bean-managed transactions (BMT)
When a d ev eloper exp licit ly m anages t h e bean demar cat ion lev els in t h e code, t hey ar e said t o be em ploy ing bean- m anag ed t r ansact ions ( BMT) . Bean - m anaged t r ansact ion s m ust d eclar e w hen t r ansact ion s st art , w hat behav ior is par t of t h e t r ansact ion, and w hen t hey end . This is accom p lished t hr oug h t he use of t h e javax.transaction.UserTransaction in t er f ace as defined about t heion EJB 2 .0 specif icat in t heLear Jav n a Tr an sact API ( JTA) . Th is ion int er face pr ov id es met h ods t o ex plicit ly begin, com m it , and r oll back t r ansact ions. Dev elop EJBs w it h St u dio App licat ion Dev eloper Figur e 8- 2 sh ows a simp le int eract ion using t he UserTransaction in t er f ace. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Figu r e Redbook 8 - 2 . Bea n- m a dna ged int rfor an ct ion onh ow ex am e ively use WebSp here St udio This I BM pr ovides et ailed m sa at ion t o efpl fect App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. // oug Thr desclare hout t hethe book user , w e transaction pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. UserTransaction tran = null; I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep try { t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist it y b ean s and m essagedr ivthe en beans. We context also pr ovid e best pr act ice guidelines f or // ence get ent a user transaction using initial successful imp lem ent at ions of EJBs. InitialContext ctx = new InitialContext(); I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an = dm essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also tran ctx.lookup("java:comp/UserTransaction"); im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he // en d,orw eusing d escrib e how t o deploy EJB ap plicat ion s t o a WebSph the EJBContext object - discussed later er e App licat ion Serv er. tran = getSessionContext().getUserTransaction();
// begin the transaction tran.begin();
Perform™ transactional work, such as db connection, execute.....
// commit the transaction tran.commit(); •
Table of Content s
•
I ndex
} catch (Exception e) { EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
if (tran != null) tran.rollback();
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
... Pub lish er: I BM
}
Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
On ly beans t h at ar e declared t o u se bean- m anaged t r ansact ions m ay use t h e UserTransaction int er face w it hin t heir code. To do ot h er w ise r esult s in except ions being t hr ow n by t he con t ainer. To d eclar e a b ean as u sin g b ean - m anaged t r ansact ion s m ean s set t in g t he deploy m en t d escrip t or at t r ibut e: Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Bean Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Not e
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing m r elat ional t rdat ab ase. Beananaged ansact ions ar e only av ailable t o session and m essage- d riv en b ean s; t hey ar e illeg al f or en t it y beans. Sp anning a t r ansact ion acr oss m an y m et h ods is on ly I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic allow ed in st at ef ul session beans ( see " Con ver sat ion al st at e" on p age 18 5) . St at eless concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he session b ean s, and t he onMessage m et hod of m essage- dr iv en b ean s, mu st end or r oll EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed back t h e t r ansact ion t h ey st ar t in t h e m et hod call. per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en transactions beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Container-managed (CMT) im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. The b ean p rov ider m ust now b e respon sible f or m anag in g t ran sact ions in BMT. This can get com plicat ed r at her fast . A simp ler an d mor e eleg ant solu t ion is t o let t h e con t ainer m anage t he t r ansact ions f or y ou. Ther e is no need t o w r it e any t r ansact ion log ic, becau se t his is han dled b y t he con t ainer at r u nt im e. The code b ecom es cleaner , because t r ansact ion d em ar cat ion is n ot int er t w ined in t he cod e. The dev elop er sim ply h as t o d eclar e t he t ran sact ional behav ior in t h e deploy m en t d escrip t or. One of t he m ost pow er fu l feat u res of EJBs is t heir abilit y t o em ploy declarat iv e t r ansact ion man agement . To d eclar e a b ean as u sin g cont ain er - m anag ed t r ansact ions m eans set t ing t he d ep loym ent descr ipt or at t rib ut e:
Container
I n addit ion, t he d ev elop er or deploy er can declare addit ional t r ansact ion al at t r ibut es t hat g over n t he behav ior of t he b ean s in t he t r ansact ions. The t ran sact ional at t r ib ut e b eh avior of CMTs can be set at eit her t he bean g lobally, or t he ind ividu al m et hods wit h in a bean. D ep en ding on how •t he t ran sact ion Table at tofr ibut Content es ar s e d ef ined, t h e cont ainer w ill eit her st ar t , cont inu e, su sp en d, ign ore, an except ion on a t r ansact ion wh en a bean met hod is ex ecut ed. •st op, or t hr ow I ndex EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
The b ean p rov ider is not r equir ed t o call ex plicit com m it or r ollbacks in t h e code. A t r ansact ion By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss st ar t s w hen a m et hod is called t hat r eq uires a t r ansact ion . The t ran sact ion bou ndar y , or scope, is f or t he leng t h of t his m et h od. I f t h e m et h od ex ecu t es su ccessf ully, t hen t he t r ansact ion is Pub lish er: I BMcom m it t ed w hen t h e m et hod en ds. aut om at ically Pub Dat e: April 1 1, 20 03
How d oes ainer I SBN:t he 0- 73cont 84- 26 09 -1 k now w hen it should or should not comm it ? I f t he bean m et h od ( or any ot her Pages: b ean 75 m 0et hod t hat it sub sequ en t ly calls) calls t h e setRollbackOnly m et hod ( u sing t h e EJBContext ob ject ) , t h en t h e t r ansact ion is m ar ked for a r ollb ack. Th e cont ainer g uar ant ees t hat t he t ran sact ion is r olled back w hen comp let ed, but does not for ce an end of t he t r ansact ion im mediat ely . The b ean dev elop er can use t h e getRollbackOnly m et hod t o det erm ine if a t r ansact ion h as been m ar k ed f or r ollb ack , an d n ot cont in ue w it h t he cur r ent cod e sequ en ce if t hey choose. Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
Not e EJBs t o W eb Sp her e Applicat ion Ser v er Deploy ion ex pr cept ions d atat ically cause ain er t o r ollWebSp back there h e St udio This IApp BM licat Redbook ovides do et not ailedaut inom for m ion on h owt he t o cont ef fect ively use t r ansact ion, so t his m ust b e hand led exp licit ly by t he d ev elop er if a r ollb ack is desir ed, App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans by u se of t he setRollbackOnly m et hod . ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase.
CMT transaction attributes
I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he The f ollow in g ar e t he t r ansact ion at t rib ut es t hat can b e set for beans em ployin g cont ain er EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed m anaged t r ansact ions. These at t r ibut es m ay be set at eit her t h e bean or indiv idual met hod level. per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or Alt hou gh t hese at t r ibut es ar e m ost com m only set at t he com ponent int er face lev el, t hey m ay successful imp lem ent at ions of EJBs. also be declar ed on t h e hom e m et hods ( su ch as create) for ent it y beans. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, anSuppor d m essag e-— d r Th iv en using St uordioa Ap plicat er .olv We N ot t ed is vbeans alu e m ean s WebSph t hat t heerb eean m et hod ion can Dev not elop be inv ed also in a im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t EJBs. At t r ansact ion at all. I f a clien t has st art ed a t r an sact ion, it is ign ored, and met h odshe w ill t he enalw d, ays w e dex escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. ecu t e out side t he t r ansact ion cont ex t . Th e init ial t r ansact ion will b e r esum ed aft er t he en d of t h ose met h ods. Av ailab le for session, en t it y, and m essage- dr iven beans, alt hou gh m et h ods of ent it y b ean s w it h CMP ar e n ot sup por t ed. Re qui re d— Th is v alu e m ean s t hat t he b ean m et hod s m ust alw ay s ex ecut e in a t r ansact ion cont ex t . I f t her e is a t r ansact ion alr eady r un ning , t he b ean p art icipat es in t hat t r ansact ion . I f t her e is no t ran sact ion , t he EJB cont ain er st ar t s a t r ansact ion on behalf of t h e bean. Av ailab le for session, en t it y, and m essage- dr iven beans. Thi s is t he de fa ul t i n a W eb Sphe r e en vi ronm e nt . Su pport s— Th is v alu e m ean s t hat t he b ean p art icipat es in a r un ning t r ansact ion b ut does not r eq uir e it . So, if t her e is no t r ansact ion, t h e m et h od ex ecu t es wit h out a t r ansact ion.
Av ailab le for session and ent it y b ean s only , alt houg h met hod s of ent it y b ean s w it h CMP ar e not supp ort ed. Re qui re sN ew — Her e t he bean r equir es t hat a n ew t r ansact ion is alw ay s st art ed w hen a m et hod is called. I f t h er e is a t r ansact ion r un ning, it is suspended. The cont ain er w ill st ar t a n ew t r ansact ion an d at t he en d of t h e m et hod ex ecu t ion, it w ill com m it or abor t it . Af t er t hat , t he cont ain er w ill resum e t he client t ran sact ion . Available f or session and en t it y beans only .
•
Table of Content s
•
Ma nd atI ndex or y— W hen t his v alu e is used, a t r ansact ion m ust alr eady b e r unn in g w h en t h e exist ing t r ansact ion init iat ed by t h e Ueli caller Wah li, .Wout I f no er Denay t r ansact er, Lar ion s Schu cont nkex , Deborah t is p resen Shaddon t , ,t hMart e in Weiss javax.ejb.TransactionRequiredException is t hr ow n b ack t o t h e caller . Av ailab le for an d en t it y beans on ly. Pubsession lish er: I BM
EJB 2 . 0bean D e ve m lopm nt wisit hcalled. W ebSphe St udio ti onicipat De v eleoper et heod Three obj ect Appl willica p art in t h e By
Pub Dat e: April 1 1, 20 03
N e ve r — I f t h is bean is called in an exist ing t r ansact ion , t he con t ainer t hr ow s a I SBN: 0- 73 84- 26 09 -1 java.rmi.RemoteException. Av ailable for session and ent it y b ean s only , alt hou gh Pages: 75 0 m et hods of ent it y beans w it h CMP ar e not su ppor t ed.
Transactional attribute summary Table 8 - 1 su mm ar izes how t r an sact ional at t r ib ut es af fect t r ansact ions f rom client - t o- b ean , or Lear n about t he EJB 2 .0 specif icat ion bean- t o- bean m et hod inv ocat ions: Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
le 8 -1 CM Tin for t r amnsact i on a tttoref ibu e sum a r y here St udio This I BM RedbookTab pr ovides d et. ailed at ion on h ow fectt ively usem WebSp App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) Tra ar ch it ect re, an d d ep loym chr ap a WebSph er e App licat er.an nsa ctui on Cli ent ent /ofOtsuhe beplicat an ion s t oTra nsa ct i on associ at eion d wServ it h be a t t ri but e t ra nsa ct i on m et h od Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional datnon ab ase. e non e NotSupported T1 as a p art of Jav a 2 En t er p rise nonEd e it ion ( J2 EE) and cover t he basic I n Part 1, we in t r oduce EJBs concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he non e T2 EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Required per sist ence ent it y b ean s T1 and m essage- dr iv en beans. WeT1 also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. non e non e Supports In Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session T1 T1 beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, nondif e fer en t m app in g st r at egies,T2and sim p le client s t h at u se t he EJBs. At RequiresNew t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. T1 T2 non e
err or
T1
T1
non e
non e
Mandatory Never
How to read the table The f ir st colu m n cont ain s t he t ran sact ion at t r ibut es.
The second colu mn show s w h et h er a t r ansact ion is alr eady r un ning in t he clien t , or ot h er EJB, w hen a bean met h od is called . I f t her e is a t r an sact ion, it is nam ed T1; ot herw ise it is m ar ked as non e. A client could be a session bean calling an ent it y b ean .
• •
The t hir d colum n show s w hat happ en s w hen t he m et hod is execut ed on t he bean. I f t h e bean par t icip at es in t h e exist ing t r ansact ion, t h is is mar k ed as T1. I f t he bean st ar t s a new t r ansact ion, t h en T2 is used. I f t he bean ig nor es t he r un ning t r ansact ion , it is m ar k ed as of Content s ires a t r ansact ion and n one is r un ning, it is m ar ked as an er r or . non e. I fTable t h e bean requ I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Woutattribute er Denay er, Lar s Schu nk , rules Deborah Shaddon , Mart in Weiss Transaction method I BM ru les w h en set t in g t he m et hod- lev el t r ansact ion al at t r ibut es b ased on bean TherPub e lish ar eer:some t y pes: Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Se ssi on b ea ns— Can only defin e at t r ibut es f or m et h ods of t h e comp onent in t erf ace. Me ssa ge - d ri ve n be an— Can only d ef ine f or t he onMessage m et hod . Ent i t y be a ns— Can define f or m et hods of t he com ponent int er face, and m et h ods d ef ined in t he b ean 's h ome in t erf ace. Lear n about t he EJB 2 .0 specif icat ion Devthe eloptransaction EJBs w it h St uattributes dio App licat ion Dev eloper Setting Deploy EJBs t o W eb Sp her e Applicat ion Ser v er The t ran sact ion at t r ibut e set t ing s ar e declar ed in t h e deploy m en t descrip t or for each bean in t h e ejb-jar.xml f ile. Figur e 8- 3d et shailed ows how t h at is ion m igon ht hlook e Banking and This I BM Redbook pr ovides in for m ow tfor o eft hfect ively usesession WebSp bean here St udio Account entDeveloper it y bean. f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans App licat ion ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Figu r e 8 - 3 . Tr an sa ct i on at t r ib ut e s i n d epl oym e nt de scr ipt or
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic
concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed
per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an dBanking m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w eRemote d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. transfer
java.lang.String java.lang.String java.math.BigDecimal
Account • •
LocalHome Table of Content s I ndex
create java/jre/lib/security/java.policy config/cells//nodes/ config/cells//nodes/
Lear n about t he EJB 2 .0 specif icat ion app.policy config/cells//nodes/ and esou rceApp adlicat apt ers a eloper Dev elop EJBs w it h rSt u dio ion on Dev nod e lev el Deploy EJBs t o W eb Sp her e Applicat ion Ser v er was.policy /META-INF of EAR f ile On e sp ecific en t er pr ise app licat ion This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopmceen t of ap plicat ion s based on t h e Ent er profise Jav(aBeans ra.xml /META-INF/was.policy.RAR RAR resour ce On e sp ecific resour ( EJB) ar ch it ect u re, adap an d dt ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. er adap t er archiv e) f ile Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an The b esting w ay t o ional edit tdat he ab p olicy un derly r elat ase. f iles is w it h t h e Java policy t ool, w hich you can find in /java/jre/bin/policytool.exe. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Mor e det one.t hI ese set t ings can be f oun concep t s ailed and tin h eforarmchatition ect ur n par t icu lar w e point outd tat he: n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed ht t p: / / j ava. sun. com / j2 se/ 1. 3 / docs/ g uide/ secur it y / per m issions.h t m l per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
J2EE I n Part roles 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im entcy fin derofman et h difion fer,en m app g st r atpegies, p ler oles. client Fr s tom h at au secu se t he I n plem t he life cle apods, plicat dift fer en t in people er f ormand diffsim erent r it EJBs. y pointAt tof hev iew en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. , w e can say t hat : The ap plicat ion p rov ider can use pr ogr am m at ic secur it y ( as in Figur e 8- 1 0) .
Figu r e 8 - 1 0 . Ex am pl e of pr ogr a m m at i c se cu r it y
if ( isUserInRole("manager") ) { showAllCustomers();
} else { showCurrentCustomer(); }
• •
Table of Content s I ndex ion assem b ler w ill use d eclar at ive secur it y t o set p er m issions on m et hod s of The ap plicat
EJB 2 . 0beans. D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
The ap plicat ion d ep loy er link s users and gr oups t o r oles. Pub lish er: I BM
The sy st em adm inist rat or t ells t he app licat ion ser v er wh er e t o fin d t he user s ( for ex am ple
Pub April 1 1, 20 03 or y) . inDat ane: LDAP d irect I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
I m por t a nt Secu rit y is not en abled by default . To enable it open t he WebSp here Adm inist rat iv e Con sole, select Secu rit y Cen t er - > Global Secu r it y an d ch eck t h e enabled ch eck box . Lear n about t he EJB 2 .0 specif icat ion Now click App ly an d t hen Sav e. Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
EJB security details This I BM Redbook pr ovides d et ailed
in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans This ion d escrib es dt he ailsent of how y canion bes tdeclared and ermeanaged ( EJB)sect ar ch it ect u re, an d epdet loym of su secur ch apitplicat o a WebSph App licatfor ionEJBs. Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derlydeclarative ing r elat ionalsecurity dat ab ase. J2EE management I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Declar at iv e secur it y ex t ern alizes t h e secur it y const r aint s so t hat t hey do not r esid e in t he code. concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he This m ak es it m uch easier t o man age t h e secur it y policy for t he ap plicat ion . Declar at iv e secur it y EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed is r ole- b ased. The d ep loy er of t he applicat ion w ill g iv e cert ain r oles access t o cert ain m et hods on per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or t he hom e and com ponent int erf ace of t he b ean s. Met hods can also f ur t her be secur ed , or p ut on successful imp lem ent at ions of EJBs. t he exclude list so t hat t hey becom e uncallable by any one. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session While d eclar at ive secur it y, lik e declarat iv e t r ansact ion man agement , is a n ice id eal, t here ar e beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also som e pr oblem ar eas wit h declarat iv e secu rit y : im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. A u ser h as a cer t ain r ole or d oes not . Th er e is n o w ay t o fine t une t h is by ad ding/ rem ovin g one or m ore specific p er m issions. A u ser g et s a p art icular r ole f or all d at a element s. For a salesperson r ole, w e m ight w ant t o giv e t he user full access t o his own cust om ers ( wit h all p ur chase det ails) , but only giv e limit ed access ( sum m ary ) t o ot h er cu st omer s. These sit uat ions cann ot b e hand led w it hin t he d eclar at ive mod el but can b e ov er com e by u sing pr ogr am m at ic secur it y . Ther ef or e, t he J2 EE declar at iv e secur it y m odel also p rov ides a w ay t o d o pr ogr am m at ic bean- b ased secur it y , similar t o t he w ay t hat bean- m anaged t r ansact ion s allow specializat ion in h andling of t r ansact ions.
Security roles A secur it y r ole is a sem an t ic gr oupin g of p er m issions t hat a user m ust h ave t o u se a par t icular par t of an ap plicat ion . The assemb ler can t hen assig n g rou ps of m et hods ( fr om h ome and com ponent int erf aces) t o t h is secur it y r ole. The assem bler is only r esponsible f or definin g an d nam ing t h e roles and can t hu s leav e t h is wor k f or t h e deploy er . A best pr act ice dur ing • Table of Content s deploy m en t is t o assign g rou ps, r at her t h an in divid ual user s, t o r oles for m or e f lex ibilit y. •
I ndex
EJB 2 . 0 rDoles e ve lopm nt w it h W ebSphe rion e Stsudio ti on These ar eelogical associat t hatAppl w illicam ap De t ov el a oper g rou p
( or user) nam e in a u ser r egist r y. The EJB r it er y roles ar Lar e def in ed or all t he EJBs, Mart in ain JAR, By Ueli Wahsecu li, Wout Denay er, s Schu nk , fDeborah Shaddon Weiss and ar e defin ed using t h e element ( Figur e 8- 1 1) . Pub lish er: I BM
Figu e e:8 -April 1 11.1,User r ole s in EJB dep loym e n t d escr ip t or PubrDat 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
......
Lear n about t he EJB 2 .0 specif icat ion A sample role Dev elop EJBs w it h St u dio App licat ion Dev eloper myRole Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
* w ill ap ply t his p olicy t o ever y m et hod of t he b ean ,
I SBN: 0- 73 84ex26t ends 09 -1 t his t o ev ery met hod on t he hom e and com ponent Add it ion ally75 , if0 m et hods are over load ed , t h en t he op t ion Pages: dist ingu ish m et hod per missions f or over loaded met hod s.
Pr eferences. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session I n t he left pane, select File edit ors u nder t h e Wor k bench h ier ar chy . beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem entcan fin tder et h ods, dif fer m app in gassociat st r at egies, t h at u se At You h enmselect a file ex en t ent sion and e anand int ersim nalp le or client ext ersnal edit or st he f or EJBs. it . t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. When y ou d ouble- click an ot her r esour ce, a d if fer ent edit or sh ow s up. You can easily sw it ch bet w een t h e diff erent opened resour ces by select ing t hem on t he t op b ar abov e t h e ed it or ar ea. I f t he t ab of y our edit or con t ains an ast er isk ( * ) , it m eans t h at it con t ains un saved ch anges.
Outline view The Out line v iew is alw ays associat ed w it h t he act iv e edit or . The Out line v iew giv es you an ov er v iew of t he k ey elem en t s t hat m ake up t he r esour ce t hat is being edit ed. I t allow s q uick and easy n avig at ion t h rou gh y our r esou rce. By select ing one of t h e
elem ent s in t he Out line v iew, t he line in t h e ed it or v iew t hat con t ains t h e select ed elem ent get s high lig ht ed and t he edit or pane is ad ju st ed t o mak e t h e elem en t v isib le.
Properties view When y ou click a r esou rce in t he Nav igat or view an d t h en op en t he Prop er t ies v iew , y ou can v iew •t he diff er ent Table of tContent pr oper ies of tshat r esour ce. Th e Pr opert ies v iew con t ains g en er al t h ings su ch as t h e •fu ll pat h of t hI ndex e file syst em , t h e dat e w hen it w as last mod ified, an d t h e siz e, as sh ow n in Figur e EJB 9- 4 .2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Figu r e 9 - 4 . Pr op er t i es vi ew Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion
Tasks view Dev elop EJBs w it h St u dio App licat ion Dev eloper The Task s v iew con t ains a list of t w o t y pes of elem ent s: Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Probl em Pr oblems fare ooldetelopm er m ined t hation hav t o beon r esolv Exam ple p rob lem s App licat ions Developer or t the d ev en t issues of ap plicat s ebased t h e ed. Ent er pr ise Jav aBeans ar ue re, Javan a com pile errent or s of or su b rok en plicat link s ion f orsHTML/ JSP f iles. ar e ion aut omat ically ( EJB) ar ch it ect d d ep loym ch ap t o a WebSph er eThey App licat Serv er. add ed t o t he Task s v iew w h en w or kin g w it h t h e t ool. When y ou d ouble- click a edov it or t he ples f ile cont ainon ingat he p rob lem opens e cu Thr oug hout tpr heoblem book ,, twhe e pr id efor exam based simp le bank ing appand licatt hion w ritsor h anis point ed at locat ion of t he pr ob lem . un derly ing r elat ional datt he ab ase. Ta sk s You can m anu ally add t asks y our self . For exam p le, y ou can ad d a t ask t hat r em ind s I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic y ou t hat you hav e t o imp lem ent a Jav a m et hod . Place t he cur sor in t he m et hod' s concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he im plem ent at ion , r ight - click and select Add - > Task . Wh en y ou doub le- click , t h e file EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed opens and t he cur sor is locat ed in t he m et hod . You can also ad d g en er al t ask s t h at per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or do not ref er t o a sp ecific f ile. successful imp lem ent at ions of EJBs. You can2,set er al ftilt er sam s t o pshow on ing ly t he t ask s ion y ouand r eally wan t tlem o see. le, by I n Part weu pinsev t r oduce he le bank applicat t hen imp ent For en t itex y amp beans, session beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also click ing t he f ilt er icon , y ou can specify t h at y ou w ant t o show only t he Java com p ile er r ors im plem ent t fin der Jav m etahclass ods, dif t mpapp in glar st rpr at oject egies, andexsim le client t h at su vseiew t hew EJBs. fr om a par icular or fer f orent he ar t icu . An ampple of t h es Task it h a At tJav he aencode d, w er e dr or escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. is show n in Figur e 9- 5 .
Figu r e 9 - 5 . Task s vi ew
Dou ble- clicking an err or op en s t he file w it h t he er ror at t h e point of t he er r or.
Console view The Console view d isp lay s console out p ut of Java pr ogr am s t h at y ou ru n or m essages of ser v er s t hat y ou st ar t .
•
Other viewsTable of Content s •
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Ther e ar e m any ot h er v iew s in t he dif fer en t per spect iv es, t ailor ed for cer t ain u ser t ask s. Som e of By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss t he ot her v iew s w ill be ex plained w hen w e d iscuss ot h er perspect iv es. Pub lish er: I BM Pub Dat e: April 1 1, 20 03 Customizing perspectives I SBN: 0- 73 84- 26 09 -1
Pages: 75 0
You can h ig hly cust om ize t he d iff er ent perspect ives b y: Closing or openin g v iews. Max im izin g t h e v iew by dou ble- clickin g t h e t it le bar . You do t his w h en y ou need a lar ge pan cod et he edEJB it ing. D oubleclickion ag ain t o r est or e t he lay out . Learen for about 2 .0 specif icat Mov ing vEJBs iew s w t oitot panApp es or st ack hem b eh ind ot her v iew s. To mov e a view : Dev elop h her St u dio licat ion ing Devt eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er - Select t he view 's t it le bar and st ar t d rag ging t h e view . This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio While y ouf or d rag , t hen e tmofouse cu r sor anges on int ot hae dEnt ropercur sor.Jav The d rop App licat ion -Developer t het hdeevview elopm ap plicat ionch s based pr ise aBeans w hatent willofh appen hen you t he view ou are r agging : ( EJB) ar ch itcur ect sor u re,indicat an d d es ep loym su ch apwplicat ion sr elease t o a WebSph er e yApp licatdion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Thedat f loat ing v iew ap pear s below t he view un dern eat h t he cur sor. un derly ing r elat ional ab ase. I n Part 1, we in t r oduce as vaiew p art Javsat o2 tEn Ed itv ion J2 EE) andhcover The f EJBs loat ing apof pear h et er lefpt rise of t he iew (und er neat t he t he basic concep t s and t h e arcur ch sor. it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed iew ap pear s to t h e r ight t heprvovid iew eun dernpreat t h eguidelines f or per sist ence ent it y bThe eanfsloat anding m vessagedr iv en beans. We of also best acthice cur sor. successful imp lem ent at ions of EJBs. The f tloat ap pear abov e tion he vand iew t hen un dern h ent t h e en curt itsor . I n Part 2, we in t r oduce h e ing samvpiew le bank ing sapplicat impeat lem y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also f loat ing v iew pearinsgas in t he as st ht eh at v iew im plem ent fin der mThe et h ods, dif fer en t ap m app st a r att ab egies, andsam simeppleane client u se t he EJBs. At h t h e cur sorap . You can dr WebSph op t he v er iew on tlicat he ion Serv er. t he en d, w e d escribun e dern how eat t o deploy EJB plicat ionalso s to a e App per sp ect iv e t oolbar t o m ak e it a fast v iew . You cannot dock t he float ing v iew at t h is point .
Add in g view s and icons. You can add a view or a set of icons as f ollow s: Select Window - > Cu st om ize Persp ect ive f r om t he main m enu bar . Select t he v iew s y ou w ant t o ad d an d t he icons ( Ot h er ) you w ant t o ad d and click OK.
Select Window - > Show View an d select t he v iew y ou j ust added. On ce y ou hav e conf ig ur ed t h e per sp ect iv e t o you r lik ing , y ou can also save it as y our ow n per sp ect iv e by select ing Window - > Sav e Perspect iv e As . When y ou w an t t o r eset a persp ect iv e t o it s or igin al st at e, select Window - > Reset Persp ect ive fr om t he m ain menu . •
Table of Content s
•
I ndex
New EJB 2 . 0 icon D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
The icon ( New) opens a win dow w her e y ou can cr eat e any t y pe of r esou rce f r om a select listI BM . Th is win dow is also op en ed w hen select in g File - > New - > Ot her f r om t he main Pubion lish er: t oolPub b arDat( Figur . e: Aprile1 91, 6 20)03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Figu r e 9 - 6 . Ne w w i za r d
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. You can also select t he d rop - dow n menu n ex t t o t h e New icon . This act ion displays a list of m ost I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session used resour ces t h at y ou m ay w ant t o cr eat e in t h e cur rent per spect iv e. I f t he d esir ed r esour ce is beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also not in t he list , select Ot h er t o open t he New w ind ow . im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Web perspective I n Figur e 9- 3 on p age 33 6 you see t he Wor k bench opened in t he Web p er spect iv e. You use t he Web perspect ive wh en y ou w ant t o dev elop Web applicat ions. The Web per spect iv e is t h e best per sp ect iv e for addin g an d or g anizing st at ic con t ent ( HTML, im ages) an d d yn amic con t en t ( ser v let s and JSPs) t o a Web app licat ion. On t op of t he perspect ive, y ou see t h e Wor k bench t oolb ar. The cont ent s of t he t oolbar ch ange based on t h e act ive edit or for a par t icular r esour ce. The cur r ent ed it or is t he p age d esigner f or edit ing our JSP pag e. The t oolbar now r eflect s JSP d ev elop m en t an d cont ain s icon s t o add JSP
t ags and a JSP m en u it em . The Out line v iew show s t he out line of a JSP pag e. I t cont ains all t he t ags f rom wh ich t h e JSP pag e is const ru ct ed . When y ou sw it ch t o t he sou rce t ab of t h e page design er and you select a t ag in t he Ou t line v iew , t he m at ching line in t he Sou rce v iew is high lig ht ed. We u se t he Web perspect iv e in t h e chapt er s t hat f ollow , w h er e w e d ev elop t h e samp le Web app licat ion and t he Web serv ices. •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Web Structure view
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
The W eb St r uct ur e v iew ( Figur e 9- 7 ) sh ows t he logical lay out of a Web applicat ion wit h Web er: act I BMion s. This is m ost usefu l in a St r ut s- b ased Web ap plicat ion : pagPub es lish and Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
Figu r e 9 - 7 . W eb St r uct ur e v ie w
Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Web pages files and St r utt he s act ion fu s. nct ions int r od uced w it h t he concep t s and t h ear arechshow it ectnurwe.it h I nr efer par ten icuced lar w e point out n ew EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Stence r ut s ent act ions are sshand ow nmwessageit h for m act ion m apping s. ovid e best pr act ice guidelines f or per sist it y b ean drsivan end beans. We also pr successful imp lem ent at ions of EJBs. IJava n Part perspective 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At When w an t t o develop a app EJB licat ions, y ou Java pererspeect iv licat e. The a per. er spect ive t he en yd,ouw e d escrib e how t oJav deploy ap plicat ionuse s t ot he a WebSph App ionJav Serv is show n in Figur e 9- 8 . I t con t ains a lot of usefu l edit or s and v iew s w hich help y ou in you r Jav a dev elop ment .
Figu r e 9 - 8 . Jav a per spe ct i ve
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
You nav igat e in t he Java ect iv eicat t hrion oug h t h e P ack a ge Ex plor er v iew , w hich enab les y ou t o Lear n about t he EJBpersp 2 .0 specif defin e and m anage Java packag es and t he Jav a classes defined in t h e pack ages. Dev elop EJBs w it h St u dio App licat ion Dev eloper When y ou select a Jav a class in t he Packag es view an d select Nav igat e - > Open Ty pe Hier ar ch y , t he H ie ra rchyEJBs v iewt of or t hat Jav ea Applicat class opens. The Deploy W eb Sp her ion Ser v erHier archy v iew allow s you t o see t he f ull hier archy of a Jav a class. I n Figur e 9- 8 , t h e Hier ar chy v iew is cu rr ent ly h id den by t h e Pack age Ex plor er vRedbook iew . This I BM pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans When y ou d ect oubleJava file, e Java or opens. can open lt iplicat le Jav f ileser. at t he ( EJB) ar ch it u re,click an d ad ep loym entt hof su ch edit ap plicat ion s t oYou a WebSph er emu App iona Serv sam e t ime. Th e Jav a edit or f eat ur es sy nt ax highligh t ing and a code assist ant by pr essing Ct sphout acebar . book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Thrr l+ oug t he un derly ing r elat ional dat ab ase. The Out line v iew in t he Jav a p er spect ive g ives an ov er v iew of all t he m et hod s and fields f or t he Jav a f ile1,t hat rr ent lyEJBs op en ou aclick m petrise hodEd inittion he Ou lin e and v iewcover , t h e cur is I n Part we is in tcu r oduce aseda. pWhen art of yJav 2 Enat er ( J2tEE) t hesor basic posit ioned in tthhee m int icu t helar Javwae ed it or.out Thet he t ool barfuat t op ain s filt to concep t s and aret chhitod ectsign ur e.atI ur n e par point n ew nctt he ions intcont r od uced w iters h t he include ex clude st at ic mpretov hod s or d ten o hanced sor t t h efun Outctline view . cont ain er - m anag ed EJB 2. 0 or sp ecif icat ion t hat id e, forf ields, ex am an ple, ion alit y for per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or Isuccessful n t he Jav aimp p erlem spect t h e of Wor kb en ch t oolbar con t ains sever al icons t o add new p ack ages, new entive, at ions EJBs. Jav a classes, and new Jav a in t erf aces, or t o cr eat e a new Scr apbook page. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At Search t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Clickin g t h e sear ch icon in vok es t he sear ch w ind ow show n in Figur e 9- 9 . Now you can eit h er do a f ull t ex t search or a m ore in t elligent Jav a sear ch t o look , f or exam ple, f or a p art icular t yp e declarat ion or r efer en ces t o it .
Figu r e 9 - 9 . Se ar ch w i ndow
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
The Se a rch v iew ( Figur e 9- 1 0) sh ows t he r esu lt s of a sear ch act ion. Fr om t he Sear ch view y ou can double- click an y of t he r esult lines t o open t he class t hat con t ains t h e declar at ion or r ef erence. Lear n about t he EJB 2 .0 specif icat ion
Figu r e 9 - 1 0 . Sea r ch r e su lt s
Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase.
J2EE perspective I n Part 1, we in t r oduce EJBs as a p art
of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he The J2EE ection iv e t (hat Figur pr ovides u sef v iew s ffun or tcth ion e J2EE d ev Theed EJB 2. 0 spper ecifsp icat pr eov9id1e,1)for ex am ple, enulhanced alit y or forEJB cont aineloper. er - m anag J2 EE H ie ra rchy v iew show s y ou a list of all t h e diff erent m odules such as Web mod ules, EJB f or per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines m odules or ser ver conf igur at ions t hat m ak e up y our ent er pr ise applicat ion. You can ex pan d t he successful imp lem ent at ions of EJBs. m odule y ou w ant t o ex plor e and y ou can edit t h e associat ed deploym ent descr ipt or s f or t hat m b ywe douinbleclicking I nodule Part 2, t r oduce t h e. sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer enr tem9app st r at egies, p lee client s t h at u se t he EJBs. At Figu - 1 1in .g J2 EE pe r and spesim ct iv t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion I n Figur e 9- 1 1, t h e EJB deploy ment descr ipt or ( ejb-jar.xml) is cu rr ent ly op en ed in t he EJB Dev elop EJBs wt or it hedit St uor dio deploy m en t d escrip . App licat ion Dev eloper to W eb Sp her eenApplicat SerHier v erar ch y v iew in Figur e 9- 1 1, show s a The J2Deploy EE N a vEJBs iga t or v iew , hidd by t he ion J2 EE hier archical v iew of all t he resour ces in t he w ork space. When you dou ble- click a r esour ce, t h e This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio r eg ist er ed edit or for t h at f ile ex t ension op en s and t h e Out line view show s t h e out line f or t h e file App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans y ou ar e edit in g. ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derlyperspective ing r elat ional dat ab ase. Data I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic You use t he Dat a p er spect ive ( Figur e 9- 1 2) for r elat ional dat abase desig n f or y our ap plicat ion . concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he You can eit her cr eat e a r elat ion al dat ab ase schem a y our self , or im por t it f rom an ex ist in g EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed dat ab ase. Af t erw ar ds, y ou can b row se, qu er y or mod if y it . Th e Dat a perspect iv e pr ovid es t he per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or v iew s t o m anage an d wor k w it h dat ab ase definit ions. successful imp lem ent at ions of EJBs.
Figu e 9 -ing 1 2 applicat . D at aion per spe ct iimp ve lem ent en t it y beans, session I n Part 2, we in t r oduce t h e sam p ler bank and t hen beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper I n t he D B Ser ve rs v iew , y ou can creat e a con nect ion t o an ex ist ing dat abase an d b r owse it s Deploy EJBs t o W eb Sp her e Applicat ion Ser v er sch em a. When y ou w ant t o m odif y or ex t en d t he sch em a, you hav e t o imp or t it in t o t h e Dat a Defin it ion v iew . This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans The D a t a D ef ini t ion v iew allow s you t o defin e new t ables, or t o m odify ex ist in g t ables. I f y ou ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. dou ble- click a t ab le in t he Dat a Definit ion v iew , t h e t able edit or opens and y ou can ad d or chang e colu m ns and pr im ar y or f oreign k ey s. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. The Nav igat or view show s all t he r esou rces in t he f old er st ru ct u r e. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0perspective XML sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lemive ent (atFigur ions eof9-EJBs. The XML p er spect 1 3) is t he p er spect iv e f or XML dev elopm ent . The XML per spect iv e cont ains sev er al edit or s an d v iews t hat help y ou in b uilding XML, XML sch em as, XSD, DTD, an d Iint n egr Partat2, oduce t hion e sam p lea bank ing applicat ion and t hen imp lem ent en t it y beans, session ionwe betinwt reen r elat al dat and XML. beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At 9 -ap 1 plicat 3 . XM ect iveree App licat ion Serv er. t he en d, w e d escrib e how t o Figu deployr e EJB ionLs pe t o ar sp WebSph
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion I n Figur e 9- 1 3, t h e XML edit or is opened on a St r ut s con figu rat ion f ile. You can sw it ch bet w een Dev elop it ht ab St us dio licatoriont oDev t he Design an dEJBs Souwrce of tApp he edit d eveloper elop y our XML f ile. The Ou t lin e v iew con t ain s all t he XML t ags t h at m ak e up t he XML docum en t t hat is cu rr ent ly opened in t he XML edit or . Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Server perspective Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. When y ou w an t t o t est a W eb app licat ion or EJB m odule y ou use t he Ser v er perspect ive ( Figur e Thr hout t hev er book w eect privov e exam based onitaors simp le en bank ingy ou appt olicat it h figu an re, 9- 1 oug 4) . Th e Ser per, sp e id cont ains ples v iews and ed t hat able defion in e,wcon un derly ing r elat ional dat ab ase. and m anag e ser v er in st an ces an d con figu rat ion s. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic e 9t icu - 1lar 4 . wSer v erout pet rhespe ctfu ivnct e ions int r od uced w it h t he concep t s and t h e ar ch it ect urFigu e. I n rpar e point n ew EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
The SeLear rv enr Conf t ion2 v.0iew ( lefticat bot t om ) en ables you t o define or m odify ser ver aboutigt ura he EJB specif ion conf ig ur at ions, and bin d t hem t o a pr oject . Wh en y ou doub le- click t he serv er config ur at ion f ile in t he Ser v erelop Conf igurwatition t he licat Ser vion er Conf igur at ion ed it or opens. Dev EJBs h Stview u dio, App Dev eloper The SeDeploy rv e rs vEJBs iew (t roight om)e Applicat list s all tion he cur W ebbot Spther Serrvent er ly def in ed serv ers. Her e y ou can st ar t or st op t heir execut ion, r est ar t pr oj ect s, an d st ar t t he adm inist r at ive con sole or t he u niver sal t est This I .BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio client App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans The Consol v iew t lyloym hiddent en by t he ver s vion iews )t oshaows all t he t putlicat listion ed by a er. ( EJB) ar ch iteect u re,( cur an dr en d ep of su ch Ser ap plicat WebSph er ou e App Serv r unn ing ser v er . Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase.
Debug perspective I n Part 1, we in t r oduce EJBs as a p art
of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he Use t he Debu p erion spect ivepr(ov Figur 9- 1ex 5)am whple, en yen ouhanced w ant t o debu youy rfor code. D -eb EJB 2. 0 sp ecifgicat t hat id e,e for fun ct iong alit contThe ain er mug anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or per sp ect iv e aut om at ically opens w hen y ou click t he D eb ug icon in t h e Java per sp ect iv e t o successful imp lem ent at ions of EJBs. r un an applicat ion in debu g mod e. I t allow s you t o st ep t h rou gh you r code, inspect t h e v alu es of vI nariab m odif you r code andprleesu m e ing ex ecu t ion. ion and t hen imp lem ent en t it y beans, session Partles, 2, we in tyr oduce t h e sam bank applicat beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also r et 9m-app 1 5in. gDstebu g perand spe ctpile veclient s t h at u se t he EJBs. At im plem ent fin der m et h ods, Figu dif fer en r at egies, sim t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
about t he EJB 2 .0iltspecif The DLear eb ugn per sp ect iv e is bu fr om icat t h eion follow ing v iew s: Dev elop EJBs w it h St u dio App licat ion Dev eloper The D e bug vi e w list s all t h r ead s w it hin t he dif fer en t p rocesses and show s y ou w here t he Deployion EJBs t o Wed ebwSp herreeach Applicat Serpoint v er . execut is halt hen ing aion b reak This IBeneat BM Redbook pr ovides d ettailed in for m at ion o ef fect use here St udio h t he Debu g view h er e is a Java editon or ht how at tshow s t hively e sour ce WebSp of t he code y ou ar e App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans st epping int o. ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. The Bretahe k poin s eallexam cur r ent def in ed point s. Thing e Ex ceplicat t ionion icon Thr oug hout bookt , vwieewprlist ov id pleslybased onbr a eak simp le bank app w it h an on t op of ing t he rBreak pointdat view allow s y ou t o def in e except ions t hat will h alt ex ecut ion w hen un derly elat ional ab ase. t hr ow n. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic The a riat hblees vi ew s all ar iables curout rentt he ly in t h efuru nnin g tint hrread . Youwcan iew concep t s Vand ar ch it ectlist ur e. I n vpar t icu lardefined w e point n ew nct ions od uced it h tvhe and m odif y t heir v alu es and set up filt er s t o ex clu de, for ex am ple, st at ic fields. EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or I n t heimp Ex lem pre ent ssi ons v ieofwEJBs. y ou can ent er Java cod e and execut e it using all t he var iables successful at ions t hat ar e v isible at t he cu rr ent br eak poin t . I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session The e veiedwr iv( bot t om ) sh ows tWebSph he ou t put licat ion. beans, anConsol d m essag en beans using er eofStyuour dio app Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
CVS Repository Exploring perspective The CVS Reposit or y Ex plor ing per sp ect iv e pr ov ides an in t er f ace t o t h e Concur r ent Ver sions Sy st em ( CVS) , w hich is one of t h e su ppor t ed pr oduct s for t eam d ev elopm en t ( Figur e 9- 1 6) .
Figu r e 9 - 1 6 . CVS Re posit or y Ex plor i ng pe r spe ct iv e
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er The CV S R eposi t orie s v iew display s connect ions t o r ep osit or ies, t he pr oj ect s t hat h ave been shar w itRedbook h t eam m b er s,deit her as e)fect or ively as v er sions ( fr ozen e) . This ed I BM prem ovides et ailed in aforbrmanch at ion( cu onrrhent ow cod t o ef use WebSp herecod St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans The CV S R esource H i st or y v iew show s t h e r ev ision s t hat hav e been p er f orm ed on a f ile. ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Rev isions of files can be com par ed and t he d iffer ences are show n in t he Com p ar e v iew . Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un ing er elat dat abpr ase. Wederly d escrib simional ple u sage oced ur es for CVS in Ch apt er 19 , " Ap plicat ion Dev elop er t eam dev elop ment env ir on ment " on page 669 . I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Help per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. The Ap plicat ion Dev elop er pr ovid es h elp in a separ at e w indow t hat you can open u sin g Help - > IHelp n Part 2,t en wet sin( tFigur r oduce p le bank ing applicat ion and t hen imp lem ent en t it y beans, session Con e 9-t h1 e7)sam . beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At FiguEJB r e ap 9 -plicat 1 7 . ion H el w WebSph in dower e App licat ion Serv er. t he en d, w e d escrib e how t o deploy s tpo a
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
The Help w in dow cont ain s a lot of useful infor m at ion about t he W ork bench. I t pr ov ides inf orm at ion abou t t h e diff erent concep t s used by t he Wor kb en ch , t he d iff er ent t ask s y ou can d o w it hinLear t henWabout ork bench and2some usef ul ion sam ples. The Sear ch f ield allows you t o do a sear ch in t he EJB .0 specif icat t he help . Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Memory considerations This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Aft w orion k ing w it h Ap plicat iondDev elop er e t imion e, syou w ill on havteh eopened Apperlicat Developer f or t he ev elopm enfor t ofsom ap plicat based Ent er prsev iseeral Jav aBeans per sp ect iv es. igh ave t h eent imof pr ession Apion plicat Dev eloper ere isApp w ork ing erer. . I t is ( EJB) ar ch it ectYou u re,man d tdhep loym su ch apt hat plicat s t oion a WebSph licat ionslow Serv good pr act ice t o close dow n t he p er spect ives y ou hav e not been using f or a w h ile, b ecause t h ey Thr oug can conhout su met he a lot book of ,mwem e pr ory ov, idan e dexam hence, plesslow based d owon na t he simp ov er leall bank p ering f orm app ance. licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Projects A p r oject is t he t op- level const r uct for or gan izin g t he d if fer en t r esour ces. I t cont ain s files as w ell as f olders. I n t he W ork bench y ou can creat e d if fer ent k ind s of pr oject s, and t hey w ill hav e a dif fer en t st r uct ur e. A W eb pr oj ect , f or exam p le, h as a d iffer ent nat ur e f rom a Jav a p r oject ; •t her efor e it wTable of Content s ill h ave a dif ferent f old er st r u ct u re. •
I ndex
EJB 2n. ow 0 D ebr veiefly lopm ediscuss nt w it h Wt he ebSphe r e St Appl ica ti on v eltoper We t yp es ofudio p r oject s ref erDe r ed o in t his docum ent : By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Jav a p roj ect Pub lish er: I BM Pub EAR Dat e:prApril oj ect 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
Web pr oject Pages: 75 0 EJB pr oject Serv er pr oj ect
n about JavaLear project
t he EJB 2 .0 specif icat ion
Dev elop EJBs w it h St u dio App licat ion Dev eloper When y ou w an t t o creat e a Java app licat ion, you f ir st h ave t o cr eat e a Jav a p roj ect t o con t ain t h e EJBs t oa W e Applicat ion v er builder p at h associat ed w it h it , w hich ar e Jav a fDeploy iles. Each Jav preb oj Sp ecther has a Java bu ildSer er and used t o com pile t he Jav a sou rce f iles. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Creating a Java project
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Here areing t he st eps t o dat cr eat a Java pr oject : un derly r elat ional abease. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he Select File - > New - > Proj ect . EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or I n t he New w indow , select Jav a in t h e lef t p anel and Jav a Pr oject in t h e rig ht panel. Click successful imp lem ent at ions of EJBs. Next . I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session Specify a nam e for t he p roj ect and t he locat ion of t he p roj ect cont ent s. By d ef ault t he beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also cont ent is st ored in t he w or kspace. Click Next . im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he enThe d, Jav w e ad escrib e how t opdeploy EJB eap9-plicat t o a fWebSph App licattion Serverer. b uild set t ings anel ( Figur 1 8) ion const ains our t ab sert oe specify h e fold s, pr oj ect s, and libr ar ies u sed for com p ilat ion.
Figu r e 9 - 1 8 . Ja va bu il d se t t i ng s
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Sou rce I n t he Sour ce t ab y ou specif y w here t he sour ce files should be st or ed, eit her it hin t he d n et orm al prinoject f old s orh ow in f tolders design edWebSp b y y ou. This I BM Redbookw pr ovides ailed for m at ioner on o ef fect ively at use here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans P roj e ct s I n t he Pr oject s t ab y ou specif y w het her ot her p roj ect s are requ ired in t he bu ild ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. pat h . For exam ple, a Web p roj ect m ay r eq uir e an EJB p roj ect . Thr ougrhout , w Libr e prar ovies id et exam ples le ext bank it hbuild an Li bra ie s t he book I n t he ab y ou canbased ad d inon t eran simp al and er ing nal app JAR licat f ilesion to w t he un derly ing r elat ional dat ab ase. pat h : I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch itAn ectint ur ern e. I al n par lar wained e point he nkew fu nctTh ions int ant r odage ucedofwan it h t he JARt icu is cont in out t he tWor bench. e adv EJB 2. 0 sp ecif icat ion t hat prnal ov id e, for y for contce ainwerit-hin m anag int er JAR is t ex h atam it ple, can en b ehanced t r eat edfun asctaion noralit mal resour t he ed per sist ence ent it y b eanWor s and m essagedr iv en beans. We malso pr ovid e .best pr act ice guidelines f or k bench, w hich allow s ver sion anagem ent successful imp lem ent at ions of EJBs. Ex t er nal JARs are ref er enced w it h absolut e p at hs in t he file syst em . This I n Part 2, we in t r oducemt ay he m sam p leit bank ing tapplicat andin t hen impen lem enent t it y. Var beans, session ake dif ficult o shar e ion t hem a t eam virent onm iables can beans, an d m essag e- d be r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also u sed t o allev iat e t he issue int r odu ced by ab solu t e pat h s. An ex am ple im plem ent fin der m et hof ods, appf ile in gwst sim p le client s tused h at ufse he EJBs. At an dif ex fer t eren nalt m JAR our at ld egies, b e t heand db2java.zip f ile or tJDBC t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. access t o DB2. Or de r/ Exp ort The Or der an d Exp ort t ab enab les y ou t o sp ecify t he or der in w hich t h e dif fer en t it em s in t he bu ild pat h ar e accessed, w hen loading Jav a classes. You can also select w hich d irect or ies an d f iles ar e ex por t ed w it h t he pr oject .
Tip Use t he pr edefined v ariab les inst ead of addin g ex t er n al JARs w it h absolut e pat hs t o y our b uild p at h wh en ev er possible. The Wor k bench con t ains v ar iou s pr edefined
v ariab les, such as t he DB2JAVA v ar iable, w hich d ef ines t he db2java.zip f ile. You can add v ar iab les f or ot h er JAR f iles t h r ough t he Window - > Pr eferences - > Java - > Classp at h Var iables w in dow .
You can m odif y t he Java build pat h af t er you hav e cr eat ed a pr oject t h rou gh t he Pr oper t ies cont ex t m enu of t he p roj ect . •
Table of Content s
•When y ou ar Iendex f inished cr eat in g t he Jav a p roj ect , t he Wor k bench sw it ches au t omat ically t o t he EJB D espect ve lopm nt w it h W ebSphe r e St udio Appl ica ti on De v el oper Jav a2 . 0p er ivee. By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Creating Pub lish er:aI BM package and a class Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
To cr eat e a Jav a packag e, select File - > New - > Java Package or click t he New Package icon 75 0 Ent er t h e pack age nam e an d click Fin ish. Th e packag e app ear s in t he Pack ages in t he Pages: t oolb ar. v iew . To cr eat e a class in t he new p ackag e, select t he p ack age and select File - > New - > Java Class or select t h e New Class icon in t h e t oolbar . I n t he Sm ar t Guid e, ch eck t h e packag e nam e an d ent er t he desir ed class nam e an d sup er class. I f y ou wan t a m ain m et hod in y our class, select t he n about t heWhich EJB 2m .0etspecif icat m ain Lear m et hod u nder hod st ubion s w ould y ou lik e t o cr eat e. Click Fin ish. Th e class ap pears un der t he p ackag e and a Jav a ed it or opens an d y ou can st ar t cod in g. Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Java editing
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App ioninDeveloper f orurt es he are d ev av elopm en t wofhen ap plicat ion sJav based on t h e Ent er pr ise Jav aBeans The licat f ollow g u sef ul f eat ailable y ou edit a code: ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Dou bleclicking t it le bar of t h e Java edit or m ax im izes t he ed it or so t hat it occup ies un derly ing r elat ional in datt he ab ase. t he wh ole per sp ect iv e. Double- click ag ain t o r est or e it s or iginal size. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Prtess Ct r lt ourlaun t h et icu codlar e assist in t out h e Java w hen concep s and t hsp e aceb ar ch ar it ect e. I ch n par w e point t he nedit ew or fu nct ionscoding. int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed I fence you select ed Java sou r ce anen d you click t h also e Show Sour of pr Select ed guidelines Elem ent Only per sist ent it y tbhe eanedit s and m essagedr iv beans. We pr ovid e ce best act ice f or successful imp lem ent at ions of EJBs. icon in t he t oolbar t hen on ly t he sour ce of t he select ed elem en t in t he Out line v iew is display ed in t he edit or . I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also I f you p lace t h e cur sor in t h e Jav a edit or on a var iable, t hen t he f ull p ack age nam e of t hat im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At v ariab le disp lays in t h e hov er help ( a small pop- u p t hat op en s at t he cur sor locat ion ov er t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. t he t ex t ) . Hid e t he h over h elp by clicking t h e Text Hov er icon in t he t oolbar . I f you select a m et hod in t h e Out lin e view an d t hen select Replace fr om Local Hist ory f r om t he con t ex t m en u, a win dow op en s and show s all t h e st at es of t he m et hod t hat y ou sav ed. You can r eplace t he m et hod w it h an old er ver sion . The sam e can be don e for t he class it self fr om t he Nav igat or v iew .
EAR project
To d ev elop a J2EE ent er pr ise applicat ion you hav e t o creat e an ent er pr ise ap plicat ion p roj ect ( EAR pr oj ect ) . An EAR p roj ect usually consist s of one or m or e EJB m odules, one or mor e W eb m odules ( Web applicat ions) , an d on e or m or e applicat ion client mod ules.
Creating an EAR project • cr eat e an Table s t he follow in g: To EAR of pr Content oject , do •
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Select File - > New Pr oj ect .
PubSelect lish er: J2EE I BM
in t h e lef t p ane and En t er p rise Applicat ion Pr oj ect in t h e rig ht pane, and click
Next . April 1 1, 20 03 Pub Dat e: I SBN: 0- 73 84- 26 09 -1
Specify a Nam e f or t h e EAR pr oject . Pages: 75 0
Specify cont ained m odules ( clien t , EJB, and Web p roj ect s) t hat you w ant t o in clu de in t he EAR pr oj ect , an d click Fin ish. We r ecom m end t hat y ou f ollow a n amin g st an dar d f or y our p r oject s. Lear n about t he EJB 2 .0 specif icat ion
EAR deployment descriptor (application.xml)
Dev elop EJBs w it h St u dio App licat ion Dev eloper
WhenDeploy y ou cr eat e an EAR pr oject a deploym EJBs to W eb Sp her e ,Applicat ion ent Ser vdescr er ipt or ( application.xml) is cr eat ed in t he /META-INF f older . The EAR d ep loym ent descr ipt or d ef ines all m odules in t he EAR f ile. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio To op en ion t h eDeveloper EAR deploy m en t or,endo t he ollow ion ing s: based on t h e Ent er pr ise Jav aBeans App licat f or t het descrip d ev elopm t of apfplicat ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Op en t he J2EE per spect iv e and J2 EE Hier ar ch y v iew . un derly ing r elat ional dat ab ase. Ex pan d En t er p rise Applicat ions an d doub le- click t he EAR pr oj ect . Alt er nat iv ely d ouble- click I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic t he application.xml f ile in t he Nav ig at or v iew . concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or J2EE packaging successful imp lem ent at ions of EJBs. IAn n Part we in tcan r oduce h e aged sam p le ingerp applicat imp lem ent ent en er t it pr y beans, session EAR 2, pr oject be ptack as bank an ent r ise ar ion chivand e filet hen ( EAR f ile) . An ise ap plicat ion beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also consist s of t he f ollow ing m odu les: im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Web applicat ions, w hich ar e p ack aged in .WAR f iles. Th e WAR file cont ains t he r esou rces t hat com pose t he Web app licat ion and a d ep loym ent descr ipt or ( web.xml) . A Web app licat ion is cont ained in a Web p roj ect , w hich w e d iscuss in " Web pr oject " on p age 35 6. EJB m odules, w hich ar e p ack aged in .JAR f iles. Th e EJB JAR f ile con t ains all t he EJBs and a deploy m en t d escrip t or ( ejb-jar.xml) . EJBs ar e cont ained in an EJB pr oject , w hich w e discuss in " EJB pr oject " on p age 35 9. Op t ionally w e can h ave st an d- alone clien t ap plicat ion s t hat use EJBs, f or exam p le. An app licat ion client is also pack aged in a JAR f ile. Th e app licat ion client JAR con t ains all t h e Jav a classes of t h e applicat ion client an d a deploy m en t d escr ip t or ( applicationclient.xml) .
Figur e 9- 1 9 sh ows how WAR f iles and JAR f iles t og et h er con st it u t e t he EAR file, w hich also cont ains t he app licat ion deploy m en t descrip t or ( application.xml) .
Figu r e 9 - 1 9 . J2 EE pa ck ag ing
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
EAR Deploy deployment EJBs t o W eb Sp her e Applicat ion Ser v er This proj ovides ailed at ion on ow t the o ef fectponent ively use WebSp here St udio Ex porI BM t ingRedbook an EAR pr ect in tdoetan EARinffor ile m assemb leshall com s ( EJB, W eb , and client App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans pr oj ect s) in t o one file t hat can b e deploy ed int o a J2EE conf orm ing app licat ion ser v er , such as (WebSph EJB) ar ch re, an d dServ ep loym er eit ect Ap uplicat ion er . ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase.
Web project
I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep e oj arect ch itw ect ur e. I n wpar w eepoint out fu nct ions r od pose uced aw it h t he You uset saand W ebt hpr hen y ou antt icu t olar cr eat and m aint the ainn rew esour ces t h atintcom Web EJB licat 2. 0 ion. sp ecif ionprtoject hat prcont ov idain e, sfor exst am ple, ion alit forf iles contt hain anag ed t o app A icat Web t he r uct ur eenofhanced f oldersfun t octcont ain yall at er ar- emneeded perild sistaence it y b ean and ically m essagedr ivapp en licat beans. alsos pr e best pr act ice es, guidelines bu Web ent ap plicat ions. Typ , a W eb ion We consist ofovid HTML p ages, imag XML, f or successful imp lem at ions of EJBs. ser v let s, JSPs, andent JavaBeans. How t o b uild a Web ap plicat ion is d escrib ed in " Dev elopin g a ser v let client " on p age 59 0. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At Creating a Web project t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. To cr eat e a W eb pr oj ect do t h e follow ing:
Select File - > New - > Proj ect . Select Web in t h e lef t p ane, Web Proj ect in t h e rig ht pane, and click Next t o st ar t t h e w izar d. Specify t h e Pr oject nam e an d t he w or kspace locat ion for t h e pr oject . Choose bet w een a J2EE Web pr oj ect ( wit h serv let s, JSPs, and EJBs) an d a St at ic Web pr oject ( HTML only) . For
t his d ocum en t , w e alw ays cr eat e J2 EE Web p roj ect . Opt ionally , select Cr eat e a CSS f ile ( HTML st y le sh eet ) . Here is also wh er e y ou specif y if you w ant St r ut s sup por t in t he J2EE Web pr oject . See " Dev elopin g a St r ut s- based W eb client " on p age 60 9 for inf or m at ion on St r ut s. On t he J2EE Set t ing s page, y ou specif y t he EAR p roj ect , eit h er an ex ist in g on e or a new one. Sp ecify t he Con t ex t root , t h e alias t hat w ill be used in URLs t o access p roj ect • Table of Content s r esour ces. Select t h e J2EE level, 1 .3 or 1 . 2. Not e t h at y ou can r un a 1. 3 pr oject on ly in a • WebSphI ndex er e Ver sion 5 ser v er , b ut you can ru n a 1. 2 pr oj ect in eit her WebSph er e Ver sion 4 EJB 2 . 0orD e ve lopm nt w it h W ebSphe r e St udio Appl ica ti on De v el oper Ver sione 5. By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
On t he Module D ep en dencies p age, y ou can specif y JAR files r eq uir ed by t he Web app licat ion, for ex amp le EJB mod ules w it hin t he sam e EAR pr oject . Pub lish er: I BM Pub Dat e: April 1 1, 20 03
On t he St ru t s Set t ings page, y ou sp ecif y if an d wh er e t he St r ut s r esour ce bun dle should be I SBN:ed. 0- 73The 84- 26 09 -1 rce bu ndle h olds t ex t const ant s t hat can b e used in Web pages. creat r esou Pages: 75 0
Click Fin ish. You r Web p r oject is aut om at ically opened in t h e Web p er spect ive. When y ou cr eat e a n ew Web p roj ect , a def ault d irect or y st ru ct u r e is cr eat ed t hat reflect s t he J2EE view of a Web app licat ion. A Web deploy ment descr ipt or web.xml is generat ed in t he /webApplication/WEB-INF f older . Lear n about t he EJB 2 .0 specif icat ion
Web Dev application elop EJBs w archive it h St u dio files App(WAR licat ionfiles) Dev eloper t o W eb Sp her Applicat ionf Ser v er As defDeploy in ed in EJBs t he J2EE specif icateion , a W AR ile is an ar chiv e for m at for Web applicat ions. The WAR file is a packag ed f orm at of you r Web applicat ion t hat con t ain s all t h e r esour ces ( HTML, This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio ser v let s, JavaBeans, JSPs, XML, XML sch em as) t hat com pose y our Web app licat ion. App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (You EJB)can ar ch it ect an dapplicat d ep loym entbyofit self su cht oapan plicat ion s ion t o aser WebSph App licat ion Serv d ep loyu are,Web ion applicat ver by ercreeat ing a WAR f ile.er. Select File - > Exp ort - > W AR an d sp ecify t he out put file and d ir ect ory . Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un ing rhelat ional ase. t o hav e t h e Wor kb en ch cr eat e t he EAR f ile t hat con t ains t h e I n derly general, ow ev er, dat it isab easier WAR file and d ep loy t he EAR t o an applicat ion ser ver . I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Struts per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. A d iff icult aspect of bu ild in g a Web applicat ion is conn ect ing com ponent s t hat com p rise dispar at e It echnologies n Part 2, we (in t r oduce t h e bu sam le bank ing rapplicat andt ert hen f or exam ple, ildping t he cont oller, inion MVC m s) .imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem der m et hlever ods, ages dif ferSt enrtutms,app g st at egies, simard p le fclient s t hruatctuinse t he EJBs. At App licatent ion fin Developer an inem errging op enand st and or const g MVCbased tWeb he enapplicat d, w e dions. escribSter ut how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. s p rov ides ( am ong ot h er t hings) an act ion ser v let t hat m anages t h e r unt im e r elat ionship bet w een JSPs and act ions. The use of St r ut s h elps t o ensur e an eff ect iv e sep arat ion of code r esponsibilit ies an d developer r oles ( see " Dev elopin g a St r ut s- based W eb client " on p age 60 9 for a m ore det ailed descr ipt ion of St r ut s) . App licat ion Developer pr ov ides a p ow er f ul v isu al com ponent assem bly env iron ment , t he St ru t s app licat ion diagr am edit or , f or St r ut s- based Web ap plicat ion s ( Figur e 9- 2 0) .
Figu r e 9 - 2 0 . Vi su al a sse m bly of W e b a ppl icat i on fl ow
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper The d iagr am edit or is u sed t o d ef ine t h e basic flow of t he Web applicat ion g r aphically , Deploy EJBsw titohWcom eb Sp her e Applicat er connect ing JSPs p onent ser vicesion ( orSer actvions) as desir ed. This appr oach sim plifies t he creat ion of an MVC applicat ion b y m ask ing t he com plex it y of t he d isp arat e t echn ologies in volv ed. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App ion ev elopm entt of of tap ion pr s based t h e Ent prelopm ise Javen aBeans The licat d iagr amDeveloper edit or is uf or sedt he initdially as par heplicat design ocess, on helping a er d ev t t eam (qu EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv ick ly lay out view ( JSP) and m odel ( act ion) comp onent s w it h out h avin g t o consider t he er. t echnical issu es of com binin g d isp arat e t ech nologies t hat hav e y et t o b e creat ed or har v est ed Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an fr om ex ist in g cap abilit y. Th r ough out t he dev elop ment pr ocess, t h e diagr am edit or can be u sed t o un derly ing r elat ional dat ab ase. ext end an d t est a Web applicat ion's cap abilit y. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic The act ions defined in t he v isu al assem bly env iron m en t can be im plement ed in diff er ent concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he t echnologies, for ex am ple, Jav aBean s or EJBs. EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per y b ean m essageen mb beans. also e best prsact icepon guidelines f or Thesist Apence plicatent ionitDev elopserand also pr ovid esdraivnu er of We wizar ds pr t o ovid d ef ine St r ut com en t s, su ch successful imp lem ent at ions of EJBs. as act ion classes, act ion for m s, and JSPs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Struts project im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. A St ru t s pr oject is a special case of a Web p roj ect t hat includes: St r ut s t aglibs and St r ut s con figu rat ion f ile ( struts-config.xml) in t h e WEB-INF f older St r ut s r u nt im e JAR file ( struts.jar) in t h e WEB_INF/lib d irect or y St r ut s r esour ce bun dle ( ApplicationResources.properties) for t h e ext er nalized u ser int er face r esou rces A St ru t s pr oject is defined as a Web pr oject . Th e St r ut s sup por t is ad ded t o t he Web p roj ect du rin g t h e creat ion of t he p r oject t hr ou gh t he n ew Web p roj ect w iz ard .
EJB project I f you w ant t o develop Ent er pr ise JavaBeans ( EJBs) , y ou hav e t o cr eat e an EJB p roj ect fir st . An EJB pr oject is a logical gr oup f or or ganizing t h e EJBs. To cr eat e an EJB pr oject : • •
Table of Content s I ndex
Select File - > New - > Proj ect .
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Select Wah li, Wout Schuand nk , Deborah Weiss EJB er inDenay t h e er, lef tLarp sane EJB prShaddon oject in, Mart t h einrig ht pane
an d click Next .
the PubSpecify lish er: I BM
Nam e of t he EJB pr oject an d t he w or ksp ace locat ion. You also h ave t o specif y an EAR pr oject n am e t hat w ill con t ain y our EJB p r oject . You can select an ex ist ing EAR Pub Dat e: April 1 1, 20 03 pr oj ect or creat e a new one. Click Next . I SBN: 0- 73 84- 26 09 -1
Pages: 0 On t he75Mod ule Depend en cies p age y ou can specify JAR files r eq uir ed b y t he EJB app licat ion, for ex am ple ot h er EJB m od ules wit h in t h e same EAR pr oject .
When y ou click Fin ish, t h e EJB pr oject op en s in t he J2EE perspect ive. Th e deploy ment descr ipt or for t he EJB m odule ( ejb-jar.xml) is cr eat ed in t he /YourProject/ejbmodule/META-INF f older . Lear n about t he EJB 2 .0 specif icat ion elop EJBs wdescriptor it h St u dio App licat ion Dev eloper EJB Dev deployment (ejb-jar.xml) Deploy EJBs t o W eb Sp her e Applicat ion Ser v er An EJB mod ule r eq uir es a deploy m en t d escr ip t or ( ejb-jar.xml) in t h e same w ay a Web app r eq uirespraovides deployd ment ( web.xml) Thislicat I BMion Redbook et aileddescript in for mor at ion on h ow. t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans I n addit ion t o t he st andar d deploy m en t d escrip t or , t he W ork bench also defines EJB b in dings and ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. ext ensions. Bot h b in ding and ext en sion descr ipt or s ar e st or ed in XMI f iles, ibm-ejb-jarbnd.xmi an dt he ibm-ejb-jar-ext.xmi, r esp ectbased iv ely . on a simp le bank ing app licat ion w it h an Thr oug hout book , w e pr ov id e exam ples un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as aeditor p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic EJB deployment descriptor concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 t sp icat ionent t hat pr ovipt idor e, ffor ple, en ule: hanced fun ct ion alit y for cont ain er - m anag ed To edit heecif deploym descr or tex h eam EJB mod per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part in t r oduce p leper bank ingivapplicat ion EJB and Modules. t hen imp lem ent en t it y beans, session I n 2, t heweJ2EE v iew oft ht e hesam J2EE spect e, exp and beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Righ click EJB moddif ule select Wit h - > and EJB Ed or just t he At im plem entt - fin dert hme et h ods, ferand en t m app inOp g en st r at egies, simitporle, client s tdoub h at uleseclick t he EJBs. t he enmd,odule. w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. The ejb-jar.xml d ep loym ent descr ipt or op en s in t he EJB deploy m en t d escrip t or edit or . Figur e 9- 2 1 sh ows t he Beans p age of t h e ed it or. The Ov er view pag e w as show n in Figur e 911 on p age 34 4.
Figu r e 9 - 2 1 . EJB dep loym e nt de scr ip t or e dit or
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
The ejb-jar.xml is pr esent ed in sever al sect ions defined by t h e t abs at t h e bot t om of t he edit or .
Lear project n about t he EJB 2 .0 Server
specif icat ion
Dev elop EJBs w it h St u dio App licat ion Dev eloper To t est an EJB or W eb pr oj ect , y ou hav e t o define a ser v er w it h a ser v er con figu rat ion t o pub lish and r un t he cod e. tSer er sSpan d ser ver conf igur at vions Deploy EJBs o Wv eb her e Applicat ion Ser er ar e d ef ined in serv er pr oject s. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ionaDeveloper Creating server project f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. To cr eat e a n ew ser v er pr oject : Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we r oduce asect a .p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Select Filein -t > New -EJBs > Proj concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2.Select 0 sp ecif Serv icat erion in t hat h e lef prtov p ane id e, and for ex Serv amer ple, pr en oj ect hanced in t h e fun rig ctht ionpane. alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or Specify a lem Nam e at f orions y our r oject and click Fin ish. We will u se ItsoServers as our p roj ect successful imp ent of pEJBs. nam e. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session Aft er cr an eatding a p roje-ect he beans Ser ver using per spect iv e opens can now ad d elop a sererv.erWe and a beans, m essag d r,ivten WebSph er e Stand u dioyou Ap plicat ion Dev also ser v er con r at ion . h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At im plem ent figu fin der m et t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Servers A ser ver ident if ies t he serv er used t o t est y our ap plicat ion . Unlike VisualAge for Jav a, Ap plicat ion Dev eloper h as t he op t ion t o deploy t o and t est w it h bot h local and r emot e inst ances of t he WebSph er e ap plicat ion serv er, and addit ion ally Ap ach e Tom cat . Her e is a br ief ex planat ion of •each of t h e ser Table v erof s. Content s •
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
WebSphere Version 5.0
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
lish er: ThisPub enab lesI BM t he dev elop er t o w or k w it h an int eg rat ed v er sion of WebSp her e Applicat ion Ser v er Pub Dat e: 20 03ort s t he ent ir e J2 EE 1. 3 and 1. 2 pr ogr am min g m odel. Th is is t he b est Version 5, wApril h ich1 1,supp I SBN: 073 8426 09 -1based ap plicat ion s. Thr ee opt ions ar e p rov ided: opt ion f or t est in g EJBPages: 75 0
Te st Env ir onm en t — Bu ilt - in serv er inside t h e Applicat ion Developer. Applicat ion Dev eloper conf igur es t he ser ver t o r un t he p r oject s fr om t h e w ork space and st ar t s t h e ser v er in a sep arat e pr ocess. Re m ot e Se rv er — St and- alone ser ver on t h e sam e or ot her m achine. When t he applicat ion Lear n about t heplicat EJB ion 2 .0 D specif icat is execut ed, Ap ev elop erion pub lishes t h e code t o t he ex t er nal ser v er and at t emp t s t o st ar t t he ser ver using t h e I BM Agent Con t r oller serv ice, w h ich is supp lied w it h Dev elop EJBs w it h St u dio App licat ion Dev eloper App licat ion Developer . This f eat ur e pr ov id es a v er y ef ficient app roach t o rem ot ely deploy ing an applicat ion. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Re m ot e Se rv er A t t ach— A ser v er inst ance t hat w ill at t ach t o a Web Spher e Version 5 This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio ser v er t hat is already st ar t ed. App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr 4.0 ov id e exam ples based WebSphere Version
on a simp le bank ing app licat ion w it h an
un derly ing r elat ional dat ab ase. This enab les t he dev elop er t o w or k w it h an int eg rat ed v er sion of WebSp her e Applicat ion Ser v er I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Adv anced Edit ion Single Ser ver Ver sion 4. 0. 1, w hich sup por t s t h e en t ire J2EE 1. 2 pr ogr am m ing concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he m odel. The sam e t h ree opt ion s as f or Ver sion 5. 0 are p r ovided. EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
Apache Tomcat Version 4.0 and 4.1
I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, d sion m essag e- dbr iv en developed beans using er e St u dio Ap plicat . We also Tom cat an Ver 4 has een by WebSph t h e Apache g rou p on a comion p letDev ely elop sep er arat e cod e base im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he At2 fr om t he Ver sion 3. 2 r elease, an d is t he r efer en ce im plem en t at ion for t h e Serv let 2. 3 andEJBs. JSP 1. tspecif he enicat d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. ion s. For m ore in for m at ion on Tom cat and t he Ap ach e Jakar t a pr oject , see ht t p: / / j akar t a. apache. or g. App licat ion Developer does n ot ship w it h t he Tom cat b in ar ies, bu t on ly a t oolk it t o su ppor t it s execut ion. You m u st already hav e a w or k in g Tomcat inst ance inst alled in ord er for t his t o w or k . Tw o op t ions ar e pr ov ided: Te st Env ir onm en t — Ap plicat ion Dev elop er con figu res Tom cat t o r un t he p r oject s fr om t h e w or kspace an d st ar t s t h e ser v er in a separ at e pr ocess. Loca l Ser v er —St and - alone Tom cat serv er on t he sam e m achine. Perm it s p ublishing and
execut ion of t he W eb app licat ion t o an ex t er nal ver sion of Tom cat 4 .0 . Unlike t he WebSph er e Rem ot e Ser ver op t ion, t h is is only supp or t ed f or a local inst an ce on t h e same m achine.
Apache Tomcat Version 3.2 •This r elease sup Table of tContent s vlet 2. 2 an d JSP 1. 1 specif icat ion s. The sam e t w o op t ions as f or por s t he ser •Version 4 areI ndex p r ovided. EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Publishing Server Pub lish er: I BM Dat e: April serv 1 1, 20er 03 supp ort s t he p ublishing of st at ic Web p r oject s, as w ell as J2EE pr oj ect s ThePub p ublishing I SBN: 073 8426 09 such as EAR pr oject s, -1Web p r oject s, and EJB pr oject s. Pages: 75 0
Static Web Server A st at ic W eb ser v er is a Web ser v er t hat ru ns st at ic Web pr oject s. Use t he st at ic Web ser v er f or t he t est ing of HTML and Jav a scr ipt f iles. JSPs and EJBs ar e not supp ort ed . Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
Remote Application Server Attach
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This is a ser v er t hat w ill at t ach t o a g en er ic ap plicat ion ser v er t hat is already st ar t ed. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
TCP/IP Monitoring Server
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly This is aing simr elat p le ional ser verdat t hab at ase. f or war d s r eq uest s and responses, and m onit or s t est act iv it y . Th is r unt im e env ir onm en t can only be ru n locally, and it on ly su ppor t s Web pr oj ect s. You cannot I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic deploy p roj ect s t o t h e TCP/ I P Monit or in g Ser v er . concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2.se 0 sp ecif icatdoes ion t hat oveidEJB e, for ex amt ,ple, hanceddeploy fun ct ion alitf iles y fort ocont - m anag Becau Tomcat not pr hav suppor youen cannot EAR it , bain ut er only WARed files per sist ence ent it y b ean s and m essagedr iv en beans. We also pr ovid e best pr act ice guidelines f or cont aining serv let s and JSPs. successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem fintder I ment por a ntm et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Befor e y ou can do a r em ot e unit t est y ou hav e t o inst all an d ru n t he I BM Ag en t Con t roller , w hich com es w it h Ap plicat ion Dev elop er , on t h e r em ot e m achine. I BM Agent Cont roller is a p r ocess t hat r uns on t h e r em ot e m achine and w hich enab les clien t app licat ions t o st ar t n ew host p rocesses.
Server configuration A ser ver conf igur at ion con t ains t h e infor m at ion about t he ser ver .
The Ser v er Conf ig ur at ion view of t h e Ser v er perspect iv e sh ow s t he ser ver s and t he pr oject s t hat ar e assig ned t o a conf igur at ion. The Ser ver s v iew sh ows t he serv ers for st ar t and st op op er at ions ( Figur e 9- 2 2) .
Figu r e 9 - 2 2 . Ser v er conf i gur a t i on an d ser v e r s •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
A ser ver conf igur at ion is st ored in XML f iles in t h e ser v er pr oject . The p rop er t ies can be set by Pages: 75 0 opening ( d oubleclick ing) t he conf igur at ion in an edit or . A ser ver conf igur at ion defines: Por t n um ber s for t he dif fer en t p rocesses such as t he n am in g ser vice Lear n about t he EJB 2 .0 specif icat ion Mim e t y pes Dev elop EJBs w it h St u dio App licat ion Dev eloper JDBC dr iv er s Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Dat a sour ces This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Secu rit y en ablem ent App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. EJB t est client and adm inist r at ive con sole enab lem ent Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Messag e list ener , qu eu e m anager s, and queue definit ions un derly ing r elat ional dat ab ase. Each pr oject h as a p ref er r ed serv er conf ig ur at ion t hat is used w hen t he p r oject is r un by I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic select ing Ru n on Ser ver f r om it s cont ex t m enu ( t his can be set in t h e pr oject Pr oper t ies - > concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he Serv er Pr ef erence ) . EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
Creating a server and a configuration
I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an der m essag e- div r iv beans using WebSph Stvuer dioand Ap plicat Dev elop er . We I n t he serv per sp ect e,en select New - > Ser ver - >er eSer ser v erion con figur at ion an dalso com plet e im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he win dow as sh own in Figur e 9- 2 3. t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Figu r e 9 - 2 3 . Cr ea t i ng a se r ve r a n d ser v er con fi gur a t i on
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
I n t he nex t w ind ow , set t he por t ( d ef ault is 808 0) an d click Fin ish. Lear n about t he EJB 2 .0 specif icat ion The n ew ser v er appear s in t he Ser ver per spect iv e and y ou can assign EAR pr oject s t o t he serv er. ThoseDev p roj ect sEJBs w ill b en licat t h e ion ser vDev er is st ar t ed. To assig n a pr oject t o a ser ver , select elop weit hloaded St u diow h App eloper t he ser ver an d Add - > Proj ect ( con t ex t ) . Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BMtemplates Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Server App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. When y ou h ave t o cr eat e sev er al sim ilar ser v er s or conf igur at ions, y ou can cr eat e t hem f r om a tThr emoug plathout e t o tsav t im e. a t empon latae simp across t eam t h licat at t eam he ebook , wYou e prcou ov idlde also examshar plese based le abank ingso app ion wmitemb h aner s can st ar t p er sonalizing t heir ser v er con figu rat ion or in st an ce st ar t ing f r om a t em plat e. To cr eat e a un derly ing r elat ional dat ab ase. t em plat e: I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Select Window - > Pr eference . per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful lemerent oft EJBs. Ex panimp d Serv onat tions h e lef p ane an d select Tem p lat es. I n Part 2, Add we inan t r doduce t h et he sam p leerbank and lat t hen Click sp ecify serv t o bing e stapplicat or ed asion a t emp e. imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Starting and stopping a server
A ser ver can be st ar t ed f r om t he Ser v er s v iew in n or mal or debu g m ode. To debu g serv let s or JSPs at t he sour ce level, y ou mu st st ar t t h e ser v er in d eb ug mod e. Not e t hat st ar t up and execut ion is slow er in debu g m ode. A ser ver can be st ar t ed ex plicit ly f r om t he Ser v er s v iew by click ing t h e St ar t icon. A ser v er can be st ar t ed imp licit ly by select ing a Web r esou r ce ( f or ex amp le an HTML f ile) and Ru n on Ser ver or Debug on Ser v er ( con t ex t ) . The f ir st t im e you select Ru n on Ser ver in a Web p roj ect , you ar e pr om pt ed t o select a ser v er
( Figur e 9- 2 4) . You can by pass t his w ind ow in t he f ut u re by select ing Do n ot show t his dialog t he nex t t im e. Fr om t hen on, t he serv er associat ed w it h t h e Web p roj ect is st ar t ed aut omat ically.
Figu r e 9 - 2 4 . Sel ect in g a se r ve r a nd m ak i ng it t he pr e f er r e d ser v e r
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Befor a ser ver is st t ed, t hdeetassociat ed m prat oject pub This I eBM Redbook prarovides ailed in for ion sonarheow t olished. ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans When y ou ar e d one w it h t est in g, y ou st op t h e ser v er ex plicit ly fr om t h e Ser v ers view . When ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. cer t ain r esour ces ar e mod ified ( for ex amp le EJB definit ions) , a ser v er mu st be rest ar t ed . Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase.
Publishing
I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s gand t h e ar ch iting ect ur t icuces lar twhat e point out irt he fu nct ions inttr oodt huced t he Pub lishin m eans copy alle.t hI en rpar esour are requ ed nt oewt est a pr oject e rigwhtit hplace EJBt h2. ov id e,I nforcases ex amwple, hanced funinctgion ainen erch - m, anag so at0 tsp h eecif sericat v erion cant hat f in dprt hem. hen en y ou ar e t est w italit hiny tfor h e cont Wor kb t he ed sistces encement b ean s band iv en beans. We walso ovidareebest prgact guidelines rper esour ig htit yalready e atmt essagehe r ightdrplace. How ev er, henpr y ou t est in w ice it h WebSph er ef or successful App licat ionimp Serlem v erent onataions localoforEJBs. r emot e mach in e, or w it h Tomcat on a local m ach in e, pub lishin g m ean s t o cop y t he r esour ces ou t sid e of t he Wor k bench. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d, tm e- datr iv en beans using WebSph er e ser St uvdio Apt plicat Dev elop er .isWe By def ault h essag e Aut om ically pub lish b ef or e st art ing er op ion is ion t ur ned on. Th optalso ion can im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he all EJBs. be f oun d b y select in g Window - > Pr eferences - > Ser v er . Wh en t h is opt ion is t ur ned on, filesAt tand he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. f old er s on t h e ser v er are sy nchr onized w it h t h e Wor kb en ch w h en ev er st art in g or rest ar t ing t hat ser v er. You can m anu ally pub lish by select ing t he ser ver in t he Ser v er s v iew of t he Ser ver p er spect ive and select ing Pub lish f r om t he con t ex t m en u.
Remote server For a r em ot e WebSph er e Ap plicat ion Serv er , you m ust con figu re how t h e files ar e t r ansfer red t o t he ser ver . The Cr eat e a Ser ver w indow pr om pt s y ou for :
The in st allat ion dir ect or y w her e t h e ser v er is in st alled, f or ex amp le C:\WebSphere\AppServer. The p lat for m ( Window s or Ot h er ) . On e of t w o possible t r ansfer m echanism s: •
Table of Content s
• EJB 2 . 0
- Cop y file t r ansfer m echanism — You m ust sp ecify t he r emot e t ar get d ir ect ory ( as I ndex seene nt fr om heebSphe local rm ach ineAppl on ica a ti LAN drviv e) , for ex am ple X:\WebSphere\AppServer. D e ve lopm w it ht W e St udio on De el oper
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er:
- FTP f ile t ran sf er m echan ism — You m ust sp ecify t he r emot e t ar get d ir ect ory ( on t he r em ot e machin e) , t he host nam e, login user I D and p assw or d, conn ect ion t im eou t , I BM and opt ion ally fir ew all set t ings.
Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Agent Controller The I BM Agent Cont r oller is a ser vice t hat m ust b e inst alled on t he r em ot e m ach ine. The Ag en t Con t roller code is p rov ided w it h t h e App licat ion Developer f or all plat f orm s su ppor t ed by WebSph er e Ap plicat ion Serv er . Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
EJB development environment App licat ion Developer pr ov ides a f ull EJB d ev elop m en t env iron m en t . We can define EJBs of all t y pes, m ap cont ain er - m anag ed ent it y EJBs t o relat ional d at abase t ab les, im plement qu er ies using EJB QL, generat e deploy ed code, and t est EJBs in a bu ilt - in WebSp here Ap plicat ion Ser ver . •
Table of Content s
•The b asic sequ I ndex en ce of op er at ions w hen d ef ining and t est ing EJBs in Applicat ion Developer is as EJB 2 . 0s: D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper follow By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
PubSet lish er: u pI BM an
EJB pr oj ect
Pub Dat e: April 1 1, 20 03
Defin I SBN:e0-EJBs 73 84- 26 09 -1 Pages: 75 0
Add bu sin ess logic Op t ionally define r elat ionships an d in herit an ce Defin e find er met h ods Map it y b ean t o r2elat ional icat t ab les Lear nent about t he sEJB .0 specif ion Gener at eEJBs t he deployed code Dev elop w it h St u dio App licat ion Dev eloper Test t heEJBs EJBst oinWt he b uilt ser ver uion sin Ser g t hveerun iv er sal t est client Deploy eb Sp her- in e Applicat eat e client app licat ions This ICr BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans I( n t he ar chap er su tre, h atan f ollow w e descr ibesuan implicat plemion en ts at obank ing samp applicat ion.Serv W eer. w ill EJB) ch ittect d d ep, loym ent of chdap a WebSph er eleApp licat ion use all t h e feat u res of t h e EJB developm ent en vir onm ent . We develop t he sam ple st ep- by - st ep, and at hout each tst epbook g en ,erwateeprd ov ep id loy codples e and t est ton he aEJBs he ser verapp . licat ion w it h an Thr oug he e ed exam based simpinle t bank ing un derly ing r elat ional dat ab ase. Descr ibeint he sam ple appas licat seta up t he t er prEd ise app( licat ionand andcover t he ser f or I n Part 1, we t r oduce EJBs a pion art and of Jav 2 En t eren p rise it ion J2 EE) t hever basic t est in g in Ap plicat ion Dev eloper . concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Dev elop anaged persist en t it y beans, includ in ge relat per sist ence entcont it y ainer b ean s- m and m essagedr ence iv en beans. We also pr ovid best ionships pr act ice and guidelines f or inher it ance. successful imp lem ent at ions of EJBs. Dev fint rder m ett hh ods u sing EJB qu y lang uage. I n Part 2,elop we in oduce e sam p le bank ingerapplicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Descr ibe d iff er ent map ping app roaches f or cont ainer - m anaged EJBs. im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he enDev d, w e d escrib t o deploy EJB ap plicat s t o a WebSph er e App licat ion Serv er. elop a b eane -how m anaged persist ence en t it ion y bean. Dev elop a st at eless an d a st at efu l session b ean . Gener at e a Web ser v ice fr om a session bean. Descr ibe an d generat e access beans. Dev elop a m essag e- d riv en b ean an d a t est client . Dev elop client app licat ions t hat u se t he EJBs. Samp le client s in clud e ser v let s, a St r ut s app licat ion, J2EE clien t s, and a Web ser v ice clien t .
Chapter 10. Introducing and preparing for the sample application •I n t his chapt er Table of dContent , we escribse t he m od el and dat abase f or a b ank ing applicat ion. I n subsequent •chapt er s, w eI ndex im plem ent par t s of t h e m odel as ent er pr ise b ean s, and par t s of t h e bu siness logic EJB 2 . 0 D e ve beans. lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper as session By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
I n addit ion, we pr ep are Applicat ion D ev eloper pr oject s and a ser ver t o im plem ent and t est t h e sam ple applicat ion. Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Bank model The I TSO ban k m odel consist s of a few ent it ies an d r elat ionships ( Figur e 10 - 1) .
•
Table of Content s
•
I ndex
Figu r e 1 0 -1 . Ba nk m odel
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs to W eb Sp herine tApplicat ionare: Ser v er The ent it ies an d r elat ionships he mod el This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans Cust om e r A cust om er of t he bank . ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Account A g en er ic b ank accou nt . A cust om er may hav e mu lt iple ban k accoun t s and an Thr oug hout t he book w emay pr ovbe id eow exam simp ing app licat accou, nt ned ples by mbased ult ip leon cuast om erle s. bank A ban k accou nt ion is eitwhiterh an a un derly ing r elat ional dat ab ase. check ing or a saving s accou nt . I Che n Part r oduce EJBs a gener p art ofic Jav a 2accoun En t er pt rise Ed it ion ( J2 EE) and cover t he basic ck i1, ngwe in tA sub class of as the bank . concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he Sa v2. ing s ecif icat Aion subtclass h ee,gener bank t . fun ct ion alit y for cont ain er - m anag ed EJB 0 sp hat prof ovtid for exicam ple, accoun en hanced per ence ent y tbran eansact s and essagedr ivisengenerat beans.edWe ovidking e best pr act ice Trasist nsRe cor d itA ion mr ecor d t hat foralso eachprban t r ansact ion,guidelines such as a f or successful imp lem ent at ions of EJBs. deposit , w it hd r awal, or t r ansfer of m oney bet w een t w o accoun t s. A b ank accou nt may hav e m any t r ansact ion r ecord s. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also We w ill ent im plement heh ods, en t it ies asen ent tm erapp p rise For illust ion p ur poses, se EJBs. At im plem fin der mt et dif fer in gbeans. st r at egies, andr at sim p le client s t hwe at uwill se tuhe cont ainer m anaged and beanm anag ed ent it y b ean s. t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. We w ill also u se t he adv anced feat ur es of t he I BM EJB pr oduct set t o im plement t h e inher it ance of t he bank account s. We w ill im plement a mon ey t ran sf er bet w een t w o accoun t s as a session bean.
Bank database The b ank m odel is b ased on an und er lyin g r elat ional d at abase. The EJBBANK d at abase consist s of t he t ables sh ow n in Figur e 10 - 2. •
Table of Content s
•
I ndex
Figu r e 1 0 -2 . Ba nk dat ab ase an d t ab le s
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Tables All t h e t ables hav e a pr im ar y k ey. Som e t ables hav e a f oreign k ey t hat relat es t he t able t o anot h er t able. The d at a t y pes u sed in t he t ab les ar e f or illu st rat ion and d o not r ep resent a r eal ban king sy st em .
Customer table
The CUSTOMER t able includes all t h e inf orm at ion r ecor ded for a bank cu st om er ( Table 1 0- 1) .
Tab le 1 0 - 1 . Cu st om e r t abl e •
Table Colum n na m eof Content s Ty pe
•
Le ng t h
Ke y
I ndex
N ul ls
D e scri pt i on
CUSTOMERI D e nt w it h IWNTEGER PK EJB 2 . 0 D e ve lopm ebSphe r e St udio Appl ica ti on De v el oper
No
Cu st om er I D
By Lar s Schu nk , Deborah Weiss TIUeli TLEWah li, Wout er Denay er, CHAR 3 Shaddon , Mart in No
No
Tit le
FI RSTNAME
VARCHAR
30
No
No
First nam e
20 03 VARCHAR
30
No
No
Last nam e
CHAR
8
No
Yes
User I D
PASSWORD
CHAR
8
No
Yes
Passw or d
ADDRESS
BLOB
20 00
NO
YES
Add ress obj ect
Pub lish er: I BM
LASTNAME Pub Dat e: April 1 1,
I SBN: 0- 73 84- 26 09 -1
USERI D
Pages: 75 0
The ad dr ess in for m at ion can be st or ed eit her as a BLOB in t h e CUSTOMER t able, or as ind iv idu al fields in t he ADDRESS t able. We w ill exp lor e t h e m appin g of cust om er ent it y beans in t o on e t able Lear n about t he EJB 2 .0 specif icat ion and int o t w o t ab les. Dev elop EJBs w it h St u dio App licat ion Dev eloper Deployaddress EJBs t o Wtable eb Sp her e Applicat ion Ser v er Customer This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio CUSTADDRESS is a depend en t t ab le of CUSTOMER. Th e CUSTADDRESS t able includes t h e det ailed App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans add ress fields w h en cust om er in for m at ion is map ped in t o t w o t ables ( Table 1 0- 2) . ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase.
Tabas le a1p0art - 2of . Cu ddr le and cover t he basic I n Part 1, we in t r oduce EJBs Jav st a 2om En teerrpa rise Edeitss ion t(ab J2 EE) concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp icat pr ov id for ple, contpt ain er - m anag ed Colum n ecif na m e ion t hat Ty pe Lee,ng t hex amKe y en hanced N ul ls fun ct ion alit y for D e scri i on per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or CUSTOMERI Dlem ent I NTEGER PK, FK No Cu st om er I D successful imp at ions of EJBs. STREET CHAR Yes andStt hen r eet imp namlem e, ent nu mb In Part 2, we in t r oduce t h e sam20 p le bankNo ing applicat ion ener t it y beans, session beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also CI TY CHAR 12 No Yes Cit y im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he STATE en d, w e d escrib eCHAR how t o deploy 12 EJB No ap plicat ion sYes t o a WebSph St at e orer e cou App nt rlicat y ion Serv er. ZI PCODE
CHAR
10
No
Yes
Post al code
The cu st om er add ress t ab le in clu des t he cust om er I D as a f oreign k ey , w h ich also ser v es as t he pr im ary key . Ther ef or e, only one ent ry in t h e cu st omer ad dr ess t ab le can p oin t t o a m at chin g ent r y in t he cust om er t able.
Account table I nf orm at ion abou t ban k accou nt s is spr ead over t h ree t ab les. Th is design enab les u s t o
im plem ent an inher it ance m odel w it h t w o t y pes of ban k accou nt s, check ing and sav in gs. The ACCOUNT t able includes all t h e inf orm at ion comm on t o bot h check ing and savin gs account s ( Table 1 0- 3) .
•
Table of Content s
•
I ndex
Tab le 1 0 - 3 . Accou nt t abl e
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Colum n na m e
Ty pe
Le ng t h Ke y N ul ls
D e scri pt i on
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
ACCI D
CHAR
8
PK
No
Accou nt I D
DEC
( 8, 2)
No
No
Balance
No
No
I nt er est rat e in p er cen t
8
No
No
Accou nt t yp e ( ACCOUNT, CHECKI NG, SAVI NGS)
DI SCRI MI NATOR CHAR
1
No
No
Accou nt t yp e ( A, C, S)
OVERDRAFT
( 8, 2)
No
Yes
Ov erd raf t amou nt
Lear n about t he EJB 2 .0 specif icat ion MI NAMOUNT DEC ( 8, 2) No
Yes
Minim um amou nt
Pub lish er: I BM
BALANCE
Pub Dat e: April 1 1, 20 03
I NTEREST I SBN: 0- 73 84- 26 SMALLI 09 -1 NT Pages: 75 0
ACCTYPE
VARCHAR
DEC
Dev elop EJBs w it h St u dio App licat ion Dev eloper The accoun t t y pe ( ACCTYPE) colum n ser v es as an indicat or t o t he t y pe of accoun t . The DISCRIMINATOR colum es et he sam e ion pu rp ose Deploy EJBs t o Wnebser Spvher Applicat Ser v erand is used in t he EJB inh er it an ce m od el. We use t w o colu mn s so t h at t h e ACCTYPE can be u sed as an EJB at t r ibut e, w h er eas t he DISCRIMINATOR is pr used t o dist guishin tfor hemEJB t y pes. Thet ocod e valu e Ause w ould be here a gener ic This I BM Redbook ovides d etinailed at ion on h (ow ef fect ively WebSp St udio accou nt .ion ) We use a t rigf or gert he t o dmev akelopm e sur eent hat heplicat t w o ion colum ns ar eonsyt h nchr onized. App licat Developer t of tap s based e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. OVERDRAFT an d MINAMOUNT ar e t he det ails of check ing and sav in gs account s and ar e used if w e use on ly onet he t able ( ACCOUNT) ot h t ples yp es based of accoun Alt er iv ely, e can at e Thr oug hout book , w e pr ovfor id e bexam on at s.simp lenat bank ing w app licatuse ion separ w it h an tun ables and gs account s. derlyf or ingcheck r elating ional datsavin ab ase. CHECKING an dinSAVINGS ar e d as ep en t ables. accoun isiteit ch eck g or a t sav ings I n Part 1, we t r oduce EJBs a pdent art of Jav a 2Each En t er p rise tEd ionher ( J2aEE) andin cover he basic accou nt and, t heref ore, is im plem en t ed w it h a r ow in t h e ACCOUNT t able and a row in eit her he concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h tthe CHECKING or SAVINGS t able. EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
Checking table
I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also The CHECKING t able cont ains t he f ields t hat ar e specific t o ch eck ing accou nt s, in our case an im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At ov er dr af t lim it ( Table 1 0- 4) . Th e balance of t h e accou nt m ay b e negat iv e up t o t his lim it . The t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. ACCID colum n is a f or eig n k ey t hat point s t o t h e r ow in t he ACCOUNT t able.
Tab le 1 0 - 4 . Ch eck i ng t a ble Colum n na m e
Ty pe Le ng t h
Ke y
N ul ls
D e scri pt i on
ACCI D
CHAR 8
PK, FK
No
Accou nt I D
OVERDRAFT
DEC
No
Yes
Ov erd raf t amou nt
( 8, 2)
Savings table The SAVINGS t able cont ains t he f ields t hat ar e specific t o sav ing s accou nt s, in our case a m inim um b alan ce amou nt ( Table 1 0- 5) . Th e balance of t h e accou nt m u st be at least t he m inim um am oun t . The ACCID colum n is a f or eig n k ey t hat point s t o t h e r ow in t he ACCOUNT t able. •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Tab le 1 0 - 5 . Sa vi ngs t abl e
Pub lish er: I BM
Colum na m Pub Dat e:n April 1 1,e 20 03
Ty pe
I SBN: 0- 73 84- 26 09 -1
ACCI D
Le ng t h
Ke y
N ul ls
D e scri pt i on
CHAR
8
PK, FK
No
Accou nt I D
DEC
( 8, 2)
No
Yes
Minim um amou nt
Pages: 75 0
MI NAMOUNT
Transaction records table The TRANSRECORD includes t h e icat inforion m at ion recor ded f or successfu l ban king t r ansact ions Lear n about ttable he EJB 2 .0 specif ( Table 1 0- 6) . Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides Tabdleet ailed 1 0 - 6in.for Trmaatnsa ion on ct ihon ow rt oecor ef fect ds ively t ab use le WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Colum n na m e Ty pe Le ng t h Ke y N ul ls D e scri pt i on Thr oug hout , w e pr ov id26 e exam ples a ansact simp leion bank TRANSI D t he book TI MESTAMP Yes based No on Tr I Ding app licat ion w it h an un derly ing r elat ional dat ab ase. ACCI D CHAR 8 FK Yes Accou nt I D I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic TRANSTYPE CHAR Tr ansact y pe D = int Debit C = Cr concep t s and t h e ar ch it ect ur e. I1n par t icuNo lar w eNo point out t he nion ew tfu nct(ions r od uced w ited h itt )he EJB 2. 0 sp ecif icat ion t hat pr ov id( e, am ple,Noen hanced fun ct ionam alitoun y for TRANSAMT DEC 8, for 2) ex No Tr ansact ion t cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. A d ep osit t r ansact ion g en er at es a record w it h t r ansact ion t y pe D ( debit ) , a w it hdr aw al gener at es tI rnansact ionwe t yp ( cr edit d palembank oneying t r ansfer g en er at es tahen r ecord f or ent eachenaccount inv olv ed, Part 2, inet rCoduce t h)e, an sam applicat ion and imp lem t it y beans, session one w it h t r ansact ion t yp e D ( t o account ) and one wit h t r ansact ion t yp e C ( fr om account ) . beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
I m por t a nt The f or eig n k ey m ust b e defined w it h NULLS f or now , b ecause t h e EJB 2. 0 im plem ent at ion does n ot allow f or non - nullable k eys in 1 : m r elat ion sh ip s.
Customer-Account table
The CUSTACCT t able ( Table 1 0- 7) defines t h e man y- t o- m any r elat ion sh ip b et w een cust om er s and accou nt s, t h at is, a cu st om er can ow n m any accoun t s ( f or exam ple, a ch eck ing and a sav ings accou nt ) , and an accoun t can be ow ned by m any cu st om er s ( for ex am ple, hu sb and and w ife) .
Tab le 1 0 - 7 . Cu st om e r - a ccou n t r e la t i onshi p t abl e
•
Table of Content s
•
I ndex
EJB Colum 2 . 0 D e venlopm e nte w it h W ebSphe r e St udio Appl icang ti on na m Ty pe Le t hDe v el operKe y By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
CUSTOMERI D
I NTEGER
ACCI PubD lish er:
CHAR
I BM
8
N ul ls
D e scri pt i on
PK, FK
No
Cu st om er I D
PK, FK
No
Accou nt I D
Pub Dat e: April 1 1, 20 03
Each colu mn is 84a for eign I SBN: 0- 73 26 09 -1 key p oin t ing t o eit her t h e cust om er or t he account t able. Bot h colum ns t oget hPages: er f or75 m0 t he pr imar y k ey.
Customer information table The CUSTOMERINFO t able holds addit ional infor m at ion about t he cu st om er , such as pict ur es. W e w ill use Lear t hnisabout t able t he o illust EJB r2at.0e specif a beanicat mion anag ed per sist ence ent it y t yp e. TableDev 1 0-elop 8 sh ows out of App t he licat CUSTOMERINFO t able. This t ab le is not link ed t o t h e cust om er EJBst he w it lay h St u dio ion Dev eloper t able b y a f or eign k ey ( alt houg h it could be) . Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d eple loym su ch plicat t om a at WebSph e App Tab 1 0ent - 8 of . Cu st ap om e r ion i nfsor ion ter a bl e licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an n na m eional Ty Le ng t h Ke y N ul ls D e scri pt i on unColum derly ing r elat datpe ab ase. CUSTOMERI I NTEGER PKJav aNo Cu omEd er itIion D ( J2 EE) and cover t he basic In Part 1, we Din t r oduce EJBs as a p art of 2 En t er p st rise concep t s and t h e arI NTEGER ch it ect ur e. I n par t icu lar wNo e pointI nf out t he n ewI D fu (nct ions I NFOI D PK orm at ion 1 , 2, .. . ) int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ean s and m50 essage- dr beans.Descr We also pr ovid bestatpr act ice guidelines f or DESCRI PTI ent ON it y bVARCHAR Noiv enYes ipt ion of infeorm ion successful imp lem ent at ions of EJBs. DATA BLOB 10 000 No Yes Binar y dat a I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- done r iv enent beans er e m Stat u ion. dio Ap plicat elopdat er . aWe This d esign allow s for r y of using each kWebSph ind of infor We m apion t heDev BLOB t o also a by t e im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At ar r ay in Jav a. t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Triggers To k eep t h e t w o colum ns ACCTYPE an d DISCRIMINATOR sy nchr onized, w e use a set of t r igg er s. These t r igger s ar e fir ed af t er inser t s and upd at es and set t he ACCTYPE colum n t o t he cor rect v alue based on t h e DISCRIMINATOR colum n. The t rig gers ar e show n below :
CREATE TRIGGER ITSO.INSERTACCOUNT NO CASCADE BEFORE INSERT ON ITSO.ACCOUNT referencing new as ACCT for each row mode db2sql BEGIN ATOMIC SET ACCT.acctype = •
Table of Content s
CASE
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
WHEN ACCT.discriminator = 'C' THEN 'CHECKING'
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
WHEN ACCT.discriminator = 'S' THEN 'SAVINGS' Pub lish er: I BM
WHEN ACCT.discriminator Pub Dat e: April 1 1, 20 03
= 'A' THEN 'ACCOUNT'
I SBN: 0- 73 84- 26 09 -1
END;
Pages: 75 0
END
CREATE TRIGGER ITSO.UPDATEACCOUNT NO CASCADE BEFORE UPDATE Lear n about t he EJB 2 .0 specif icat ion ON ITSO.ACCOUNT referencing new as ACCT for each row mode db2sql Dev elop EJBs w it h St u dio App licat ion Dev eloper BEGIN ATOMIC Deploy EJBs t o W eb Sp her e Applicat ion Ser v er SET ACCT.acctype This I BM Redbook pr ovides = d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch itCASE ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. WHEN ACCT.discriminator = based 'C' THEN Thr oug hout t he book , w e pr ov id e exam ples on a 'CHECKING' simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. WHEN ACCT.discriminator = 'S' THEN 'SAVINGS' I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic = w'A' THEN concep t s and WHEN t h e arACCT.discriminator ch it ect ur e. I n par t icu lar e point out'ACCOUNT' t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist enceEND; ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. END I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Database definition DDL
The d at abase obj ect s can be cr eat ed u sin g t he DDL show n in Ex am ple 10 - 1.
Exa m pl e 1 0 -1 . Ba nk dat aba se DD L
echo --- create the EJBBANK database --CREATE DATABASE EJBBANK
echo --- connect to EJBBANK database --CONNECT TO EJBBANK
•
Table of Content s
•echo --- drop I ndextables and triggers --EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
DROP TABLE ITSO.TRANSRECORD By Ueli Wah li, Wout er Denay er, Lar s Schu nk ,
Deborah Shaddon , Mart in Weiss
DROP ITSO.CUSTACCT Pub TABLE lish er: I BM Pub Dat e: April 1 1, 20 03
DROP TABLE ITSO.CUSTADDRESS I SBN: 0- 73 84- 26 09 -1
0 DROP Pages: TABLE75ITSO.CHECKING
DROP TABLE ITSO.SAVINGS DROP TABLE ITSO.ACCOUNT n about t he EJB 2 .0 specif icat ion DROP Lear TABLE ITSO.CUSTOMER elop ITSO.CUSTOMERINFO EJBs w it h St u dio App licat ion Dev eloper DROP Dev TABLE EJBs t o W eb Sp her e Applicat ion Ser v er DROP Deploy TRIGGER ITSO.INSERTACCOUNT This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio DROP TRIGGER ITSO.UPDATEACCOUNT App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an echo --- create tables --un derly ing r elat ional dat ab ase. CREATE I n Part 1,TABLE we in tITSO.CUSTOMER r oduce EJBs as a(p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) \and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he NOTexNULL, EJB customerid 2. 0 sp ecif icat ion INTEGER t hat pr ov id e, for am ple, en hanced fun ct ion alit y for\cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or title imp lem entCHAR(3) \ successful at ions of EJBs.NOT NULL, firstName VARCHAR(30) \ en t it y beans, session I n Part 2, we in t r oduce t h e sam p le NOT bankNULL, ing applicat ion and t hen imp lem ent beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also lastName NULL, im plem ent fin der m etVARCHAR(30) h ods, dif fer enNOT t m app in g st r at egies, and sim p le client\s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. userID CHAR(8), \ password
CHAR(8),
\
address
BLOB(2000),
\ PRIMARY KEY (CUSTOMERID)
)
\
CREATE TABLE ITSO.CUSTADDRESS ( customerid
INTEGER
street
CHAR(20),
\
city
CHAR(12),
\
• •
NOT NULL,
\ \
Table of Content s
state
I ndex
CHAR(12),
\
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
zipcode
CHAR(10),
\
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
PRIMARY KEY (CUSTOMERID)
\
Pub lish er: I BM
)
Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
CREATE TABLE ITSO.CUSTACCT ( customerid
INTEGER
\ NOT NULL,
\
accID CHAR(8) NOT NULL, Lear n about t he EJB 2 .0 specif icat ion
\
KEY (CUSTOMERID,ACCID) Dev elop EJBs w it h St u dio App PRIMARY licat ion Dev eloper )
\
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans CREATE TABLE \ licat ion Serv er. ( EJB) ar ch it ect uITSO.ACCOUNT re, an d d ep loym(ent of su ch ap plicat ion s t o a WebSph er e App Thr oug accid hout t he bookCHAR(8) , w e pr ov id e exam NOT ples NULL, based on a simp le bank ing app \ licat ion w it h an un derly ing r elat ional dat ab ase. balance DEC(8,2) NOT NULL DEFAULT 0.00, \ I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icuNULL lar w eDEFAULT point out5,t he n ew fu nct ions interest INTEGER NOT \ int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essageiv en DEFAULT beans. We'ACCOUNT', also pr ovid e best\pr act ice guidelines f or acctype VARCHAR(8) NOT dr NULL successful imp lem ent at ions of EJBs. discriminator CHAR(1) NOT NULL DEFAULT 'A', \ I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e-DEC(8,2), d r iv en beans using WebSph er e St u dio Ap plicat ion Dev overdraft \ elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he minamount en d, w e d escrib eDEC(8,2), how t o deploy EJB ap plicat ion s t o a WebSph er e App licat \ ion Serv er. PRIMARY KEY (ACCID)
\
)
CREATE TABLE ITSO.CHECKING ( accid
CHAR(8)
\ NOT NULL,
\
overdraft
DEC(8,2)
NOT NULL DEFAULT 200.00,
\
PRIMARY KEY (ACCID)
\
)
•
Table of Content s
•
I ndex
CREATE TABLE ITSO.SAVINGS (
\
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
accid
CHAR(8)
NOT NULL,
\
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
minamount
DEC(8,2)
NOT NULL DEFAULT 100.00,
\
PRIMARY KEY (ACCID)
\
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
)
Pages: 75 0
CREATE TABLE ITSO.TRANSRECORD (
\
transid TIMESTAMP NOT NULL, Lear n about t he EJB 2 .0 specif icat ion
\
accid CHAR(8) , Dev elop EJBs w it h St u dio App licat ion Dev eloper
\
transtype CHAR(1) NOT NULL, Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
\
Thistransamt I BM Redbook prDEC(8,2) ovides d et ailedNOT in forNULL, m at ion on h ow t o ef fect ively use \WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans KEY (TRANSID) \ licat ion Serv er. ( EJB) ar ch it ect u re, an d d ep loym entPRIMARY of su ch ap plicat ion s t o a WebSph er e App ) Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic CREATE \ int r od uced w it h t he concep t sTABLE and t h ITSO.CUSTOMERINFO e ar ch it ect ur e. I n par t(icu lar w e point out t he n ew fu nct ions EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed customerid INTEGER NOT dr NULL, per sist ence ent it y b ean s and m essageiv en beans. We also pr ovid e best\pr act ice guidelines f or successful imp lem ent at ions of EJBs. infoid INTEGER NOT NULL, \ I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e-VARCHAR(50), d r iv en beans using WebSph er e St u dio Ap plicat ion Dev description \ elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he data en d, w e d escrib eBLOB(10K), how t o deploy EJB ap plicat ion s t o a WebSph er e App licat \ ion Serv er. PRIMARY KEY (CUSTOMERID, INFOID)
\
)
echo --- referential integrity --ALTER TABLE ITSO.TRANSRECORD
\
ADD CONSTRAINT "AccountTransrecord" FOREIGN KEY (ACCID)
\
REFERENCES ITSO.ACCOUNT ON DELETE RESTRICT
ALTER TABLE ITSO.CUSTACCT • •
Table of Content s
ADD CONSTRAINT "CAtoCustomer" I ndex
\ FOREIGN KEY (CUSTOMERID)
\
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
REFERENCES ITSO.CUSTOMER ON DELETE RESTRICT
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM
ALTER TABLE Pub Dat e: AprilITSO.CUSTACCT 1 1, 20 03
\
I SBN: 0- 73 84- 26 09 -1
ADD CONSTRAINT "CAtoAccount" Pages: 75 0
FOREIGN KEY (ACCID)
\
REFERENCES ITSO.ACCOUNT ON DELETE RESTRICT
ALTER TABLE ITSO.ADDRESS Lear n about t he EJB 2 .0 specif icat ion ADD CONSTRAINT "CustAddress" FOREIGN KEY (CUSTOMERID) Dev elop EJBs w it h St u dio App licat ion Dev eloper
\ \
REFERENCES ITSO.CUSTOMER ON DELETE RESTRICT Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ALTER \ licat ion Serv er. ( EJB) arTABLE ch it ectITSO.CHECKING u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App ADDhout CONSTRAINT FOREIGN (ACCID) \ licat ion w it h an Thr oug t he book , "CheckingAccount" w e pr ov id e exam ples based on aKEY simp le bank ing app un derly ing r elat ional dat ab ase. REFERENCES ITSO.ACCOUNT ON DELETE RESTRICT I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed ALTER TABLE per sist ence entITSO.SAVINGS it y b ean s and m essage- dr iv en beans. We also pr ovid e best\pr act ice guidelines f or successful imp lem ent at ions of EJBs. ADD CONSTRAINT "SavingsAccount" FOREIGN KEY (ACCID) \ I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans WebSph er e St u dio Ap plicat ion Dev elop er . We also REFERENCES ITSO.ACCOUNT ONusing DELETE RESTRICT im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. echo --- triggers ------------------CREATE TRIGGER ITSO.INSERTACCOUNT NO CASCADE BEFORE INSERT
\
ON ITSO.ACCOUNT referencing new as ACCT for each row mode db2sql \ BEGIN ATOMIC SET ACCT.acctype =
\ \
CASE
•
\
WHEN ACCT.discriminator = 'C' THEN 'CHECKING'
\
WHEN ACCT.discriminator = 'S' THEN 'SAVINGS'
\
WHEN ACCT.discriminator = 'A' THEN 'ACCOUNT'
\
Table of Content s
END;
•
\
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
END
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM
CREATE TRIGGER NO CASCADE BEFORE UPDATE Pub Dat e: April 1 1, ITSO.UPDATEACCOUNT 20 03
\
I SBN: 0- 73 84- 26 09 -1
ON ITSO.ACCOUNT referencing new as ACCT for each row mode db2sql \ Pages: 75 0
BEGIN ATOMIC
\
SET ACCT.acctype =
\
CASE Lear n about t he EJB 2 .0 specif icat ion
\
WHEN ACCT.discriminator = 'C' THEN 'CHECKING' Dev elop EJBs w it h St u dio App licat ion Dev eloper
\
WHEN ACCT.discriminator = 'S' THEN 'SAVINGS' Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
\
WHEN pr ACCT.discriminator THEN \ St udio This I BM Redbook ovides d et ailed in for m=at'A' ion on h ow'ACCOUNT' t o ef fect ively use WebSp here App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch itEND; ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion\ Serv er. END Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic echo execute GRANT ---w e point out t he n ew fu nct ions int r od uced w it h t he concep--t s and t h e ar ch it ect urstatements e. I n par t icu lar EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed GRANT CONNECT DATABASE per sist ence ent it yON b ean s and m essage- dr ivTO en PUBLIC beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. GRANT ALL ON ITSO.CUSTOMER TO PUBLIC I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also GRANT ALL ONe-ITSO.ACCOUNT TO PUBLIC im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At tGRANT he en d,ALL w e d escrib how t o deploy EJB apTO plicat ion s t o a WebSph er e App licat ion Serv er. ON eITSO.CHECKING PUBLIC GRANT ALL
ON ITSO.SAVINGS
TO PUBLIC
GRANT ALL
ON ITSO.TRANSRECORD
TO PUBLIC
GRANT ALL
ON ITSO.CUSTACCT
TO PUBLIC
GRANT ALL
ON ITSO.CUSTADDRESS
TO PUBLIC
GRANT ALL
ON ITSO.CUSTOMERINFO TO PUBLIC
echo --- connect reset --CONNECT RESET
•
Table of Content s
Database content • I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
The d at abase can be f illed wit h sam ple dat a, as show n in Ex am ple 10 - 2. By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
ExaPubmlishpler:e I BM 1 0 -2 . Ba nk sa m ple d at a Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 75 0 echo Pages: --- load the EJBBANK database ---
echo --- connect to EJBBANK database --Lear TO n about t he EJB 2 .0 specif icat ion CONNECT EJBBANK Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy t o W eb Sp her e Applicat ion Ser v er DELETE FROMEJBs ITSO.TRANSRECORD This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio DELETE FROM ITSO.SAVINGS App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. DELETE FROM ITSO.CHECKING Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an DELETE FROM ITSO.CUSTACCT un derly ing r elat ional dat ab ase. DELETE I n Part 1,FROM we inITSO.ACCOUNT t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he DELETE FROM ITSO.CUSTADDRESS EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or DELETE FROM successful imp ITSO.CUSTOMER lem ent at ions of EJBs. DELETE I n Part 2,FROM we inITSO.CUSTOMERINFO t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. echo --- insert into CUSTOMER tables --INSERT INTO ITSO.CUSTOMER
\
(customerid, title, firstname, lastname, userid, password) VALUES
\
(101, 'Mr', 'Martin',
'Weiss',
'cust101', 'MW'),
\
(102, 'Mr', 'Lars',
'Schunk',
'cust102', 'LS'),
\
(103, 'Mr', 'Wouter',
'Denayer', 'cust103', 'WD'),
\
(104, 'Ms', 'Deborah', 'Shaddon', 'cust104', 'DS'),
\
(105, 'Ms', 'Unknown', 'Lady',
\
(106, 'Mr', 'Ueli',
•
'Wahli',
null,
null),
'cust106', 'UW')
Table of Content s
INSERT INTOI ndex ITSO.CUSTADDRESS •
\
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
(customerid, street, city, state, zipcode) VALUES
\
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
(101, 'A St', 'A Village', 'Switzerland', '11111'), \ Pub lish er: I BM Pub (102, Dat e: 'B AprilBl', 1 1, 20 03 'B
City',
'Germany',
'22222'), \
(103, Pages: 'C 75 0 Rd', 'C City',
'Belgium',
'33333'), \
(104, 'D Ln', 'D Metro',
'Michigan',
'44444'), \
(105, 'E Ct', 'E Farm',
'Hawaii',
'55555'), \
I SBN: 0- 73 84- 26 09 -1
(106, 'F Av', 'F Town', 'California', Lear n about t he EJB 2 .0 specif icat ion
'66666')
Dev elop EJBs w it h St u dio App licat ion Dev eloper echo Deploy --- insert ACCOUNT tables EJBs t ointo W eb Sp her e Applicat ion--Ser v er INSERT ITSO.ACCOUNT This I BMINTO Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use \WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (accid, balance, discriminator) VALUES \ licat ion Serv er. ( EJB) ar ch it ect u re, an d dinterest, ep loym ent acctype, of su ch ap plicat ion s t o a WebSph er e App Thr oug ('101-1001', hout t he book , 80.00, w e pr ov4, id e 'CHECKING', exam ples based 'C'), on a simp le bank ing app \ licat ion w it h an un derly ing r elat ional dat ab ase. ('101-1002', 375.26, 5, 'SAVINGS', 'S'), \ I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar9375.26, ch it ect ur e.5, I n 'SAVINGS', par t icu lar w e point out t he n ew fu nct ions ('102-2001', 'S'), \ int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m3, essagedr iv en beans. We also pr ovid e best\pr act ice guidelines f or ('102-2002', 75.50, 'CHECKING', 'C'), successful imp lem ent at ions of EJBs. ('103-3001', 100.00, 6, 'SAVINGS', 'S'), \ I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e-888.88, d r iv en beans using WebSph'S'), er e St u dio Ap plicat ion Dev ('104-4001', 4, 'SAVINGS', \ elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he ('104-4002', en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat 88.88, 4, 'CHECKING', 'C'), \ ion Serv er. ('105-5001',
0.00, 2, 'CHECKING', 'C'),
\
('106-6001', 1000.00, 3, 'CHECKING', 'C'),
\
('106-6002', 2000.00, 4, 'SAVINGS',
'S'),
\
('106-6003', 3000.00, 6, 'SAVINGS',
'S'),
INSERT INTO ITSO.CHECKING
\
(accid, overdraft) VALUES \
• •
('101-1001',200.00),
\
('102-2002',200.00),
\
Table of Content s
('104-4002',250.00), I ndex
\
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
('105-5001',200.00),
\
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
('106-6001',300.00) Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
INSERT INTO ITSO.SAVINGS Pages: 75 0
\
(accid, minamount) VALUES \ ('101-1002',100.00),
\
('102-2001',100.00), \ Lear n about t he EJB 2 .0 specif icat ion ('103-3001',150.00), \ Dev elop EJBs w it h St u dio App licat ion Dev eloper ('104-4001',200.00), \ Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This('106-6002',100.00), I BM Redbook pr ovides d et ailed\ in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ('106-6003',250.00) ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. echo --- insert into CUSTACCT table --I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic INSERT concep t sINTO and t ITSO.CUSTACCT h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu\nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed (customerid, per sist ence ent it y baccid) ean s andVALUES m essage- dr iv en beans. We also pr ovid\e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. (101,'101-1001'), (101,'101-1002'), \ I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat\ion Dev elop er . We also (102,'102-2001'), (102,'102-2002'), im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he (103,'103-3001'), en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er\e App licat ion Serv er. (104,'104-4001'), (104,'104-4002'),
\
(105,'105-5001'),
\
(106,'106-6001'), (106,'106-6002'), (106,'106-6003'), (106,'105-5001')
echo --- insert into TRANSRECORD table ---
INSERT INTO ITSO.TRANSRECORD (transid,
accid,
\ transtype, transamt) VALUES
(CURRENT TIMESTAMP, '101-1001', 'C',
80.00 )
INSERT INTO ITSO.TRANSRECORD • •
Table of Content s
(CURRENT TIMESTAMP, '101-1002', 'D', I ndex
\
VALUES
\
VALUES
\
VALUES
\
VALUES
\
VALUES
\
VALUES
\
200.00 )
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
INSERT INTO ITSO.TRANSRECORD
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
(CURRENT TIMESTAMP, '102-2001', 'C',
1000.00 )
Pub lish er: I BM
INSERT INTO Pub Dat e: AprilITSO.TRANSRECORD 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
(CURRENT TIMESTAMP, '102-2002', 'D',
70.00 )
Pages: 75 0
INSERT INTO ITSO.TRANSRECORD (CURRENT TIMESTAMP, '103-3001', 'C',
100.00 )
INSERT INTO ITSO.TRANSRECORD Lear n about t he EJB 2 .0 specif icat ion (CURRENT TIMESTAMP, '104-4001', 'C', Dev elop EJBs w it h St u dio App licat ion Dev eloper
88.00 )
INSERT INTO ITSO.TRANSRECORD Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
TIMESTAMP, ) This(CURRENT I BM Redbook pr ovides '104-4002', d et ailed in for m'C', at ion on h ow t o88.88 ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans INSERT INTO VALUES \ ion Serv er. ( EJB) ar ch it ectITSO.TRANSRECORD u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat (CURRENT 'D', 66.66 ) ing app licat ion w it h an Thr oug hout t heTIMESTAMP, book , w e pr '106-6001', ov id e exam ples based on a simp le bank un derly ing r elat ional dat ab ase. INSERT INTO ITSO.TRANSRECORD VALUES \ I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic (CURRENT 'C', concep t s and t TIMESTAMP, h e ar ch it ect ur'106-6001', e. I n par t icu lar w e point out t 10.00 he n ew )fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed INSERT INTO \ ice guidelines f or per sist ence entITSO.TRANSRECORD it y b ean s and m essage- dr iv en beans. We also pr ovidVALUES e best pr act successful imp lem ent at ions of EJBs. (CURRENT TIMESTAMP, '106-6002', 'C', 66.66 ) I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, d m essag e- d r iv en beans using WebSph er e St u dio Ap plicatVALUES ion Dev elop INSERTan INTO ITSO.TRANSRECORD \ er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he (CURRENT en d, w e d escrib e how t o'106-6003', deploy EJB ap'C', plicat ion s t o 3000.00 a WebSph)er e App licat ion Serv er. TIMESTAMP,
echo --- insert into CUSTOMERINFO table --INSERT INTO ITSO.CUSTOMERINFO
\
(customerid, infoid, description,data) VALUES
\
(101, 1, 'Picture', blob('xxxxxxxxxx')),
\
(101, 2, 'Scanned image', blob('yyyyyy'))
echo --- connect reset --CONNECT RESET •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Creating the EJBBANK database and tables By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss ThePub D DL and SQL st at ement s are pr ovid ed in t he ad dit ional m at er ial t hat accom pan ies t his lish er: I BM r ed book as f iles ejbbank.ddl an d ejbbank.sql. See " Set t ing up t he EJBBANK d at abase" on Pub Dat e: April 1 1, 20 03 pag e 697 f or inst r uct ions. You can alw ay s r er un t h e comm an ds t o rest or e t he d at abase t o it s I SBN: 0- 73 84- 26 09 -1 init ial st at e. . Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Preparing Application Developer To im plem en t t h e samp le bank ing app licat ion, w e pr ep ar e t he env ir onm en t in App licat ion Dev eloper . •
Table of Content s
•
I ndex
Workspace EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
We u se a sep arat e w ork space t o im p lem ent t he sam ple: d:\WSAD5sg246819, w her e d is any disk dr iv e on t he m achine. Pub lish er: I BM Pub e: April 1 1, 03ev eloper w it h t his w or kspace, creat e a copy of t he Applicat ion D ev eloper To st ar Dat t Applicat ion20D icon anI SBN: d ad 0d 73 t he ect 84-d 26ir09 -1 ory t o t h e t ar get ex ecut ab le using t he -data f lag: Pages: 75 0
d:\\wsappdev.exe-data d:\WSAD5sg246819
Lear n about t he EJB 2 .0 specif icat ion
Preparing a J2EE 1.3 enterprise application Dev elop EJBs w it h St u dio App licat ion Dev eloper Befor e w e st ar t d ev elop ing EJBs, w e pr epar e a J2EE 1. 3 ent er pr ise ap plicat ion w it h an EJB and a Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Web pr oject ( Figur e 10 - 3) . This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio r e 1 0 -3f or . En r ise en app cat ionionf or EJBBANK: t iise al Jav st aaBeans te App licatFigu ion Developer t het er d evpelopm t ofliap plicat s based on t h e Enti ni er pr ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
We st ar t w it h one EJB m od ule and one W eb m odu le. We will ad d mor e m odu les w h ile developing our EJBs and clien t ap plicat ion s.
Naming convention
We u se a nam ing conv en t ion t hr ough out t his docum ent : ItsoBank5EAR— Ent er pr ise applicat ion ItsoBank5XxxxEJB— EJB p r oject s for d iff er ent t yp es and imp lem ent at ions, w h er e Xxxx st ands for Cmp, Bmp, Session, Mdb, an d so for t h • •
Table of Content s
ItsoBank5Web— I ndex Web p roj ect for W eb - based clien t ap plicat ion s
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
ItsoBank5Util— Ut ilit y pr oj ect
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
ItsoBank5Struts— Web p roj ect for a St r ut s- based Web clien t Pub lish er: I BM
ItsoBank5Client— Pub Dat e: April 1 1, 20 03
J2EE app licat ion client pr oject
I SBN: 0- 73 84- 26 09 -1
itso.bank5.xxxx— Conv ent ion for p ack age nam es, f or exam p le, itso.bank5.cmp f or Pages: 75 0 cont ainer - m anaged per sist ence
Creating the enterprise application n about t he Developer EJB 2 .0 specif St ar t Lear t he App licat ion w it hicat t heion w or k sp ace for t h is r ed book and op en t he J2EE per sp ect iv e. All EJB developm ent is done in t h e J2EE p er spect iv e. Dev elop EJBs w it h St u dio App licat ion Dev eloper Select File - > New - > Proj ect ( or use t he New icon) . Select J2EE in t h e lef t p ane and En t er p rise Deploy Applicat SerNext v er . App licat ion PrEJBs ojectt oinWt heb e Sp righer ht e pane, and ion click This I BM Redbook pr ovides et ailed for ,mselect at ion Cr oneat h ow t o ef fect here I n t he J2 EE Specif icat ion v erdsion w inin dow e J2EE 1. 3 ively Ent eruse pr iseWebSp App licat ionStprudio oject App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( Figur e 10 - 4) . EJB 2 .0 r equir es t he J2 EE 1. 3 lev el. Click Next . ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
ee1exam 0 -4 ples . Selbased ect inong at simp he J2 EE le v el Thr oug hout t he book , wFigu e pr ovrid le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
I n t he En t erp rise App licat ion Pr oject w ind ow ( Figur e 10 - 5) :
Figu r e 1 0 -5 . En t er p r ise app li cat ion w it h EJB an d W eb m odul es
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he En t er ItsoBank5EAR f or t h e en t erp rise app licat ion nam e. EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or Deselect App licat ion clien t m odu le . For n ow, w e do not cr eat e st and- alon e client s. successful imp lem ent at ions of EJBs. Select EJB m odule an d over t y pe t he nam e w it h ItsoBank5CmpEJB. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d Web m essag e-ule d r ivan end beans u dio Ap plicat ion Dev elop er . We also Select m od over t yusing pe t heWebSph nam e weriteh St ItsoBank5Web. im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he enUse d, wt he e ddef escrib how ion t o deploy apodu plicat aulte locat for all tEJB he m les.ion s t o a WebSph er e App licat ion Serv er. Click Fin ish an d t he t h ree p roj ect s are cr eat ed . For det ails abou t App licat ion Dev eloper p roj ect s, see " Pr oject s" on p age 35 1. Figur e 10 - 6 sh ows t he J2 EE Hier ar ch y and Nav ig at or v iew s af t er t he cr eat ion of t he pr oj ect s.
Figu r e 1 0 -6 . En t er p r ise app li cat ion v ie w s
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion
Module dependencies Dev elop EJBs w it h St u dio App licat ion
Dev eloper
t o an W eb Sp e Applicat The mDeploy od ules EJBs w it h in ent erher pr ise application ionSer canv er be d ep en dent on each ot her . I n m any cases, t he Web m odu les ar e d ep en dent on t h e EJB m odules, t hat is, a Web applicat ion accesses This BM ovides d et ailed ent erIpr iseRedbook b ean s inpran EJB m odu le. in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (To EJB) ch it ect u re,enan set ar t he depend cyd: d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. Select t he ItsoBank5Web p r oject or m odu le and Pr oper t ies ( fr om t h e con t ext m en u) . I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Select Javt ha eJAR antdicuselect ItsoBank5CmpEJB.jar f ile. Appw lyit. h t he concep t s and ar chDependencies it ect ur e. I n par lar w et he point out t he n ew fu nct ions intClick r od uced EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Select h anm d essageyou candrsee at t h e ItsoBank5CmpEJB p r oject select ed on t fhor e per sist ence Jav entait Bu y bild eanPat s and iv ent hbeans. We also pr ovid e best pr actisice guidelines Pr oject s pag e. successful imp lem ent at ions of EJBs. Close t heinProp er t ies . bank ing applicat ion and t hen imp lem ent en t it y beans, session I n Part 2, we t r oduce t h ew indow sam p le beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At tPreparing he en d, w e d escrib e how t o deploy test EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. the WebSphere environment To t est t he sam ple app licat ion w e pr epar e a WebSp her e ser v er w it h in Ap plicat ion Dev eloper .
Server project Defin it ion s of ser v er s ar e st or ed in a ser ver p roj ect . To def in e such a pr oject :
Select File - > New - > Proj ect ( or use t he New icon) , Serv er in t h e lef t p ane and Serv er Pr oject in t h e rig ht pane, and click Next . En t er ItsoServers as pr oject n ame and click Fin ish ( Figur e 10 - 7) .
Figu r e 1 0 -7 . Cr ea t i ng a se r ve r p r oje ct •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
The Ser v er p er spect ive op en s. Lear n about t he EJB 2 .0 specif icat ion
Server
Dev elop EJBs w it h St u dio App licat ion Dev eloper Now w e d ef ine a Web Spher e ser v er for t est ing of t he sam ple. Because w e w ill use EJB 2. 0 beans o W eb Applicat ion Ser v er w e m Deploy ust u se EJBs a Vertsion 5 Sp serher v eref or t est ing: This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans > New v erent and v erapConf igu r ats ion use t he New Server. er in ( EJB) Select ar ch itFile ect u-re, an d -d>epSer loym of Ser su ch plicat ion t o a( or WebSph er e Appicon licat and ion Serv t he lef t p ane and Serv er and Ser ver Con figur at ion in t h e rig ht pane, and click Next . ) Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Ening t er EjbbankServer t he n ame. Th e ItsoServers p r oject is pr eselect ed . Ex pan d un derly r elat ional dat abas ase. WebSph er e v ersion 5 . 0 an d select Test En vir onm en t ( Figur e 10 - 8) . I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and Figu t h e arrch par e point out t he nrew fu nct ions od uced w it h t he eit1ect 0 ur -8e.. IDn ef int icu in lar g twhe W e bSphe e Test Envintirronm e nt EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear nNext Click about . Check t he EJB t hat2 t.0hespecif por t icat is set iont o 9080. Dev elop EJBs w it h St u dio App licat ion Dev eloper Click Fin ish. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Adding the enterprise toon the server This I BM Redbook pr ovides d etapplication ailed in for m at ion h ow t o ef fect ively use WebSp here
St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) To t estartch heitent ect er u re, pr ise an dapplicat d ep loym ionent w eofadsu d ch it tap o tplicat he serv ioner: s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n t he Ser ver Con figu rat ion v iew , select t he EjbbankServer an d Add - > I t soBank 5EAR. Th e ent pr ise plicat EJBs ion and t o t hEd e ser v er( J2 ( Figur e 10cover - 9) . t he basic I n Part 1,erwe in tap r oduce as ait sp m artodofules Jav are a 2 added En t er p rise it ion EE) and concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion pr ov id e, for ex hanced ct ionicat alit yi on for cont er -rmve anag Figu r et hat 1 0 -9 . Addin g am anple, e ntener pr i sefun a ppl t o aainse r ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Configuring the server with a data source We w ill use t h e EjbbankServer t o access t h e EJBBANK d at abase using EJBs. W e hav e t o conf ig ur e a d at a sour ce for t his access:
Op en t he ser ver conf igur at ion by d ouble- click ing EjbbankServer. Look t hr ough t h e diff erent pag es of conf igur at ion inf orm at ion. Go t o t h e Secu rit y p age t o defin e a user I D and passw or d for t he d at a sour ce. Click Add f or JAAS Au t hent icat ion Ent r ies. Com plet e t he w ind ow as show n in Figur e 10 - 10 an d click OK. •
Table Figu of Content r e s1 0 -1 0 .
•
I ndex
De fi ni ng a use r I D for a dat a sou r ce
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
The ent ry appear s in t he list on t he Secur it y pag e ( Figur e 10 - 11) . Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w itFigu h St urdio e 1App 0 -1 licat 1 .ion SeDev r veeloper r con f igu r at ion :
se cur i t y
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Next , go t o t h e Dat a sou rce p age. Not e t hat t her e ar e Node Set t ing s and Serv er Set t ings. I n t he t est env ir onm en t w e on ly ru n one ser v er on one nod e. Theref ore, it does not m at t er w her e w e define t he d at a sour ce for t he EJBBANK d at abase. Scroll dow n t o Ser ver Set t ing s and select t h e Default DB2 JDBC Pr ovider ent ry .
Tip
We could also use an XA- com pliant D B2 JDBC d r iv er . This w ould be r equ ir ed if an app licat ion has ot h er resour ces t o be sy nchr onized w it h t he dat ab ase upd at es. See " Tr ansact ion t y pes" on p age 58 4 for a discu ssion. Click Add n ex t t o t h e dat a sour ce list .
• •
I n t he Cr eat e a Dat a Sou r ce w ind ow , select Version 5 . 0 d at a sour ce f or t h e t y pe of dat a sour ce and click Next . Table of Content s
Com pletI ndex e t h e Modif y Dat a Sour ce win dow ( Figur e 10 - 12) .
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Figu r e 1 0 -1 2 . De fi ne t he da t a sour ce for t h e EJB BAN K
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed 1. Enent t eritEJBBANK as t he n ame t odrbe displayed in t also he list of dat sourprces. per sist ence y b ean s and m essageiv en beans. We pr ovid e abest act ice guidelines f or successful imp lem ent at ions of EJBs. 2. En t er jdbc/ejbbank as t he JND I nam e ( t h is is w hat w e w ill specify in t he EJB I n Part 2, we deploy in t r m oduce en t dtescrip h e sam t or p let obank access ing t applicat he d at abase) ion and . t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also ionally t er adifd fer escrip ion. im plem3. ent Op fintder m et en h ods, en t tm app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. 4. Verif y t he D at a st or e helper class nam e ( DB2DataStoreHelper) .
5. Select t he DB2user f or cont ain er - m anaged aut h en t icat ion alias. 6. Ch eck t h at Use t his dat a sou rce in con t ain er - m anaged persist en ce ( CMP) is select ed. This is r equir ed for EJB access t o t he d at abase.
7. Not e t h at w e d o not specif y t he dat abase nam e an yw h er e! 8. Click Next . I n t he Mod ify Resour ce Pr opert ies w ind ow, select t he dat ab aseNam e p r opert y and ch ange
t he value t o EJBBANK ( Figur e 10 - 13) .
Figu r e 1 0 -1 3 . Se t t i ng t h e da t a base n am e
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Click Fin ish. Dev elop EJBs w it h St u dio App licat ion Dev eloper The d at a sour ce is add ed t o t he list an d disp layed in t he ser v er con figu r at ion ( Figur e 10 14 ). Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides et ailed in for m at h owr at t o ion ef fectw ively Figu r e 1 0d -1 4 . Se r ve r ion cononf igu it h use datWebSp a souhere r ceSt udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Sav e t he ser ver conf igur at ion and close t h e edit or .
I m por t a nt The db2java.zip f ile m ust b e in t h e syst em class pat h. This is don e aut om at ically w hen you inst all D B2. •
Table of Content s
•
I ndex
EJB 2 . 0 D eproject ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper Utility By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Ut ilit y pr oject s ar e Java pr oj ect s cont ain in g cod e t hat sh ould be av ailable t o mu lt ip le m odules of an en erper: rise Pubtlish I BMapp licat ion. Pub Dat e: April 1 1, 20 03
J2EE 1. 3 p r ovides sup por t f or ut ilit y JAR f iles at t he ent er pr ise applicat ion lev el. Su ch JAR f iles I SBN: 0- 73 84- 26 09 -1 ar e t hen m ade av ailab le t o W eb and EJB m odules as JAR f ile d ep en dencies. Pages: 75 0
When w e im plem ent session b ean s w it h r em ot e int er faces, we w ill use t he h ome fact or y pat t er n descr ibed in " Hom e fact or y pat t er n" on p age 26 8. W e w ill im p lem ent t he HomeFactory class in t his u t ilit y pr oject in a m om ent .
Lear naabout EJB 2 .0 specif icat ion Creating utilityt he project Dev elop EJBs w it h St u dio App licat ion Dev eloper The u t ilit y pr oject is a sim ple Java pr oject : Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This IIBM Redbook prspect ovides d etselect ailed New in for m on -h>ow here. St udio n t he Jav a p er ive, - >at Prion oject Javtao -ef > fect Jav ively a Projuse ect .WebSp Click Next App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) En artch ect u re, an d d ep loym su ch plicat ion sclick t o a Next WebSph er e App licat ion Serv er. er it ItsoBank5Utility asent t heofp roj ectap nam e and . Thr oug hout , wt ings, e pr ov id e exam based onsacont simp le bank app licat w itCrheat an e new For Javtahebubook ild set select Use ples sou rce f older ained in ting he pr oject . ion Click un derly ing r elat ional dat ab ase. folder , ent er source as t he n ame and click OK. I n Part 1, Yes we in EJBs a dat p arte tof a p2utEnfolder t er p rise Ed it ion ( J2 EE) and cover t he basic Click int rt oduce h e pr om pt t as o up h eJav out t o bin. concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2.Select 0 sp ecif icat ionarties hat t pr for ex amvple, en hanced ion alit y for cont ain erv -ar miable anag and ed t he Libr abov anidde,click Add ar iable. Select fun t hectWAS_50_PLUGINDIR per sist ence it y Exp b ean s and essageiv enselect beans. We also pr ovid best pr act ice guidelines f or click Ex ent t end. and t he m lib f olderdrand t he j2ee.jar f ile.e Click OK. successful imp lem ent at ions of EJBs. Click Fin ish t o cr eat e t he pr oj ect . I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods,to difan fer en t m app in g application st r at egies, and sim p le client s t h at u se t he EJBs. At Adding a utility project enterprise t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Jav a p roj ect s su ch as t h e ItsoBank5Utility p r oject can be add ed t o en t erp rise app licat ions so t hat t heir code is av ailable t o t he Web and EJB m odules:
I n t he J2 EE Hier ar ch y v iew , op en ( dou ble- click) t he ItsoBank5EAR p r oject . I n t he app licat ion deploy m en t d escrip t or edit or , go t o t h e Modu les p age. Click Add u nder Pr oj ect Ut ilit y JARs.
I n t he Add Ut ilit y JAR win dow , select t h e ItsoBank5Utility p r oject and click Fin ish. Figur e 10 - 15 sh ows t he d ep loy m ent d escr ip t or w it h t he u t ilit y JAR.
Figu r e 1 0 -1 5 . Add in g a u t il it y JAR t o an e nt er pr i se appl icat ion •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This ISav BM eRedbook pr ovides d etloy ailed in for m atipion t he ap plicat ion d ep m ent d escr t or on . h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans e pruom t he ent EjbbankServer ser ion ver sconf at ion er m eust b elicat repair ( Figur ( EJB) You ar char it ect re, pt aned d dt hat ep loym of su ch ap plicat t o aigur WebSph App ion ed Serv er. e 10 - 16) . Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ionalFigu dat ab r ease. 1 0 -1 6 . Re pai r in g a se r ve r con f igu r at ion I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Click OK an d t he u t ilit y p r oject is add ed in t he Ser ver Config ur at ion v iew . The u t ilit y JAR file is now par t of t h e en t erp rise app licat ion and can be m ade av ailable t o t he mod ules.
Implementing the home factory We im plem en t t h e hom e f act or y in t he ItsoBank5Utility p r oject :
Select t he source f older and New - > Pack age ( con t ex t ) . Ent er it so. ban k5 . ut ilit y as t he pack age n ame an d click Fin ish. Select t he new p ack age and New - > Class ( con t ex t ) . Ent er HomeFactory as t he n ame, select Con st r u ct or s f r om super class, an d click Fin ish. Com plet e t h e class w it h t he cod e sh ow n in Figur e 10 - 17. • •
Table of Content s
Figu r eI ndex 1 0 -1 7 . Hom e f act or y im pl em e nt at ion
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pubpackage lish er: I BM
itso.bank5.utility;
Pub Dat e: April 1 1, 20 03
import javax.naming.InitialContext; I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
import javax.naming.NamingException; import javax.ejb.EJBHome; import javax.ejb.EJBLocalHome; Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper public class HomeFactory { Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio static private HomeFactory HomeFactory(); App licat ion Developer f or t he d ev elopm enhomefactory t of ap plicat ion=s new based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. private InitialContext initialContext = null; Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1,protected we in t r oduce EJBs as a p art {of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic HomeFactory() concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed super(); per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem{ent at ions of EJBs. try I n Part 2, we in t initialContext r oduce t h e sam p le=bank applicat ion and t hen imp lem ent en t it y beans, session newing InitialContext(); beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin m et h(NamingException ods, dif fer en t m app in g st r at egies, and sim } der catch namingException) { p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. namingException.printStackTrace(); } }
static public HomeFactory singleton() { return homefactory;
}
public Object getHome(String ejbRef) throws NamingException { if (initialContext != null) { •
Table of Content s
•
I ndex Object nsObject =
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
initialContext.lookup( By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss new StringBuffer("java:comp/env/")
Pub lish er: I BM Pub Dat e: April 1 1, 20 03
.append(ejbRef).toString());
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
System.out.println("class=" + nsObject.getClass()); if (nsObject instanceof EJBLocalHome) { System.out.println("ejbRef " + ejbRef + " is a local ref.");
return Lear n about t he EJB 2 .0nsObject; specif icat ion Dev elop EJBs } else w it h St { u dio App licat ion Dev eloper Deploy EJBs t oEJBHome W eb Sp her e Applicat Ser v er javax.rmi.PortableRemoteObject ejbHome = ion (EJBHome) This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively useEJBHome.class WebSp here St udio .narrow( (org.omg.CORBA.Object)nsObject, ); App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s tejbRef o a WebSph e App licat ion ref."); Serv er. System.out.println("ejbRef " + + " eris a remote Thr oug hout t he bookreturn , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an ejbHome; un derly ing r elat ional dat ab ase. } I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he } else { EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or throw new NamingException("HomeFactory: no InitialContext"); successful imp lem ent at ions of EJBs. I n Part 2, we}in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also } fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At im plem ent t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. }
This HomeFactory class pr ov ides access t o r em ot e and local EJB hom es using EJB ref er ences only . I t d oes not pr ovid e any caching, as descr ibed in " Hom e fact or y pat t er n" on p age 26 8. A client can use t h is cod e t o access a r em ot e or local hom e:
try {
CustomerHome custRemoteHome = (CustomerHome) HomeFactory.singleton().getHome("ejb/CustomerRemote"); AccountLocalHome accountHome = (AccountLocalHome) HomeFactory.singleton().getHome("ejb/Account"); •
Table of Content s
• catch (NamingException I ndex } ex) { EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
ex.printStackTrace(); By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss throw new EJBException("Error looking up homes: "+ex.getMessage());
Pub lish er: I BM
}
Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Module dependency The ItsoBank5Web p r oject w ill depend on t he ItsoBank5Utility p r oject t o hav e access t o t he hom eLear f actnory . To set he d2ep dency: about t he tEJB .0en specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Select he ItsoBank5Web oject or ion m odu and Pr oper t ies ( fr om t h e con t ext m en u) . Deployt EJBs t o W eb Sp her pe rApplicat Serlev er Jav a JAR select t heon ItsoBank5Utility.jar f ile ( in here additSt ion to the This ISelect BM Redbook pr Dependencies ovides d et ailedanindfor m at ion h ow t o ef fect ively use WebSp udio ItsoBank5CmpEJB.jar f ile) . Click OK t o close t he Pr oper t ies w in dow . App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Summary I n t his chapt er , w e d escrib ed t h e bank ing scenar io t hat will b e im plem ent ed in t he chapt er s t h at follow . •We also p repar Table Content ion s ed ofApplicat D ev eloper w it h t he basic pr oj ect st r u ct u re for im plem en t ing and •t est in g t he sam I ndex ple app licat ion. EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Chapter 11. Container-managed entity bean development •I n t his chapt er Table of dContent , we iscusss t h e developm ent of con t ainer- m an aged ent it y beans t o imp lem ent •t he sam ple ap I ndex plicat ion p resen t ed in Ch apt er 10 , " I nt rod ucing and pr epar in g for t h e samp le EJB . 0 D ion" e ve lopm nt w it 3 h 69. W ebSphe r e St udio Appl ica ti on De v el oper app2licat on epage By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
The im plem en t at ion of t he sam ple m odel is in t hr ee st ages: Pub lish er: I BM
1 .PubWe Datim e: April plem1 1, en20 t a 03 sin gle CMP t o illust r at e t h e developm ent pr ocess in det ail, t hen w e im plem0-ent t he ot-1her CMPs. I SBN: 73 8426 09 Pages: 75 0
2 . We im plem en t t h e r elat ionship s as cont ainer - m anaged relat ionships. 3 . We im plem en t t h e inher it ance st ru ct ur e for t he account subclasses. I f you w ant t o follow t h e descr ipt ion pr ov id ed in t his chap t er, you m ust h ave in st alled t he ITSOBANK d at abase ( see " Cr eat ing t he EJBBANK dat ab ase and t ables" on p age 38 2) and Lear t hes EJB .0e specif pr epar ed tnh about e pr oject an d 2t h ser v ericat in ion Ap plicat ion Dev elop er ( see " Pr ep ar in g Ap plicat ion Dev eloper " on p age 38 2) . Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Developing the account CMP entity bean We can add a cont ain er - m anag ed per sist ence ent it y b ean t o an EJB p r oject in diff er ent w ays:
•
Cr eat ingTable beans of Content w it h tshe EJB cr eat ion w izar d ( t his is w hat w e w ill do now ) .
•
I ndex
t ingeant bwean om anr eEJB JARAppl file. t yp EJB 2 . 0I m D epor ve lopm it h WfrebSphe St udio icaTw ti ono De v elical operscenar ios
w here w e hav e t o im por t
beans are:
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss Pub lish er: -I BM We
creat e a r elat ionship w it h a CMP 2. 0 t hat w as dev elop ed in anot h er EJB p roj ect . beans hav e t o be p art of t he sam e EJB m odu le. See "Dev elopin g r0-elat ion 73 8426sh 09ip -1 s" on p age 43 5 for d et ails.
ed20ent Pub Dat e: Relat April 1 1, 03 it y I SBN:
Pages: 75 0
- We der iv e a bean fr om a CMP 2. 0 t hat w as developed in an ot her EJB pr oject . Par ent and child en t it y beans m u st be in t he sam e EJB m odu le. See " Dev elopin g in herit an ce st r uct ur es" on p age 44 7 for d et ails.
Gener at ing t he b ean w it h t he EJB- t o- RD B m appin g t ool ( bot t om - u p ap pr oach) . For a det ailed d escrip t ion, see " Bot t om - up " on p age 47 0. Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
CMP development with the Enterprise Bean Creation wizard Deploy EJBs t o W eb Sp her e Applicat ion Ser v er The Ent er pr ise Bean Cr eat ion w izar d sh or t en s t he d ev elopm en t cy cle of EJBs by g en er at in g code This I BMesRedbook ovides d etinailed inizar for md at ion on h ow t o ef fect ively use WebSp here St udio t em plat based onprour in put t he w panels. App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (The EJB)d ev arelopm ch it ecten u re, d ep loym ent of su chstap plicat ion s t o a WebSph er e App licat ion Serv er. t ofan and EJB includes t h ree eps: Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. 1 . We cr eat e an EJB w it h t he wizar d. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic 2 . The w izart hdegener esurt he he com ponent andfuhom e int int er fraces. Wewditescrib concep t s and ar ch itat ect e. Ibean n parclass t icu laranwdet point out t he n ew nct ions od uced h t he e t he out pu t of t h e w izard in " Gener at ed classes" on p age 40 4. EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or 3 . We cust om izeent t he cod eofand add bu sin ess logic m et hod s as descr ibed in " Add in g business successful imp lem at ions EJBs. logic t o t he ent er pr ise bean" on p age 41 2. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin m et hentity ods, difbean fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At Creating ader CMP t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. I n t his sect ion, w e develop t he Account CMP 2 . 0 ent it y bean of t h e samp le m odel. We do not descr ibe t he dev elop ment of CMP 1. 1 ent it y beans in t h is r ed book .
Creating a package We st or e all t h e Jav a code of t h e CMPs in t h e itso.bank5.cmp p ackag e. To creat e t he Jav a pack age, exp and t he ItsoBank5CmpEJB p r oject in t he J2 EE Navig at or v iew ( in t h e J2EE per sp ect iv e) , select t h e ejbModule f older and New- > Package ( con t ex t ) .
En t er itso.bank5.cmp as t he n ame and click Fin ish ( Figur e 11 - 1) .
Figu r e 1 1 -1 . Ja va pa ck a ge cr e at ion
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Create a CMP 2.0 I n t he J2 EE Hier ar ch y v iew , select t he ItsoBank5CmpEJB p r oject and New- > En t er pr ise Bean Lear he EJB 2 .0mspecif icat ( cont ex t ) .nI nabout t he f tirst panel, ak e su r e ion t hat t he p roj ect is pr eselect ed. Click Next an d t he Cr eat e an Ent er pr ise Bean w izar d st ar t s ( Figur e 11 - 2) . Dev elop EJBs w it h St u dio App licat ion Dev eloper
1 1 -2 .ion CrSer eavt er e Deploy EJBs t o W ebFigu Sp herreeApplicat
a CM P 2 .0 be a n
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Select En t it y bean w it h con t ain er - m anaged per sist en ce ( CMP) fields an d CMP 2. 0 Bean. I n t he Bean nam e field, ent er Account. The Sou r ce f older field is set t o ejbModule; ot herw ise, click t h e Br ow se b ut t on t o select t he sour ce folder . The D ef ault packag e is set t o itso.bank5.cmp; ot herw ise, click t h e Br ow se b ut t on t o select
t he default pack age. Click Next t o display t h e En t er pr ise Bean D et ails p anel ( Figur e 11 - 3) .
Figu r e 1 1 -3 . Cr ea t e CM P 2 .0 de t a il s •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful lemsu ent at ions EJBs. I n t heimp Bean pert yp e of dr opdow n list , accept , b ecause t h e Account ent it y does not der ive f r om anot h er CMP 2. 0 in t h e sam e EJB p roj ect . CMP 2. 0 in herit an ce is an EJB I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session ext ension t o t he specif icat ion . beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At Accept t he nam es given for t he Bean class, EJB bind in g nam e, and Key class. t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Accept t he select ed Local client v iew ch eck box and t he n ames f or Local h om e int erf ace and Local in t er f ace. Do not select t he Rem ot e clien t v iew ch eck box . For t h is bean, w e d o n ot use t h e r em ot e capabilit ies at t h e ent it y level ( see design con sid er at ions in " Using a session f acad e t o ent it y b ean s" on p age 26 2) .
I m por t a nt
CMP 2. 0 ent it ies in com binat ion w it h local in t erf aces y ield b ig p er for m ance im pr ovem ent s com par ed t o CMP 1. 1. For a det ailed d iscussion , see " Local v er su s r em ot e in t er f aces" on p age 43 .
Create attributes •
Table of Content s
•
I n t he nex t stI ndex ep, we cap t ur e t he CMP field s for t he Account b ean . Click Add n ex t t o t h e CMP EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper at t r ibut es list b ox and t h e Cr eat e CMP At t r ib ut e p anel app ear s ( Figur e 11 - 4) . By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Figu r e 1 1 -4 . Cr ea t e a CM P a t t r i but e ( k e y f ie ld an d n on- k ey f i el d) Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. Defin e each field as sh ow n in Table 1 1- 1. Ent er t he field n ame an d t he f ield t y pe. For t y pes t hat ar e not in t he dr op - dow n list , click Br ow se an d en t er t he f irst ch aract er s of t he t yp e, I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic for ex am ple, bigdec ( for java.math.BigDecimal) . concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
le 1 1t-h1e .sam List CM P applicat f i el dsion f orand t htehen accoun bea I n Part 2, weTab in t r oduce p leof bank ing imp lem tent en tnit y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent r atyegies, Pand t h at ot u se At t rfin ibder ut e m et h ods, dif fer Ty en pet m app in g stKe romsim ot ep le client sP rom e t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv fi e ld ge t t e r an d ge t t e r an d er. set t e r t o local set t e r t o in t er fa ce re m ot e in t er fa ce ---
---
java.math.BigDecimal No
Yes
---
int
No
Yes
---
accountType java.lang.String
No
y es
---
account ID
java.lang.String
balance interest
Yes
Select t he Key field ch eck box , if t h e field r epr esent s t he p rim ar y k ey ( or par t of it ) , accountID in our sam p le. For t h e k ey f ield , you cann ot select ot her opt ions. A bean can hav e m or e t han one key f ield ( com pound k ey) . For non - key f ields, select Pr om ot e get t er and set t er m et hod s t o local int er f ace , if t h e m et hods shou ld b e exp osed t o t he com ponent int erf ace. • •
Click App ly an d repeat t he st eps ab ove f or all at t ribu t es. Table of Content s
Click CanI ndex cel, w hen fin ished.
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM
Not es
Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages:The 75 0 at t rib ut e
n ame mu st begin w it h a low er case let t er ( EJB specificat ion) .
CMP at t r ibut es can be Java pr imit iv e t y pes, for ex am ple, int. CMP at t r ibut es can be p r im it iv e w rap per s ( Integer, Long, Double) . CMP at t r ibut es can be ser ializable classes t h at m ap nat u r ally t o fields in r elat ion al Lear ndat about t he EJB 2 .0 specif icat ion ab ase ( java.lang.String, java.sql.Timestamp, java.util.Date) . Dev elop EJBsalso w it hdefine St u dioour App ionializable Dev eloper We can owlicat n ser Jav aBean and u se t hem as com plex field s ( depend en t v alue class) . I n " I m plem ent ing a Jav aBean for t he ad dr ess Deploy EJBsattion" o W eb her e42 Applicat Sert vh er inf orm onSp p age 9, w e dion ef ine e cust om er ad dr ess as a Jav aBean an d assig n it t o a CMP at t r ibut e. D ep en dent value classes ar e n ot ex posed b y t he This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio deploy m en t d escrip t or and can be m app ed t o a single d at abase field in a App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans com pr essed f orm ( BLOB) or t o indiv idual colum n s in a t able. ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I f t he k ey field t yp e is a Jav a class, for ex am ple, java.lang.String, select Use t he sin gle k ey at ibut e1,t yp for t he k ey class. Supch pr tevent s t he plicat ion Dev er tf he r ombasic I nt rPart wee in t r oduce EJBs as a artaofselect Jav aion 2 En er p rise Ed itAp ion ( J2 EE) and elop cover gener at ing a k ey w rap per class. concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed A com pou nd key or a Jav a p rim it ive ( int) alw ay s r equir es a w r apper class. per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful ions at of t EJBs. Figur e 11 - 5imp sh lem owsent t heatCMP r ibu t e list w it h all t he at t ribu t es defined and t he sin gle k ey at t r ibut e t y pe as k ey class. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also r et m 1 1app -5in. gCM P egies, a t t r i but es pl ist im plem ent fin der m et h ods, Figu dif fer en st r at and sim le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Click Next t o open t he EJB Jav a Class Det ails panel ( Figur e 11 - 6) .
Figu r e 1 1 -6 . CM P cl ass de t a ils
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
I n ourLear case, no f urt the herEJB inf or mat ion is r equir n about 2 .0 specif icat ion ed ( n o sup er class for t h e bean, n o ex t en sion s t o t he com ponent int erf ace) . Dev elop EJBs w it h St u dio App licat ion Dev eloper Click Fin ish t o gener at e t h e bean class and int er faces. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Generated classes App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. As a result of t he cr eat ion pr ocess, t h e Applicat ion Developer gener at es t hr ee classes for t h e accou nthout bean: Thr oug t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. AccountBean— Abst ract class t h at d ef ines t he at t rib ut es of t he b ean I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect urHome e. I n in par larwit w eh point t hefind n ew fu hods nct ions int r od uced w it h t he AccountLocalHome— t ert icu f ace cr eat eout and m et EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b eanCom s and m essageiv en pr ovid pr act ice guidelines f or AccountLocal— ponent int erdr face w itbeans. h met hWe odsalso av ailab le teo best a client successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session Account beans, an dbean m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At The accoun ( AccountBean class) abstion r act class wit h d ef imlicat plem enServ t at ions t he en d, w et dbean escrib e how t o deploy EJBisapan plicat s to a WebSph erault e App ion er. of t h e javax.ejb.EntityBean callback m et hods and a pair of g et t er / set t er m et h ods f or each CMP at t r ibut e. The f ir st par t are t he callb ack met h ods, m ost ly in t h e for m of em pt y m et hod bod ies. Th e only int er est ing m et hod is t he ejbCreate m et hod , w hich in it ializes t h e at t r ibut es of a new ent it y bean. The second par t ar e t h e get t er and set t er m et h ods: The g et t er / set t er m et h ods ar e public abstract.
The m et hod n ames ar e der ived fr om t he at t r ibut e nam e, w it h t he f ir st let t er in upp er case and a get or set p r ef ix: balance ==> getBalance and setBalance
• •
Table of Content s
Not es I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er I nDenay con ter, rast Lar st o Schu CMP nk , 1 Deborah .1 , t h eShaddon con t ainer, Mart inman Weiss aged
Pub lish er: Pub Dat e:
p er sist en t f ields are n ot defin ed in t he ent it y bean class. They ar e v ir t ual f ield s only , and ar e I BM accessed t hr ou gh get an d set m et hod s. The r eal field definit ions ar e p art of t he con cr et e bean class, w hich w ill be gener at ed d ur ing deploy ment . April 1 1, 20 03
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0ar chit ect ur e for con t ainer- m an aged p er sist ence enab les a sep arat ion bet w een t h e The EJB abst r act b ean class, w h ich is defin ed by t he bean pr ov ider, and it s per sist ent r ep r esent at ion, wh ich is p art of t he d ep loym ent pr ocess. This allow s an en t it y bean t o b e r ed ep loyed acr oss dif fer en t cont ain er s an d d iff er ent persist en t d at a st or es, wit h out r eq uir ing t he r edefinit ion or r ecom pilat ion of t he ent it y bean class.
Lear n about t he EJB 2 .0 specif icat ion Figur eDev 11elop - 7 shEJBs ows an exSt tru act t helicat generat ed eloper code of t he bean class. Com m en t s hav e been w it h dioofApp ion Dev r em ov ed t o fit t he cod e int o t he f ig ur e. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Figu r e 1 1 -7 . Ge ne r at ed be an cla ss f or a CM P 2 .0 ( com m e nt s r em ov ed) This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. package itso.bank5.cmp;
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an public abstract class implements javax.ejb.EntityBean { un derly ing r elat ional dat abAccountBean ase. private javax.ejb.EntityContext myEntityCtx; I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic public setEntityContext(javax.ejb.EntityContext { int r od uced w it h t he concep t s andvoid t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fuctx) nct ions myEntityCtx = ctx; EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed } ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or per sist publicimp javax.ejb.EntityContext getEntityContext() { successful lem ent at ions of EJBs. return myEntityCtx; } 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session I n Part public void { beans, an d m essagunsetEntityContext() e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also null; im plem myEntityCtx ent fin der m et h=ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he }en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. public void ejbActivate() { } public java.lang.String ejbCreate(java.lang.String accountID) throws javax.ejb.CreateException { setAccountID(accountID); return null; } public void ejbLoad() { } public void ejbPassivate() { }
public void ejbPostCreate(java.lang.String accountID) throws javax.ejb.CreateException { public void ejbRemove() throws javax.ejb.RemoveException { } public void ejbStore() { } //getter and setter methods • •
Table of Content s
public abstract java.lang.String getAccountID(); I ndex public abstract void setAccountID(java.lang.String newAccountID);
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon getBalance(); , Mart in Weiss public abstract java.math.BigDecimal
public abstract void setBalance(java.math.BigDecimal newBalance); Pub lish er: I BM
public abstract Pub Dat e: April 1 1, 20 03
int getInterest(); public abstract I SBN: 073 84- 26 09 -1void setInterest(int newInterest); Pages: 75 0
public abstract java.lang.String getAccountType(); public abstract void setAccountType(java.lang.String newAccountType); }
Home interface Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it hgener St u dio App ione Dev eloper App licat ion Developer at es t helicat h om int erf ace( s) , local an d/ or r em ot e, depending on t h e creat ion set t in gs ( see Figur e 11 - 3 on p age 40 0) . Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Figur e 11 - 8 sh ows t he h ome in t er f ace of t he account bean ( AccountLocalHome) . This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans Figu r e 1 1 -8 . Ge ne r at ed local h om e i nt er f a ce ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an package un derly ingitso.bank5.cmp; r elat ional dat ab ase. /** Local Home I n* Part 1, we in t rinterface oduce EJBs for as a pEnterprise art of Jav a 2Bean: En t er pAccount rise Ed it ion ( J2 EE) and cover t he basic */ concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he public interface extends javax.ejb.EJBLocalHome EJB 2. 0 sp ecif icat ion AccountLocalHome t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont{ain er - m anag ed /**ence Creates instance from adr key Entity Bean: Account per sist ent it y an b ean s and m essageiv en for beans. We also pr ovid e best pr*/ act ice guidelines f or publicimp itso.bank5.cmp.AccountLocal create(java.lang.String accountID) successful lem ent at ions of EJBs. throws javax.ejb.CreateException; I n Part we in tan r oduce t h e sam p le bank ing applicat ion and t hen Account imp lem ent*/en t it y beans, session /** 2,Finds instance using a key for Entity Bean: beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat public itso.bank5.cmp.AccountLocal findByPrimaryKey( ion Dev elop er . We also im plem java.lang.String ent fin der m et h ods, primaryKey) dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d,throws w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. javax.ejb.FinderException; }
Component interface App licat ion Developer gener at es t he com pon en t int er face( s) , local and / or r em ot e, depend ing on t he cr eat ion set t ing s ( Figur e 11 - 9) .
Figu r e 1 1 -9 . Ge ne r at ed local com pon en t in t e r fa ce
package itso.bank5.cmp; /** * Local interface for Enterprise Bean: Account */ public interface AccountLocal extends javax.ejb.EJBLocalObject { /** Get accessor for persistent attribute: balance */ public java.math.BigDecimal getBalance(); • of Content s persistent attribute: balance */ /** Set Table accessor for • I ndex setBalance(java.math.BigDecimal newBalance); public void EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
public inter getInterest(); By Ueli Wah li, Wout Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss public void setInterest(int newInterest); Pub lish er: I BM
public java.lang.String getAccountType(); public void setAccountType(java.lang.String newAccountType);
Pub Dat e: April 1 1, 20 03
}
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
The t ool gener at es get t er and set t er met hod p air for CMP at t ribu t es w here we select ed Pr om ot e get t er and set t er m et hod s t o int er face ( Figur e 11 - 4 on p age 40 1) .
Lear n about t he EJB 2 .0 specif icat ion
Not e EJBs w it h St u dio App licat ion Dev eloper Dev elop Get t er and sett ot er h her ods,e w hich are posed Deploy EJBs Wm ebet Sp Applicat ionexSer v er in t he com pon en t in t er f aces, t igh t ly couple EJB clien t s w it h t he un der ly ing per sist ence lay er . D ef ining t he in t er f ace en t lyprofovides t he p er sist en ceinmod s thhe ence layuse er t oWebSp changhere e ov er This Iindepend BM Redbook d et ailed for melat allow ion on owper t o sist ef fect ively St udio t im ion e w it hout EJB client licat ions. ort t ion his sinbased dependency e could er App licat Developer f or t heimp d ev elopm enTo t ofsupp ap plicat on t h e w Ent er pr iseconsid Jav aBeans pritom ing an t hed accessor m etof hod t h plicat e int er face im plement g our ( EJB)not ar ch ectot u re, d ep loym ent suschin ap ion s t oand a WebSph er e in App licatow ionn Serv er. bu sin ess logic or ient ed get t er and set t er m et hod s. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Primary key class concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed We d o ence not get r apper k ey m class, because e selectWe ed Use heovid sin gle k eypr atact t r ibice ut eguidelines t y pe for t hf or e per sist entait yw b ean s and essagedr iv enwbeans. also t pr e best ksuccessful ey class. Wlt h out t his select ion, Ap plicat ion Dev eloper w ou ld g en er at ed an AccountKey class imp lem ent at ions of EJBs. t hat w r aps t h e key f ield( s) . I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At Generated deployment descriptor t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. App licat ion Developer gener at es an element int o t he d ep loym ent descr ipt or ( ejbjar.xml) . To open t he d ep loy m ent d escr ipt or , dou ble- click t he EJB m odu le in t h e J2EE Hier archy v iew or doub le- click t he ejb-jar.xml f ile in t he J2EE Nav igat or view . The EJB d ep loy m ent descr ipt or edit or con sist s of a num ber of p ages: The Over v iew p age act s lik e a sum m ar y ( Figur e 11 - 10) .
Figu r e 1 1 -1 0 . De ploy m en t descr i pt or : O ve r vi ew
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM
The Beans page list s all EJBs w it h t heir at t r ibut es, WebSph er e b in ding s ( t he JND I nam e) , and t h e generat ed classes ( Figur e 11 - 11) .
Pub Dat e: April 1 1, 20 03
I SBN: 0- 73 84- 26 09 -1
Pages: 75 0
Figu r e 1 1 -1 1 . De ploy m en t descr i pt or : Be an s
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. For now we sk ip t he Assem bly D escr ip t or , Refer en ces, an d Access p ages. The Sou r ce p age show s t h e XML sour ce ( Figur e 11 - 12) .
Figu r e 1 1 -1 2 . De ploy m en t descr i pt or : Sou r ce
ItsoBank5CmpEJB
Account itso.bank5.cmp.AccountLocalHome itso.bank5.cmp.AccountLocal itso.bank5.cmp.AccountBean • Table of Content s Container • I ndex java.lang.String EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper False By Ueli Wah li, Wout er2.x Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss Account Pub lish er: I BM Pub Dat e: April 1 1, 20accountID 03
I SBN: 0- 73 8426 09 -1
Pages: 75 0 balance
interest
Lear n about t he EJB 2 .0 specif icat ion accountType
Dev elop EJBs w it h St u dio App licat ion Dev eloper accountID
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase.
Not e
I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic The yp e tof m anaged per sist fieldout is nt ot t heions deploym ent concep t s tand h eaarcont ch itain ecter ur-e. I n par t icu lar went e point he pnart ew of fu nct int r od uced w it h t he descr ipt or sou r ce. Du r in g t he deploy m en t p rocess, Ap plicat ion D ev elop er generat t heed EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - mes anag concr et e b ean class and inspect s t he accessor m et hods of t he ab st r act class t o per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or det er min t heent t y at pes of of t h eEJBs. inst ance v ariab les. successful impelem ions I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Iim n plem additent ion fin t o der t he m ejb-jar.xml here anegies, ibm-ejb-jar-bnd.xmi ile. cont ain et h ods, dif ferf ile, en t tm app is in galso st r at and sim p le client s tf h at This u se tfile he EJBs. Ats tt he e bindeing infor at ion:EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. he Web en d, Spher w e d escrib how to m deploy
Tailoring the create method When creat ing a new bean by calling t he create m et hod of t h e hom e int er face, t he ejbCreate m et hod and t he ejbPostCreate m et hod w it h t he sam e m et hod sig nat ur e ar e inv ok ed on t h e bean. The gener at ed ejbCreate m et hod of an ent it y b ean only set s t he key f ield: •
Table of Content s
•
I ndex
EJB 2 . 0 D ejava.lang.String ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper public ejbCreate(java.lang.String accountID) throws javax.ejb.CreateException { By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss setAccountID(accountID); return null; Pub lish er: I BM } Pub Dat e: April 1 1, 20 03
I SBN: 0- 73 84- 26 09 -1 Pages: Set t ing only75t 0h e key f ield m ay not sat isf y t he d at abase r equir em ent for t he colum ns t h at h ave a NOT NULL sp ecificat ion and no d ef ault valu e. There ar e a num b er of w ay s t o solv e t h e pr oblem :
Set all t he at t r ibut es t o a default valu e in t he ejbCreate m et hod . The client or session facad e can t h en use t h e set t er m et hod s t o ch ange t h e defau lt v alues t o good values. The dr awnback of tt he h is EJB appr2oach is t hat t w o dat abase calls ar e issued, an inser t ( don e at Lear about .0 specif icat ion ejbPostCreate) and an u pdat e. The EJB cont ainer of WebSph er e cr eat es t he ent it y b ean w hen ejbPostCreate et urApp ns. licat ion Dev eloper Dev elop EJBs w it h St ur dio Ch ange EJBs t he ejbCreate m et t o hav a pvar Deploy t o W eb Sp her e hod Applicat ioneSer eramet er f or each at t r ibut e. How ev er , t his pr act ice has a maj or disadv ant ag e: w hen addin g or rem ovin g an at t r ibu t e, w e hav e t o eejbCreate, ejbPostCreate he hon ome f ace. An d tuse he client code to be This Ichang BM Redbook pr ovides d et ailed in foran m dat tion h owin ttoer ef fect ively WebSp hereh as St udio chang as w ell. f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans App licat ion ed Developer ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Use a t r ansfer ob ject t h at car ries all t h e at t r ibut es as t he single p aram et er in t he ejbCreate call. , Wit ion ,ples w e hav e only e dat ase ing access ( inser t ) .wHow ev er , w e Thr oug hout t he book w ehprt his ov idsolut e exam based on aonsimp leab bank app licat ion it h an hav e ar elat dependency bet w een t h e t r anspor t lay er and t h e dom ain lay er. un derly ing ional dat ab ase. II n his sect ion,in w discuEJBs ss how odify t hea ejbCreate m et set t heand r est of t hteheatbasic t r ibut es n tPart 1, we tre oduce as tao pmart of Jav 2 En t er p rise Edhod it iont o( J2 EE) cover and how t o ad d anot her cr eat e met h od w it h m or e par am et er s. concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp ent at ions method of EJBs. Changing thelem ejbCreate I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session The ejbCreate m et hod shou ld alw ay s be chan ged t o set all t h e fields. I n our case w e ch ange t he beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also code t o: im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. public java.lang.String ejbCreate(java.lang.String accountID) throws javax.ejb.CreateException { setAccountID(accountID); setBalance( new java.math.BigDecimal (0.00) ); setInterest(0); setAccountType("ACCOUNT"); return null; }
Adding an ejbCreate method An en t it y bean can h ave m u lt ip le ejbCreate m et hod s w it h diff er ent sig nat ur es. For each ejbCreate m et hod , m at ching ejbPostCreate m et hod s m ust b e defined. We enh ance t he account bean w it h t hese met hod s: •
Table of Content s
public java.lang.String ejbCreate(java.lang.String accountID, • I ndex EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r java.math.BigDecimal e St udio Appl ica ti on De v el operbalance, int interest) throws javax.ejb.CreateException { By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss setAccountID(accountID); setBalance(balance); Pub lish er: I BM setInterest(interest); Pub setAccountType("ACCOUNT"); Dat e: April 1 1, 20 03 return null; I SBN: 073 84- 26 09 -1 } Pages: 75 0 public void ejbPostCreate(java.lang.String accountID, java.math.BigDecimal balance, int interest) throws javax.ejb.CreateException { } Lear n about t he EJB 2 .0 specif icat ion
Promoting the new ejbCreate method to the home interface Dev elop EJBs w it h St u dio App licat ion Dev eloper The n ew ejbCreate m et hod m ust be pr om ot ed t o t h e hom e int er face as a create m et hod : Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion ejbCreate(int, Developer f or t he BigDecimal, d ev elopm en t of ap plicat s based on t of h eAccountBean Ent er pr ise Javan aBeans Select int) in t hion e Out line view d ( EJB) En artch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. er p rise Bean - > Prom ot e t o Local Hom e I n t erf ace ( con t ex t ) . Thr oug t he gbook , w empretov id e is exam plest obased on a simp le bank ing app licat ion w it h an A hout m at chin create hod ad ded AccountLocalHome. un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. ecifticat I 0msp por a ntion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful I f you imp h ave lem anent edit at ions or opofenEJBs. on t he class ( AccountLocalHome) , y ou hav e t o ex plicit ly save t he up dat ed int erf ace. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At tHere he enare d, w d escrib t o em deploy a efew p ointes how t o r em ber: EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. I f you d ecide t o upd at e t h e ex ist ing ejbCreate m et hod w it h par am et er s, inst ead of creat ing a new m et hod, t hen you hav e t o pr om ot e t he ch anged met h od t o t he hom e int er face and ex plicit ly d elet e t he old create m et hod f rom t he h ome in t erf ace. I f you choose t o hav e local and r em ot e in t er f aces, t hen y ou h ave t o k eep bot h t h e local and t he rem ot e hom e in t er f ace sy nchr onized w it h t h e bean, t hat is, y ou hav e t o pr om ot e any new or ch anged ejbCreate m et hod t o bot h int er faces.
Adding business logic to the enterprise bean All t h e business m et h ods f or an en t erp rise bean ar e creat ed fir st in t he b ean class and t hen pr om ot ed t o t h e com p onent in t er f ace in a second st ep . For our ex am ple, w e w ant t o ad d t h e follow ing m et hods:
• •
deposit, t o of add fu nds Table Content s t o t h e accou nt balance I ndex
t o wr item ovebSphe e fu nds fr udio om tAppl h e accoun t bv el alance EJB 2 . 0withdraw, D e ve lopm e nt h W r e St ica ti on De oper By Ueli Wah li, Wout Larsignal s Schu nkan , Deborah , Martenoug in Weiss The withdraw meretDenay hod er, w ill ex ceptShaddon ion if not h
f un ds ar e av ailab le.
Pub lish er: I BM Pub Dat e:an April 1 1, 20 03 Creating exception class I SBN: 0- 73 84- 26 09 -1 Pages: 75 0e t h e InsufficientFundException class t hat is used t o signal a f ailed w it hdr aw al: First we cr eat
I n t he J2 EE Navigat or view , select t he ejbModule f older and New - > Class. En t er itso.bank5.exception as packag e and InsufficientFundException as class Lear n about t he EJB 2 .0 specif icat ion nam e. Dev elop EJBs w it h St u dio App licat ion Dev eloper For t he su perclass, click Br ow se an d locat e Exception ( in java.lang) . Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Select Con st r u ct or s f r om super class an d click Fin ish ( Figur e 11 - 13) . This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio eelopm 1 1 -1en3t. ofI napsu f fi en dEx t ion App licat ion Developer f orFigu t he drev plicat iontsFun based on cep t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Creating business logic methods
Next , w e upd at e t he bean class ( AccountBean) wit h t h e business m et hods:
// business methods /** * deposit funds */ public java.math.BigDecimal deposit(java.math.BigDecimal amount) { • Table of Content s setBalance( getBalance().add(amount) ); • I ndex return getBalance(); EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper } By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss /** * withdraw funds Pub lish er: I BM */ Pub public Dat e: April java.math.BigDecimal 1 1, 20 03 withdraw(java.math.BigDecimal amount) I SBN: 0- 73 84- 26 09 -1 throws itso.bank5.exception.InsufficientFundException { if (75getBalance().compareTo(amount) == -1) Pages: 0 throw new itso.bank5.exception.InsufficientFundException ("Not enough funds"); else setBalance( getBalance().subtract(amount) ); return getBalance(); } Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
I m por t a nt
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio We ion u seDeveloper t he abst r act et t er anelopm d set ten ert m hods t oion access t h eon v alu p ise er sist enaBeans t App licat f or gt he d ev ofetap plicat s based t hee of Entt he er pr Jav f ield . an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. ( EJB)balance ar ch it ect u re, Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. Sav t heinchan ged EJBs cod e as ( praess Ct rof l+ Jav S ora click e in Ed t h eit ion con t( ext m en u) .cover t he basic I n Part 1,e we t r oduce p art 2 En tSav er p rise J2 EE) and concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- drinterface iv en beans. We also pr ovid e best pr act ice guidelines f or Adding methods to the component successful imp lem ent at ions of EJBs. Aft er t he bu sin ess m et hods of an ent er pr ise bean are cr eat ed in t he b ean class, t hey can b e I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session add ed t o t he com ponent int er face ( local and / or r emot e) . beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im et dh ods, dif fer enst for m app g st rfat egies, sim p le client sart h se t he Theplem g enent er atfin edder getman set met hod t heinCMP ields of tand he AccountBean e at alrueady in EJBs. t he At tlocal he encomp d, w eonent d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. in t erf ace ( AccountLocal) . For new b usin ess m et hod s: Select t he deposit an d withdraw m et hod s in t he Ou t line v iew and En t er p rise Bean - > Pr om ot e t o Local I nt er face . App licat ion Developer add s t he met hod s t o t he local com ponent int er face. Not e t h at t h e Pr om ot e t o Rem ot e I nt er face select ion is disabled b ecause w e d id n ot specify t o hav e a rem ot e int er face g en er at ed.
Deployment The d ep loym ent of a CMP ent it y b ean includ es t hr ee st eps:
• •
Map t heTable CMPoffield s t os a t able in a relat ional d at abase Content I ndex
at e tehe ent r ecode, t hat all hevuelnder EJB 2 . 0Gener D e ve lopm nt wdeploym it h W ebSphe St udio Applis, ica ti ont De oper lyin g
classes t h at t h e cont ainer uses t o inst ant iat e t he bean and hom e an d t o int er act w it h t he r elat ional d at abase
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Com plet e t h e deploy ment descr ipt or w it h W eb Sp her e sp ecific binding s Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0-an 73 8426 09 -1 bean to a table Mapping entity Pages: 75 0
App licat ion Developer of fer s diff erent m apping st r at egies, su ch as t op - dow n, meet - in- t h em iddle, and b ot t om - up. For now we w ill use t he m eet - in- t he- m iddle st r at egy b ecause w e h ave an ex ist ing t ab le for accou nt dat a. For a det ailed discussion of m appin g, see " Map ping st rat eg ies" on p age 46 8. Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Meet-in-the-middle mapping Deploy EJBs t o W eb Sp her e Applicat ion Ser v er We w ant t o map t h e AccountBean t o t h e ACCOUNT t able in t he EJBBANK d at abase: This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. I n t he J2 EE Hier ar ch y v iew , select t he EJB pr oject ( ItsoBank5CmpEJB) and Gener at e - > EJB t ohout RDB tMap ping., w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Thr oug he book un derly ing r elat ional dat ab ase. I n t he EJB t o RDB Mappin g p anel, select Cr eat e a n ew back en d f old er an d click Next . App us of t o Jav m ap bean m ult le backend stt or f or I n Part 1,licat we ion in t r Developer oduce EJBsenables as a p art a 2anEnen t ert itpyrise Ed itt oion ( J2ipEE) and cover hees, basic exam ple, d if fer ent r elat ional d at abase syst em s ( Figur e 11 14) . concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed e 1 1 -1 e at e We a back nde fbest olde per sist ence ent it y b ean s Figu and mressagedr4 iv.enCrbeans. also pr-e ovid prract ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
I n t he Cr eat e new EJB/ RDB Mapp ing panel, select Meet I n t he Middle an d click Next . The D at abase Con nect ion pan el opens ( Figur e 11 - 15) .
Figu r e 1 1 -1 5 . Da t a base con ne ct i on
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. 1. En t er any conn ect ion nam e ( ConEJBBANK) Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an t er EJBBANK as ase. dat abase n ame. un derly2. ing En r elat ional dat ab Theinut ser I D and y if y our ser Iand D iscover aut h orized to the I n Part3. 1, we r oduce EJBspassw as a ord p artf ields of Javcan a 2 be En lef t ertpem riseptEd it ion ( J2u EE) t he basic EJBBANK d at abase and t ab les. Ot herw ise, use t he user I D t hat w as used t o inst all D B2 concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he and t o define t he EJBBANK d at abase. EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or 4. For dat ab vendofor, select DB2 UDB V7. 2 . successful imp lem entase at ions EJBs. For inJDBC dr ivter select DB2 DRI VER. I n Part5. 2, we t r oduce h e, sam p leI BM bank ing APP applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also 6. The class locat ion is p ref illed if D B2 is in st alled on y our sy st em. Make su re t hat t he im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At locat ion p oin t s t o t he db2java.zip f ile. t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
7. You can u se t he Filt ers b ut t on t o lim it t he n um ber of t able d ef init ions t h at ar e im por t ed. I n our case, w e w ill use all t he t ables once w e im plem ent t he com plet e m odel. Ther ef or e, we do n ot u se a filt er . Click Next t o open t he Select iv e Dat abase I m p ort pan el ( Figur e 11 - 16) . Select t he ITSO sch em a t o im por t all t he t ables.
Figu r e 1 1 -1 6 . Se le ct i ng t h e t a ble s f or i mp or t
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev it h StMat u diochApp eloper Clickelop NextEJBs an d wselect by licat Namion e ( Dev Figur e 11 - 17) . Th is opt ion should m at ch ent it ies and at t r ibut es t o t ab les an d colum ns wit h t h e same n am e. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
1m 1 at -1ion 7 . on M at chi opt i ons This I BM Redbook pr ovides d etFigu ailed rinefor h ow t ong ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem attions Click Fin ish t oent st ar t h e of datEJBs. ab ase schem a im por t and t o op en t h e map ping edit or ( Figur e 11 - 18) . I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Figu r e 1 1 -1 8 . M app in g e dit or im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion - As you can see, m at ching by n am e has alr eady m apped t he Account b ean t o t he Dev elop EJBs wtitable h Stand u diot App licat ion Dev ACCOUNT he balance an deloper interest at t rib ut es t o t heir r espect ive colum ns. The m ap ped it em s car ry a lit t le t r iangle as an ind icat or and t hey ar e list ed in t he Deploybot EJBs W eb Sp her e Applicat ion Ser v er t omt opane. This I BM Redbook prgovides et ailed in for m at ion dr onoph.ow t o ef fectnot ivelymat use heree,Stwudio - Mappin is p er fdorm ed by dr ag and I f we had chWebSp ed by nam e w ould App licat ion dr Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise ag t he Account b ean t o t he ACCOUNT t able, and t hen dr ag t he at t r ibu tJav es taBeans o the ( EJB) ar ch itcolum ect u re, d ag d epand loym of subch ap plicat o a WebSph e lef App ion m Serv ns.anDr drent op can e done lef tion - t o-s rtight or r ight -er t ot . licat A bean uster. be m apped t o a t able b ef or e y ou can m ap t he at t r ibu t es of t he bean t o t he colum ns. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing -r elat dat abselect ase. an it em in t he left pane and in t h e r ig ht pane and Cr eat e Youional can also Map pin g ( con t ex t ) . This is t he sam e as d r ag an d d r op. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h eou arrchexam it ect ur e. w I ne par lar e point out tby hednr ew fu nctaccountID ions int r od tuced w it han t he - For p le, comt icu plet e twhe map ping agging o ACCID d EJB 2. 0 sp ecif icat ion t hat tpr id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed accountType o ov ACCTYPE. per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ions tof EJBs. - Not e ent t heat icons hat enab le you t o nav igat e b et w een it em s and t o chang e t he lay out of t he panes. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, anOut d mline essag e- d of r iv en using WebSph er arizes e St u dio ion act Dev . We ealso The v iew t he beans m ap ping edit or sum m ou Ap r mplicat apping iv elop it ies er ( Figur 11 - 19) . im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Figu r e 1 1 -1 9 . Ou t l in e vi ew of t he m a ppi ng ed it or
Sav e t he m ap ping ( pr ess Ct r l+ S) and close t h e ed it or.
Back-end folder App licat ion Developer cr eat es a back- en d f older in t he EJB pr oj ect folder , ejbModule\METAINF\backends\DB2UDBNT_V72_1 in our sam p le, w it h a n um ber of f iles ( Figur e 11 - 20) . •
Table of Content s
•
I ndex
Figu r e 1 1 -2 0 . Back -e nd f olde r
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly elat ional dat ab ase. App licating ion rDeveloper cr eat es a separ at e XMI file f or each im por t ed t able defin it ion ( .tblxmi) , t oget h er w it h one for t h e sch em a ( .schxmi) and one f or t he d at abase ( .dbxmi) . Th e m apping I n orm Partat1,ion weisinstt or r oduce as a p art of f Jav 2 En t er p rise Edf ile it ion ( J2 EE) t and cover inf ed in EJBs t he Map.mapxmi ile. aThe Table.ddl includes he DDL fort he t hebasic t ab le concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he defin it ion s. EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
Generating the deployed code
I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, essag e- datr ing iv ent he beans using cod WebSph e St u dio Dev elop . We also The lastanstdepm is gener d ep loyed e for er t he cont ainAp er .plicat Th is ion sh ould onlyerbe d one w hen im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At all t h e definit ion s are com plet e: t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Cr eat e m et hod( s) defined and pr om ot ed t o t he hom e int er face Business logic m et hods d ef ined and pr om ot ed t o t he com ponent int er face( s) Map ping comp let ed Finder m et hods defined and added t o t he hom e in t er f ace We w ill im plement f in der m et hods lat er w hen t he mod el is com p let e, and t h en r egenerat e t he deploy ed code ( see " Dev elopin g cust om find er s" on p age 45 5) . For n ow w e w ant t o descr ibe t he gener at ion pr ocess and t hen t est t h e sim ple b ean .
Code generation To g en er at e t he deploy ed code, select t he ItsoBank5Cmp p r oject and Gener at e - > Deploy ed and RMI Code. I n t he g en er at ion w indow , select t he Account b ean an d click Fin ish. This op t ion lau nches t he com m and- line ejbdeploy t ool t o generat e t he deploy ed cod e. W hen • Table of Content s gener at ion is com plet e, exp and t he ejbModule f older t o ex plor e t h e generat ed code ( Figur e 11 • I ndex 21 ): EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Figu r e 1 1 -2 1 . De ploy ed code of t he a ccou nt be an
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed I n t he EJB pack age ( itso.bank5.cmp) , y ou find : per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. - ConcreteAccount_xxx, t h e concret e b ean class, w h ich ext end s our abst ract I n Part 2, we in t r oduce t hclass. e sam The p le bank ion and hen imp lem ent ent en tat it ion y beans, AccountBean concring et eapplicat bean class pr ovtides an im p lem of t hesession beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also abst r act g et t er s and set t ers. im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e- EJSLocalCMPAccount__xxx, d escrib e how t o deploy EJB ap ionEJB s t oobject a WebSph er ewApp t hplicat e local , w hich rap licat s t heion Serv er. ConcreteAccountBean class and int er cep t s each m et hod inv ocat ion t o en able EJB cont ainer serv ices for t h e bean. Th e class im plem en t s t he itso.bank5.cmp.AccountLocal in t er f ace. - EJSCMPAccounHomeBean_xxx, t h e EJB hom e class, an d EJSLocalCMPAccountHome_xxx, w hich w rap s t he hom e class an d im plem en t s t h e itso.bank5.cmp.AccountLocalHome in t er f ace t o act as a m et hod int er cept or t o enable EJB cont ainer ser vices for t he hom e. I n addit ion, t he d ep loy m ent t ool creat es t w o packag es w it h deploy ed code:
- itso.bank5.cmp.websphere_deploy— Web Spher e int er nal int er faces - itso.bank5.cmp.websphere_deploy.DB2UDBNT_V72_1— Dat a st or e specif ic helper classes t hat im plem ent t he W eb Sp her e int ern al int er faces
• Tablethe of Content s Completing deployment descriptor •
I ndex
EJB 2 . 0e Dwee vecan lopmteest nt wt it h W ebSphe r e St udio icatti v eleoper Befor he ent it y bean, w e Appl h ave oon upDe dat the
WebSph specif ic bind in gs Schu in fornkm at ion: By Ueli Waher li,eWout er Denay er, Lar , Deborah
deploy ment descr ipt or w it h
Shaddon , Mart in Weiss
Pub lish er: I BM Pub e: April 1 1,Hier 20 03ar ch y I nDat t he J2 EE
v iew , d ouble- click t he ItsoBank5CmpEJB m od ule ( or select Op en Wit h Ed it or ( con t ex t ) .
0- 73 8426 09Descr -1 - >I SBN: Deploy m ent ip t or Pages: 75 0
I n t he Ov er view pan e of t he d ep loym ent descr ipt or edit or ( Figur e 11 - 22) , w e ad d t w o v alues for t he CMP Fact ory Connect ion Bin ding:
Figu r e 1 1 -2 2 . W ebSph er e bi nd ing s in de pl oy m en t de scr i pt or Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem at ions 1. For t he ent JNDI nam of e, EJBs. ent er jdbc/ejbbank. Th is is t he JND I nam e w e used for t h e dat a sour ce definit ion du ring t h e set u p of t he WebSph er e Test Env iron ment ( see " Pr ep ar in g I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session t he Web Spher e t est env iron ment " on p age 38 7) . beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem2. ent For fin der et h ods, en izat t m app g e, st rselect at egies, and simect p leion_Fact client s or t hy. at u se t he EJBs. At t he m Cont ainerdif aufer t hor ion tinyp Per_ Conn t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Ch eck t h at t h e corr ect cur r en t Back en d I D is select ed ( DB2UDBNT_V72_1) . For our f ir st t est act ivit ies, w e do not hav e t o chan ge ot h er pr opert ies of t h e deploy ment descr ipt or .
Testing the enterprise bean I n " Pr ep ar in g t he WebSph er e t est en vir onm ent " on p age 38 7 we discu ssed h ow t o set up a ser v er for t est ing . • Table Content s Now w e can st art of t he Web Spher e t est env iron ment and t est t h e EJB: •
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli IWah li, Wout Denay er, Lar nk , Deborah Martst inart Weiss n t he Serer ver s v iew ofs tSchu he Serv er perShaddon sp ect iv, e, t he
EjbbankServer an d wait un t il t he ser v er is r ead y. The Con sole v iew opens an d you should see t hat t he d at a sour ce is Puballocat lish er: Ied BM, t he W eb and EJB m odules ar e loaded, an d t he m essage Serv er ser v er1 open for Pub e-Dat business e: April 1 1, is 20 disp 03 layed. I SBN: 0- 73 84- 26 09 -1
Select t he ItsoBank5CmpEJB p r oject and Ru n on Ser ver ( con t ex t ) . Pages: 75 0
I n t he Ser ver Select ion w indow , m ak e sur e t hat t he EjbbankServer is pr eselect ed as t he exist ing ser v er. Select Do n ot show t his dialog next t im e ( set t his ser v er as t he p ref er r ed ser v er) an d click Fin ish. The u niv er sal t est clien t laun ch es and w e can t est t he ent it y b ean . Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
Universal test client
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er I n t his sect ion, w e descr ibe som e of t he operat ion s you can per for m w it h t he u niver sal t est This clientI .BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) We w ill aruse ch it ect t h eu tre, est an client d d ept oloym findent t h eofAccount su ch ap EJB plicat h ion ome, s t of ind a WebSph and creat er e App in st an licat ces ionofServ t h e er. Account b ean , and p er for m m et hod s on t hose inst ances. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase.
Home page I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he Figur ows p age of ex t h am e t est as it appear s alit in ay br ser ain winerdow af t ered EJB 2.e011 sp- 23 ecifsh icat ion t he t hath ome pr ov id e, for ple,client en hanced fun ct ion forowcont - m anag select ing an EJB p roj ect and Ru n on Ser ver . per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
Figu r e 1 1 -2 3 . Uni ve r sa l t e st clie nt : hom e pa ge
I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
The t est client can also be st ar t ed fr om t h e Serv ers view b y select in g a ser v er and Ru n u niver sal t est client ( con t ex t ) . The URL of t he t est client is:
http://localhost:9080/UTC/
The t wo m ain p ages t o wor k w it h ar e t he JND I Ex plor er and t he Bean pag e. The JNDI Ex plorer is • of Content s used t o locat Table e EJBs and t he Bean Page is u sed t o w or k w it h EJBs an d JavaBeans. •
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
JNDI Explorer
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er:Ex I BM Click JNDI plorer t o display t h e JNDI Exp lorer p age ( Figur e 11 - 24) . Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
Figu r e 1 1 -2 4 . Uni ve r sa l t e st clie nt : JND I Ex pl or er
Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Aft er ext spand antdicu jdlar bc w y ou can see: concep anding t h e[ Local ar ch it EJB ect urbeans] e. I n par e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or AccountLocalHome— The hom e in t er f ace of t he Account EJB. The JNDI nam e successful imp lem ent at ions of EJBs. ejb/itso/bank5/cmp/AccountLocalHome w as assigned t o t he bean by Ap plicat ion Dev t he deploym ( see Figur 11 -t hen 11 onimp p age 40 8)en . t it y beans, session I n Part 2,eloper we in tin r oduce t h e sament p le descr bank ipt ingorapplicat ion eand lem ent beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also jdbc/ejbbank— The dat a sour ce assign ed t o t he EJBBANK d at abase. im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At tTo hewen witehd tescrib e how tEJB, o deploy EJB AccountLocalHome, ap plicat ion s t o a WebSph e ings App licat Serv er. p age. ord, k w he Account w e click w hicherbr us tion o t he Beans
Beans page The Beans page show s EJB Ref erences ( hom es and com ponent s) , Object Ref erences ( any object s t hat ar e used an d sav ed d ur ing t he session) , Class Refer en ces ( classes t h at ar e loaded exp licit ly ) , and Ut ilit ies ( v ar iou s fu nct ions, such as load a class, cast an inst ance) . I n t he JNDI ex plor er, w e select ed t he AccountLocalHome, an d t herefor e it show s up un der EJB Refer ences on ce w e ex pan d t hat sect ion ( Figur e 11 - 25) .
Figu r e 1 1 -2 5 . Uni ve r sa l t e st clie nt : EJB h ome
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
The create an d findByPrimaryKey m et hod s of t he h om e int erf ace ar e visible un der t he accoun t hom e. I n addit ion, t he remove m et hod is v isib le. Click Met hod Visibilit y an d you can see f rom w hich sup er classes and int er faces t he m et hod s ar e inher it ed, and w hich ones ar e select ed t o be d isplay ed f or t h e accou nt h om e. Lear n about t he EJB 2 .0 specif icat ion To f ind an EJB in st ance, select t he findByPrimaryKey m et hod . Ex pan d t he par am et er , ent er a elop num EJBsbwerit h( 101-1001) St u dio App eloper v alid Dev account , licat an d ion clickDev I n vok e. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er The EJB is in st an t iat ed f rom t he t ab le dat a and show s up in t he r esu lt s sect ion. Click Wor k w it h Ob ject t o add t h e EJB in st ance und er EJB References ( Figur e 11 - 26) . This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans Figu e d1d1ep -2loym 6 . ent Uniofvesur ch saap l tplicat e st ion clie fi nd EJB nstlicat a nce ( EJB) ar ch it ect u re, ran s tnt o a: WebSph er e iApp ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Ex pan d t he AccountLocal in st an ce, select t he getBalance m et hod , and click I n vok e. Th e balance v alue is show n un der r esult s. Click Wor k w it h Object t o save t hat BigDecimal ob ject f or lat er use ( Figur e 11 - 27) .
Figu r e 1 1 -2 7 . Uni ve r sa l t e st clie nt : in vok e a be an m e t h od
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Next , select t he deposit m et hod , w hich requ ir es a BigDecimal p ar amet er ( Figur e 11 - 28) .
Figu r e 1 1 -2 8 . Uni ve r sa l t e st clie nt : in vok e bu si ne ss m e t h od Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. Tr y t he withdraw m et hod w it h a lar ge v alue an d you w ill r eceive t he IInsufficientFundException n Part 2, we in t r oduce t h e sam( pFigur le bank applicat ion and t hen imp lem ent en t it y beans, session e 11ing - 29) . beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At 1 1 -2 9 . Uni ve r saEJB l t eapstplicat clie nts :t obusin e sserm e t hlicat od ion e xce pter.ion t he enFigu d, w e rdeescrib e how t o deploy ion a WebSph e App Serv
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Next , w e cr eat e a new accou nt . Select t he create(String) m et hod of t h e hom e. Ent er a new accou nt num ber ( 101-1111) and click I n vok e. Th e inst ance is creat ed and app ear s in t he r esult s sect ion. Click Wor k w it h Object t o add t h e inst ance t o t he EJB Refer ences ( Figur e 11 - 30) . Lear nFigu about specif ionl r et he 1 1EJB -3 20.0 . Uni veicat r sa
t e st clie nt : cr e at e a n e w accoun t
Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plemd ent deraccount m et h ods, dif fer enrtun m app in gofsttrhe at egies, and sim p le client s t htat EJBs. At Ex pan t he fin new bean and some get m et h ods. You can see hatu se t het he b alance is t0, het he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. int er est is 0, and t he account t y pe is ACCOUNT. These ar e t he d ef ault values t h at w e ad ded t o t h e ejbCreate m et hod ( see " Ch anging t h e ejbCr eat e m et hod " on p age 41 0) . We can use t he set met h ods t o ch ange t he def ault v alues.
I m por t a nt
When y ou u se t he setAccountType m et hod t o change t he account t y pe an d t h en r et riev e it using getAccountType, y ou not ice t hat t he t yp e is st ill ACCOUNT. Th is is because of t he d at abase t r igger t hat m ak es sur e t hat t he account t y pe is consist ent w it h t he DISCRIMINATOR colum n. We w ill be able t o cr eat e ot her account t y pes only w h en w e im plement t he in herit an ce st r uct ur e. •
Table of Content s
•
I ndex
EJB D e ve lopm w it hed W ebSphe r e St udio Next2 ,. 0 select t hee nt t ailor ejbCreate m etAppl hodicawtiitonh De t hrv el eeoper par am et er s.
En t er valu es f or t h e accountID, balance, interest an d click I n vok e.inClick By Ueli Wah li, Wout er Denayan er, dLar s Schu nk , Deborah Shaddon , Mart Weiss Wor k w it h Object t o save t he n ew accou nt ( Figur e 11 - 31) . Pub lish er: I BM
Figu r e 1 1 -3 1 . Uni ve r sa l t e st clie nt : t a ilor e d cr ea t e m e t h od
Pub Dat e: April 1 1, 20 03
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. Use a DB2 comm an d w ind ow t o ver ify t h at t h e inst ances w er e ad ded t o t h e dat abase. En t er tI hese t o list EJBs t he ACCOUNT n Partcom 1, man we inds t r oduce as a p artt able: of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or db2 connect successful imp to lem ejbbank ent at ions of EJBs. db2 select itso.account I n Part 2, we *in tfrom r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. To r em ove t he new accoun t inst an ces, exp and t he inst an ce, select t he remove m et hod an d click I n vok e. Th is delet es t h e accou nt EJB an d also t he r ow in t he ACCOUNT t able. The remove m et hod is inh er it ed f r om t he EJBLocalObject in t er f ace. To r em ove unw ant ed obj ect s fr om t he univ er sal t est clien t p ane, click t h e scissor icon display ed n ex t t o t h e object n am e. This conclud es t he t ou r of t he u niver sal t est client . Close t he b r owser and st op t he EjbbankServer in t h e Ser v er s v iew .
t hat is
Developing the customer and transaction record EJBs I n t his sect ion, w e develop t he cust om er and t r ansact ion r ecord CMP ent it y b ean s.
•
Table of Content s
Customer EJB • I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Table 1 1- 2 sh ows t he at t r ibu t es of t he cust om er bean. To illu st rat e t hat an ent it y bean can hav e By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss a local an d a r em ot e in t er f ace, w e w ill im plem en t bot h. Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
Tab le 1 1 - 2 . List of CM P f i el ds f or t h e cu st om e r b ea n
Pages: 75 0
At t r ib ut e
Ty pe
customerID int
Ke y fi e ld
Loca l int e rf a ce
Yes
---
--
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
firstName java.lang.String No Lear n about t he EJB 2 .0 specif icat ion lastName java.lang.String No Dev elop EJBs w it h St u dio App licat ion Dev eloper title java.lang.String No Deploy EJBs t o W eb Sp her e Applicat ion Ser v er userID java.lang.String No
Re m ot e in t er fa ce
This I BM Redbook pr ovides d et ailed in for m at ion o ef fect ively use WebSp password java.lang.String No on h ow tYes Yes here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans address itso.bank5.beans.Address No ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s tYes o a WebSph er e AppYes licat ion Serv er. Thr t he bookat, tw e ut pre; ovitid e based on tahat simp le bank app licat ion w it h or anmat ion . Not oug ice thout he address rib r efexam er s t oples a Jav aBean holds t h e ing det ailed ad dr ess inf un derly ing r elat ional dat ab ase. We w ill m ap t his at t r ibu t e t o a BLOB colum n in t he CUSTOMER t able. I n " Map ping a d ep en dent v alue class" on p age 47 9, w e im plem en t a m apping int o a separ at e CUSTADDRESS t able. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Implementing JavaBean fordrthe information per sist ence ent it y ba ean s and m essageiv en address beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. Befor e w e im plement t he cu st om er EJB, w e hav e t o define t he add ress bean w it h it s pr opert ies Istreet, n Part 2, we state, in t r oduce e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session city, an dt hzipcode: beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. I n t he ItsoBank5CmpEJB p r oject , creat e a new itso.bank5.beans p ackag e und er ejbModule ( see " Cr eat ing a p ack age" on p age 39 8) . I n t he new packag e, cr eat e a class nam ed Address:
1. Select t he pack age an d New - > Class ( con t ex t ) . 2. En t er Address as nam e. 3. Click Add f or I nt er faces, en t er serial, select java.io.Serializable, click OK. 4.
4. Select n one of t h e m et h od st u bs. 5. Click Fin ish. The edit or opens t h e class. Add f our f ields:
private String street; • •
Table of Content s I ndex private String city;
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli private Wah li, Wout String er Denay er,state; Lar s Schu nk , Deborah Shaddon , Mart in Weiss Pubprivate lish er: I BM
String zipcode;
Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0 f our f ields in t h e Out line view and Gener at e Get t er and Set t er ( con t ex t ) . Click OK Select t he t o generat e t he g et t er / set t er met hod s.
Add a con st r u ct or wit h f our p aram et ers:
public Address(String aStreet,String aCity,String aState,String aZipcode) { Lear n about t he EJB 2 .0 specif icat ion aStreet; Devstreet elop EJBs =w it h St u dio App licat ion Dev eloper Deploy eb Sp her e Applicat ion Ser v er cityEJBs t=o W aCity; This I BM state Redbook = pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio aState; App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re,=anaZipcode; d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. zipcode Thr oug } hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s eand e ar ch it ect ur e. t Ihe n par Sav t het hclass and close editt icu or . lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
Implementing the customer EJB
I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, essag descr e- d r ivibed en beans er eitSt plicat ion39Dev er .eWe Follow tan hedpm rocess in " Cr using eat ingWebSph a CMP ent y u b dio ean "Apon p age 8 t oelop cr eat t healso cust om er im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At CMP ent it y b ean : t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Use t he sam e itso.bank5.cmp p ackag e. The n am e of t he b ean is Customer. Select Local client v iew an d Rem ot e clien t v iew . Add t h e at t r ibut es of Table 1 1- 2 on p age 42 8. For t h e address at t rib ut e, select itso.bank5.beans.Address as t yp e. Pr omot e t he at t r ibu t es t o t h e r em ot e an d local int er faces.
Not e t h at y ou can not select Use t he sin gle k ey at t r ib ut e t y pe for t h e k ey class .
Generated classes The g en er at ed classes are: • •
CustomerBean—t h e abst r act bean Table of Content s I ndex
e hom int er face EJB 2 . 0CustomerHome—r D e ve lopm e nt w it h emot W ebSphe r e Steudio Appl ica ti on De v el oper By Ueli Customer—r Wah li, Wout er Denay s Schu , Deborah , Mart in Weiss emoter, e Lar com ponnken t in t er f Shaddon ace PubCustomerLocalHome—local lish er: I BM
hom e int er face
Pub Dat e: April 1 1, 20 03
CustomerLocal—local com ponent int erf ace I SBN: 0- 73 84- 26 09 -1
Pages: 75 0 CustomerKey—w r apper class for t he k ey
Key wrapper class The wLear r appnerabout classt fhe or EJB t he k2 ey at t r ibut esion con t ain s t he key v alue. The k ey class is r equ ir ed in t h e .0 specif icat findByPrimaryKey m et hod an d t o r et r ieve t he k ey valu e of a bean: Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Customer customer = home.findByPrimaryKey( new CustomerKey(101) ); This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio int keyvalue = ( (CustomerKey)customer.getPrimaryKey() ).getCustomerID(); App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. Create method I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Updat gener m ett hod t o winit ialize out all tthe concepet st he and t h e at ared ch itejbCreate ect ur e. I n par icu lar e point he fnield ew s:fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. public itso.bank5.cmp.CustomerKey ejbCreate(int customerID) I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session throws javax.ejb.CreateException { beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he setCustomerID(customerID); en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. setFirstName(""); setLastName(""); setTitle(""); setUserID(""); setPassword("");
setAddress(null); return null; }
•
Table of Content s
•
I ndex
We d o not add a t ailor ed ejbCreate m et hod w it h par am et er s. EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Business logic
We Pub ad lish d aer:buI BM siness logic m et hod t o t he cust om er bean t o r et riev e t he f ull n ame of a cust om er , Pub 20 03 such asDat Mre:. April Fir st1 1,Last . Use t his code: I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
public String getName() { return new StringBuffer( getTitle() ).append(" ") .append( getFirstName() ).append(" ") Lear n about t he EJB 2 .0 specif icat ion .append( ).toString(); Dev elop EJBs w it h St u diogetLastName() App licat ion Dev eloper
}
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Pr omIot e tRedbook he getName m et hod o bot hintfor hemrem ot eon and local intfect er face. t he mhere et hod t he This BM pr ovides d ett ailed at ion h ow t o ef ively Select use WebSp St in udio Ou e vion iewDeveloper an d En t erfporrise - > Prom t o.. . ( conion t ext ). Appt lin licat t heBean d ev elopm en totofe ap plicat s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Transaction record EJB un derly ing r elat ional dat ab ase. Table sh ows t he atEJBs t r ibu tas es aofp tart he of t r ansact d b ean We( J2 onEE) ly im p lem ent tthe I n Part1 11, 3we in t r oduce Jav a 2ion En recor t er p rise Ed it.ion and cover he local basic int er face f or t his EJB because t he dat a will on ly be r et r iev ed as p ar t of an accou nt t hr e concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od ucedough w it h t thhe rEJB elat2. ion sh ip . 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session leessag 1 1 -e-3d. r iv List of CM P f iWebSph el ds f or h uedio t r aApnsact ionDev r eelop corerd. We beaalso n beans, Tab an d m en beans using er etSt plicat ion im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. At t r ib ut e Ty pe Ke y fi e ld Loca l int e rf a ce Re m ot e in t er fa ce transID
java.sql.Timestamp
Yes
---
--
transType
java.lang.String
No
Yes
--
transAmount java.math.BigDecimal No
Yes
--
Not ice t hat w e do not define t h e for eign k ey ( accountID) as an at t r ib ut e. This f ield w ill be u sed w hen w e define t he r elat ion sh ip b et w een accoun t an d t r an sact ion recor d.
Implementing the transaction record EJB Follow t he p rocess descr ibed in " Cr eat ing a CMP ent it y b ean " on p age 39 8 t o cr eat e t he cust om er CMP ent it y b ean :
• •
Use t heTable sam e p ackag e. of itso.bank5.cmp Content s I ndex
The n am e of t he b ean is TransRecord.
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Select Wah li, Wout er client Denay er, Lar s on Schu nk , Deborah Shaddon , Mart in Weiss Local v iew ly.
t hIeBMat t r ibut es PubAdd lish er:
of Table 1 1- 3 on p age 43 1. Pr om ot e t h e at t r ibut es t o t he local int er f ace.
Pub Dat e: April 1 1, 20 03
Be sur e t o select Use t he sin gle k ey at t r ib ut e t y pe for t h e k ey class . I SBN: 0- 73 84- 26 09 -1
Pages: 75 0
Generated classes The g en er at ed classes are: TransRecordBean—t h e abst r act bean Lear n about t he EJB 2 .0 specif icat ion TransRecordLocalHome—local hom e int er face Dev elop EJBs w it h St u dio App licat ion Dev eloper TransRecordLocal—local com ponent int erf ace Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Create method App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. The g en er at ed ejbCreate m et hod t ak es a Timestamp as par am et er . This is n ot u sef ul in a clien t Thr t he bookw, ew w e ant pr ovt oid hav e exam a simp bank ing app licat appoug licathout ion because e t h ples e actbased ual t imon e of t h e t rleansact ion inser t ed ion an dwtithhe an t y pe an d un derly ing r elat ional dat ab ase. am ount pr ov ided b y t he business log ic. I n ad dit ion, t h e Timestamp v alues m ust be un ique f or t he pr imar y k ey of t he t ab le. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep andejbCreate t h e ar ch it ect urhod e. I n lar w e point out t he n ew fu nct ions int r od uced w it h t he Ch anget st he m et t o par t hist icu code: EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. // new field in the TransRecordBean class I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an java.util.Random d m essag e- d r iv en beans using er e St u dio Ap plicat ion Dev elop er . We also private random = WebSph new java.util.Random(); im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. public java.sql.Timestamp ejbCreate(String type, java.math.BigDecimal amount) throws javax.ejb.CreateException { java.sql.Timestamp ts = new java.sql.Timestamp(System.currentTimeMillis());
ts.setNanos( ts.getNanos() + random.nextInt(999999) ); setTransID(ts); setTransType(type); setTransAmount(amount); • •
Table of Content s
return null; I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
}
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM
Be sur t oe: upd e t20 he Pub eDat Aprilat 1 1, 03 sign at ur e of t he mat ch in g ejbPostCreate m et hod : I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
public void ejbPostCreate(String type, java.math.BigDecimal amount) throws javax.ejb.CreateException { }
Lear n about t he EJB 2 .0 specif icat ion Pr om ot e t he ejbCreate m et hod t o t he local h ome in t erf ace fr om t h e Out line view . Op en t he Dev elop EJBs w it h St u dioand Appdelet licat ion Dev eloper TransRecordLocalHome class e t he old create m et hod . Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Mapping the customer and intransaction beans This I BM Redbook pr ovides d et ailed for m at ion on h record ow t o ef fect ively use WebSp here
St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (We EJB) ar cht oit ect u re, ent of tsu ap plicat ion s t ab o ales. WebSph er et App iongServ h ave m ap t heanndewd ep enloym t it y beans o ch t heir m at ching To open h e mlicat appin edit er. or , select t h e ItsoBank5CmpEJB m od ule and Op en Wit h - > Map pin g Edit or - > DB2UDBNT_V72 _1 . Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly elat dat ase. n in Figur e 11 - 32: Com pleting e t hr e m ional apping asabshow I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Figu r e 1 1 -3 2 . M app in g of cust om er an d t r a nsact ion r e cor d concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans Customer t oent t heof CUSTOMER t able. ( EJB) Dr arag ch itt he ect u re, an d d bepean loym su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Drhout ag t he om, er t rov ibut esexam t o t hples eir colum Thr oug t hecust book w eatpr id e basedns. on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. Dr ag t he TransRecord b ean t o t he TRANSRECORD t able. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Drt sagand t het ht ran r ecor t r tibicu utlar es twoet heir s. n ew fu nct ions int r od uced w it h t he concep e arsact ch ition ect ur e. I dn at par pointcolu outmn t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Not e t h at t h e ACCID colum n in TRANSRECORD is not m ap ped. This is t he f oreign k ey and w e per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or w ill t ak e care of t his t hr oug h a r elat ion sh ip . The relat ionship also show s up as successful imp lem ent at ions of EJBs. "AccountTransrecord" : ACCOUNT in t h e TRANSRECORD t able. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session Sav e t he m ap ping w hen d one. beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Generate the deployed code
Select t he ItsoBank5CmpEJB m od ule and Gener at e - > Deploy and RM I C Code . You can select all t he beans or j ust t he Customer an d TransRecord b ean s ( w e did not chang e t he Account b ean ) .
Testing the EJBs in the universal test client St ar t t he EjbbankServer an d t he u niver sal t est client t o t est t he Customer an d TransRecord beans. Her e ar e a f ew hint s on h ow t o pr oceed:
I n t he JNDI ex plor er, you f in d t he local hom es by ex pan ding [ Local EJB beans] - > ejb - > it so - > b ank 5 - > cm p . You find t h e r em ot e h ome for t h e Customer b ean b y ex pan ding ejb - > it so - > bank 5 - > cm p. • •
Content You canTable u se of local an ds rem ot e hom es t o ret r iev e ent it y beans. I ndex
EJB 2 . 0To D er ve etlopm r iev e nta wcuit st h W omer ebSphe , select r e St udio t h e Appl findByPrimaryKey ica ti on De v el oper By
m et hod , t hen exp and t he CustomerKey, ent er a key v alue ( f or exam p le 101) , an Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss d click I n vok e. Click Wor k w it h Ob ject t o access t h e Cust om er obj ect . PubUse lish er: I BM t he get
met h ods t o ret r ieve t he cust om er dat a. For ex amp le, t he getName m et hod title, firstname, an d lastname.
Pub Dat e: April 1 1,at20t03 should for m he
I SBN: 0- 73 84- 26 09 -1
Not e t h75at Pages: 0 all t he getAddress m et hod r et r iev es null v alues. The d at abase does n ot hav e an y dat a init ialized in t he ADDRESS colum n, w hich is a BLOB. To cr eat e a cust om er w it h an ad dr ess st or ed in t h e table:
1. Ret r iev e a Customer as descr ibed abov e, t hen click Wor k w it h Object . Lear n about t he EJB 2 .0 specif icat ion 2. Ex pan d t he Customer ob ject an d select t he setAddress m et hod . Dev elop EJBs w it h St u dio App licat ion Dev eloper 3. For t he par amet er , exp and t he Ad dr ess p aram et er, en t er fou r st rin g v alues f or st reet , Deploy eb Sp her e Applicat ion Ser v ere. cit EJBs y, st att oe,Wand zipcode, t hen click I n vok This I BM pr ovides d etisailed in for at ion on h owcolum t o efn. fect ively use WebSp udioD B2 4. Redbook The Address ob ject st or ed in mt he ADDRESS You can check t hishere w it hStt his App licat ionselect Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans st at em en t in a D B2 com m and w indow : ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug houtdb2 t he select book , w length(address) e pr ov id e exam plesfrom baseditso.customer on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and e arech ect ur e. I n parbtean icu lar e point inttransType, r od uced w it1h5.t he You can tcrh eat a itTransRecord . Exwapn d t heout part he am netew er , fuennct t erions C as 00 EJB 2.as 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont er - m anag t h e transAmount, an d select null f or t h e AccountLocal. Not e t hat t heain f oreign k ey ed per sist ence ent it y bn) ean m essageiv en beans. also pr ovid best pr act guidelines ( ACCID colum w silland be NULL an d tdr his t r ansact ionWe r ecord w ill note belong t o ice an account . f or successful imp lem ent at ions of EJBs. Ret r iev ing an ex ist ing TransRecord b ean is alm ost im possible because our ejbCreate I n Part 2,hod we adds in t r oduce e sam p le bank applicat henThe impf lem en t itey is beans, m et a nant hosecond v alu e t oing t he k ey t oion b e and uniq tue. ull kent ey valu not session beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also display ed in a D B2 com m and w in dow . im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he enTo d, see w e tdhe escrib e how EJB apob plicat pr opert iest o ofdeploy an Address ject :ion s t o a WebSph er e App licat ion Serv er.
1. Select an d ex ecu t e t he getAddress m et hod on a Customer ob ject . 2. Click Wor k w it h Object an d t he Address ob ject is ad ded u nder Object Ref er ences. 3. Ex pan d t he Address ob ject an d in vok e t h e get m et hod s t o see t he v alues of t h e pr oper t ies. Close t he univ er sal t est clien t an d st op t h e EjbbankServer.
Developing relationships I n t his sect ion, w e add t he t wo r elat ion sh ip s of t he b ank m odel ( Figur e 11 - 33) as con t ainer m anaged r elat ionsh ip s ( CMR) t o t he ex ist in g CMP ent it y mod el. •
Table of Content s
•
I ndex
Figu r e 1 1 -3 3 . Ban k m ode l r el at ionsh ips
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BMelopin Redbook pr ovides d et ailed in for ion44 on7,hw ow t o plem ef fecten ively WebSp here udio ; for I n " Dev g in herit an ce st r uct u res" onmpat age e im t t h euse inher it ance hierStarchy App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans now w e are im p lem ent ing t he relat ionships bet w een t he ent it y beans. ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Support relationships in Application Developer un derly ing rfor elat ional dat ab ase. IApp n Part licat1, ionwe Developer in t r oducesupp EJBsoras t s tahe p art cr eat of ion Jav of a 2t hEn r ee t ert pyp rise es of Ed ritelat ion ionships ( J2 EE) and as CMR. cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b relationship ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or One-to-one (1:1) successful imp lem ent at ions of EJBs. II n ch 2, a case, of t he it y ing b ean s is t heion mast er t hen an d imp t h e lem ot her a tdependent of t he n su Part we in tusually r oduce on t h ee sam p leent bank applicat and entisen it y beans, session m ast er ( alt houg h t h ey cou ld b e eq ual in im por t ance and ind ep en dent as w ell) . beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At I n t he dat abase, su ch a relat ionship is im plem ent ed as a f or eig n k ey in one of t he t ables. To t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. enfor ce t h at each f or eign k ey point s t o anot her inst ance in t he t ar get t able, a u niqu e index m ust be d ef ined on t he for eign k ey . Ou r m odel does not show a 1: 1 r elat ion sh ip. How ever , w e could imp lem ent t he Address as a sep arat e en t it y t hat is r elat ed by a 1: 1 r elat ion sh ip f r om t he Customer ent it y.
One-to-many (1:m) relationship An en t it y inst ance of one t yp e is r elat ed t o m an y en t it y inst ances of a secon d t yp e, but each of t he in st ances of t he secon d t y pe is relat ed t o only on e inst ance of t h e fir st t y pe. I n m any r eal
cases, t h is is a par ent - child comp osit ion st r uct ur e, t hat is, t he secon d en t it y t y pe is a subor din at e and inst ances can on ly ex ist if t h ey h ave a par ent inst ance in t he f ir st t yp e. I n t he dat abase, su ch a relat ionship is im plem ent ed as a f or eig n k ey in t he t ab le of t he secon d t y pe. I n our m odel, Account-TransRecord is a 1: m relat ionship. One accoun t can hav e m any t r ansact ion r ecord s. •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Many-to-many (m:m) relationship
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Tw o in dependent en t it y t y pes ar e r elat ed, and an in st ance of each t y pe can be r elat ed t o man y lish er:ofI BM instPub ances t he ot her t y pe. Pub Dat e: April 1 1, 20 03
I n t he Idat SBN:abase, 0- 73 84-su 26 ch 09 -1a relat ionship is im plem ent ed as an int er mediat e t able t hat cont ains t wo for eignPages: k ey s, each poin t ing t o t h e t able of on e of t he r elat ed ent it ies. The int er m ed iat e t able 75 0 does n ot cont ain any ot her dat a; ot her w ise t h e m odel should hav e an ent it y t o m ap t o t hat t able. I n our m odel, Customer-Account is an m : m r elat ionship. A cust om er can hav e m any accoun t s and an accou nt can belong t o man y cust om er s. The in t erm ediat e t ab le is called CUSTACCT in t h e EJBBANK d at abase. Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
Relationship roles
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er A r ole is assigned t o a relat ionship in each dir ect ion. These role n ames b ecom e t he met h od This I BM ailed at ion on h ow t o ef fect ively use WebSp St udio nam es ( wRedbook it h a get pr p r ovides ef ix) t odtet rav erseint for hem r elat ionship f rom one ent it y in st an ce t o here anot her. App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Navigation CMR Thr oug hout t and he book , w efields pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. Each dir ect ion of a r elat ionship can be mar k ed as Nav igable. For a nav igable r elat ion sh ip, a CMR I n Part field is generat 1, we ined t r oduce int o t he EJBs b ean as . a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he The t yp t he CMR is ov eitid her h e ex t aram getple, enten it yhanced b ean t yfun pe ct( in h e y: 1for dircont ect ion) her ed a EJB 2. 0 espofecif icat ion field t hat pr e, tfor iont alit ain ,eror - meit anag java.util.Collection or java.util.Set ( in t h e : m dir ect ion ) . A Collection or Set h olds per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines all f or tsuccessful he t arg et inst impances. lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Deployment descriptor im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Relat ionships ar e d ef ined in t h e deploy ment descr ipt or using t he d ep loy m ent descr ipt or edit or . Click Add f or Relat ionships 2. 0 in t h e Ov er v iew page ( Figur e 11 - 34) .
Figu r e 1 1 -3 4 . Add in g r e la t i on ship s in t h e de ploy m en t descr i pt or edi t or
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Alt ern at ively t her e is also an Add b ut t on f or r elat ionship s in t he Beans p age af t er select ing an ent it y b ean . Pub lish er: I BM
From d ep loy1m Pubt he Dat e: April 1, ent 20 03 descr ipt or , t h e deploy ed code is g en er at ed. I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Defining the account to transaction record relationship Now let u s defin e t he 1 : m r elat ion sh ip b et w een account s and t r ansact ion r ecor d s:
Lear n about t he EJB 2 .0 specif icat ion Click Add t o open t he Ad d Relat ion sh ip w ind ow . I n t he fir st p anel, select t h e t w o ent it y beans, Account an t h en click Next ( Figur e 11 - 35) . Dev elop EJBs w it h dStTransRecord, u dio App licat ion Dev eloper Deploy EJBs tFigu o W ebr Sp er e 1her 1 e-3Applicat 5 . Re ion la t iSer onvship :
en t it y be an sel ect ion
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also I n t he Relat ionships Roles panel ( Figur e 11 - 36) : im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Figu r e 1 1 -3 6 . Re la t i on ship : r ol es f or a ccou n t t r an sa ct ion r ecor d
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion
1. elop For EJBs t he Account eanApp , set t hion e role am e t o transrecords ( t o ind icat e m an y) , leav e Dev w it h St u bdio licat Devn eloper m ult iplicit y at On e ( one accou nt ) , leav e t he CMR field t he sam e as t he role n ame, an d setEJBs t he tCMR t y Sp pe her t o Collection. Deploy o W eb e Applicat ion Ser v er 2. Redbook For t he TransRecord b eanin, for setmt at h eion role theAccount ( t oWebSp ind icat e onSt e)udio , set This I BM pr ovides d et ailed onn ham owe ttoo ef fect ively use here App licat ionmDeveloper ult iplicit y tfoorMan t he yd(ev man elopm y t r en ansact t of ap ionplicat r ecorion ds)s ,based and leav on et hteheEnt CMR er prfield ise Jav t heaBeans sam e as ( EJB) ar ch it t he ect role u re, nan ame. d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug3. houtSet t heb ot book h d ,irect w e ions pr ovtid o eNav exam igable. ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. 4. Select Cascade d elet e t o delet e all t r ansact ion r ecord s w hen an account is delet ed . I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic ThetForeign che. eckI nbox select and disabled. mnct r elat ionship concep5. t s and h e ar ch itk ey ect ur par is t icu lar wed e point out t he nA ew1:fu ions int r od requ ucedires w it ha t he for eign k ey in t he t able of t he sin gled valu e en t it y bean and no for eig n key opt ioned EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag set t in g is available. I n a 1: 1 r elat ionship, y ou hav e t o in dicat e on w hich side of t h e f or per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines r elat ion sh ip t h e for eign key ex ist s. successful imp lem ent at ions of EJBs. Click ish. t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session I n Part6. 2, we inFin t r oduce beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At tDeployment he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. descriptor and generated methods Sav e t he d ep loym ent descr ipt or . You can select t he Sou rce p age and scroll dow n t o see t he defin it ion of t h e r elat ionship as an element in t he XML sour ce. For a d et ailed discu ssion of t he r elat ion ship element , see " Declar ing t h e r elat ionship in t he deploy m en t d escrip t or" on p age 12 5.
BankAccount methods
Op en t he AccountBean class and t h e AccountLocal in t er f ace and look at t h e m et h ods g en er at ed for t he r elat ionship:
public java.util.Collection getTransrecords(); public void setTransrecords(java.util.Collection aTransrecords); •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
TransRecord methods Pub lish er: I BM
Sim ilar ly m et hods ar e g en er at ed int o t he TransRecordBean an d t he TransRecordLocal: Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
public itso.bank5.cmp.AccountLocal getTheAccount(); public void setTheAccount(itso.bank5.cmp.AccountLocal aTheAccount);
Lear n about t he EJB 2 .0 specif icat ion
Code customization for transaction records Dev elop EJBs w it h St u dio App licat ion Dev eloper Ou r dat ab aseEJBs m odtel does allow t he fion or eig n vker ey ( ACCID) in t h e TRANSRECORD t able is NULL. Deploy oW eb Sp her et hat Applicat Ser This w as set t o enable t h e creat ion of TransRecord in st an ces f or t est ing. I n r ealit y , t h e for eign kThis ey sh ould be set tpr o ovides NOT NULL b ecauinsefort ran r ecor cann ex ist it hout an accoun t. I BM Redbook d et ailed m atsact ion ion on h ow tds o ef fect ot ively usew WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans We h ave setutre, he an relat o t he elat nt dur t he cr eaterion of alicat t r ansact ioner. ( EJB) ar cht oit ect d dionship ep loymtent of rsu ched apaccou plicat ion s t oing a WebSph e App ion Serv r ecor d. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an We m odif rcedat code b y addin g t he accou nt local in t erf ace ( AccountLocal) as a p aram et er un derly ingy tr he elatsou ional ab ase. in t he ejbCreate an d ejbPostCreate m et hod s of t he TransRecord b ean , and m odif ying t h e create m et hod TransRecordLocalHome. I n Part 1, we in t of r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or TransRecordBean successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also public java.sql.Timestamp ejbCreate(String type, java.math.BigDecimal im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. amount, AccountLocal anAccount) throws javax.ejb.CreateException { ...... // no change to code return null; }
public void ejbPostCreate(String type, java.math.BigDecimal amount, AccountLocal anAccount) throws javax.ejb.CreateException { setTheAccount(anAccount); •
Table of Content s
•
I ndex
}
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
TransRecordLocalHome Pub lish er: I BM
Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0 public itso.bank5.cmp.TransRecordLocal create(
String type, java.math.BigDecimal amount, AccountLocal anAccount) throws javax.ejb.CreateException; Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
IDeploy m porEJBs t a ntt o W eb Sp her e Applicat ion Ser v er This ITo BMenab Redbook ovides df et ailed kiney for onSpher h ow et oApp ef fect ively usev WebSp le non-prnullable oreign s,mt hateion Web licat ion Ser er v5 . 0here cont St ainudio er App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans defer s t h e inser t of CMP 2. 0 ent it ies in t he dat a st or e u nt il t h e ejbPostCreate m et hod ( EJB)r et arur chns. it ect u re, din an dg dt oept he loym entspecificat of su ch ions, ap plicat ionion s tsh o aip WebSph er ece, Appinlicat Accor EJB r elat m aint enan cludion ing Serv er. for eign k ey set t ing , ar e par t of ejbPostCreate. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat dat ab WebSph er eional Ap plicat ionase. Serv er v 4. x insert s con t ainer- man aged ent it ies af t er t er min at ion of ejbCreate. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed perpsist b ean essagedr iv en Weaccount also pr ovid e best ice eguidelines To r evence ent tent he it myov in g sofand t ranmsact ion r ecor ds fbeans. r om one t o anot her ,prwact e hav t o delet e f or imp tsuccessful w o g en er at ed lem set tent er at m ions et hodofs EJBs. fr om t h e local int er faces: I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- dinr ivt hen beans using WebSphinert er e fSt u dio Ap plicat ion Dev elop er . We also setTheAccount e TransRecordLocal ace im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At in ttoh edeploy AccountLocal in tion er f ace t he ensetTransrecords d, w e d escrib e how EJB ap plicat s t o a WebSph er e App licat ion Serv er. I nst ead of d elet ing t he met hod s, w e can com m en t ou t t h e m et h ods in t he cod e:
// public void setTheAccount(itso.bank5.cmp.AccountLocal aTheAccount); // public void setTransrecords(java.util.Collection aTransrecords);
I m por t a nt I nv okin g setTransrecords can lead t o SQL except ions if an account for eign key cannot be set t o null.
•
Table of Content s
Mapping and deployed code • I ndex EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
We w ill wli, aitWout w iterh Denay the m code gShaddon en er at ion er w e define t h e m : m r elat ion sh ip By Ueli Wah er,apping Lar s Schuand nk , Deborah , Martaft in Weiss bet w een cust om er and accou nt . Pub lish er: I BM Pub Dat e: April 1 1, 20 03
Defining to account relationship I SBN: 0-the 73 84-customer 26 09 -1 Pages: 75 0
Now let u s defin e t he m : m r elat ionship bet w een cust om er an d accoun t s:
I n t he Ov er view pag e of t he d ep loym ent descr ipt or edit or , click Add t o open t he Ad d Relat ionship w in dow . I n t he f ir st panel, select t he t w o en t it y beans, Cu st omer an d Account, about t he tLear hen nclick Next . EJB 2 .0 specif icat ion Dev elop EJBs w it h StRoles u dio App licat ion Dev eloper I n t he Relat ionships panel ( Figur e 11 - 37) : Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Figu r e 1 1 -3 7 . Re la t i on ship : r ol es f or cust om e r a ccou nt
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
1. Set t h e r ole nam es t o accounts an d customers ( t o ind icat e m an y) . 2. Set m ult iplicit y t o Man y on b ot h sides.
3. Accept t he default s in t h e r est of t he f ields. 4. Click Fin ish, t h en sav e t he d ep loym ent descr ipt or . Deployment descriptor •
Table of Content s
Figur e 11 - 38I ndex sh ows t he Ov er view pag e of t he d ep loym ent descr ipt or w it h t h e t w o r elat ion sh ips. • EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Figu r e 1 1 -3 8 . De ploy m en t descr i pt or w i t h r e la t ion sh ip s
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Code customization for customer to account Deploy EJBs t o W eb Sp her e Applicat ion Ser v er We w ant t o hav e t ailored m et h ods t o add and r emov e an accou nt t o/ fr om a cust om er and t o ad d This rIemov and BM Redbook e a cu st omer pr ovides t o/ fdr om et ailed an accou in for m ntat . We ion on remhove ow ttoheefgfect en er ively at eduse met WebSp h ods fhere rom tSt heudio CustomerLocal an d AccountLocal in t eren f aces: App licat ion Developer f or t he d ev elopm t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an // public setAccounts(java.util.Collection anAccounts); un derly ing r void elat ional dat ab ase. I n Part 1, wevoid in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic // public setCustomers(java.util.Collection aCustomers); concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
Customer methods to add and remove accounts
I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session We ad dan t wo ionship met hod sWebSph in t he CustomerBean classion and t h elop en prerom ot ealso the beans, d mr elat essag e- d r iv- sp enecific beans using er e St u dio Ap plicat Dev . We m hods t o fin t he local er f ace, CustomerLocal: imet plem ent der m etin h tods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. public void addAccount(AccountLocal anAccount) { getAccounts().add(anAccount); } public void removeAccount(AccountLocal anAccount) { getAccounts().remove(anAccount);
}
Account methods to add and remove customers I n t he AccountBean class, w e add t w o m et hod s for cu st om er s and t hen pr om ot e t h e met h ods t o •t he local in t erTable Content s f ace,ofAccountLocal. •
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
public void addCustomer(CustomerLocal aCustomer) { Pub lish er: I BM
getCustomers().add(aCustomer);
Pub Dat e: April 1 1, 20 03
}
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
public void removeCustomer(CustomerLocal aCustomer) { getCustomers().remove(aCustomer); } Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Not e
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat Developer f oratted he fdorevt elopm enionships t of ap plicat on tt oh er em Entot ereprint ise aBeans Theion m et hod s gener he relat are ion n otspbased rom ot ed er Jav faces. ( EJB)Relat ar chionships it ect u re, ar anedhdandled ep loymby entlocal of su ch ap plicat ion s t o a WebSph er e App licat ion Serv int er faces, CustomerLocal an d AccountLocal. er. Thr oug hout t he book , wan e pr id e exam ples based le bank ion it h s.an The getCustomers d ov getAccounts m et hod s on r etau rsimp n collect ionsing of app locallicat EJB obwject un derly ing r elat ional dat ab ase. To u se su ch d at a in a r em ot e m et h od, t h e r ef erences w ould hav e t o b e conv er t ed. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Creating a remote method per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. The con t ain er - m anag ed r elat ion ships g en er at e m et hods using local in t erf aces on ly. To use such Ianr elat Partionship 2, we inmt et r oduce samot p ele int bank ing applicat imp lem it y beans, session h od int hae rem er face, w e hav ion e t o and cod et hen special metent hoden s. tFor ex am ple, to beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also r et riev e t he account nu mb er s f or a cu st om er f or t he r em ot e int er face, w e can w r it e a im plem ent fin der m etm h ods, ferden t m app at egies, p le client s t h at u se t he EJBs. At getAccountNumbers et hoddifan p rom ot e in it gt osttrhe r emot eand in t sim erf ace: t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
// CustomerBean public String[] getAccountNumbers() { java.util.Collection accounts = getAccounts(); if ( accounts.size() == 0 ) return null; String[] result = new String[ accounts.size() ];
java.util.Iterator it = accounts.iterator(); for (int i=0; it.hasNext(); i++ ) { AccountLocal account = (AccountLocal)it.next(); result[i] = (String)account.getPrimaryKey(); • •
Table of Content s
}
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
return result;
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
} Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
JNDI names and references Op en t he deploym ent descr ipt or on t he Refer ences p age ( Figur e 11 - 39) .
Figu r e 1 1 -3 9 . EJB r e fe r e nce s in t he de pl oy m e nt de scr ipt or Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also For ever y ent it y bean w it h a r elat ion sh ip , a refer ence t o t he r elat ed ent it y bean is insert ed. You im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At can see t h e nam e, w hich is t h e local JNDI n ame used in t he d ep loyed cod e, and t he W eb Sp her e t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. bind ing, wh ich is t h e global JNDI nam e. For exam ple, a cu st omer ent it y bean f inds t he account hom e int er face t o access r elat ed accoun t s t hr ou gh t he local nam e ejb/Account, w hich point s t o t h e global nam e ejb/itso/bank5/cmp/AccountLocalHome.
Changing JNDI names Becau se we generat ed r em ot e an d local in t er f aces f or t he Customer b ean , t h e global JND I nam e for Customer is ejb/itso/bank5/cmp/CustomerHome.
JNDI n am es w it h m any q ualificat ions ar e not good f or per for m ance. Theref ore, w e chan ge all t he global JNDI n ames t o a n ew nam ing conv en t ion :
ejb/itsobank/Account ejb/itsobank/Customer •
Table of Content s
•
I ndex ejb/itsobank/TransRecord EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM
First , on t h e Bean s pag e ( Figur e 11 - 40) , select each en t it y bean and chang e t he JND I nam e un der t he W eb Spher e Binding s heading ( t h ree chan ges) .
Pub Dat e: April 1 1, 20 03
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Figu r e 1 1 -4 0 . Ch an gin g t he g loba l JNDI na m e
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er d, on t hpr e ovides Refer endces pag ein(for Figur 11 -on 41)h,ow select EJBLocalRef enthere r y and chan ge This ISecon BM Redbook et ailed m ateion t o efeach fect ively use WebSp St udio t heion JNDI nam e u nder t h ed ev WebSph er et of Bind gs h ion eadsinbased g ( fouroncht hanges) . pr ise Jav aBeans App licat Developer f or t he elopm en apinplicat e Ent er ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Figu r e 1 1 -4 1 . Ch an gin g t he EJB r ef e r en ce s t o t he JN DI n am e
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase.
I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Sav e and close t h e deploy m en t d escrip t or w hen done.
Mapping the relationships Op en t he map ping edit or b y select in g t he ItsoBank5CmpEJB m od ule and Op en Wit h- > Map pin g Ed it or - > DB2UDBNT_V72 _1 . Com plet e t h e m apping b y dr aggin g t h e CMR f ields of t he ent it y b ean s t o t he f oreign k ey r elat ion sh ip s in t he t ab les ( Figur e 11 - 42) :
Figu r e 1 1 -4 2 . M app in g of r e la t i onshi ps
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic I nt st he CMR ( left side) t o uced w it h t he concep andAccount t h e ar chbitean ect,urdre.agI nt he parcustomers:Customer t icu lar w e point out t he n ewfield fu nct ions int r od "CAtoCustomer":CUSTOMER ( righ t side) . Th is map s t he relat ionship fr om Account t o ed EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag Customer. per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n t he Customer b ean , dr ag t he accounts:Account CMR field ( left side) t o "CAtoAccount":ACCOUNT t side) is mapion s Customer o Account. I n Part 2, we in t r oduce t h e sam(prigh le bank ing. Th applicat and t hen timp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also I n t he TransRecord b ean , dr ag t he theAccount:Account CMR field ( left side) t o im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At "AccountTransrecord":ACCOUNT ( righ t side) . Th is map s t he relat ionship bet w een Account t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. and TransRecord. Not e t h at t h e transrecords:TransRecord CMR field u nder Account is now m app ed as w ell. ( You cann ot d rag t h e CMR f ield und er Account t o t h e for eig n key —t he m app ing m ust be d one in t he direct ion of t he f oreign k ey . ) Sav e t he m ap ping w hen d one.
Generating the deployed code for relationships
An upd at e in t he int er faces ( r elat ion sh ip m et hod s) and in t he d at abase m apping s r eq uir es a r eg en er at ion of t he deployed code:
I n t he J2 EE Hier ar ch y v iew , select t he ItsoBank5CmpEJB p r oject and Gener at e - > Deploy and RM I C Code ( con t ex t ) . • •
Table Content Select all t heofent it y b sean s and click Fin ish. I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
I m por t a nt
Pub lish er: I BM
Pub e: tApril 1 1, 20s03v iew ChDat eck h e Task
aft er g en er at ion. I f t here are er r ors, it is somet im es necessary t o delet I SBN: e 0all73t84he26deployed 09 -1 code and r egenerat e again. Pages: 75 0
Select t he ent it y b ean s and Delet e ( con t ex t ) . I n t h e Delet e w ind ow , deselect Delet e Bean Only an d Delet e Bean Classes. Th en deselect Delet e Access Bean; onl y sel ect D e le t e D ep lo y ed Co d e . Click OK. These ar e t he p ack ages an d classes t h at shou ld b e delet ed: Lear n about t he EJB 2 .0 specif icat ion The w h ole packag e itso.bank5.cmp.websphere_deploy Dev elop EJBs w it h St u dio App licat ion Dev eloper The w h ole packag e itso.bank5.cmp.websphere_deploy.DB2UDBNT_V72_1 Deploy EJBs t o W eb Sp her e Applicat ion Ser v er I n packag e itso.bank5.cmp, all classes st ar t ing w it h an u nder scor e ( _ Xxx x xx x ) , This I BM Redbook prXx ovides in for at.ion on h ow t o ef fect ively use WebSp here St udio Co n cre t e xx x xxd, et orailed EJS Xx xx m x xx App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Testing in the universal test client un derly ing relationships r elat ional dat ab ase. IThe n Part t r oduce a ped artwof 2 En p riseionships. Ed it ion (Th J2 eEE) and of cover he basic u niv1,erwe sal in t est clien t EJBs is n otassuit ellJav t o at est t hteerrelat r esult t he rt elat ionship concep t s Collection and t h e ar chob it ect urse.t hat I n par point out t he nct ions r ods.uced it hetthod he calls are ject ar et icu invlar alidwateed as soon as n t hew e t rfuansact ionint end Eachwm EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er m anag ed call fr om t he t est clien t is a t r ansact ion , so w hen you w ant t o ex plor e a collect ion r esu lt , t he d at a peralr sist ence ent it y and b eany ou s and essagedr iv en beans. We also pr ovid e best pr act ice guidelines f or is ead y inv alid getman IllegalStateException. successful imp lem ent at ions of EJBs. We w ill t est t h e r elat ionship s using a session bean ( see " Test in g t he bu sin ess logic in t he Iun n iv Part 2, twe t r oduce e sam le. bank ing applicat ion and t hen imp lem ent en t it y beans, session er sal est in client " on tphage 53p4) beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Developing inheritance structures To com plet e t h e bank ing m odel show n in Figur e 11 - 33 on p age 43 5, w e n ow im plement t he t w o subclasses of Account, Checking an d Savings. •
Table of Content s
•
I ndex
Support for inheritance in Application Developer EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
The EJB 2 .0 specificat ion d oes not sup por t in herit an ce. How ev er , I BM pr oduct s, such as VisualAge f or Jav a, WebSph er e App licat ion Serv er, and t he Ap plicat ion Dev elop er , p rov ide Pub lishfer: BM supp ort or Iinher it ance st ru ct ur es of en t it y EJBs. Pub Dat e: April 1 1, 20 03
En t it y EJBs can inher I SBN: 073 8426 09it-1at t ribu t es and m et hods f r om t he su perclass en t it y bean, define ad dit ional at t r ibut es an d met h ods, and ov er w r it e inher it ed m et hods. Pages: 75 0
Defining the inheritance structure App licat ion Developer enables us t o d ef ine ent it y b ean s as subclasses of ex ist ing ent it y b ean s in about t he EJBBean 2 .0 specif icat t he CrLear eat en an Ent er pr ise w izard . ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
Checking Deployaccounts EJBs t o W eb Sp her e Applicat ion Ser v er This BM Redbook pr ovidesent d et at ion onofh ow t o ef fect ively use WebSp here St udio Let ' s Idefin e t he Checking it yailed EJB in asfor am sub class Account: App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. I nhout t he J2 EEbook Hier,arwch iew , select t he ItsoBank5CmpEJB od ule - > Ent pr ise Thr oug t he e yprvov id e exam ples based on a simp le m bank ingand appNew licat ion w it heran Bean ex t ) . dat ab ase. un derly ing (rcon elattional The r oject is pr eselect clickofNext 0 it Bean, ent er and Checking I n Part 1, pwe in t r oduce EJBs ed as, asop art Jav a. Select 2 En t erCMP p rise2.Ed ion ( J2 EE) cover as t hebean basic nam e, m ak e su r e t he itso.bank5.cmp p ackag e is select ed, and click Next . concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Com plet e itt hy ebEnt isemBean Detdr ails panbeans. el ( Figur 11 - 43) as follow per sist ence ent eaner s pr and essageiv en Wee also pr ovid e bests:pr act ice guidelines f or successful imp lem ent at ions of EJBs.
Figu r e 1 1 -4 3 . De fi ni ng t h e ch eck i ng a ccoun t en t i t y be an I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
1. Select Account as bean su pert y pe. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er 2. Set t h e EJB bindin g n am e t o ejb/itsobank/Checking ( t o f ollow t he conv ent ion w e This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio im plem ent ed in " Ch anging JNDI nam es" on p age 44 4) . App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. 3. Select Local client v iew ( su bclasses m ust h ave t he sam e int erf aces) . Thr oug4. houtClick t heAdd bookf or , wCMP e pr ov e exam ples based app licat ion w it h an at tidr ibu t es. D ef ine one on at t raibsimp ut e wleit bank h t heing nam e overdraft, un derly ing java.math.BigDecimal, r elat ional dat ab ase. an d p rom ot e get t er / set t er m et h ods. I n Part 1, Fin we ish in t ran oduce as a p ent art itofy Jav a 2appear En t er psrise ion Account ( J2 EE) and cover t heJ2EE basic Click d t heEJBs Checking bean undEd er itt he b ean in t he concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he Hier ar chy v iew . EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
Savings accounts
I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, m essag e- dp rrocess iv en beans using er e St u dio Ap plicat ion Dev elop er . We also Repeat an t hed def in it ion for sav ings WebSph accoun t s: im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. The b ean n ame is Savings The su perclass is Account The EJB b in ding nam e is ejb/itsobank/Savings Defin e one at t r ibu t e: minAmount, java.math.BigDecimal
Generated classes and interfaces
The t wo subclasses Checking an d Savings ar e n ow v isible in t h e J2EE Hierar chy v iew, t oget her w it h t he b ean class ( CheckingBean, SavingsBean) , t h e local hom e int er faces ( CheckingLocalHome, SavingsLocalHome) , an d t he local comp onent int erf aces ( CheckingLocal, SavingsLocal) . The b ean classes ex t en d t he AccountBean class and cont ain only t h e abst r act get t er an d set t er m et hod for t he at t r ibu t e t hat w as add ed t o t he sub class. •The h om e intTable of Content s create an d findByPrimaryKey m et hod s. Not e t hat t he hom e erf aces con t ain •int er faces doI ndex not ex t end t he AccountLocalHome in t er f ace. EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
The com en terint er faces ain he g et tShaddon er and, set erWeiss m et hod for t he at t ribu t e t h at w as ad ded By Ueli Wahpon li, Wout Denay er, Larcont s Schu nk , tDeborah Marttin t o t he sub class. The com ponent int er faces do ex t end t he AccountLocal in t er f ace and inher it all t he met h ods of t he Account b ean . Pub lish er: I BM
Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
Tailoring generated code Pages: 75the 0 Tailorin g of t h e gener at ed code in volv es t he cr eat e m et h ods an d t h e bu siness m et hods.
Create methods Lear n about t he EJB 2 .0 specif icat ion I n t he cr eat e met h ods, w e hav e t o set t he cor rect accoun t t y pe an d in it ialize t he ext r a at t r ibut e. EJBs w it h fSt u dio App licat eloper BecauDev se telop he hom e in t er ace does not in ion her itDev fr om t he par ent bean, w e can design t he cr eat e m et hods f or each class indiv idually . Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Checking App licat ion bean Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. We ad d an ejbCreate m et hod w it h a p aram et er for t he ex t ra at t r ibut e ( do not for get t o hav e a Thr hout t he book , w e m pret ovhod id e) exam ples based on a simp le bank ing app licat ion w it h an m atoug ching ejbPostCreate : un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t sjava.lang.String and t h e ar ch it ect ur e. I n par t icu lar w e point out t he naccountID, ew fu nct ions int r od uced w it h t he public ejbCreate(java.lang.String EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essagedr iv en beans. We also balance, pr ovid e best pr act ice guidelines f or java.math.BigDecimal int interest, successful imp lem ent at ions of EJBs. java.math.BigDecimal overdraft) I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also throws javax.ejb.CreateException { im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he super.ejbCreate(accountID, en d, w e d escrib e how t o deploy EJB ap plicatinterest); ion s t o a WebSph er e App licat ion Serv er. balance, setAccountType("CHECKING"); setOverdraft(overdraft); return null; } public void ejbPostCreate(java.lang.String accountID,
java.math.BigDecimal balance, int interest, java.math.BigDecimal overdraft) throws javax.ejb.CreateException { } •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Pr om ot e t he ejbCreate m et hod t o t he local h ome in t erf ace and it ap pears as a create m et hod By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss in CheckingLocalHome. Delet t he m et hod f rom CheckingLocalHome. We only k eep t he m et hod w it h Pubelish er: create(String) I BM par am et er s. Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Savings bean Mak e t he sam e chan ges in t he SavingsBean. Ad d an ejbCreate m et hod :
Lear n about t he EJB 2 .0 specif icat ion public java.lang.String ejbCreate(java.lang.String accountID, Dev elop EJBs w it h St u dio App licat ion Dev eloper java.math.BigDecimal balance, int interest, Deploy EJBs t o W eb Sp her e Applicat ion Ser v er java.math.BigDecimal minAmount) This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio javax.ejb.CreateException { ap plicat ion s based on t h e Ent er pr ise Jav aBeans Appthrows licat ion Developer f or t he d ev elopm en t of ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. super.ejbCreate(accountID, balance, interest); Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an setAccountType("SAVINGS"); un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic setMinAmount(minAmount); concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB return 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed null; per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. } Ipublic n Part 2,void we inejbPostCreate(java.lang.String t r oduce t h e sam p le bank ing applicat accountID, ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t java.math.BigDecimal m app in g st r at egies, and balance, sim p le client s t hinterest, at u se t he EJBs. At int t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. java.math.BigDecimal minAmount) throws javax.ejb.CreateException { }
Do not for get t o pr om ot e t h e ejbCreate m et hod t o t he local h ome in t erf ace and delet e t he create(String) m et hod f rom SavingsLocalHome. We only k eep t he m et hod w it h par am et er s.
Business methods The b usiness met h ods ar e deposit an d withdraw. Th er e is n o chang e r eq uir ed f or deposit ; how ev er , f or withdraw w e hav e t o com par e w it h t he overdraft v alue f or checking account s and t he minAmount v alue f or sav in gs account s. Ov erw r it e t h e withdraw m et hod in t he CheckingBean an d SavingsBean classes: •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Checking bean
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 public java.math.BigDecimal withdraw(java.math.BigDecimal amount) I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
throws itso.bank5.exception.InsufficientFundException {
if ( getBalance().add( getOverdraft() ).compareTo(amount) == -1) throw new itso.bank5.exception.InsufficientFundException ("Checking: Not enough funds - overdraft"); Lear n about t he EJB 2 .0 specif icat ion ...... Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Savings bean App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an public java.math.BigDecimal un derly ing r elat ional dat ab ase. withdraw(java.math.BigDecimal amount) I n Part 1, we in t r oducethrows EJBs as itso.bank5.exception.InsufficientFundException a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he { basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he ).compareTo(amount) ==er-1) EJB if 2. 0(spgetBalance().subtract( ecif icat ion t hat pr ov id e, for getMinAmount() ex am ple, en hanced fun ct ion alit y for cont ain - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or throw itso.bank5.exception.InsufficientFundException successful imp new lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ("Savings: ing applicat Not ion enough and t henfunds imp lem - ent minAmount"); en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ...... ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Not e t h at w e d o not hav e t o pr om ot e t he withdraw m et hod t o t he local int er face b ecause it is inher it ed an d w e did n ot chan ge t h e sig nat ur e of t h e m et hod.
Relationships for inherited beans The su bclasses inher it t h e bean an d com pon en t in t er f ace m et hod s fr om t he par ent . Th er efor e w e can use t he g en er at ed met h ods ( getTransrecords, getCustomers) t o f ollow relat ionships fr om Checking an d Savings b ean s t o relat ed ent it ies.
When t r aver sin g a r elat ion sh ip f r om a Customer or TransRecord t o Account b ean s, t he act u al inst ance of a relat ed accoun t is eit her a Checking or a Savings ob ject . The client cod e can exam ine t he t y pe of ob ject and t h en cast t h e Account ob ject t o t he cor r ect su bclass for f ur t her oper at ions. This is illust r at ed in " List a cust om er wit h account s and t r ansact ion r ecor ds" on p age 53 0. •
Table of Content s
Mapping approaches for inheritance • I ndex EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Ther ar eli,t w o ap oach m ap in herShaddon it ance, st ruinctWeiss u r e: By Uelie Wah Wout er pr Denay er,es Lar tsoSchu nk , an Deborah Mart PubMap lish er:all I BM ent it y
b ean s int o one t ab le t hat con t ains colum ns f or all t h e at t r ibut es. At t r ibu t es of m app ed t o nullable colum ns.
Pub Dat e: April 1 1, 20 03 be subclasses m ust I SBN: 0- 73 84- 26 09 -1
Map Pages:t he 75 0su perclass ent it y t y pe in t o a root t able ( par en t ) t h at cont ain s t he shar ed at t r ibut es. Map t h e subclass en t it y t y pes int o leaf t ab les ( childr en) t hat cont ain a f oreign k ey t o t h e par en t t able and t he at t r ib ut es of t he su bclass. Root / leaf m appin g shou ld n ot b e used w it h deep in herit an ce st r uct u res, because m any t ables m ust be j oin ed t o r et r ieve a single ent it y b ean . n about t het he EJBt able 2 .0 specif ion I n botLear h appr oaches, of t heicat t oplevel ent it y b ean m ust inclu de a sp ecial colum n called t he discr im inat or colum n. The v alue of t h e discr im inat or colum n det erm ines t h e t y pe of t he elop ent it yDev b ean . EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans I m por t a nt ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. The d iscr imin at or colum n m u st not be u sed as an at t r ibut e of t he t op - lev el ent it y bean. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s andthe t h e account ar ch it ect ur e.inheritance I n par t icu lar wstructure e point out t he n ew fu nct ions int r od uced w it h t he Mapping EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence entm it yodel, b eanwseand essagedr iv en beans. WeAccount, also pr ovid e best pran actdice guidelines I n our sam ple can m u se t he ACCOUNT t able for Checking, Savings ent itfyor successful imp lem ent at ions of EJBs. beans, or we can use ind ividu al t ab les f or all t h ree b ean s. For n ow , w e w ill m ap all t hr ee beans int o one t ab le. I n Ch apt er 12 , " Mapping st r at egies for CMPs" on pag e 467 , w e show r oot / leaf I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session t able m ap ping. beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im ent fint able der m et h ods, adif fer en mat app g st r n at nam egies, sim p le client swt hhich at ucont se t he EJBs. At Theplem ACCOUNT includes discr imt in or in colum ed and DISCRIMINATOR, ains t h ese t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. v alues: A— A gener ic accoun t t hat is n ot a check ing or sav in gs account C— A check ing accou nt S— A saving s accou nt We also includ ed an ACCTYPE colum n t hat cont ains t he account t y pe ( ACCOUNT, CHECKING, or SAVINGS) . Th is colu mn is u sed as an at t r ibut e of Account. The ACCTYPE colum n v alu e is syn ch r onized w it h t he DISCRIMINATOR colum n v alu e t hr oug h
t r igger s.
Mapping the inheritance beans Op en t he Mappin g edit or by select ing t he ItsoBank5CmpEJB m od ule and Op en Wit h - > Map pin g Ed it or - > DB2UDBNT_V72_ 1 ( Figur e 11 - 44) : •
Table of Content s
•
I ndex
Figu r e 1 1 -4 4 . M app in g a n in he r it an ce st r uct u r e
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Drhout ag t he ean t oet exam he ACCOUNT t ableon and t he overdraft at t rib ut eion t o wt he Thr oug t heChecking book , w ebpr ov id ples based a simp le bank ing app licat it h an OVERDRAFT colum n. un derly ing r elat ional dat ab ase. Dr 1, ag we t heinSavings b ean as t o at he ACCOUNT t he minAmount at t rib ut ecover t o t he MINAMOUNT I n Part t r oduce EJBs p art of Jav at able 2 En and t er p rise Ed it ion ( J2 EE) and t he basic colum n. concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed The m ap t he sub classes appear s indent un also der t pr heovid su perclass. per sist ence entping it y bof ean s and m essagedr iv en beans.edWe e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we Mapping theindiscriminator t r oduce t h e samcolumn p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plemt ent fin der m betean h ods, enttom m app in gofsttrhat client s tvhiew at ,u se t he tEJBs. At Select he Account in dif t hefer bot p ane e egies, ed it or.and I n tsim he p Prleopert ies select he tDISCRIMINATOR he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. colum n fr om t he pu ll- d ow n an d en t er t he v alue A ( Figur e 11 - 45) .
Figu r e 1 1 -4 5 . Se le ct i ng t h e di scr i m in at or col um n a nd it s va lu e
Select t he Checking b ean an d set t he discrim inat or value t o C ( Figur e 11 - 46) . Select t he Savings b ean an d set t he discrim inat or value t o S.
Figu r e 1 1 -4 6 . Se t t i ng t h e di scr i m in at or v al ue f or a sub cla ss
•
Table of Content s
•
I ndex
Sav e t he m ap ping and close t h e edit or . EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Generating the deployed code for inheritance Pub lish er: I BM
An upd at e in t he st r uct ur e an d in t he dat abase m app ings r equir es a r egener at ion of t h e Pub Dat e: April 1 1, 20 03 deploy ed code: I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
I n t he J2 EE Hier ar ch y v iew , select t he ItsoBank5CmpEJB p r oject and Gener at e - > Deploy and RM I C Code ( con t ex t ) . Select t he t h ree account en t it y beans and click Fin ish. We did not t ouch t he cust om er and t r ansact ion r ecord beans. Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
IDeploy m porEJBs t a ntt o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Ch eck t h e Task s v iew aft er g en er at ion. I f t here are er r ors, it is somet im es necessary t o App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans delet e all t he deployed code and r egenerat e again. Select t he bean an d Delet e ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. ( cont ex t ) . Then deselect t h e bean and t he classes and on ly select Delet e Deployed Cod e. t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Thr oug hout un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur I n par t icu lar w e point t he n ew fu nct ions int r od uced w it h t he Testing inheritance ine.the universal testout client EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or St ar t t he EjbbankServer an d t he u niver sal t est client t o t est t he inh er it ance. Her e are a f ew successful imp lem ent at ions of EJBs. hint s on how t o pr oceed: I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem der ex m et h ods, dif fer m app in ghom st r at sim p[leLocal client s t hbeans] at u se -t > heejb EJBs. I n ent t he fin JNDI plor er, you f inen d tt he local esegies, by ex and pan ding EJB - > At t he enitd, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. sob ank . Select t he Account local h ome in t erf ace. Ret r iev e an accou nt using t he findByPrimaryKey m et hod w it h an accountID of 101-1001. You w ill see t h at t h e r esult obj ect is a CheckingLocal ob ject . Click Wor k w it h Object an d in vok e t h e getOverdraft m et hod t o see t h e at t r ibut e valu e. I nv oke ot her get met hod s as w ell. Do t he sam e for accountID 101-1002 an d a SavingsLocal is ret ur ned. I n t he JNDI ex plor er, select t he Checking local h ome in t erf ace. Select t he create m et hod an d ent er p aram et er v alu es of 101-1008, 99.00 ( balance) , 9
( int er est ) , and 90.00 ( over dr aft ) . Click I n vok e t o cr eat e a new checkin g accoun t .
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Cleaning the model To h ave a clean and com plet e m odel and im plement at ion, w e shou ld :
•
Table of Content s
Rem oveI ndex t he create m et hod s fr om AccountLocalHome, b ecause all account s sh ould be eit her check orWsav in gsr eaccount s. ica ti on De v el oper EJB 2 . 0 D e ve lopm e nting w it h ebSphe St udio Appl •
By Ueli Rem Wah li,ove Wout er Denay er, Lar s Schu nk , Deborah Shaddon Weiss t he setAccountType m et hod f rom , tMart he in AccountLocal
in t er f ace, b ecause t h e t y pe
of an accou nt cann ot b e chang ed . Pub lish er: I BM
Rem ove t he Pub Dat e: April 1 1,setBalance 20 03
m et hod f rom t he AccountLocal in t er f ace, b ecause t h e balance can only ged t hr ough deposit and w it hdr aw t r an sact ions. I SBN: 0- 73be 84-chan 26 09 -1 Pages: 75 0
This is n ot n ecessar y f or our sam ple, bu t shou ld g iv e y ou ideas for d esign issu es you should t hin k about in a r eal app licat ion.
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Developing custom finders I n t his sect ion, w e develop cu st om fin ders for t h e bank ing m odel w it h Applicat ion Developer . For t he basics of f inder m et hods, r ead " EJB cust om qu er y m et hods" on p age 14 0. • of Content s Here is a shorTable t r ecapit ulat ion: •
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Ther e ar e t w o k ind of m et hods: f inder m et hod s in t he hom e int er face an d select m et h ods in t he bean class.
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
PubThese lish er: Im BMet h ods
can be m app ed t o EJB quer y langu age.
Pub Dat e: April 1 1, 20 03
EJB QL0-st ement I SBN: 73at 8426 09 -1s are sp ecified in t h e deploy m en t descrip t or. Pages: 75 0
SQL st at ement s ar e generat ed f rom EJB QL in t o d ep loy ed classes. The p r ocess of definin g cust om find er m et hod s is v er y sim ple:
Op enn tabout he deploym ent2 .0 descr ipt or it or and select an ent it y b ean . Lear t he EJB specif icated ion Cr eat e a EJBs n ew find et h od anlicat d sp ecify m et hod nam e and signat ur e ( par am et er s) , and Dev elop w it herStm u dio App ion Devt he eloper select t h e local or r em ot e in t er f ace. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Specify t h e EJB QL st at em ent . This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio When y ou en er at e t hef or deploy code, en t h et of EJB st at ement is conv st at em en t App licat iong Developer t he ded ev elopm apQL plicat ion s based on t er h et ed Entint er opran iseSQL Jav aBeans in t he ar d ep edu re, codan e. d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. ( EJB) chloy it ect Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional datcustom ab ase. Creating a simple finder method I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic We d escrib e t he evch elopm ene. t pIrocess in lar d et w aile for t h eout fir st f inder hod. concep t s and t h edar it ect ur n par t icu point t he n ew m fuet nct ionsThe int r od uced w it h t he findGoldAccounts m et hod r et r iev es accoun t s w it h a lar ge b alan ce: EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. Op en t he deploym ent descr ipt or of t he ItsoBank5CmpEJB p r oject . I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, d mBean essag d r iv en beans WebSph ed Stscr u dio plicat Dev elop er . We also Onant he s epage, select t heusing Account b eaneran oll Ap dow n t oion Queries. im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he enI nd,t he w e Add d escrib e how t o deploy EJB ap (plicat s t-o47) a :WebSph er e App licat ion Serv er. Finder D escrip t or w indow Figurion e 11
Figu r e 1 1 -4 7 . Add f in der de scr ipt or : m e t h od sign at ur e
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
1. Select New f or m et hod. ( Select ing Ex ist ing w ou ld look int o t h e hom e int er face t o find fin der m et h od definit ions. )
2. Select find m et hod an d Local ( Rem ot e is not av ailable because t he Account b ean d oes not hav e a rem ot e int er face) . Lear n about t he EJB 2 .0 specif icat ion
3. En t er findGoldAccounts as t he m et hod nam e. Dev elop EJBs w it h St u dio App licat ion Dev eloper 4. Select java.util.Collection as t he r et ur n t y pe ( t h e ot her choice is a sing le object , Deploy EJBs t o W eb Sp her e Applicat ion Ser v er AccountLocal) . This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio 5. Click Add f or p aram et ers. I n t he Ad d Met hod Param et er w indow , ent er balance as t he App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans nam e an d Br ow se t o BigDecimal ( java.math) for t h e t y pe. ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Click Next . I n t he second panel of t h e w indow , w e ent er a descr ipt ion ( opt ion al) and t h e Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an EJB QL st at ement ( Figur e 11 - 48) : un derly ing r elat ional dat ab ase. I n Part 1, we in Figu t r oduce r e 1EJBs 1 -4as 8 .a Add p art of Fin Javder a 2 En De t erscr p rise iptEd orit:ionEJB ( J2 EE) Q Land st acover t e m ten he tbasic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
- You can add a d escr ipt ion for t h e m et hod. Keep in m ind t hat an en t erp rise bean Lear n about t he EJB 2 .0 specif icat ion should be a r eu sab le com p onent . I f y ou are cr eat ing com p lex f inder s for you r cor e bu sin esswobj of you ent ion erp rise d an yon e else h as t o r euse t his com pon en t in Dev elop EJBs it h ect St us dio Apprlicat Dev an eloper anot h er pr oject , t his sh ort descr ipt ion of t he pur pose an d t h e par am et er s ar e r eally ul.t oThis escr ipet ion is also par tv er of t he d ep loym ent descr ipt or . Deployhelpf EJBs W ebdSp her Applicat iona Ser - You can t y pe t hdeetcom p let at em enttoyef our self . Altuse ern at ively , here selectStone This I BM Redbook pr ovides ailed in eforEJB m atQL ionston h ow fect ively WebSp udioof he sam ple fqoruer y st at em ent senf rt om t he pull-ion dow n. Ev eron y select ion putise s aJav sam ple App licat ion tDeveloper t he d ev elopm of ap plicat s based t h e Ent er pr aBeans o dt he quer y ent st at of emsu ent an d ion y ous can se it as aer m odel.licat Ex ion plor Serv e t heer. ( EJB) ar ch itqu ecteru yre,intan d ep loym ch box ap plicat t o auWebSph e App dif fer en t opt ion s and y ou can see t he m odel st at em en t s. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an gle dat W here Predicat e select ion r esult s in t h is st at em ent : un derly ing The r elatSin ional ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic select o where o.accountID null concep t s and object(o) t h e ar ch it ectfrom ur e. I Account n par t icu lar w e point out t he n ew is fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. Mod if y t he st at em ent t o r ead: I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At Account o plicat where > ?1 t he enselect d, w e d object(o) escrib e how from t o deploy EJB ap ion o.balance s t o a WebSph er e App licat ion Serv er.
Click Fin ish an d sav e t h e deploy ment descr ipt or .
Generated deployment descriptor The q uer y and find er m et h od ap pear s in t h e deploy m en t d escrip t or ( Figur e 11 - 49) .
Figu r e 1 1 -4 9 . Fi nde r m e t h od i n dep loy m en t d escr ip t or e di t or
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
lish er:t o I BM Sw itPub ching t he Sour ce pag e rev eals t he t ag ad ded under t he ent it y b ean ( Figur e 11 50 ) .Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 0 0 . Fi nde r m e t h od d epl oym e nt de scr ipt or Figu Pages: r e 1 175-5
Lear n about t he EJB 2 .0 specif icat ion ItsoBank5CmpEJB Dev elop EJBs w it h St u dio App licat ion Dev eloper
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio ..... App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Retrieve gold accounts with a large balance. un derly ing r elat ional dat ab ase.
I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he
EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp findGoldAccounts lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also java.math.BigDecimal im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
select object(o) from Account o where o.balance > ?1
.....
The f inder is com plet ely descr ibed in t his t ag in clud ing t he EJB QL st at em en t . You can chan ge t he definit ion in t he sour ce view of t h e deploy m en t descript or and save t he chang es. •
Table of Content s
Generate method in the home interface • I ndex EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
The f ollow g meretDenay hod er, is g en er atnk ed, Deborah in t o t h Shaddon e hom e, Mart int er By Ueli Wah li,inWout Lar s Schu in face, Weiss eit her in t he local hom e ( ou r case) or in t he r emot e hom e int er face, depend in g on y our select ion. Pub lish er: I BM
Local h om e int erf ace: Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
public java.util.Collection findGoldAccounts (java.math.BigDecimal balance) throws javax.ejb.FinderException;
Lear n about t he EJB 2 .0 specif icat ion Rem ot e hom e int er f ace ( as it w ould be) : Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er public java.util.Collection findGoldAccounts(java.math.BigDecimal balance) This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio throws java.rmi.RemoteException; App licat ion Developer f or tjavax.ejb.FinderException, he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part we tinatnt r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic I m1,por concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he Not e tecif h aticat t h eion quer y stpratov em and h e ple, find er m et h od fun ar ecthion andled separ at ely: EJB 2. 0 sp t hat id en e, tfor extam en hanced alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I f you cr eat e a qu er y st at em en t an d t he cor respond ing met h od in t he sam e st ep as in we d id in our am plebank by select ing New) the m et hod selfent andent he pingsession I n Part 2, (we t r oduce t h e ex sam p le ing applicat ion, and t hen impitlem t it ymap beans, m et ar ebeans aut omusing at ically cr eat ed beans, an dt omt his essag e-hod d r iv en WebSph er.e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At can creeat e tthe sign at ur e ofapaplicat f inder y our selferin t he licat homion e inServ t er f ace, t he en d, wYou e d escrib how o deploy EJB ionmset t ohod a WebSph e App er. t hen select Ex ist ing in t h e w in dow t o ent er t h e EJB QL st at em ent . I f you d elet e t he quer y st at em ent , t he t ask s list s claim s a missing map ping f or t he find er or select m et hod t o a quer y st at ement . You hav e t o cr eat e a n ew quer y st at ement and m ap it t o t he m et hod , or y ou hav e t o d elet e t h e m et h od man ually t o rem ov e t he er ror .
When y ou f inish t he w indow , t he code an d d ep loym ent descr ipt or ar e g en er at ed. Check t he Tasks view . The EJB QL st at em en t is an alyzed and y ou m ay see err or m essag es t hat point t o t h e ejb- jar . xm l f ile. By look ing at t he er r or m essage, y ou m ay b e able t o fix t h e st at ement dir ect ly in
t he Sour ce view of t he deploym ent descr ipt or . When y ou g en er at e t he deploy ed code, m or e er ror s m ay b e foun d in t h e EJB QL st at em ent and y ou h ave t o g o back an d f ix t he er r or:
• •
Select t he st at em en t in t he d ep loym ent descr ipt or an d click Ed it . Th e sam e w ind ow is Content opened Table w it h of t he def insit ion f illed in an d you can mak e cor rect ions. I ndex
ange t he erhyWstebSphe at em en t dudio ir ect ly in e De Sour v iew EJB 2 . 0Ch De ve lopm e ntqu w it r e St Appl ica ttihon v elce oper By
save it . Ueli Wah li, Wout er Denay er,
of t he deploy m en t d escrip t or and
Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM
Creating finder methods using relationships and inheritance Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
Let ' s creat a 0few m or e f in ders t o ex perim ent wit h r elat ion ships an d inh er it an ce. Follow t he Pages:e 75 pr ocess ou t lined in "Cr eat ing a sim ple cust om f in der m et hod" on p age 45 5 t o specif y t h e m et h od nam e an d sign at ur e and t h e EJB QL st at em ent .
Retrieve all accounts Lear n about t he EJB 2 .0 specif icat ion Cr eat e a f inder f or t h e Account b ean t o r et r ieve all t he account s in t h e dat ab ase: Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Method: findAllAccounts(), returns Collection This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. EJB QL: select object(o) from Account o Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Retrieve customers gold accounts concep t s and t h e ar ch itthat ect ur have e. I n par t icusavings lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed pereat Cr sist e ence a f inder ent itf or y btean h e Customer s and m essageb ean tdr o iv r et enr ieve beans. cu stWe om also er s t hat pr ovid haveebest savin prgs actaccount ice guidelines s wit h f or successful lar ge balanimp ces:lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem entfindGoldCustomers(BigDecimal Method: fin der m et h ods, dif fer en t m app in gbalance), st r at egies,local, and sim returns p le client sCollection t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
EJB QL: select distinct object(c) from Customer c, in(c.accounts) a where a is of type (Savings) and a.balance > ?1
This cust om finder u ses inh er it an ce t o check for t h e accou nt t yp e. Tr y t o cr eat e t he sam e f inder ( findGoldCustomers) for t h e r em ot e in t er f ace; Applicat ion
Dev eloper d oes not allow it !
Re st r ict i on The sp ecificat ion say s ( last p arag r aph in 1 0. 5. 6) : I f hom e and local hom e int er face s hav e t h eTable samofe Content m et hod s ( nam e an d sign at ur e ar e t he sam e) , t hen t hey h ave t o be • I ndex m apped t o t he sam e EJB QL st at em ent . But t his does not w or k in t he App licat ion EJB 2 .Dev 0 D eeloper ve lopm.e nt h W ebSphe St udio Applchan ica ti on De v el oper I t wisit possible brye m anually ging t he sou rce of t h e deploy ment By Uelidescr Wah li,ipt Wout or ,eror Denay by er, cr eat Lar sinSchu g t he nk , rDeborah emot eShaddon in t erf ace , Martaft in er Weiss t h e local int erf ace has b een creat ed w it h t he fin der and using t he " cop y m et h ods f r om ot her h ome in t er f ace" opt Pub lishion er: .I BM •
Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Retrieve customers with high interest accounts and a deposit Cr eat e a f inder f or t h e Customer b ean t o r et r ieve cu st om er s t hat hav e account s w it h a high int er est rat e and at least on e deposit t r ansact ion in t h at account : Lear n about t he EJB 2 .0 specif icat ion Method: findHighInterest(int remote, returns Collection Dev elop EJBs w it h St u dio App interest), licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er EJB select distinct This IQL: BM Redbook pr ovides d object(c) et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans from c, ent in(c.accounts) t ion Serv er. ( EJB) ar ch it ect u re,Customer an d d ep loym of su ch ap plicata, ionin(a.transrecords) s t o a WebSph er e App licat Thr oug houtwhere t he book , w e pr ov id>e ?1 exam ples based on a simp le bank ing app licat ion w it h an a.interest and t.transType = 'C' un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s om andfinder t h e art ch ect ur e. icu lar w e point out t he n ew fu nct ions int r od uced w it h t he This cust raviterses t wIon rpar elatt ionships. EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. Retrieve accounts for a transfer I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session Cr eat e a inder f or teh ed rAccount b ean t o rWebSph et r ieve all her account s ofion a cust a talso r ansfer of beans, anf d m essag iv en beans using er eotSt u dio Ap plicat Dev om elopererfor . We fu imnds: plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Method: findTransferAccounts(int customerID, String accountID), returns Collection
EJB QL: select object(b) from Account a, in(a.customers) c, in(c.accounts) b
where c.customerID = ?1 and a.accountID = ?2 and a.accountID b.accountID
This cust om finder t rav erses t he sam e r elat ionship back and for t h . •
Table of Content s
•
I ndex
Largest account customer EJB 2 . 0 D e ve lopm e nt wof it h a W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Cr eat e a f inder f or t h e Account b ean t o r et r ieve t he account w it h t he lar gest balance f or a cust om er : Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
Method: findLargestAccount(int customerID), returns AccountLocal Pages: 75 0
EJB QL: select object(a) from Account a, in(a.customers) c where c.customerID = ?1 Lear n about t he EJB 2 .0 specif icat ion ( select max(a1.balance) from in (c.accounts) al Dev elop and EJBsa.balance w it h St u dio=App licat ion Dev eloper )
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (Home EJB) arinterfaces ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Aft er sav ingr elat t he ional d ep loym entase. descr ipt or , op en t he hom e int er faces of t he Customer an d Account un derly ing dat ab beans and ver ify t h at t h e m et h ods h ave b een add ed : I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed AccountLocalHome: per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. java.util.Collection findGoldAccounts(java.math.BigDecimal balance) I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session java.util.Collection findAllAccounts() beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At findTransferAccounts(int ..., String ...)ion Serv er. t he java.util.Collection en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat itso.bank5.cmp.AccountLocal findLargestAccount(int customerID)
CustomerLocalHome: java.util.Collection findGoldCustomers(java.math.BigDecimal balance)
CustomerHome: java.util.Collection findHighInterest(int interest)
Creating a select method in the bean class •
Table of Content s
•
The second t Iyndex pe of fin der m et h ods ar e select m et hod s t hat ar e imp lem ent ed in t h e bean class. EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper We im plem en t t w o met h ods: By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Ret r iev e all cust om er nam es
Pub lish er: I BM
Pub Datr e: 20 03 nt Ret ievApril e t h1e1, accou
n um ber s for a giv en cust om er nam e
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Retrieve all customer names We d ef ine a select m et h od in t h e Customer b ean t o r et r ieve all t he cust om er nam es. This can be usefu l t o d isp lay a list of cust om er s in a client applicat ion. We w ill im plem en t a hom e m et hod for t hat pu rp ose ( see " Dev elopin g a hom e m et hod" on p age 46 3) . Lear n about t he EJB 2 .0 specif icat ion The p r ocess of addin g a select m et hod is t he sam e as f or f in der m et hods: Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er I n t he deploy m en t d escr ip t or , select t h e Customer b ean an d click Add f or q ueries. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat t he d tev en t (of ap plicat t h e Ent er pr ise Jav aBeans I n ion t he Developer Add Finderf or D escrip orelopm w indow Figur e 11 -ion 51)s, based d o as fon ollows: ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Figu r e 1 1 -5 1 . De fi ni ng a sel ect m e t h od
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
1. Select New an d ejbSelect m et hod . 2. En t er ejbSelectAllCustomerNames as t he n ame, n o par am et ers, an d java.util.Set
2. as r et ur n t yp e.
3. Click Next . 4. En t er a descrip t ion an d t he EJB QL st at ement : select o.lastName from Customer o •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
5.
FinDenay ish. er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss By Ueli Wah li, Click Wout er Sav e t he d ep loym ent descr ipt or .
Pub lish er: I BM
Dat e: April m 1 1,et20 03 is cr eat ed in t he CustomerBean class: ThePub ejbSelect hod I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
public abstract java.util.Set ejbSelectAllCustomerNames() throws javax.ejb.FinderException; Lear n about t he EJB 2 .0 specif icat ion Ther eDev is not h ing mor in tApp he bean class. m et hod is n ot accessible ov er an y int er face. elop EJBs we it ht oStdo u dio licat ion DevThis eloper I t is f or use w it hin t h e bean class only . Ther efor e, an ejbSelect m et hod is som et im es called a pr iv atDeploy e fin derEJBs m et ht ood. You it f rom W eb Spcan her euse Applicat ionnow Ser on v er in t he b ean class becau se of t he ab st r act m et hod declar at ion. The im plement at ion is g en er at ed in t h e concret e b ean class. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (Retrieve EJB) ar ch the it ect account u re, an d dnumbers ep loym ent for of su ap plicat ion s t o aname WebSph er e App licat ion Serv er. a ch given customer Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Add anot her select m et hod t o t h e Customer b ean in t he sam e w ay : un derly ing r elat ional dat ab ase. I n Part 1, e: weejbSelectNumberOfAccounts in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Nam concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2.Par 0 sp ecif icat ion et hat e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed am et er : nam of tpr ypov e id java.lang.String per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp at ions of EJBs. Ret ur n t y lem pe: ent java.util.Collection I n Part 2, QL westinatt rement oduce: t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session EJB beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he enselect d, w e d a.accountID escrib e how t o from deploy EJB ap plicat s t o a WebSph erae where App licato.lastName ion Serv er. = ?1 Customer o, ion in(o.accounts)
Using select methods On e of t he t y pical u ses of an ejbSelect m et hod is t o inv oke t he m et hod w it hin a hom e m et hod . I f you w ant t o t est t he ejbSelect m et hod , y ou can creat e a sim p le hom e m et h od t hat in vok es t he ejbSelect m et hod . This enab les y ou t o t est t he m et hod t hr oug h t he local or r em ot e hom e int er face.
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Developing a home method For an int r oduct ion t o t he hom e m et hod s, see " EJB hom e m et hods" on p age 24 . Cr eat ing a h ome m et hod is ver y simp le. I t does not m ake any dif fer en ce wh et her t he ent it y bean is con t ain er m anaged or bean- man aged. •
Table of Content s
•Ther e ar e no I ndex w izar ds or w indow s t o creat e a hom e m et hod. We hav e t o cod e t he met hod in t he EJB 2 . 0class D e ve lopm w itot he W ebSphe r e St udio Appl ti on De v el oper bean an d epntrom it t o t he h ome in t ericaf ace. By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
We cr eat e a h ome m et hod called ejbHomeGetAllCustomers in t h e CustomerBean class. This m et hod ret u rn s an arr ay of st r ings wit h each element cont ainin g a cust omer n am e and t he Pubnt lishnum er: I BM accou ber s. This h ome met hod u ses t h e t w o ejbSelect m et hod s w e creat ed ear lier. Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
public String[] ejbHomeGetAllCustomers() throws javax.ejb.FinderException { Object[] objects = ejbSelectAllCustomerNames().toArray(); String[] names = new String[objects.length]; Lear n about t he EJB 2 .0 specif icat ion for (int i=0; i < objects.length; i++) { Dev elop EJBs w it h St u dio App licat ion Dev eloper String custname = (String)objects[i]; Deploy EJBs t o W eb Sp her e Applicat ion Ser v er StringBuffer text = new StringBuffer(custname).append(" "); This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio accounts ejbSelectNumberOfAccounts(custname).toArray(); App licatObject[] ion Developer f or t he=d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. for (int j=0; j < accounts.length; j++) { Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing text.append( r elat ional dat ab(String)accounts[j] ase. ).append(" "); I n Part }1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0names[i] sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed = text.toString(); per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. System.out.println("Customer: " + names[i]); I n Part } 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At return names; t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. }
The Task s v iew sh ow s an in for m at ion messag e w hen we sav e t h e class. This r em inds u s t hat w e hav e t o pr om ot e t he met hod t o t he h om e int erf ace:
Select t he ejbHomeGetAllCustomers m et hod in t he Out line v iew and En t er p rise Bean - > Pr om ot e t o Local Hom e I nt er face ( con t ex t ) . ( We could also pr om ot e t he m et hod t o t he
r em ot e in t er f ace. ) The m et hod signat ur e is added t o t he CustomerLocalHome in t er f ace:
public java.lang.String[] getAllCustomers() • •
throws javax.ejb.FinderException; Table of Content s I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
The getAllCustomer hod is nnkow av ailab le t o ,clien ( for ex am ple, session b ean s) t h rou gh By Ueli Wah li, Wout er Denaym er,etLar s Schu , Deborah Shaddon Mart int sWeiss t he hom e int er face. Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Code generation for finder and select methods When y ou g en er at e t he deploy ed code, EJB QL st at em en t ar e conv ert ed int o SQL. You can fin d t h e gener at ed SQL st at em ent s f or t he Account b ean in t he packag e itso.bank5.cmp.websphere_deploy.DB2UDBNT_V72_1 an d class called AccountFunctionSet. Let ' •at t w o exam pTable les. of Content s •
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Retrieve all accounts
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
lishple er: IEJB BM QL st at em en t select object(o) from Account o t ran slat es int o: ThePub sim Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
select
q1.\"ACCID\",
q1.\"BALANCE\",
q1.\"INTEREST\",
q1.\"DISCRIMINATOR\",
q1.\"MINAMOUNT\",
ITSO.ACCOUNT q1 where
( ( q1.\"DISCRIMINATOR\" = \'A\')
q1.\"ACCTYPE\",
q1.\"OVERDRAFT\" from or
(
Lear n about t he EJB 2 .0 specif icat ion q1.\"DISCRIMINATOR\" = \'S\') or ( q1.\"DISCRIMINATOR\" = \'C\') ) Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Find the largest account of a customer This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o
ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans The QLit ect st atuem ( EJB)EJB ar ch re, ent an dis: d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. findlargestAccount(int customerID), returns AccountLocal I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic select from Account c n ew fu nct ions int r od uced w it h t he concep t sobject(a) and t h e ar ch it ect ur e. I n para, t icuin(a.customers) lar w e point out t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed where c.customer = s?1 per sist ence ent it y b ean and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. and a.balance = ( select max(a1.balance) from in (c.accounts) a1 ) I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At The SQL steatdem ent eis:how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. t he en d, w escrib
select
q1.\"ACCID\",
q1.\"DISCRIMINATOR\",
q1.\"BALANCE\", q1.\"MINAMOUNT\",
q1.\"INTEREST\",
q1.\"ACCTYPE\",
q1.\"OVERDRAFT\" from
ITSO.ACCOUNT q1, ITSO.CUSTOMER q2, ITSO.CUSTACCT q3 where q1.\"DISCRIMINATOR\" = \'A\')
or
( q1.\"DISCRIMINATOR\" = \'S\')
or
(
q1.\"DISCRIMINATOR\" = \'C\') ) q1.\"BALANCE\" =
( select
ITSO.CUSTACCT q5 where
Table of Content s
•
I ndex
( q5.\"CUSTOMERID\" =
( q2.\"CUSTOMERID\" = ?)
( ( q4.\"DISCRIMINATOR\" = \'A\') or
and
or
(
(
( q4.\"DISCRIMINATOR\" = \'C\') )
q2.\"CUSTOMERID\")
and
( q3.\"ACCID\" =
and
( q4.\"ACCID\" =
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
q5.\"ACCID\") ) )
and
max( q4.\"BALANCE\") from ITSO.ACCOUNT q4,
q4.\"DISCRIMINATOR\" = \'S\') •
and
q1.\"ACCID\")
and
(
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
q2.\"CUSTOMERID\" =
q3.\"CUSTOMERID\")
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0 with root/leaf mapping Inheritance
Top- d ow n m ap ping of t he in herit an ce st r uct u re w it h r oot / leaf is cov ered in " Top- d ow n m ap ping of t ban k m odel" on p age 47 5 and " Map ping inher it ance" on p age 47 8. Not e t hat SQL st at em ent s g et m com plex wit h r oot / leaf map ping of inher it ance, because t he t ab les for all sub classes mu st be joined Lear n about t he EJB 2 .0 specif icat ion
SQL Dev statements with errors elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat Developer f orLt Er he rdor ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans I mion por t a nt : SQ s. ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. The SQL cod e for som e of t h e EJB QL st at em ent s is w r ong w hen using r oot / leaf m app ing for Thr oug inher hout it ance. t he book When , wter aver pr ovsin id eg exam t he mples : m rbased elat ionon ship a simp f r omleCustomer bank ing app t o Account, licat ion ww it rhong an j oin un derly ing r elat ab ase. clauses ar eional g en erdat at ed. I n Part in t ri soduce EJBs Jav 2 En t eres p rise ( J2f ix EE) Thi1, s ewe rror docum en tas eda ipnart t h eofre le a se n ot an dEdwitiion ll be e dand i n 5cover . 0 . 1 . t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he For exam ple:ion fi ndGold Cuidste,om in CustomerBeanFunctionSet ( DB2UDBNT_V72_2) EJB 2. 0 sp ecif icat t hat pr ov fore rs ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or Gene rimp at elem d: ent at ions of EJBs. successful I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also select q1.\"CUSTOMERID\", im plem ent fin der m et h ods, dif fer en t m q1.\"FIRSTNAME\", app in g st r at egies, and q1.\"LASTNAME\", sim p le client s t h at u seq1.\"TITLE\", t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. q1.\"USERID\", q1.\"PASSWORD\", q1.\"ADDRESS\" from ITSOTOP.CUSTOMER q1 left outer join ITSOTOP.ACCOUNT q2 on
left outer join ITSOTOP.SAVINGS q3 on
(
q2.\"ACCOUNTID\" =
q3.\"ACCOUNTID\")
left outer join ITSOTOP.CHECKING q4 on
q2.\"ACCOUNTID\" =
q4.\"ACCOUNTID\")
left outer join ITSOTOP.Accounts_Custome
q5 on
( q2.\"ACCOUNTID\" =
q5.\"ACCOUNTS_ACCOUNTID\")
q2.\"ACCOUNT_DISCRIM\" = \'Account\')
or
where
( (
( q2."ACCOUNT_DISCRIM\" = \'Savings\
or
( q2.\"ACCOUNT_DISCRIM\" = \'Checking\') )
\'Savings\')
and
q1.\"CUSTOMERID\")
• •
( q2.\"BALANCE\" > ?) order by
and
and
( q2.\"ACCOUNT_DISCRIM\" =
( q5.\"CUSTOMERS_CUSTOMERID\" =
q1.\"CUSTOMERID\" asc
Table of Content s
Corre ct ed ( one possi ble w a y ) : I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
select
q1.\"CUSTOMERID\",
q1.\"FIRSTNAME",
q1.\"LASTNAME\",
q1.\"TITLE\",
Pub lish er: I BM
q1.\"USERID\", Pub Dat e: April 1 1, 20 03
q1.\"PASSWORD\",
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
ITSOTOP.ACCOUNT q2
q1.\"ADDRESS\" from ITSOTOP.CUSTOMER q1 left outer join ITSOTOP.SAVINGS q3 on
, (
q2.\"ACCOUNTID\" =
q3.\"ACCOUNTID\")
left outer join ITSOTOP.CHECKING q4 on
q2.\"ACCOUNTID\" =
q4.\"ACCOUNTID\")
left outer join ITSOTOP.Accounts_Custome
q5 on ( q2.\"ACCOUNTID\" = q5.\"ACCOUNTS_ACCOUNTID\") where ( ( Lear n about t he EJB 2 .0 specif icat ion q2.\"ACCOUNT_DISCRIM\" = \'Account\') or ( q2.\"ACCOUNT_DISCRIM\" = \'Savings\ Dev elop EJBs w it h St u dio App licat ion Dev eloper or ( q2.\"ACCOUNT_DISCRIM\" = \'Checking\') ) Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
and
( q2.\"ACCOUNT_DISCRIM\" =
\'Savings\') and ( q2.\"BALANCE\" > ?) and ( q5.\"CUSTOMERS_CUSTOMERID\" = This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans q1.\"CUSTOMERID\") order by q1.\"CUSTOMERID\" asc ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. Ba d SQL st a t e m e nt s ar e al so ge ne ra t e d f or: I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed findHighInterest (CustomerBeanFunctionSet) per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. findTransferAccounts and ejbSelectNumberOfAccounts (AccountBeanFunctionSet). I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Chapter 12. Mapping strategies for CMPs This chap t er descr ibes t he map ping st r at eg ies f or CMPs: • •
Table of Content s
Top- d ow n
I ndex
EJB 2 . 0Bot D etve lopm om - upe nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Meet - in t he- mid dle Pub lish er: I BM
I n addit ion, we descr ibe m app in g st r at egies for relat ionships and inher it ance, as w ell as special Pub Dat e: April 1 1, 20 03 cases of d ep en dent en t it ies an d w r it ing com poser s an d conv ert er s. I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Mapping strategies App licat ion Developer supp or t s t hr ee m appin g st r at egies for ent it y beans an d r elat ionships:
• •
Top- dow Table n—ofFr Content om ent s it y b ean s t o new t ables I ndex
t om - up ex ist ing ab les t o ica new yoper beans EJB 2 . 0Bot D e ve lopm e nt— w Fr it hom W ebSphe r e Stt udio Appl ti onent De vitel By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Me e t - i n- t h e- m idd le — Map ent it y beans t o exist ing t ables
Figur Pub elish 12er: - 1I BM su mm ar izes t he t h ree ap pr oach es. Pub Dat e: April 1 1, 20 03
Figu r e 1 2 -1 . M a ppi ng st r at egi es f or CM Ps
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he IEJB n t he ion sicat t hat , wov e id look at ex t heam t hple, r ee en st rhanced at egies fun in dctetion ail.alit y for cont ain er - m anag ed 2. 0sect sp ecif ionfollow t hat pr e, for per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
Top-down
I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also The t op- dow n ap pr oach is t he easiest t o han dle, becau se no legacy dat abase is imp osed on you im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At ( Figur e 12 - 2) . You hav e f ull f r eedom t o design t he ent it y beans. t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Figu r e 1 2 -2 . EJB m app ing : t op- dow n a ppr oa ch
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
The t op- dow n app roach is also called for w ar d en gin eer ing .
Approach Lear n about t he EJB 2 .0 specif icat ion The stDev epselop in t hEJBs e t opw ditown h St uap dio pr App oachlicat ar e: ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er eat e an EJBprprovides oject , dadd ent itiny for b ean and r equir ed use r elatWebSp ionships f orStt he This ICr BM Redbook et ailed m ats,ion ondefine h ow t ot he ef fect ively here udio appion licat ion. App licat Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. On ce t he ent it y m odel is com plet e, y ou can gener at e t he map ping by select ing Gener at e - > EJB t o RDB Map,pin ( con t ex ). Thr oug hout t he book w eg pr ov id e texam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. Ch eck t h e sch em a an d map f or err or s. Ty pical er r or s ar e due t o serialized ob ject s ( EJB han ex ampEJBs le) being apped t oaVARCHAR inst(ead of and BLOB. Fix tthem an d I n Part 1,dles, we inf or t r oduce as a pmart of Jav 2 En t er pcolum rise Edns it ion J2 EE) cover he basic y ou ar e d one. concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Tailor he itschem a:s tand ab lemand colu m ames, colum ch aract er ist ics ( CHAR or guidelines VARCHAR, f or per sist ence tent y b ean essagedrnivnen beans. We nalso pr ovid e best pr act ice m axim um len gt h of st rin gs, pr ecision of D ECI MAL colum ns) . successful imp lem ent at ions of EJBs. Sav t heinschem a and he m apping I n Part 2,e we t r oduce t h e tsam p le bank.ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Gener at eder t hemDDL for tdif hefer t ab d rin ug n tsthreatDDL int and o a dat im plem ent fin et h ods, enles t man app egies, simabase. p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
I m por t a nt You should avoid designing ob ject m odels t hat lead t o un realist ic dat abase design. Alw ays hav e a d at abase adm inist r at or check t he r esu lt in g dat abase design.
Bottom-up
The bot t om - u p ap pr oach is t he exact opposit e of t h e t op- d own ap pr oach . Basically , y ou cr eat e an EJB lay er on t op of an ex ist in g dat abase d esign ( Figur e 12 - 3) . Th is appr oach is also called r ev er se engineering .
Figu r e 1 2 -3 . EJB m app ing : bot t om - up a ppr oach •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Here you r fr eed om of dev elopm ent is on t op of t he EJB lay er . You hav e t o add session b ean s t o pr ov ide t h e business logic t hailed at t h ein en it yatbeans n ott op ef rov ide. This I BMall Redbook pr ovides d et fortm ion on do h ow fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Approach Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. The st eps in t h e bot t om - u p ap pr oach ar e: I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2.I m 0 plem sp ecifent icataion pr ov for an ex am ple, d att hat abase ( orid e, h ave ex ist ingend hanced at abase)fun . ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful ent aatfions I m porimp t a lem schem r om of t heEJBs. dat abase an d g en er at e t h e ent it y b ean s and t he m ap ping. I n Part 2, we t r oduce t h e sam p le and bankadj ingust applicat imp lem t it y ple, beans, session Tailor t hein mod el ( Java t y pes) t he mion appand in g ift hen n ecessar y . ent For en exam m:m beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also r elat ion sh ip s ar e not imp or t ed ( an int er m ed iat e en t it y is creat ed) , and inher it ance is not im plem ent fin der. m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At r ecogn ized t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Meet-in-the-middle Meet - in - t he- m id dle is t he m ost com m on ap pr oach u sed in t he e- business pr oject s. I t com bines an ex ist ing dat ab ase an d an ent it y mod el desig n ( Figur e 12 - 4) .
Figu r e 1 2 -4 . EJB m app ing : me e t - in -t he -m i ddl e app r oa ch
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
The r eal d if ficult y is in f in ding a g ood balance bet w een t h e desig n an d t he m app in g. Th e en t it y m odel m ust mat ch t he dat abase d esign w it h en ough com mon alit y so t hat a m app ing is feasib le. The schem a h as a v ery lim it ed r ole her e, because it is im posed an d can not be chang ed . The Lear n about t he 2 .0 specif icat ioncle b et w een t he EJB m odel and t h e m apping , w her e dev elop ment life cy cleEJB m ainly w ill b e a cy one h as t o accom m odat e chan ges t o m ak e t he ot her v iable. Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Approach
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat The st eps ioninDeveloper t h e m eet - finor t thhe e- mid d ev dle elopm appr enoach t of ap arplicat e: ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an I ming plem entional a d atdat abase ( or h ave an ex ist ing d at abase) . un derly r elat ab ase. Cr eat e an pr oject , add it y of b ean r elat ionships I n Part 1, we in tEJB r oduce EJBs as aent p art Javs, a 2and En define t er p riset he Edritequir ion ( ed J2 EE) and cover ftor het he basic app licat ion. concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Perf or m t he bymim por t ingdraivschem a f r om dm t he ent it iesf or per sist ence ent it y map b eanping s and essageen beans. Wet he alsodat prabase ovid e an best prapp act ing ice guidelines and r elat ion sh ips t o t he t ab les. successful imp lem ent at ions of EJBs. The r eal2,challenge is t o tm aksam e t he andapplicat t h e legion acy and t ables mat chlem ! Her e you hav e tsession o I n Part we in t r oduce he p led esign bank ing t hen imp ent en t itwyillbeans, deploy all y our kn ow ledg e of comp oser s, conv er t er s, as w ell as com p rom ising sk ills t o h ave beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also t he tim w oplem coex ist .finI tder m ay happ en dif t h at our t o msim appin s entsirtet y and, herefor ent m et h ods, feryen t mdesign app in gisstim r atpossible egies, and le itclient h at u se tthe EJBs.e,At rt he ed esign w ill hav e t o be d one. en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. I n most cases, t his is an it er at iv e p rocess t hat t ak es sev er al cy cles t o result in a good ent it y bean design and also good per for m ance.
Mapping examples I n Ch apt er 11 , " Cont ainer - m anaged en t it y bean developm ent " on page 3 97, we used an it erat iv e p rocess and m eet - in- t h e- midd le m apping b et w een ou r ent it y m odel an d t he exist ing t ables. •
Table of Content s
• I ndex Now let u s look at bot t om - up and t op - dow n m apping . EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Bottom-up mapping from the EJBBANK database Pub lish er: I BM Pub Dat e:eApril 20t om 03 - u p p rocess, we cr eat e an ent it y m odel f rom t he ex ist in g t ab les. To illust r at t h e1 1, bot I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Restriction Or iginally w e h ad an address colum n in t he CUSTOMER t able and an ADDRESS t able t hat w as r elat ed t o t he CUSTOMER t able. Bot t om - up does not w or k for su ch a com bin at ion, because t h e Customer ent it y bean wou ld h ave an address CMP at t rib ut e an d an address CMR relat ionship. Lear n about t he EJB 2 .0 specif icat ion We r enam ed t h e ADDRESS t able t o CUSTADDRESS t o get arou nd t his p rob lem . Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Create a project This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Cr eat e aion n ew EJB pr oject amed at t ach t o a new er prerise applicat ion App licat Developer f or nt he d ev ItsoBank5xEJB, elopm en t of ap plicat ioned s based on t ent h e Ent pr ise Jav aBeans nam ItsoBank5xEAR. ( EJB)edar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing a r elat ional dat abmodel ase. Generate bottom-up I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic To g en er at e t he bot t om- up EJB mod el, f ollow t his p r ocess: concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful ent at ions of EJBs. Selectimp t helem ItsoBank5xEJB p r oject and Gener at e - > EJB t o RDB M apping ( con t ex t ) . I n Part 2, we in t reoduce e sam bank ion. and t hen imp lem ent en t it y beans, session Select Cr eat a n ewt hback enpdlef old er ing an dapplicat click Next beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin et han ods, dif ferNext en t .m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At Select Botder t ommUp d click t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. I n t he Dat abase Conn ect ion w indow , ent er ConEJBBANKbot as nam e and EJBBANK as dat ab ase. Select t he dat abase v endor ( D B2 Univ ersal D at abase™ V7. 2) and check t hat t he class locat ion is corr ect . Click Next . Select all t he t ab les of t h e ITSO schem a an d click Next . Select Gener at e 2. 0 ent erp r ise beans. Ent er itso.bank5.bottom as packag e. Click Fin ish. Wait . . .. it t ak es a w h ile t o gener at e t he en t it ies an d relat ionships. When t he pr ocess is fin ished, t he m app ing ed it or is opened and show s t he map ping of t he gener at ed ent it y beans. Close t he ed it or .
Analyze the generated model The b ot t om - up pr ocess cr eat es one ent it y b ean f or each t able. I t also cr eat es t hese 1: m r elat ion sh ip s: • •
Table of Content s
Custaddress_To_ Customer I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Custacct_To_ Customer
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Custacct_To_ Account Pub lish er: I BM
Checking_To_ Account
Pub Dat e: April 1 1, 20 03
I SBN: 0- 73 84- 26Account 09 -1 Savings_To_ Pages: 75 0
Transrecord_To_ Account
Many-to-many relationship Learionship n aboutbet t he EJB cust 2 .0 om specif icatdion The r elat w een er an accoun t is n ot r ecog nized as an m : m relat ionship, despit e t h e fact t hat t he CUSTACCT t able has on ly for eign key s and no ot her at t r ibu t es. Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Re st r ict i on App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB)Bot ar ch it ect an in dg d ep loym entrecogn of su ch plicat ion sionships. t o a WebSph er eesApp ion Serv e er. t om - upu re, m app does not izeap m: m relat I t creat anlicat int er mediat ent it y b ean and t w o 1: m r elat ionships. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and Change the tmodel h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed perm sist y b ean s and essagedr ivion en beans. pr tovid e odel best in pr act To akence e t h eent m it odel usable f ormour ap plicat w e chanWe ge also t h e en it y m t heice d epguidelines loym ent f or successful descr ipt or : imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Onent t hefinBean select it yegies, ( it h as nosim datpa) click Rem ove. is also im plem der smpage, et h ods, dif fert he en tCustacct m app in g ent st r at and le and client s t h at u se t heTh EJBs. At ovees t he t wo r elatt oionship o t he be ean s. licat ion Serv er. t he enr em d, w d escrib e how deploys tEJB apCustomer plicat ion s an t o da Account WebSph er App On t he Ov er v iew page, def in e an m : m r elat ionsh ip bet w een Customer an d Account w it h r oles set as accounts an d customers. Delet e t he r elat ionships f r om Checking an d Savings t o Account. On t he Bean s page, d elet e t h e at t r ibut es discriminator, overdraft, an d minamount f r om t he Account b ean ( w e w ill u se r oot / leaf inher it ance) . Mak e Checking a subclass of Account:
1.
1. Use t he I nher it ance sect ion on t he Over view p age. 2. Select t he Checking class and click Ed it . 3. Select I n her it s f r om super t yp e an d select t he Account b ean . 4. Click Fin ish. • •
Table of a Content s Mak e Savings subclass of Account in t h e sam e w ay . I ndex
t hee Customerinfo ( wAppl e igica nor e tDe hisv el d oper at a f or EJB 2 . 0Delet D e veelopm nt w it h W ebSphe rb e ean St udio ti on
t his ex am ple) .
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Delet e t he Custaddress b ean ( w e w ill m ap cust om er at t rib ut es t o t he CUSTADDRESS t able) .
PubSelect lish er: I tBM he
Customer b ean , delet e t he address at t rib ut e, add street, city, state, an d ( all st r ings) .
zipcode t rib Pub Dat e: Aprilat1 1, 20ut 03es I SBN: 0- 73 84- 26 09 -1
Ed it t h e relat ionship bet w een Transrecord an d Account an d set t he r oles and t he CMR Pages: 75 0 field nam es t o theAccount ( und er Transrecord) and transrecords ( und er Account) . Sav e t he d ep loym ent descr ipt or .
Fixing the code Lear n about t he EJB 2 .0 specif icat ion SeverDev al erelop ror sEJBs in t he e umu be licat fix edion : Dev eloper w itcod h St diostApp Deploy EJBs t o W eb Sp her e Applicat ion Ser v er d SavingsBean r efer o t heir class: This ICheckingBean BM Redbook pran ovides d et ailed in for m att ion on how own tkey o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. 1. Ch ange all refer ences t o t h e key class t o AccountKey, t h e shar ed class. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an 2. Also t he setCheckingaccount_accid an d setSavingsaccount_accid m et hod calls un derly ing r elat ional dat ab ase. m ust b e delet ed f rom ejbCreate an d ejbPostCreate m et hod s. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic I n t he AccountBean, t h e ejbCreate m et hod s r ef ers t o t h e t hr ee delet ed fields: concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent ove it y btean s and essagealso ovidateurbest pr act f or 1. Rem he last t hm r ee p aramdr etiveren s frbeans. om t h eWe m et h odprsign e and t h eice t hrguidelines ee set calls successful for impdiscriminator, lem ent at ions ofoverdraft, EJBs. an d minamount. I n Part2. 2, we in t r oduce t h e sam p le bank applicat ion hensam impelem ent en t it y beans, session Ch ange t he ejbPostCreate ming et hod signat ur eand in tt he w ay. beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem3. ent Op finen dert he m et h ods, dif fer en t m app in g and st r atchang egies,e and p le client s t h at u se ur t he EJBs. AccountLocalHome class t he sim create m et hod signat e in t he At t he en d, w sam e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. e w ay.
Adjust the mapping We h ave t o syn ch r onize t he map ping w it h t he m odel. Op en t h e m apping edit or ( select t he pr oj ect and Op en Wit h - > Map pin g Edit or - > DB2UDBNT_V72 _1 ) :
Ch eck t h e m apping of Checking an d Savings. Th e sin gle at t r ibut e sh ould be m app er t o t h e colum n.
Map Customer t o CUSTADDRESS ( dr ag/ dr op) , t hen m ap t h e fou r at t r ibut es t o t he colu mn s of CUSTADDRESS. Map t he in her it an ce discr im inat or :
1. Select t he Account b ean in t he map ped sect ion ( b ot t om p ane of t he ed it or) and set t heTable discr im in at or in t he Pr oper t ies view t o t he DISCRIMINATOR colum n and t h e valu e of Content s A. I (ndex Exp and t he Bean t o Table St r at egy. )
• •
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
2. Select t he Checking an d Savings b ean s and set t h e discr im inat or v alues t o C an d S
By Ueli Wah li, rWout er Denay espect iv elyer, . Lar s Schu nk , Deborah Shaddon , Mart in Weiss PubMap lish er:t he I BMCustomer
- Account r elat ionship:
Pub Dat e: April 1 1, 20 03
1.
I SBN:Dr 0- ag 73 8426 09 -1 t he accounts Pages:CAtoAccount 75 0
at t rib ut e of t h e Customer b ean t o t he CustAcct t able f or eig n k ey.
2. Dr ag t he customers at t rib ut e of t h e Account b ean t o t he CustAcct t able CAtoCustomer f or eig n k ey. Sav e t he m ap ping. Lear n about t he EJB 2 .0 specif icat ion At t his poin t , all er ror s h ave d isap peared and t he mod el and m apping is com plet e. We could generDev at e elop t he deployed buApp t w elicat d o ion not Dev im plem ent t his m app ing appr oach. EJBs w it hcode, St u dio eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Top-down mapping of the bank model
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans To illust r at e t h e t op- d ow n ap pr oach , w e t ak e t he CMP en t it y m odel of t he ItsoBank5CmpEJB ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. pr oj ect and perf or m a t op- dow n m appin g: Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. Select t he ItsoBank5CmpEJB p r oject and Gener at e - > EJB t o RDB M apping . I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h eeara ch it ectback ur e.en I nd par icu(lar e point n ewwfu int r od he Select Cr eat n ew f oldt er wewdo n ot wout an t tthe o over ritnct e tions he g ood imuced plemwenit thatt ion) EJB 2.and 0 spclick ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er m anag ed Next . per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful ent of EJBs. Selectimp Toplem Dow n at anions d click Next . I n Part 2, we in tUDB r oduce h eassam le bank ing applicat and t hen imp lem ent en t it yanbeans, session Select DB2 V7.t 2 t arpget dat abase, en t er ion EJBBANK as dat abase n ame, d ITSOTOP beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also as schema ( t o diff er ent iat e f r om t he ex ist in g I TSO sch em a) . Select Gener at e DDL. Click im plem ent. fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At Next t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. For adv anced in herit an ce opt ions, select t h e Checking an d Savings b ean s t o generat e r oot / leaf t ables. Click Fin ish. A n ew back - en d f older DB2UDBNT_V72_2 is cr eat ed w it h t he t ables and t he m ap ping ( Figur e 12 5) .
Figu r e 1 2 -5 . Top- dow n ma ppi ng ba ck - en d f old er
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
Analyzing the generated table model
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er The D DL is gener at ed int o a f ile nam ed Table.ddl ( Ex am ple 12 - 1) . This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans Exa m pl e 1 2 -1 . Top- dow n t ab le m ode l ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. CREATE TABLE ITSOTOP.ACCOUNT I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic (ACCOUNT_DISCRIM concep t s and t h e ar ch itVARCHAR(32), ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed ACCOUNTID NOT NULL, per sist ence ent itVARCHAR(250) y b ean s and m essagedr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. BALANCE DECIMAL(10, 2), I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r ivNOT en beans INTEREST INTEGER NULL,using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he ACCOUNTTYPE en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. VARCHAR(250));
ALTER TABLE ITSOTOP.ACCOUNT ADD CONSTRAINT PK_ACCOUNT PRIMARY KEY (ACCOUNTID);
CREATE TABLE ITSOTOP.CHECKING
(ACCOUNTID VARCHAR(250) NOT NULL, OVERDRAFT DECIMAL(10, 2));
ALTER TABLE ITSOTOP.CHECKING • •
Table of Content s
ADD CONSTRAINT PK_CHECKING PRIMARY KEY (ACCOUNTID); I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
CREATE TABLE ITSOTOP.SAVINGS Pub lish er: I BM
(ACCOUNTID NOT NULL, Pub Dat e: April 1VARCHAR(250) 1, 20 03 I SBN: 0- 73 84- 26 09 -1
MINAMOUNT DECIMAL(10, 2)); Pages: 75 0
ALTER TABLE ITSOTOP.SAVINGS ADD CONSTRAINT PK_SAVINGS PRIMARY KEY (ACCOUNTID); Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper CREATE TABLE ITSOTOP.Accounts_Customers Deploy EJBs t o W eb Sp her e Applicat ion Ser v er (CUSTOMERS_CUSTOMERID NOT NULL, This I BM Redbook pr ovides d INTEGER et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ACCOUNTS_ACCOUNTID NULL); ( EJB) ar ch it ect u re, an d d epVARCHAR(250) loym ent of su chNOT ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. ALTER TABLE ITSOTOP.Accounts_Customers I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic ADD tCONSTRAINT PK_Accounts_Custo1 concep s and t h e ar ch it ect ur e. I n par t icu lar PRIMARY w e point KEY out t (CUSTOMERS_CUSTOMERID, he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed ACCOUNTS_ACCOUNTID); per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also CREATEan TABLE ITSOTOP.CUSTOMER im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he(CUSTOMERID en d, w e d escrib e how tNOT o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. INTEGER NULL, FIRSTNAME VARCHAR(250), LASTNAME VARCHAR(250), TITLE VARCHAR(250), USERID VARCHAR(250), PASSWORD VARCHAR(250),
ADDRESS VARCHAR(1000) FOR BIT DATA);
ALTER TABLE ITSOTOP.CUSTOMER ADD CONSTRAINT PK_CUSTOMER PRIMARY KEY (CUSTOMERID); •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
CREATE TABLE ITSOTOP.TRANSRECORD
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
(TRANSID TIMESTAMP NOT NULL, Pub lish er: I BM
TRANSTYPE Pub Dat e: April 1VARCHAR(250), 1, 20 03 I SBN: 0- 73 84- 26 09 -1
TRANSAMOUNT DECIMAL(10, 2), Pages: 75 0
THEACCOUNT_ACCOUNTID VARCHAR(250));
ALTER TABLE ITSOTOP.TRANSRECORD Lear n about t he EJB 2 .0 specif icat ion ADD CONSTRAINT PK_TRANSRECORD PRIMARY KEY (TRANSID); Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Aft er I st u dy in g t he gpren er at edd et t able odel, ou m on ay h wow an tt o t o eft ailor t he tuse ables: This BM Redbook ovides ailedmin for myat ion fect ively WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) The ar chditiscr ectimin u re, at anord colum d ep loym ent of su ch ap plicat t o a WebSph er is e App licat ion Serv er. m ay n ACCOUNT_DISCRIM in ion t h es ACCOUNT t able VARCHAR(32). You use a sh ort er VARCHAR or ev en a CHAR. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly r elatational Alling st r ing t r ibutdat es ab defase. ault t o VARCHAR(250). You hav e t o look at each st r ing at t r ibu t e and decide w hat t he cor r ect colu mn size is and if VARCHAR or CHAR is mor e ap pr opr iat e. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep and t h e ar ch ectut ures e. defau I n parltt icu w e point out t he ions r od uced Allt sBigDecimal atitt rib t o lar DECIMAL(10,2). I s tnhew at tfu h enct corr ectintlengt h an dw it h t he EJB 2.pr0 ecision? sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem colum ent at ions The address n in of t heEJBs. CUSTOMER t able is VARCHAR(1000) FOR BIT DATA. You m ay w ant t o chan ge t o a BLOB d at a t yp e. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an can d m essag e- dt hr iv using as WebSph er e not St u delet dio Ap ion Dev elop er . We You chan ge e en colubeans mn n ames w ell. Do e plicat t he colum n an d redef in ealso it ; t h at im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At w ould inv alid at e t he map ping. t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. St ud y w hat colu mn s w er e g en er at ed as NOT NULL ( key s and int eger s) . Decid e w hich colum ns shou ld b e NOT NULL f or a realist ic desig n. All t h ese chang es w ould not af fect t he m appin g. Ch anges t h at w ould aff ect t he map ping: Ch anging t h e nam es of t ables. You wou ld h ave t o def in e t he new t able, chang e t he m appin g, an d d elet e t h e old t ab le. Ch anging t h e m : m r elat ion sh ip t ab le t hat w as gener at ed as Accounts_Customers w it h t w o
k ey field s and t w o f or eig n k ey defin it ion s.
Deploying a design I n t he deploy m en t d escr ip t or of t h e ItsoBank5CmpEJB p r oject , y ou can now select t he b ack - end fold er t o be used w hen deploy ing t he EJBs. Open t he d ep loy m ent d escr ipt or edit or an d at t h e •bot t om of t heTable ofview Content s Over p age y ou can select t h e back - en d folder . •
I ndex
When en ere nt at ew itt he code, a pack ageDeis EJB 2 . 0 yDou e veglopm h Wdeploy ebSphe red e St udio Appl ica ti on v elgener oper at ed f or each b ack - end fold er as itso.bank5.cmp.websphere_deploy.DB2UDBNT_V72_x. By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM
Mapping a CMP Pub Dat e: April 1 1, 20 03to multiple tables I SBN: 0- 73 84- 26 09 -1
An en tPages: it y , such as Customer, can be m app ed t o t w o ( or mor e) t ables t hat are in a 1: 1 75 0 r elat ion sh ip . As show n in "Ch ange t he m odel" on p age 47 3 ( bot t om - up m app ing) , we can m ap t he com bin ed at t r ibu t es of t he Customer at t rib ut es t o t w o t ables, CUSTOMER an d CUSTADDRESS.
Mapping relationships Lear n about t he EJB 2 .0 specif icat ion The m ap ping of r elat ionship s is st raig ht f orw ar d. A 1: 1 or 1 : m r elat ion sh ip m aps t o t h e for eign EJBs St u dio Dev eloper t he t w o CMR f ields m ap t o t he f oreign k ey s k ey ofDev t heelop subor d in w atiteht able. I nApp an licat m: mion relat ionship, in t he int er m ed iat e t able. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Mapping inheritance App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. I nher it ance bet w een CMP ent it y b ean s can b e m apped int o one t able or m ult iple t ables Thr oug hout. t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an ( r oot / leaf) un derly ing r elat ional dat ab ase. I n " Map ping appr oaches f or inher it ance" on p age 45 1, w e d iscussed t h e m apping of inh er it ance I nr uct Partur1, t r oduce EJBs asmaapp p art t er p (rise Ed it iont hat ( J2 EE) and basic st es.we Weinimp lem ent ed a in gofinJav t o aon2eEn t able ACCOUNT) holds allcover t he att the r ibu t es of concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he all account subclasses. EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed ence y bping ean sofand dr iv en We acting ice of guidelines f or Iper n "sist Topd owent n mitap t hembessageank m odel" onbeans. p age 47 5, also w e gpr enovid er ateedbest a mpr app t he account successful imp entidual at ions of tEJBs. subclasses int olem indiv leaf ab les. II n 2, classes we in t r oduce t h eat sam p let es, bank ingmapplicat en t itMap y beans, session f tPart he sub hav e few t r ibu t hen ap ping ion int oand onet hen t ab leimp is lem advent isable. ping int o beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also r oot / leaf t ables mak es access slower , because SQL j oin s hav e t o be g en er at ed. Also EJB QL im plem der m et hplicat ods, ed dif fer t m app in gm st r at egies, and sim p le client s t h at u se t he EJBs. At qu er ies ent get fin m or e com w iten h root / leaf appin g. t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. For exam ple, t h e findGoldAccounts q uer y ( see " Cr eat ing a sim ple cust om f in der m et hod" on pag e 455 ) g en er at es t h ese SQL st at em en t s: Si ngl e t a ble m a ppi ng ( meet - in- t h e- m idd le map ping ) :
"select
q1.\"ACCID\",
q1.\"ACCTYPE\",
q1.\"BALANCE\", q1.\"INTEREST\",
q1.\"DISCRIMINATOR\", q1.\"MINAMOUNT\",
q1.\"OVERDRAFT\" from ITSO.ACCOUNT q1 where ( ( q1.\"DISCRIMINATOR\" = \'A\')
or
( q1.\"DISCRIMINATOR\" = \'S\') or ( q1.\"DISCRIMINATOR\"
= \'C\') )
and
( q1.\"BALANCE\" >?)");
•
Table of Content s
•
I ndex
Root / l ea f t a ble m a ppi ng ( t op- dow n m app in g) : EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
"select
q1.\"ACCID\", q1.\"BALANCE\",
q1.\"INTEREST\",
Pub lish er: I BM
q1.\"ACCTYPE\",
q1.\"DISCRIMINATOR\", q2.\"MINAMOUNT\",
Pub Dat e: April 1 1, 20 03
I SBN: 0- 73 84- 26 09 -1
q3.\"OVERDRAFT\" from ITSO.ACCOUNT q1 left outer join ITSO.SAVINGS q2 on Pages: 75 0
( q1.\"ACCID\" = q1.\"ACCID\" =
q2.\"ACCID\") q3.\"ACCID\")
left outer join ITSO.CHECKING q3 on where
(
( ( q1.\"DISCRIMINATOR\" = \'A\')
or
( q1.\"DISCRIMINATOR\" = \'S\') or ( q1.\"DISCRIMINATOR\" = \'C\') Lear n about t he EJB 2 .0 specif icat ion ) and ( q1.\"BALANCE\">?"); Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans Not e ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. You find t h e SQL in t he AccountBeanFunctionSet_xxxx class. The g en er at ed SQL for Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an t he t op- dow n m app in g uses it s generat ed nam es, for ex am ple ITSOTOP ( in st ead of un derly ing r elat ional dat ab ase. ITSO) , ACCOUNTID ( for ACCID) , ACCOUNT_DISCRIM ( for DISCRIMINATOR) , v alue 'Account' f or 'A', an d so for t h. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he Som e of t he EJB QL q ueries cur r en t ly gener at e bad SQL st at em en t s w hen root / leaf EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed m appin g is u sed ( see " SQL st at ement s w it h er r ors" on p age 46 5) . per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d maessag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Mapping dependent value class im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Any ser ializable Java class can be u sed as t he t yp e of a CMP f ield ( not as an CMR f ield) . The st r uct ur e is not descr ibed in t he deploy m en t d escrip t or . The cont ent is nor m ally st ream ed in t o a single t ab le colu mn . For exam ple, t h e address at t rib ut e of t h e Customer b ean in Ch apt er 11 , " Con t ainer- m an aged ent it y bean d ev elopm en t " on p age 39 7 is map ped t o t he address colum n of t he CUSTOMER t able. We h ave t o use a com p oser t o m ap t he address at t rib ut e int o indiv idual colu mn s of a t able. WebSph er e Ap plicat ion Serv er En t erp rise allow s such a m apping b y st andar d t ooling w it hout a com poser .
Converters and composers Con ver t ers an d com poser s ar e used t o per for m t h e m apping oper at ion bet w een an en t it y at t r ibut e and one or mor e t ab le colu mn s: •
Table of Content s
Aconv er t er m ap s one at t ribu t e t o one colum n. Many conv er t er s ar e pr ov ided t o conv er t I ndex dat a int t y pes,r ebut y ouAppl canicawtir on it eDe you ow n as w ell. A conv er t er is specif ied in t he EJB 2 . 0SQL D e ve lopm e ntowJava it h W ebSphe St udio v elroper m appin g edit or and is used f or simp le map ping . By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss •
Acom p ose r m ap s one at t ribu t e ( w hich can b e a com plex class) t o m ult iple t ab le colum n s. is specif ied in t he m app ing ed it or and is used for com plex m apping .
PubAlish er: Iposer BM com
Pub Dat e: April 1 1, 20 03
The m ap ping I SBN: 0- 73 84- 26edit 09 -1or allow s y ou t o specif y a " com plex m appin g" f or a CMP field . When y ou use t h is opt ion, you hav e t o pr ov id e a com poser class. You t y pically u se t his opt ion f or Pages: 75 0 com plex at t r ib ut es, such as t he Address ob ject u sed in t he Customer b ean . The cod in g f or conv ert er s and com poser s is v er y sim ilar ; bot h m ust pr ov id e m et h ods f or bot h w ay s of t he m app ing—f r om an en t it y at t r ibut e t o colum n( s) and fr om colum n( s) t o an ent it y at t r ibut e. Lear n about t he EJB 2 .0 specif icat ion
Using Devcomposers elop EJBs w it h St u dio App licat ion
Dev eloper
Deploy t o of W eb Sp her e Applicat ion Ser va er To illust r at e tEJBs h e use a com poser, w e develop com poser for t he cust om er ' s address at t r ibut e: This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Cr eat e a n ew EJB pr oject n amed ItsoBank5ComposerEJB at t ach ed t o an en t er p rise Thr oug hout w eItsoBank5ComposerEAR. pr ov id e exam ples based on a simp le bank ing app licat ion w it h an app licatt he ion book nam,ed un derly ing r elat ional dat ab ase. Cop y t he itso.bank5.beans p ackag e fr om t h e ItsoBank5CmpEJB p r oject t o t he I n Part 1, we in t r oduce EJBs as p art so of tJav t errpeu rise ion ( J2 EE)class. and cover t he basic ItsoBank5ComposerEJB p raoject h ataw2e En can se Ed t heit Address concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2.Defin 0 sp ecif e a icat CustomerComposer ion t hat pr ov id e, CMP for ex ent amit ple, y bean, en hanced w it h atfun t r ibut ct ion esalit customerid y for cont ain ( iner t )-,mtitle, anag ed per sist firstName, ence ent it ylastName b ean s and ( all m st essagering s) ,drand iv enaddress beans. We ( Address also pr ovid class) e .best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session Writing a composer beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At A com designed a com plex t r ib ut ult iple colum ns.licat Weion develop an t he en poser d, w e is d escrib e howt ot split o deploy EJB ap at plicat ione sint t ooamWebSph er e App Serv er. AttributeComposer f or t h e addr ess at t r ibut e:
Select t he ItsoBank5ComposerEJB p r oject and New - > Ot her - > EJB - > Conv ert er or Com poser an d click Next . I n t he EJB Con ver t er or Com poser Wizar d ( Figur e 12 - 6) :
Figu r e 1 2 -6 . D ef in in g a com pose r
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
1. Select Com poser . Deploy EJBs t o W eb Sp her e Applicat ion Ser v er 2. The p r oject ( ItsoBank5ComposerEJB) is pr ef illed. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat f or t he d ev elopm en t of ap plicat ion on tof h et he Entcom er prposer ise Jav 3.ionEnDeveloper t er itso.bank5.beans.AddressComposer ass tbased he n ame . aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. 4. Leav e t he su pert y pe as VapAttributeComposer. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly5. ing For r elat ional ase.click Br ow se an d locat e t he Address class ( in itso.bank5.beans t he t argdat et ab t y pe, of t he ItsoBank5ComposerEJB p r oject ) . I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep6. t s and t h ejava.lang.String ar ch it ect ur e. I n paras t icut he lar nwew e point out t he n ew fu nct ions int rAdd od uced it hes. t he Select com posed field t y pe and click f our wt im EJB 2. 0 sp ecif icat ion ov id es e, for ex am ple, en hanced ion alit y for cont ain er - m anag ed Ch ange t het hat fieldprnam t o street, city, state, fun an dctzipcode. per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent atat ions EJBs. 7. Select Gener e a of com poser st ub class . I n Part8. 2, we inFin t r oduce Click ish. t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Opent en tfin heder generat class itso.bank5.beans.AddressComposer. Yous hav let e t wo im plem m et h ed ods, dif fer en t m app in g st r at egies, and sim p le client t h ate ut osecomp t he EJBs. At hods, dataFrom objectFrom. Th eseion ar se ttoh ea tWebSph w o m et hods t hat con verServ t b eter. ween an t he enmd,etw e d escrib e howan t od deploy EJB ap plicat er e App licat ion obj ect ( Address) and an ar r ay of f ields f or t h e t able m app ing. Not e t h e getAttributeNames m et hod :
public static String[] getAttributeNames() { String[] attributes = { "zipcode", "state", "city", "street" }; return attributes;
}
We h ave t o code t h e dataFrom an d objectFrom m et hod s in t he sam e seq uence.
•
The getTargetClassName m et hod t h at r et ur ns itso.bank5.beans.Address, t h e t y pe of Table of Content s t he bean.
•
I ndex
EJB 2 . 0The D e dataFrom ve lopm e nt wm it h etW hod ebSphe conv r eert St udio s anAppl Address ica ti on ob De vject el oper int o By
show n her Ueli code Wah li, as Wout er Denay er,e: Lar s Schu nk ,
f our f ields f or t h e t able. Ch ange t he
Deborah Shaddon , Mart in Weiss
Pub lish er: I BM
public Object[] dataFrom (Object anObject) {
Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Object[] anArray
= new Object[] {null, null, null, null};
Pages: 75 0
if (anObject != null) { Address address = (Address) anObject; anArray[0] = address.getZipcode(); Lear n about t he EJB 2 .0 specif icat ion anArray[1] = address.getState(); Dev elop EJBs w it h St u dio App licat ion Dev eloper anArray[2] = address.getCity(); Deploy EJBs t o W eb Sp her e Applicat ion Ser v er anArray[3] = address.getStreet(); This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion } Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. return anArray; Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly } ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2.The 0 sp ecif icat ion tm hat pr ovconv id e, ert for sex am ple, hanced ion alit y for cont. ain - m anag ed objectFrom et hod four t ableenf ields in t ofun anctAddress ob ject Cher ange t he code per sist ence ent it y b ean s and m essagedr iv en beans. We also pr ovid e best pr act ice guidelines f or as show n her e: successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session public Object objectFrom (Object[] anArray) { beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At String name, street, city, state, zipcode; t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. zipcode = (String) anArray[0]; state
= (String) anArray[1];
city
= (String) anArray[2];
street
= (String) anArray[3];
return new Address(street, city, state, zipcode); }
Remarks about the code The singleton m et hod is u sing lazy - init ializat ion on t h e singleton v ar iable. Mak e sur e t hat t he or t r ibof ut Content es is ident ical in t he getAttributeNames, objectFrom, an d dataFrom • der of t he at Table s •m et hods. I ndex EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Composer definition file Pub lish er: I BM
I n t Pub he META-INF f older , y ou can find t h e comp oser d ef init ion file, UserDefinedComposers.xmi. Dat e: April 1 1, 20 03 This f ile can b e cop ied t o ot her pr oj ect s t o m ake t he com poser available. I SBN: 0- 73 84- 26 09 -1
Pages: 75 0
This I BM Redbook href="java:/itso.bank5.beans#Address"/> pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio
ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. EJB t o RDB M apping . Select Cr eat e a n ew back en d f old er an d click Next . Select Meet I n Th e Middle an d click Next .
En t er con nect ion inf or mat ion ( ConEJBBANKcomposer, EJBBANK d at abase) an d click Next . Select t he ITSO t ables and click Fin ish. I n t he m app in g ed it or:
1. Dr ag t he CustomerComposer b ean t o t he CUSTOMER an d t o t h e CUSTADDRESS t ables. • •
Table of Content s
2. DrIag t he customerid, title, firstName an d lastName at t rib ut es t o t he colum ns of ndex
he CUSTOMER able. r e St udio Appl ica ti on De v el oper EJB 2 . 0 D e vetlopm e nt w it h WtebSphe By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
3. Select t he address at t rib ut e ( left sid e) and t h e fou r colu mn s ( street, city, state, zipcode) of t he CUSTADDRESS t able ( r ight sid e) and Cr eat e Map pin g ( con t ex t ) .
Pub lish er: I BM
4.
t he Pub Dat e:I n April 1 1,EJB 20 03Com poser t he I SBN:t o 0- 73 84-m 26at 09ching -1
Wizar d, select t he AddressComposer an d map t h e fou r at t r ibut es colum ns ( Figur e 12 - 7) .
Pages: 75 0
Figu r e 1 2 -7 . Sel ect in g t he comp ose r
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part5. 1, we Click inFin t r oduce ish anEJBs d savas e tah ep art m apping of Jav.a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he The m ap ping show in Figur e e, 12for - 8. ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed EJB 2. 0 sp ecifis icat ion n t hat pr ov id per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent atFigu ions of r eEJBs. 1 2 -8 . M a ppi ng w i t h a com p ose r I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
Generate deployed Deploythe EJBs t o W eb Spcode her e Applicat ion Ser v er This Redbook d et ailed for m at iond on h ow t o ef BeforI eBM gener at ing tpr heovides d ep loyed code,in open t he ep loy m ent d fect escrively ip t or use on tWebSp he Overhere v iewStpudio age an d App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise set t he JNDI - CMP Fact or y Con nect ion Bind in g nam e t o jdbc/ejbbank an d t he contJav ainaBeans er (aut EJB) ch ion it ectt u re, tan d d ep loymect ent of su chorap t o a- end WebSph licat ion Serv2_1 er. . h orar izat y pe o Per_ Conn ion_Fact y. plicat Set tion he sback folderer teo App DB2UDBNT_V7 Thr ougathout book , wcode. e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Gener e t het he deployed un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Testing the tcomposer concep t s and h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed To estence t he com , ysouand havmeessaget o at t ach p r oject t oice t heguidelines f or pertsist ent itposer y b ean dr ivt he en ItsoBank5ComposerEAR beans. We also pr ovid e best pr act EjbbankServer: successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Select t he sermvet erh and t hen e tEAR pr oject im plem ent fin der ods, add dif fer m app in g st. r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. St ar t t he ser v er and t he univ er sal t est clien t . I n t he JNDI Ex plor er , select t he CustomerComposer b ean u nder [ Local EJB beans] - > ejb - > it so - > bank 5 - > com poser - > Cu st om er Com poser LocalHom e ( we used t h e defau lt nam e g en er at ed by t h e t ool) . Ex pan d t he hom e int er f ace, select t he findByPrimaryKey m et hod , exp and t he k ey, en t er a v alue ( 10 1) , an d click I n vok e. Click Wor k w it h Object . Exp and t he CustomerComposerLocal r efer ence, select and inv oke t he getAddress m et hod . Click Wor k w it h Object t o access t h e fields of t he Address ob ject .
I nv oke get m et hod s of t he Address ob ject t o v iew t he f ield valu es. I nv oke set m et hod s of t he Address ob ject t o chang e t he d at a. Select t he setAddress m et hod of t h e CustomerComposerLocal r efer ence. I n t h e Obj ect s pu ll- d ow n, select t he Address ob ject an d click I n vok e. Th is upd at es t he ad dr ess d at a in t he CUSTADDRESS t able. You can v er ify t he u pdat e in a DB2 com m and w indow : • •
Table of Content s I ndex db2 connect to ejbbank
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
* from By Ueli db2 Wah li,select Wout er Denay er, Lar sitso.custaddress Schu nk , Deborah Shaddon , Mart in Weiss Pub lish er: I BM Pub Dat e: April 1 1, 20 03
This sequence v erif ies t hat t he com poser is wor k ing pr oper ly. Close t he br ow ser and st op t he SBN: 0- 73 84- 26 09 -1 ser v er Iw hen d one. Pages: 75 0
Using converters Aconv er t er is alw ay s associat ed w it h a colu m n d ef init ion. Th e conv er t er has t he r esp onsibilit y t o conv er t a nJav a t yptehet oEJB an 2 SQL t y pe,icat an dion v ice ver sa. Lear about .0 specif We could, for ex amw ple, a conv t er ion to m ap eloper t he address at t rib ut e t o some st r ing Dev elop EJBs it h use St u dio Apper licat Dev r ep r esent at ion t hat is st ored as a VARCHAR in t h e t able. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Anot h er ex am ple w ould be t o m ap a bean t hat cont ains a collect ion of st r ing s. I f y ou let t he defau lt m Redbook app ing hand le it , t dhe collectinion is at ser ialized in at oBLOB. is t ohere w r it e sim ple This I BM pr ovides et ailed for m ion on h ow ef fectThe ivelysolu uset ion WebSp Staudio conv er t er at r eads t hf eorcollect ionelopm an d st it in VARCHAR or CLOB n er aspran st r eam . Of App licat iont hDeveloper t he d ev enort es of ap plicat ion s based on colum t h e Ent iseXML Jav aBeans heit ect conv er t er m dust be abent le tof o han dleapt hplicat e conion vers sion t he ot her dir ect ionion also, readin (cour EJB)se, artch u re, an d ep loym su ch t o a in WebSph er e App licat Serv er. g t he XML and par sin g it t o recreat e t he collect ion. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an We d o not ov ide an dat exam ple of a conv er t er . The coding is sim ilar t o t h at of a com p oser . You un derly ing pr r elat ional ab ase. w ould creat e a su bclass of VapAbstractConverter an d im p lem ent t he sam e m et h ods as f or a com poser ex cept t hat yEJBs ou d eal h onofe Jav obj aect ead of an arion ray(.J2 EE) and cover t he basic I n Part 1, , we in t r oduce as aw pit art 2 inst En t er p rise Ed it concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he The b est fort hat b uilding ourforowex n am conv er ten erhanced w ould be o ion st ud y yt he plem of ed t he EJB 2. 0 spappr ecifoach icat ion pr ov idye, ple, funtct alit forim cont ainen ert-at mion anag conv er t er s pr ov ided w it h App licat ion Developer . per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Chapter 13. Bean-managed entity bean development •This chap t er Table Content discuof sses hows t o w r it e bean- m an aged p er sist en ce ( BMP) ent it y b ean s. We descr ibe •in det ail how I ndex t o b uild a bean- m anag ed ent it y b ean , w hat m et h ods h ave t o be im plem en t ed, and EJB D eSQL ve lopm e ntem w iten h tWs ebSphe e Stcoded. udio Appl ica ti on De v el oper how2 .t0he st at m ust rbe By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
We also d iscuss adv ant ages and disadv ant ag es of BMP beans ver su s CMP beans. Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Bean-managed persistence (BMP) Bean- m anaged per sist ence ( BMP) en ables y ou t o m anage t he per sist ence of y our b ean inst ead of delegat ing it t o t he con t ainer. I n or der t o ach iev e t h is, y ou hav e t o w rit e a nu mb er of met hod s t hat ar e usually han dled b y t he CMP lay er of t he con t ain er . •
Table of Content s
• I ndex This b ean has t o im plem en t t h e sam e in t er f aces as a CMP ent it y bean: EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
The b ean class has t o im plem ent t he javax.ejb.EntityBean in t er f ace. All callback m et hods in t h is in t erf ace hav e t o b e im plem ent ed.
Pub lish er: I BM
The com pon int er face Pub Dat e: April 1 1,en 20t03
h as t o ex t end eit h er t he javax.ejb.EJBObject or t he javax.ejb.EJBLocalObject, d ep en ding on w het her it is a local or a r em ot e com ponent I SBN: 0- 73 84- 26 09 -1 int er face. I n t his int er face, you w ill m ainly f in d business met h ods as w ell as accessor Pages: 75 0 m et hods. The h om e int erf ace has t o ext end t h e javax.ejb.EJBHome or t he javax.ejb.EJBLocalHome, d ep en ding on w het her it is a local or a r em ot e int er face. This int er face m ay cont ain creat e, f in der, and hom e m et hods. Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Why BMP? So w h y w ould you need BMP? I n some cases, t her e are som e r eason s w hy y ou cannot r ely on CMP t o do t h e w or k. Most of t hem concer n legacy : • •
Table of Content s
The d at Iabase ndex schema is im possible t o m ap t o t h e EJBs.
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
You hav e a r elat ional d at abase for en t it y beans t hat is not cur r ent ly supp or t ed f or CMP or does n ot hav e JD BC 2 .0 level d r iv ers and, t heref ore, y ou w ill hav e t o u se SQLJ, st or ed pr oced ur es, or some ot her t ooling t o access it .
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub You Dat e: hav April e a1 1,r elat 20 03ional
d at abase t hat r eq uir es a non st andar d w ay of hand lin g sp ecific d at a t yI SBN: pes such as or CLOB. 0- 73 8426 BLOB 09 -1 Pages: 75 0
You r eq uire non- r elat ional p er sist ence m et hod , for ex am ple, WebSph er e MQ, CI CS wit h VSAM files, or I MS.
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Recommendations Use BMP only if t h er e is n o w ay t o hand le persist ence w it h CMP. Ther e ar e m any r eason s t o use CMP, su ch as perf or man ce or por t abilit y ov er diff er ent t y pes of dat ab ases. •I f you h ave t Table Content s im plem en t t he bean by sub classin g a CMP 2. 0 bean. We ar e o useofBMP, t hen •follow ing t hisI ndex r ecomm end at ion an d w e u se t his app r oach in developing t h e BMP sam ple in t his EJB 2 . 0erD.e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper chapt By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
BMP as of a CMP Pub lish er: subclass I BM Pub Dat e: April 1 1, 20 03
How d oes roach I SBN:t his 0- 73app 84- 26 09 -1 w or k? Pages: 75 0
Cr eat e a CMP ent it y bean as if y ou w ere g oing t o im plem en t p er sist ence u sing CMP. Do not cr eat e any in t erf aces an d d o n ot g en er at e d ep loy ed cod e. The only p art w e ar e going t o use is t h e abst r act bean class. Learen t about t he EJB 2 .0 specif Cod he b usiness m et hod s in icat t he ion bean class u sin g t he abst r act g et t er an d set t er m et hods. Dev elop it hend St usdio licat ion . Dev The BMPEJBs beanwext t heApp CMP b ean Youeloper can consider t h e BMP as just an alt er nat iv e im plem ent at ion of t h e abst r act CMP class. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Du ring d ep loy m ent t he cont ainer w ou ld cr eat e a concr et e su bclass of t he CMP bean w it h This Iper BMsist Redbook pr ovides ailedisinex foract m at on wheow fect use WebSp Strudio ence f unct ionalitdyet . This ly ion wh at ar teo def oin g ively by subclassin g t hhere e abst act App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans bean class. We ar e n ot d elegat ing t h e persist ence job t o t he cont ain er ; we are ( EJB) im arplem ch it ect u in re,g an d d selv ep loym ent it our es. ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout book , w e pr ovt he id eBMP exam based a simp bankeing ion h an . The The EJBt he clien t accesses t yples pe ov er it s on bean homle e nam in app t he licat nor m al w mitanner un derly ing r elat ional dat ab ase. EJB client does not k now an yt h ing about out CMP t y pe. I n Part 1, we in t rof oduce EJBs as ofinJav En t ner er p rise The ad van t ages im plement in ga ap art BMP t hais2man ar e:Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed You can criteat t hes CMP atessaget ribu t esdrwiv it en h t he t ool. We also pr ovid e best pr act ice guidelines f or per sist ence ent y beean and m beans. successful imp lem ent at ions of EJBs. You can easily sw it ch t o t h e CMP t y pe lat er if t he EJB cont ain er is ab le t o h andle t h is par2,t icular w it h tCMP. The st r act class ill st ay t heimp sam e.ent I n ten h et itEJB client ,session you I n Part we in jt ob r oduce h e sam p leab bank ingbean applicat ionw and t hen lem y beans, only t o chang t hebeans t yp e using you arWebSph e accessing. beans, an dh ave m essag e- d r iveen er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At at ically separ at e t h eEJB bu siness logic he im plem t at ion by uer. sing t he enYou d, waut e dom escrib e how t o deploy ap plicat ion sf rom t o a tWebSph er eenApp licatcod ion eServ dif fer en t classes. You do not hav e t o code t he get t er s and set t er s m anu ally . You can u se t he pr im ar y key class fr om t h e CMP, t hat is, you do not hav e t o im plem en t t h e k ey class y our self. You get t he signat u res of t h e ejbCreate an d ejbPostCreate m et hod s.
Developing a BMP entity bean I n t his sect ion, w e develop a CustomerInfoBmp b ean t o illu st rat e t he u se of BMP. The CustomerInfoBmp b ean r epr esent s addit ional in for m at ion for a cu st omer of t h e ban k. For •each k ind of Table s addit of ionContent al inf orm at ion, w e cr eat e a row in a CUSTOMERINFO t able. Addit ional •inf orm at ion could I ndex be a phot o for pr in t ing on t he cr ed it car d or a scanned imag e for t he EJB 2 .if 0 icat D e ve lopm e nt .w it h W ebSphe r e St udio Appl ica ti on De v el oper ident ion card By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Underlying Pub lish er: I BM table for customer information Pub Dat e: April 1 1, 20 03
Table 1I SBN: 3- 1 sh ows t he lay out of t he CUSTOMERINFO t able. 0- 73 84- 26 09 -1 Pages: 75 0
Tab le 1 3 - 1 . Cu st om e r i nf or m at ion t a bl e Learnn na about EJB ion y N ul ls Colum m e t he Ty pe2 .0 specif Le ng ticat h Ke
D e scri pt i on
CUSTOMERID PK Dev NoeloperCu st om er I D Dev elop EJBs wINTEGER it h St u dio App licat ion INFOID PKion Ser No v er I nf orm at ion I D ( 1 , 2, .. . ) Deploy EJBs t o INTEGER W eb Sp her e Applicat DESCRIPTION VARCHAR 50 No Yes Descr ipt ion of inf orm at ion This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d10 ev000 elopmNo en t ofYes ap plicat ion sy based DATA BLOB Binar dat a on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. This d esign allow s for one ent r y of each k ind of infor m at ion. We m ap t he BLOB dat a t o a by t e Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an ar r ay in Jav a. un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and h e ar ch it ect ur e. for I n par t icuBMP lar w e entity point outbean t he n ew fu nct ions int r od uced w it h t he Create an tEJB project the EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per entEJB it y bprean s and med essagedr iv en beans. f We pr ovid e best ice guidelines f or We sist u seence a new oj ect nam ITSOBank5BmpEJB or t halso e BMP im plem en tpr atact ion: successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d File m essag e- d r -iv>enProj beans er e St Select - > New ect , using EJB - >WebSph EJB Pr oject . u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he enSelect d, w eCrd eat escrib e 2.e0how EJB tPr o oject deploy . EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. En t er as ITSOBank5BmpEJB n am e, and select t he ex ist in g en t er pr ise app licat ion pr oject ItsoBank5EAR. Click Fin ish. Cr eat e a p ackag e nam e itso.bank5.bmp in t h e new pr oj ect .
Creating the superclass CMP entity bean
Not e To cr eat e a BMP, w e ar e follow ing t he r ecomm endat ion d escrib ed abov e b y su bclassing a CMP 2 . 0 class. I f y ou do not wan t t o use t h is appr oach, y ou can sk ip t his st ep .
•We cr eat e t he Table s supof erContent class CMP en t it y bean wit h t h e nam e CustomerInfoCmp. Th is pr ocess is •descr ibed in Idndex et ail in " Cr eat e a CMP 2 . 0" on p age 39 9: EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Cr eat e a CMP 2 . 0 w it h t he nam e CustomerInfoCmp in t h e itso.bank5.bmp p ackag e. Pub lish er: I BM
Set EJB Pub Datte:h e April 1 1,bindin 20 03 g
n am e t o ejb/itsobank/CustomerInfoCmp.
I SBN: 0- 73 84- 26 09 -1
Select t he r emot e in t erf ace only . Pages: 75 0
Set t h e k ey class nam e t o itso.bank5.bmp.CustomerInfoKey. Add t h e CMP field s show n in Table 1 3- 2.
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs wTab it h St ion leu dio 1 3 App - 2 . licat Fi el dsDev ofeloper t h e CM P
su per t yp e
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Ty pe Nam e key N ot e This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio int cutomerID Pr imar y App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans k ey ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. int infoID Pr imar y Com posed pr imar y k ey Thr oug hout t he book , w e pr ov id e exam ples k eybased on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. java.lang.String description I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic data Select t ypions e andintselect concepbyte[] t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t hebyte n ew as fu nct r od uced w it h t he Ar ray of d imension EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for1 cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful Click Fin imp ish. lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also The key class im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. The m ost im p ort an t p art of w hat w e get b y defin in g t he CMP ent it y b ean is t h e key class. A k ey class is requ ir ed for t he BMP en t it y bean and inst ead of coding it our selv es, w e can get it gener at ed. The m ain m et hods of t h e k ey class ar e: The con st r uct or t hat cr eat es a k ey fr om t h e t w o k ey fields The g et and set m et hods t o set t h e k ey f ield s The CustomerInfoKey class is show n in Figur e 13 - 1.
Figu r e 1 3 -1 . Key cl ass f or BM P en t i t y be an
package itso.bank5.bmp; /** • •
Table of Content s I ndex
* Key class for Entity Bean: CustomerInfoCmp
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By*/ Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
public Pub lishclass er: I BM CustomerInfoKey implements java.io.Serializable { Pub Dat e: April 1 1, 20 03
static final serialVersionUID = 3206093459760846163L; I SBN: 073 84- 26long 09 -1 Pages: 75 0
public int customerID; public int infoID; Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper public CustomerInfoKey() { }
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio CustomerInfoKey(int customerID, intion infoID) { t h e Ent er pr ise Jav aBeans Apppublic licat ion Developer f or t he d ev elopm en t of ap plicat s based on ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. this.customerID = customerID; Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an = infoID; un derlythis.infoID ing r elat ional dat ab ase. } 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic I n Part concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he boolean otherKey) EJB public 2. 0 sp ecif icat ion tequals(java.lang.Object hat pr ov id e, for ex am ple, en hanced fun ct{ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or if imp (otherKey successful lem ent atinstanceof ions of EJBs. itso.bank5.bmp.CustomerInfoKey) { I n Part 2, we in t r oduce t h e sam p le bank ing applicat itso.bank5.bmp.CustomerInfoKey o =ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At (itso.bank5.bmp.CustomerInfoKey) otherKey; t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. return ( (this.customerID == o.customerID) && (this.infoID == o.infoID)); } return false; }
public int hashCode() { return ( (new java.lang.Integer(customerID).hashCode()) + (new java.lang.Integer(infoID).hashCode())); • •
Table of Content s
}
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
public int getCustomerID() {
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
return customerID; Pub lish er: I BM Pub } Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
public Pages: 75 void 0 setCustomerID(int newCustomerID) { customerID = newCustomerID; } public int getInfoID() { Lear n about t he EJB 2 .0 specif icat ion return infoID; Dev elop EJBs w it h St u dio App licat ion Dev eloper } Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Thispublic I BM Redbook pr ovides d et ailednewInfoID) in for m at ion {on h ow t o ef fect ively use WebSp here St udio void setInfoID(int App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) arinfoID ch it ect u=re,newInfoID; an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug } hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. } I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp Creating the lemBMP ent at ions entity of EJBs. bean ITo n Part t r oduce sam p le bank ing applicat cr eat2, e twe he in BMP ent it yt hbeean , follow t hese st eps: ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Cr eat e a n ew ent erp rise b ean by select ing New - > Ent er pr ise Bean f r om t he con t ex t m en u of t he EJB p roj ect . Click Next on t h e pr oject w ind ow . I n t he Cr eat e a 2. 0 Ent er pr ise bean p anel ( Figur e 13 - 2) :
Figu r e 1 3 -2 . Cr ea t i ng a BM P e nt it y b ea n: n am e
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
1. Select En t it y bean w it h bean- m anaged persist en ce ( BM P) f ield s. 2. En t er CustomerInfoBmp as t he b ean nam e an d m ak e sur e t h at t h e itso.bank5.bmp pack age is select ed. Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o eW eb Sp her e Applicat ion Ser v er Not This I BM Redbook We pr arovides e u singd et t hailed e suffin ix for Bmp m at t oion m ar onk hitow as tao tef ech fect nical ivelysubt usey pe WebSp of t he here CMP St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans t y pe. Nor m ally it m akes n o sen se t o p oin t t o t he t yp e of t he b ean in it s ( EJB) ar ch it ect unam re, an e. dI fdyou ep loym d o not ent use of suach suap pert plicat yp e,ion you s t oshould a WebSph use CustomerInfo er e App licat ionas Serv t heer. bean nam e. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly3. ing Click r elatNext ional. dat ab ase. I n Part t r oduce EJBs Det as aails p art of Jav a2 e En13 t er- 3) p rise ( J2 EE) and cover t he basic I n 1, t heweEnint erp rise Bean panel ( Figur , d oEd asit ion f ollows: concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hatr pr e, .forCrexea amt iple, y for Figu e ov 1 3id-3 ng en a hanced BM P efun ntctition y alit b ea n: cont de tain a ilers- m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Set t h e EJB bindin g n am e t o ejb/itsobank/CustomerInfo. Select on ly Rem e clien v iew .icat ion Lear n about t he ot EJB 2 .0 tspecif IDev f you ar eEJBs subclassin t he App CMP 2. 0ion bean, you can use t he p rim ar y k ey class fr om t he CMP elop w it h Stgu dio licat Dev eloper t y pe. Click Class an d en t er cust t o locat e and select t h e CustomerInfoKey class. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er You cannot ch ange t he b ean super t y pe in t his w ind ow. The sup er t y pe on ly applies t o CMP This Iinher BM Redbook it ance ( see pr ovides " Defindinetgailed t he inh in for er it mance at ionston r uct h ow ur e"t oon ef fect p age ively 44 8) use . WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) Click ar chNext it ect .u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. I nhout t he EJB Jav a ,Class etid ails p anelples ( Figur e 13on - 4)a, simp if y oulewbank ant ting o subclass he CMP 2 . 0 b ean Thr oug t he book w e prDov e exam based app licat tion w it h an class, Br owdat se fab or ase. t h e Bean sup er class and locat e t he CustomerInfoCmpBean class. un derly ing rclick elat ional I n Part 1, we in tFigu r oduce p arteaoft iJav t erPp rise ( J2n: EE)supe and cover t he basic r eEJBs 1 3 as -4 a. Cr nga a2 En BM e ntEd it yit ion b ea r cl ass concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Click Fin ish. The BMP it y b ean ad ded t o t heicat pr oject Learent n about t he isEJB 2 .0 specif ion as show n in Figur e 13 - 5. Dev elop rEJBs it h. St u dio licat eloper Figu e 1 3w-5 J2 EEApp hi er a rion chDev y of th e
I t soBa nk 5 Bm pEJB pr oje ct
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem der m et ht yods, ferw enert emgenerat app in g ed st r at and ent simitpyleb ean client Table 1 ent 3- 3 fin sh ows t he pes dif t h at f oregies, t he BMP . s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Tab le 1 3 - 3 . Gen er a t e d t yp es
Ty pe
D e scri pt i on
CustomerInfoBmpHome The h om e int erf ace of t he b ean w it h a create an d a findByPrimaryKey m et hod . CustomerInfoBmp
The com pon en t int er face of t h e bean. No m et h ods h ave b een gener at ed in t o t h is in t erf ace.
• CustomerInfoBmpBean Table of ContentThe s b ean it self , w hich w ill con t ain t he act ual im p lem ent at ion. Not e •
I ndex
t hat t his class is concr et e. Only t he CMP b ean classes ar e abst r act .
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
This b ean show s m any er r or s in t h e Tasks view because w e hav e n ot y et pr ov ided t h e im plem ent at ion of t h e abst r act m et hods of t h e super class.
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM
CustomerInfoKey
Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
The p r im ar y k ey class, w hich h olds all t h e at t r ibut es u sed t o com pose t he pr imar y k ey of t he b ean . Not e t h at t h is is t he class fr om t h e CMP 2. 0 t y pe an d w e d o not hav e t o imp lem ent t his k ey class.
Pages: 75 0
I f you d o not use inh er it ance f rom a CMP 2. 0 t y pe, t h en y ou hav e t o im plem ent t he k ey class y our self , w it h code t h at m at ches t his gener at ed class.
Abstract methods Lear n about t he EJB 2 .0
specif icat ion
Dev elop EJBs w it hprSt dio App licat Dev eloper App licat ion Developer ovuides a sim pleion w indow t o cr eat e skelet on im plem en t at ions of ab st r act m et hods an d t o ov erw r it e super class m et hod s: Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat f or t he d ev elopmclass. en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans Op ion en tDeveloper he CustomerInfoBmpBean ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. I n t he Ou t line v iew , select t he CustomerInfoBmpBean class and Ov err ide Met hod s Thr oug houtext the ( cont ) . book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n t he Ov er ride Met hods w ind ow , t he abst ract m et h ods ar e pr eselect ed ( Figur e 13 - 6) . I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he Figu r e 1 3 -6 . O ve r r id e M et hods EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
You could cr eat e skelet ons f or t hese m et h ods, bu t t h en y ou w ould hav e t o w r it e t h e im plem ent at ion or specif each m etion hod . Lear n about t he code EJB 2f.0 icat IDev t is elop easier t o def e tuhe en tion f ields creat e g et t er an d set t er m et hods. EJBs w it hin St diopersist App licat Devand eloper Select ly t he d ejbPostCreate(int,int) m et hod s. We w ant t o Deployon EJBs t o ejbCreate(int,int) W eb Sp her e Applicat ionanSer v er ov er w r it e t hese m et h ods in t he BMP b ean . The ejbCreate m et hod t h at w as g en er at ed has am et er pr s. ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio This Ino BMpar Redbook App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) Click ar chOK. it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat dat ab ase. fields Defining theional persistent I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic The BMP n ot ant icu y per fields. defin fields t hat m athcht he t he concep t s ent andit tyhbeean ar chdoes it ect ur e. hI ave n par lar sist w eent point out tWe he nhav ewefut onct ionse int r od uced w it CMP bean: EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. // the 2,BMP I n Part we fields in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also private customerID = 0; im plem entint fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. private int infoID = 0; private String description = null; private byte[] data = null;
Select t he f our f ields in t h e Out line view and Gener at e Get t er and Set t er ( con t ex t ) . This oper at ion cr eat es t he g et t er an d set t er m et hods t hat im p lem ent t he abst r act m et hod s of t he CustomerInfoCmpBean su perclass.
Create method with parameters The CustomerInfoBmpBean n ow has t w o ejbCreate m et hod s:
• •
Delet e tTable he ejbCreate of Content s an d ejbPostCreate m et hod s w it h no p aram et ers. I ndex
Select t he ejbCreate(int,int) m et hod an d En t er p rise Bean - > Prom ot e t o Hom e I n t erf ace ( con t ex t ) .
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Op en t he CustomerInfoBmpHome in t er f ace and delet e t he create m et hod w it hou t We k eep on ly t he create m et hod w it h t w o p aram et ers.
Pubpar lish am er: Iet BMer s.
Pub Dat e: April 1 1, 20 03
Now w IeSBN: h ave t o im plement t he cod e t hat is r espon sib le for t h e per sist ence han dling. Let 's t ak e a 0- 73 84- 26 09 -1 look on t he m et hod s in t he bean class f irst . Pages: 75 0
A first look at the bean methods Aft er t he bean has b een creat ed, w e can exam ine t he m et hods of t he bean. There are t w o t y pes of m etLear hods: t he r equir ed callback micat et hion ods f or w hich we hav e t o p rov ide an im plem en t at ion, an d n about t he EJB 2 .0 specif som e ut ilit y m et hods t h at h elp us t o r euse some cod e and also r ed uce t he com plexit y of t he elop EJBs w it h St u dio App licat ion Dev eloper r eq uirDev ed m et hods. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Callback methods This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans A n umar ber ofect callb h ods hav t o su bechimap plem enion t edsint oor t o su er ppor t t he beanman er. aged ( EJB) ch it u re,ack an met d d ep loym ente of plicat a der WebSph e App licat ion Serv per sist ence. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Not e t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he concep t s and EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Allence t h ese e been g en erdratived App licat . Sompreact of ice t hem ar e per sist entmitet y hods b ean shav and m essageen by beans. Weion alsoDeveloper pr ovid e best guidelines f or ju st a sk elet on , w hile ot her s m ust h ave a usable im plem en t at ion. successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Table 1 ent 3- 4 fin sh ows t he m et im plem der m et hcallback ods, dif fer enhtods: m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. The f ir st fiv e m et hods ( ejbLoad, ejbStore, ejbFindByPrimaryKey, ejbCreate, an d ejbRemove) in vok e SQL st at em en t t o syn ch ron ize t he b ean d at a w it h t he d at abase t able. The setEntityContext an d unsetEntityContext ar e u sed t o allocat e an d f r ee r esour ces, such as t he d at a sour ce used t o int er act w it h t he d at abase. We w ill not t ou ch t he ejbActivate an d ejbPassivate m et hod s.
Tab le 1 3 - 4 . Ca llb ack m et hods t ha t m ust b e im pl em e nt ed Me t hod
D e scri pt i on This m et hod has t o ensur e t h at t h e bean dat a is con sist ent wit h t h e dat a in t he per sist ent st or e. W e w ill ret r iev e t h e dat a using an SQL select st at em en t .
ejbLoad
• •
ejbStore
Table of Content s I ndex
EJB 2 . 0 D e ve lopm e nt w it h
The con t ain er inv okes t h is met h od t o allow t he bean t o up dat e t h e per sist ent st ore wit h it s ow n dat a. I t is up t o t he b ean t o det erm ine if W ebSphe r e St udio Appl ica ti on De v el oper such upd at e is r elevan t or not . We w ill use an SQL upd at e st at em ent .
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
ejbFindByPrimaryKey This m et hod has t o b e imp lem ent ed t o r et riev e t he b ean u sing t h e pr im ary key . A FinderException or ObjectNotFoundException can Pub lish er: I BM be t hr ow n. We w ill u se an SQL select st at em ent . Pub Dat e: April 1 1, 20 03
ejbCreate I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
This m et hod has t o b e upd at ed w it h t he cor r ect num ber of p aram et ers and it has t o be im plement ed . I t is u sed t o cr eat e a n ew bean in t h e per sist ent st ore. Please not e t h at a CreateException can be t hr ow n if som et h in g goes w ron g dur ing creat ion. We w ill use an SQL inser t st at ement .
When a clien t is calling t he remove m et hod on a r em ot e int er face or is callin g t h e remove(key) m et hod on t he hom e int er face, it r esu lt s in Lear n about t he EJBt 2he .0 con specif icat inv ion okin g t h e ejbRemove m et hod t o delet e t he m at ching t ainer dat a in t he per sist ent st or e and t o r elease h eld r esour ces. We w ill use Dev elop EJBs w it h Stan u dio App licat eloper SQL delet e ion st atDev em ent .
ejbRemove
setEntityContext Deploy EJBs t o W eb Sp hermeetApplicat ion Serwvhen er a bean inst ance is cr eat ed in t he p ool but This hod is called is not y et associat ed wit h an EJBObject. Th e dat a receiv ed is st or ed This I BM Redbook pr ovides d et ailed in for m atv ion onle.h ow o ill ef fect ively WebSp inside an inst ance ar iab We t w allocat e ruse esour ces inhere t his St m udio et hod . App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans unsetEntityContex When his m is plicat calledion , t he is ab out be licat remion ov ed fr om ( EJB) ar ch it ect u re, an d d ep loymtent ofetsuhod ch ap s t ob ean a WebSph er et oApp Serv er. t he pool and dest roy ed. We will f r ee resour ces in t his met hod . Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an ejbActivate This m et hod is inv ok ed w hen a clien t calls a m et hod on a EJBObject un derly ing r elat ional dat ab ase. w it h no associat ed bean inst ance. The cont ain er t hen t akes an inst ance and ok es t o allow e inst ance I n Part 1, we in t r oduce EJBs as a pf rom art oft he Javpaool 2 En t erinv p rise EdejbActivate it ion ( J2 EE) and cover t h t he basic t o acq uir e w hat ever r esou r ces it need s in or d er t o be in t h e r ead y concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat st prat ove.id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essagedr iv en beans. Wecealso pr ovid f or ejbPassivate When a p art icular bean in st an is not usede best any mpr oract e, ice t he guidelines cont ainer is successful imp lem ent at ions of EJBs. passivat in g it . The r esu lt of t hat oper at ion is a callb ack t o ejbPassivate t o allow t h e bean t o r elease t he resour ces acqu ired I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session du rin g t h e act ivat ion . beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At tImplementing he en d, w e d escribthe e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. bean methods I nst ead of im plem en t ing t he JD BC access in t he BMP ent it y bean w e cr eat e a p er sist er b ean nam ed CustomerInfoBmpPersister. Ther e ar e fiv e m ain t ask s f or t he p er sist en ce: Load a st at e Sor e a st at e
Find a st at e Cr eat e a st at e Delet e a st at e For each t ask , w e creat e a m et h od in t h e per sist er class ( Table 1 3- 5) : •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Tab le 1 3 - 5 . M et h ods of t he JDB C per sist e r cla ss
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: Purp ose
I BM
Me t hod
Pub Dat e: April 1 1, 20 03
Load a I SBN: st at e
loadState
0- 73 84- 26 09 -1
Pages: 75 0
St or e a st at e
storeState
D e scri pt i on Load all v alues f rom t he dat abase for t he r ow w it h t he g iven k ey by inv ok in g an SQL select an d readin g all v alues f rom t he r esult set . St or es all v alu es of t he giv en b ean in t he dat abase by in vok ing an SQL update.
findPrimaryKey Ch eck s if t her e is a r ow w it h t he given k ey u sin g an SQL Find a select. Ret u rn s t he key if fou nd. st at e Lear n about t he EJB 2 .0 specif icat ion createState Cr eat e a r ow in t h e dat ab ase b y execut ing an SQL insert. Cr eat e a Dev elop EJBs w it h St u dio App licat ion Dev eloper st at e deleteState DeletDeploy ea e a ion d at abase EJBs t o W eb Sp her eDelet Applicat Ser v err ow for t he g iv en key by in vok ing an SQL delete. st at e This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App Developer t he per d evsist elopm endescr t of ap plicat based ersist pr ise Jav aBeans Howlicat w e ion im plem en t t h ef or JDBC er is ibed in ion " I msplem enton ingt ht e h eEnt per er class" on p age (50 EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion 7. Th e int erest ing point is w hich persist er act ion t o call in w hich callb ack m et hod . Serv er. Thr houten t he , w e prm ovet idhod e exam based simp bank ing app licat ical ion w h an We oug im plem t t hbook e callback s by ples d elegat ing on t o at he p erle sist er class. Techn exitcep t ions un derly ing r elat ional dat ab ase. ar e caugh t an d w r app ed in an appr opr iat e except ion. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he ejbFindByPrimaryKey EJB 2. 0 sp ecif icat ion t hat method pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or We st ar t w imp it h im plement in g of t heEJBs. ejbFindByPrimaryKey m et hod . We h ave t o sear ch for t he k ey successful lem ent at ions and r et ur n it if fou nd by t h e per sist er class. I f an SQL ex cep t ion occur s, w e w r ap it in a FinderException ( Figurt hee13 - 7)p. le bank ing applicat ion and t hen imp lem ent en t it y beans, session I n Part 2, we in t r oduce sam beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem der et ple h ods, in g dBy st r at egies, Figu r eent1 3fin-7 . Imm m dif enfer t ien ngt meapp jbFin Pr i m and ar y sim Ke pyle client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
public itso.bank5.bmp.CustomerInfoKey ejbFindByPrimaryKey( itso.bank5.bmp.CustomerInfoKey primaryKey) throws javax.ejb.FinderException { boolean found = false; try {
found = persister.findPrimaryKey(primaryKey); } catch (SQLException e) { e.printStackTrace(); •
Table of Content s
•
throwI ndex new FinderException(e.getMessage());
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli } Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
if lish (found) Pub er: I BM Pub Dat e: April 1 1, 20 03
return primaryKey;
I SBN: 0- 73 84- 26 09 -1 Pages: else
75 0
throw new ObjectNotFoundException(); } Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
ejbCreate method
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er The im plem en t at ion of t he ejbCreate m et hod cr eat es a new inst an ce of t h e bean key class and This I BM Redbook prtovides d et ailed for mSQL at ionexcept on h ow t o ar efefect ively here passes t his inst ance o t he persist er in class. ions caug ht use and WebSp w rap ped in aSt udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans CreateException. Th e DuplicateKeyException is a subt y pe of t he SQLException ( Figur e 13 (8)EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. . Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Figu r eing1 3r elat -8 ional . I m dat pleab mase. en t i ng e jbCr ea t e un derly I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he public CustomerInfoKey customerID, int infoID) EJB 2. 0 sp ecif icat ion t hat pr ejbCreate(int ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or throwsimp CreateException { successful lem ent at ions of EJBs. I n Part CustomerInfoKey 2, we in t r oducekey t h e sam = new p le CustomerInfoKey(customerID, bank ing applicat ion and t hen imp infoID); lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem try ent { fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. persister.createState(key); return key; } catch (SQLException e) { e.printStackTrace(); throw new CreateException(e.getMessage());
} }
ejbLoad method •
Table of Content s
•
I ndex
The im plem en t at ion of t he ejbLoad m et hod is show n in Figur e 13 - 9. SQL ex cept ion s ar e caught . EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper We ar e d elegat ing t o t he p er sist er class, w hich loads t he st at e f r om t he d at abase and copies t he By Ueli Wah er Denay Lares s Schu , Deborah Mart v alues t o li,t hWout e bean at t er, r ibut b y nkinv oking tShaddon h e set ,t er s in ofWeiss t he bean class. An EJBException is t hr ow n t o cau se a r ollb ack if SQL er r ors occur . Pub lish er: I BM PubrDat Figu e e:1 3April -91.1,I 20 m03ple m en t i ng e jbLoa d I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
public void ejbLoad() { try { persister.loadState(this); Lear n about t he EJB 2 .0 specif icat ion } Dev catch { licat ion Dev eloper elop (SQLException EJBs w it h St u dioe) App Deploy EJBs t o W eb Sp her e Applicat ion Ser v er e.printStackTrace(); This I BM Redbook ovides d et ailed in for m at ion on h ow tcode: o ef fect"+e.getErrorCode()); ively use WebSp here St udio throw new pr EJBException(e.getSQLState()+" App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) } ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr } oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he ejbStore method EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful entof at tions of EJBs. m et hod in Figur e 13 - 10 is ver y sim ilar . I t d elegat es t o t he The im plemimp en tlem at ion he ejbStore per sist er class, w h ich r ead s all t he at t r ib ut e v alues by in vok ing t h e get t ers of t he bean class an d Iwnr itPart 2, wet ointthe r oduce t h e sam le bank ing applicat hen imp ent en it y beans, es t hem dat abase. AnpEJBException is t hrion owand n t o tcause a rlem ollback if tSQL err or s session occu r . beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At Figu r e 1 3 -1 0 . I m pl em e n t in g e jb St or e t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
public void ejbStore() { try { persister.storeState(this); }
catch (SQLException e) { e.printStackTrace(); throw new EJBException(e.getSQLState()+" code: "+e.getErrorCode()); } •
Table of Content s
•
I ndex
}
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
ejbRemove method Pub lish er: I BM
Pub Dat e: April 1 1, 20 03
The im plem en t at ion of t he ejbRemove m et hod j ust d elegat es t o t he p er sist er an d t hr ow s a I SBN: 0- 73 84- 26eit 09her -1 if t h e r et u rn v alue is f alse or if an y ot her SQL ex cept ion s ar e t hr ow n RemoveException 75 0 . ( Figur Pages: e 13 - 11)
Figu r e 1 3 -1 1 . I m pl em e n t in g e jb Re m ove
Lear n about t he EJB 2 .0 specif icat ion public void ejbRemove() throws javax.ejb.RemoveException { Dev elop EJBs w it h St u dio App licat ion Dev eloper try { Deploy EJBs t o W eb Sp her e Applicat ion Ser v er boolean success = persister.deleteState(this); This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licatif ion(!success) Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. throw new RemoveException("Not able to delete state"); Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an } catch (SQLException un derly ing r elat ional dat ab ase.e) { I n Part throw 1, we innew t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic RemoveException(e.getMessage()); concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB }2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. } I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At Completing thee BMP bean t he en d, w e d escrib how t o entity deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. We h ave t o add t h ese im por t st at em ent s f or t he ex cep t ions and t h e per sist er class u sed in t he code:
import javax.ejb.*; import java.sql.SQLException; import itso.bank5.bmp.persister.CustomerInfoBmpPersister;
We h ave t o allocat e an in st ance of t he p er sist er class and init ialize it f or subsequent m et hod calls.
• •
Cr eat e a f ield f or t he p er sist er class: Table of Content s I ndex
EJB 2 . 0// D e persister ve lopm e nt w it class h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
private CustomerInfoBmpPersister persister = null; Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73t84I nit ialize he26 p 09 er -1 sist er in st an ce in t h e setEntityContext m et hod : Pages: 75 0
public void setEntityContext(javax.ejb.EntityContext ctx) { myEntityCtx = ctx; (persister persister = new CustomerInfoBmpPersister(); Learif n about t he EJB == 2 .0 null) specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper } Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This IFree BM Redbook prces ovides ailed in for at ion ow t o ef fect ively use WebSp here St udio t h e r esour in t hd eet un set En t it ymCont exon t mhet hod: App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. public void unsetEntityContext() { Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing ional dat ab ase. ifr elat (persister != null) persister.freeResources(); I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic persister = null; concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed myEntityCtx = null; per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. } I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Promoting the business methods We h ave t o m ak e t he b usin ess m et hod s av ailab le t o clien t s. I n t h e Out line view , select all t he get t er m et hods ( getCustomerID, getData, getDescription, getInfoID) and t w o set t er m et hods ( setData, setDescription) and En t er p rise Bean - > Prom ot e t o Rem ot e I n t erf ace( cont ex t ) . Not e t h at w e d o not pr om ot e setCustomerID an d setInfoID, b ecause k ey at t r ibut es of a bean cannot be chan ged. The p r omot ed m et hods ar e v isib le in t h e CustomerInfoBmp in t er f ace.
Completing the deployment descriptor We h ave t o set up t h e deploy ment descr ipt or w it h cont rol in for m at ion for t h e dat ab ase access. Op en t he deploym ent descr ipt or of t he ItsoBank5BmpEJB p r oject . •
Table of Content s
•
I ndex
WebSphere bindings EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
On t he Ov er v iew page, scroll dow n t o set t h ese pr op er t ies: Pub lish er: I BM Pub Dat e: April 1 1, 20 03
Set t h e0- 73 JNDI n09 ame I SBN: 84- 26 -1 und er JND I - CMP Fact or y Con nect ion Bindin g t o jdbc/ejbbank. Pages: 75 0
Set t h e Cont ain er aut hor izat ion t y pe t o Per_Connection_Factory. Set t h e Backend I D t o DB2UDNNT_V72_1. This is t he sam e as for t he CMP pr oj ect ( see Figur e 11 - 22 on p age 42 1) . Lear n about t he EJB 2 .0 specif icat ion
Environment variable for the data source
Dev elop EJBs w it h St u dio App licat ion Dev eloper
The pDeploy er sist er EJBs classt ouses d at a esour ce f orion conSer nect ion m anag em ent . This is t he desir ed ap pr oach W ebaSp her Applicat v er w it h JDBC 2. 0 so t hat connect ions can be shar ed b et w een CMP an d BMP ent it y b ean s. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio IApp nst licat ead ion of hDeveloper ard - codingf or t het he d atdaevsour ce en nam e ap in plicat t he persist er class, t he ame as an elopm t of ion s based on w t heedefin Ent erepr isenJav aBeans env iron v ariab le dofdtep heloym BMPent en tof it ysu bean. ( EJB) arment ch it ect u re, an ch ap plicat ion s t o a WebSph er e App licat ion Serv er. On t he hout Bean tshe page, CustomerInfoBmp d scr dow n tapp o Env iron Thr oug bookselect , w e prt he ov id e exam ples basedb ean on aan simp le oll bank ing licat ionment w it hVar an iables. Click Adding t o rdef e a vdat ar iable and com plet e t he w ind ow as show n in Figur e 13 - 12. un derly elatinional ab ase. I n Part 1, we Figu in t r oduce p art ir ofonm Jav a e 2 nEn p rise cover r e 1EJBs 3 -1as 2 .a Env t tverar i abEd leit ion f or( J2 daEE) t a and sour ce t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Not e t h at w e d ef ine a local JND I nam e ( java:comp/env/....) . Th at m eans w e h ave t o def in e a r ef erence t h at m at ches t h is nam e t o a global JNDI n ame. We u se t he bmp/dbdatasource v ar iable in t he p er sist er class t o r et r iev e t h e JNDI n ame of t he dat a sour ce.
Not e We could defin e m ore env ir onm ent v ar iables, su ch as t h e pr ov ider URL or t he cont ex t fact or y class. The dat a sour ce en vir onm ent var iable illust r at es t he concept of passing v alues fr om t h e deploy ment descript or t o t he per sist er class. •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah JNDI reference li, Wout er Denay for the er, Lardata s Schusource nk , Deborah Shaddon , Mart in Weiss
ThePub p er sist lish er: er I BMclass get s t h e nam e of t h e dat a sou r ce t hr ough t he bmp/dbdatasource env Pub ironDat ment v ariab e: April 1 1, 20le. 03 Th e per sist er t hen inv okes a JNDI look up t o g et t h e dat a sou rce ob ject . I SBN: 0- 73 84- 26 09 -1
We associat e t he dat a sour ce nam e used in t h e per sist er class w it h a r eal JNDI nam e t hr ou gh a Pages: 75 0 r ef erence. On t he Refer en ces pag e, select t he CustomerInfoBmp b ean an d click Add . I n t he Ad d Ref erence w indow :
Lear n about t he EJB 2 .0 specif icat ion Select EJB r esou r ce r efer en ce and click Next . Dev elop EJBs w it h St u dio App licat ion Dev eloper Com plet e t h e w indow as sh ow n in Figur e 13 - 13: Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Figu r e 1 3 -1 3 . De fi ni ng a da t a sour ce r e f er e nce
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
1. En t er jdbc/MYejbbank as t he local nam e 2. Select javax.sql.DataSource as t yp e 3. Select Con t ain er f or au t hent icat ion 4. Select Shar eable f or shar ing scop e 5. Click Fin ish.
The r efer ence appear s in t he list of r efer en ces. For WebSph er e b in dings, ent er jdbc/ejbbank as t he JNDI nam e ( Figur e 13 - 14) . Sav e t h e file w hen done.
Figu r e 1 3 -1 4 . De ploy m en t descr i pt or : r e fe r en ce s
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
Implementing the persister class
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
The er sist er class,prCustomerInfoBmpPersister, t heively JDBC forhere t h e St BMP This pI BM Redbook ovides d et ailed in for m at ion onimhplem ow t oeneft sfect useaccess WebSp udioen t it y bean. App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Cr eat e t he CustomerInfoBmpPersister class in a new itso.bank5.bmp.persister p ackag e and com plet tehe t he code det in t his sect ion . on a simp le bank ing app licat ion w it h an Thr oug hout book , was e pr ovailed id e exam ples based un derly ing r elat ional dat ab ase. On e of t he im por t ant issu es in t h e per sist er class is t o alw ays close t he SQL object s, such as ResultSet, PreparedStatement, d Connection. is accom using a try/catch I n Part 1, we in t r oduce EJBs as a an p art of Jav a 2 En tTh er pisrise Ed it ionplished ( J2 EE)b yand cover t he basic block w it h a finally clause. concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful Import statements imp lem ent at ions of EJBs. IAdd n Part 2, we in t rt oduce e sam bank ing applicat ion andand t hen t h ese im por st at emt hent s f orp le SQL access, JNDI access, eximp ceptlem ionent s: en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. import itso.bank5.bmp.CustomerInfoBmpBean; import itso.bank5.bmp.CustomerInfoKey; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;
import javax.sql.DataSource; import javax.naming.InitialContext; import javax.naming.NamingException;
•
Table of Content s
•
I ndex Constants and variables EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
The p er sist er class k eeps t he d at a sour ce cach ed . To cr eat e t he d at a sour ce, t he env ir onm en t v ariab le defined in t h e deploy m en t d escrip t or is used. For d at abase access, t he SQL st at em en t s lish er: ar e Pub defin ed .I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
// sql datasource and connection private DataSource ds = null;
Lear n about tvariable he EJB 2 .0key specif icatvalue ion // environment and - must exist in bean environment Dev elop EJBs w it h St u dio App licat ion Dev eloper final String DB_DATASOURCE_KEY = "bmp/dbdatasource"; Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio // statements AppSQL licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. final String SELECT_SQL = "SELECT T1.CUSTOMERID, T1.INFOID, T1.DESCRIPTION, Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an T1.DATA FROM ITSO.CUSTOMERINFO T1 un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as aWHERE p art ofT1.CUSTOMERID Jav a 2 En t er p rise = Ed ? AND it ion T1.INFOID ( J2 EE) and cover = ?";t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0String final sp ecif icat INSERT_SQL ion t hat pr ov = id"INSERT e, for ex am INTO ple,ITSO.CUSTOMERINFO en hanced fun ct ion alit(CUSTOMERID, y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. INFOID, DESCRIPTION) VALUES (?, ?, ?)"; Ifinal n Part String 2, we in tUPDATE_SQL r oduce t h e sam p le bank ing applicat ion and t henSET imp DESCRIPTION lem ent en t it y beans, = "UPDATE ITSO.CUSTOMERINFO = ?, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif ferDATA en t m = app g st r atCUSTOMERID egies, and sim clientINFOID s t h at u = se ?"; t he EJBs. At ? inWHERE = p?le AND t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. final String DELETE_SQL = "DELETE FROM ITSO.CUSTOMERINFO WHERE CUSTOMERID = ? AND INFOID = ?";
Initialization When t he persist er in st ance is creat ed it get s t h e JNDI n am e of t he d at a sour ce fr om t he env iron ment v ariab le and t hen ret r iev es t he dat a sou rce it self .
publicCustomerInfoBmpPersister() { initializeResources(); } •
Table of Content s
•
I ndex
public void initializeResources() { EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
try {
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
InitialContext ctx = new InitialContext(); Pub lish er: I BM
String Pub Dat e: April 1dbDatasource 1, 20 03
= (String)
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
ctx.lookup("java:comp/env/"+DB_DATASOURCE_KEY);
ds = (DataSource)ctx.lookup(dbDatasource); System.out.println("BMP persister datasource: "+dbDatasource); } catch (Exception e) { e.printStackTrace(); } Lear n about t he EJB 2 .0 specif icat ion }
Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Returning the data source App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (The EJB)d at arachsour it ectce u re, d dned ep loym ent of su plicat ion sa t connect o a WebSph is ran et ur t o callback mch et hap ods t o get ion. er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. private DataSource getDatasource() throws SQLException { I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he if (ds == null ) throw new SQLException("Data source is null"); EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or else return ds; successful imp lem ent at ions of EJBs. } I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. The callback m et hods can get a connect ion using:
Connection con = getDatasource().getConnection();
Freeing resources When t he BMP ent it y b ean is r emov ed, t his m et hod is called t o f ree t h e r esour ces.
public void freeResources() { System.out.println("BMP persister free resources"); ds = null; •
Table of Content s
} •
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Create state Pub lish er: I BM Pub Dat e: April 1 1, 20 03
The createState m et hod is called by t he ejbCreate m et hod of t h e bean wh en a new inst ance is I SBN: 0- 73 84- 26 09 -1 creat ed. Pages: 75 0
public void createState(CustomerInfoKey key) throws SQLException { System.out.println("BMP persister createState"); Lear n about t he EJB 2 .0 specif icat ion Connection con = null; Dev elop EJBs w it h St u dio App licat ion Dev eloper PreparedStatement ps = null; Deploy EJBs t o W eb Sp her e Applicat ion Ser v er try { This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licatcon ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans = getDatasource().getConnection(); ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. ps = con.prepareStatement(INSERT_SQL); Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. ps.setInt(1, key.customerID); I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep tps.setInt(2, s and t h e ar ch itkey.infoID); ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed ps.setString(3, ""); // description per sist ence ent it y b ean s and m essagedr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. // data is left as null I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session result beans, int an d m essag e-=d rps.executeUpdate(); iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At e) { EJB throw e; }ion s t o a WebSph er e App licat ion Serv er. t he }encatch d, w e d(SQLException escrib e how t o deploy ap plicat finally { if (ps
!= null) ps.close();
if (con != null) con.close(); } }
Find by primary key The findPrimaryKey m et hod is called by t he ejbFindByPrimaryKey m et hod of t h e bean wh en an ex ist ing bean is locat ed. •
Table of Content s
• I ndex findPrimaryKey(CustomerInfoKey key) throws SQLException { public boolean EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
System.out.println("BMP persister findByPrimaryKey"); By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss Connection
Pub lish er: I BM
con = null;
Pub Dat e: April 1 1, 20 03
PreparedStatement ps
= null;
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0 ResultSet
rs
= null;
boolean result = false; try { con = getDatasource().getConnection(); Lear n about t he EJB 2 .0 specif icat ion Dev EJBs w it h St u dio App licat ion Dev eloper pselop = con.prepareStatement(SELECT_SQL); Deploy EJBs t o W eb Sp her e Applicat ion Ser v er ps.setInt(1, key.customerID); This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio ps.setInt(2, key.infoID); App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) arrs ch it=ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. ps.executeQuery(); Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an result = rs.next(); un derly ing r elat ional dat ab ase. } catch (SQLException e) { throw e; } I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he finally { EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or if (rs != null) rs.close(); successful imp lem ent at ions of EJBs. (psin t r!= null) ps.close(); I n Part if 2, we oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem if ent (con fin der!= m etnull) h ods, con.close(); dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. } return result; }
Load state The loadState m et hod is called by t he ejbLoad m et hod of t h e bean wh en ev er t he bean's dat a
has t o be ret r iev ed fr om t h e dat abase.
public void loadState(CustomerInfoBmpBean bean) throws SQLException { System.out.println("BMP persister loadState"); •
Table of Content s
CustomerInfoKey key = (CustomerInfoKey)
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
bean.getEntityContext().getPrimaryKey();
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Connection
con = null;
Pub lish er: I BM
PreparedStatement Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
ResultSet Pages: 75 0
ps
= null;
rs
= null;
try { con = getDatasource().getConnection(); ps = con.prepareStatement(SELECT_SQL); Lear n about t he EJB 2 .0 specif icat ion ps.setInt(1, key.customerID); Dev elop EJBs w it h St u dio App licat ion Dev eloper ps.setInt(2, key.infoID); Deploy EJBs t o W eb Sp her e Applicat ion Ser v er rs = ps.executeQuery(); This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans rs.next(); ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. bean.setCustomerID( ); on a simp le bank ing app licat ion w it h an Thr oug hout t he book , w e pr ov idrs.getInt(1) e exam ples based un derly ing r elat ional dat ab ase. bean.setInfoID( rs.getInt(2) ); I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep tbean.setDescription( s and t h e ar ch it ect ur e. I nrs.getString(3) par t icu lar w e point); out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed bean.setData( per sist ence ent it y b ean srs.getBytes(4) and m essage- dr iv); en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. } catch (SQLException e) { throw e; } I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session finally beans, an d m{essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At != enull) t he en d,ifw e(rs d escrib how t ors.close(); deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. if (ps
!= null) ps.close();
if (con != null) con.close(); } }
Store state The storeState m et hod is called by t he ejbStore m et hod of t h e bean wh en ev er t he bean's dat a m ust b e saved t o t he dat ab ase.
public voidTable storeState(CustomerInfoBmpBean bean) throws SQLException { • of Content s •
I ndex
EJB 2System.out.println("BMP . 0 D e ve lopm e nt w it h W ebSphe r e persister St udio Appl icastoreState"); ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Connection
con = null;
Pub lish er: I BM PreparedStatement
ps
= null;
Pub Dat e: April 1 1, 20 03
tryI SBN: {
0- 73 84- 26 09 -1
Pages: 75 0
con = getDatasource().getConnection(); ps = con.prepareStatement(UPDATE_SQL); ps.setString(1, bean.getDescription()); Lear n about t he EJB 2 .0 specif icat ion ps.setBytes(2, bean.getData()); Dev elop EJBs w it h St u dio App licat ion Dev eloper ps.setInt(3, bean.getCustomerID()); Deploy EJBs t o W eb Sp her e Applicat ion Ser v er ps.setInt(4, bean.getInfoID()); This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licatps.executeUpdate(); ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. } catch (SQLException e) { throw e; } Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat finally { ional dat ab ase. I n Part if 1, we oduce EJBsps.close(); as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic (psin t r!= null) concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0if sp ecif icat!= ion null) t hat pr con.close(); ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed (con per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. } I}n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Delete state The deleteState m et hod is called f rom t he ejbRemove m et hod of t h e bean wh en an inst ance is delet ed .
public boolean deleteState(CustomerInfoBmpBean bean) throws SQLException { System.out.println("BMP persister deleteState");
CustomerInfoKey key = (CustomerInfoKey) bean.getEntityContext().getPrimaryKey(); Connection
con = null;
PreparedStatement ps • •
= null;
Table of Content s
int result; I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
try {
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
con = getDatasource().getConnection(); Pub lish er: I BM Pub Dat e: 1, 20 03 psApril = 1con.prepareStatement(DELETE_SQL); I SBN: 0- 73 84- 26 09 -1
ps.setInt(1, Pages: 75 0
key.customerID);
ps.setInt(2, key.infoID); result = ps.executeUpdate();
// is 0 for successful delete
} catch (SQLException e) { throw e; } Lear n about t he EJB 2 .0 specif icat ion finally { w it h St u dio App licat ion Dev eloper Dev elop EJBs if EJBs (ps t o!= null) Deploy W eb Sp herps.close(); e Applicat ion Ser v er if (con pr !=ovides null) This I BM Redbook d etcon.close(); ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) } ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr ougreturn hout t he(result book , w e!=pr 0); ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. } I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
Not e
I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, e- d r iv en beans using er e u dior ow Ap plicat elop er . We Notan e dt hm atessag t h e createState m et hod WebSph on ly adds a St t able w it h tion he Dev customerID an also d im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At infoID. Th e description is set t o an emp t y st r in g and t he data colum n is left null ( it t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. is not set in t he SQL inser t st at em en t ) .
Generating the deployed code The BMP ent it y b ean and it s p er sist en t im plement at ion is com plet e and w e can generat e t he deploy ed code. Select t he ItsoBank5BmpEJB p r oject and Gener at e - > Deploy and RM I C Code ( cont ex t ) . •
Table of Content s
•This act ion gener I ndexat es t he d ep loy ed cod e for b ot h t he CMP an d t h e BMP en t it y bean. For t h e CMP EJB e ve lopm ntault w it h tW ebSphe Stapp udioinAppl De vis el oper ent it2 y. 0 bDean , a deef opdow nr e m g t oicaati on t able gener at ed. We can use t h ese d ef init ions t o creat e t he CUSTOMERI NFO t able. By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Creating the database table You can u se t he Dat a per sp ect iv e t o define d at abases, schem as, and t ables. We could define t he CUSTOMERINFO t able fr om scr at ch . How ever , it is easier t o u se t he skelet on definit ions t hat t he deploy m en t st ep generat ed. •
Table of Content s
Op • en t he DatI a ndex p er spect ive an d ex pan d t he ItsoBank5BmpEJB p r oject in t he D at a Def in it ion v iew EJB 2 . 0 eD 13 e ve-lopm ( Figur 15) .e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Figu r e 1 3 -1 5 . Da t a de f ini t i on cr ea t e d by t op- dow n m appi ng Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Op en t he NULLID.CUSTOMERINFOCMP t able. On t he Colu m ns pag e: This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans Ch ange t he lengt h of t he DESCRIPTION colum n t o 50 ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Ch ange t he dat a t y pe of t h e DATA colum n t o BLOB Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. Ch ange t he lengt h of t he DATA colum n t o 10 ( leav e t he LOB mu lt ip lier as K, so t he act u al lengt h is 10000) I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he Sav e t he t able definit ion . Not e t h at y ou can not ch ange t he t able n am e. EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
Generate the DDL
I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, m essag e- d r iv en beans using WebSph er e St Ap(plicat iont ) .Dev er . We Select tan hedNULLID.CUSTOMERINFOCMP t able and Gener atuedio DDL cont ex I n elop t he Gen er atalso e SQL im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At DDL w ind ow ( Figur e 13 - 16) : t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Figu r e 1 3 -1 6 . Gen er a t i ng t h e DD L f or t he t abl e
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Select Gener at e associat ed d rop st at em en t s ( so t hat w e can r er un t he D DL) . Select Op en SQL DDL file f or edit in g w hen done . Lear n about t he EJB 2 .0 specif icat ion Click Fin ish. Dev elop EJBs w it h St u dio App licat ion Dev eloper The g en er at ed DDL f or t he t able can b e m odified t o suit our r equir ement ( Figur e 13 - 17) . Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Figu r e 1 3 -1 7 . Gen er a t e d DDL a nd m odif icat ion s This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. DROP CUSTOMERINFOCMP; ==> ing ITSO.CUSTOMERINFO Thr ougTABLE hout t he book , w e pr ov id e exam ples based on a simp le bank app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic CREATE ITSO.CUSTOMERINFO concep t sTABLE and t h CUSTOMERINFOCMP e ar ch it ect ur e. I n par t icu lar w e point out t he n ew==> fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed NOTm essageNULL, dr iv en beans. We also pr ovid e best pr act ice guidelines f or per(CUSTOMERID sist ence ent it yINTEGER b ean s and successful imp lem ent at ions of EJBs. INFOID INTEGER NOT NULL, I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essagVARCHAR(50), e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also DESCRIPTION im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he DATA en d, wBLOB(10K)); e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
ALTER TABLE CUSTOMERINFOCMP
==> ITSO.CUSTOMERINFO
ADD CONSTRAINT PK_CUSTOMERINFOCMP PRIMARY KEY (CUSTOMERID, INFOID);
Run the DDL
Select t he CUSTOMERINFOCMP.sql f ile and Ru n on Dat ab ase Ser v er ( con t ex t ) . This st ar t s t he Run Scrip t w ind ow:
Select t he st at em en t s t o r u n ( cr eat e and alt er) , and click Next . • •
Select Com m it changes on ly u pon su ccess, an d click Next . Table of Content s
I ndex I n t he Dat abase Conn ect ion panel, leave t he conn ect ion nam e as is, bu t set t h e dat ab ase
EJB 2 . 0nam D e ve e nt w it h WClick ebSphe r e ish. St udio Appl ica ti on De v el oper e lopm t o EJBBANK. Fin By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
This act ion execut es t he DDL and t he CUSTOMERINFO t able is defin ed in t he EJBBANK d at abase. Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
Not Pages: e 75 0 The D DL ( Ex am ple 10 - 1) and SQL ( Ex am ple 10 - 2) jobs list ed in Ch apt er 10 , " I nt r odu cin g an d p r ep aring f or t he sam ple ap plicat ion " on pag e 36 9 d ef ine and load t he CUSTOMERINFO t able. This sect ion illu st rat es h ow a new t able could be defined fr om App licat ion Developer . Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper
Load sample data Deploy EJBs t o W eb Sp her e Applicat ion Ser v er You se a few pr SQL st at em s ininafor Dm B2atcom t oively load use samp le dathere a: St udio This can I BMuRedbook ovides d etent ailed ion m onand h oww indow t o ef fect WebSp App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. db2 connect ejbbank Thr oug hout t hetobook , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. db2 insert into itso.customerinfo (customerid, infoid, description, data) I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic 1,I n'Picture', concep t s and values t h e ar ch(101, it ect ur e. par t icu lar w eblob('xxxxxxxxxx') point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed db2 insert into description, per sist ence ent it y bitso.customerinfo ean s and m essage- dr (customerid, iv en beans. We infoid, also pr ovid e best pr act icedata) guidelines f or successful imp lem ent at ions of EJBs. values (101, 2, 'Scanned data', blob('yyyyyy') I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, db2 select an d mcustomerid, essag e- d r iv en infoid, beans using description, WebSph er e St substr(data,1,10) u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e dfrom escribitso.customerinfo e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Testing the BMP entity bean The ItsoBank5BmpEJB p r oject is par t of t he ItsoBank5EAR ent er pr ise applicat ion and at t ached t o t he EjbbankServer. St ar t t he serv er and w ait for t h e r ead y m essage. •
Table of Content s
•
I ndex
Testing the BMP with the universal test client EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
When t he ser ver is r eady , select t h e ser v er in t h e Serv ers view and Ru n u niver sal t est client ( cont ex t ) . Pub lish er: I BM PubisDat 1, 20scenar 03 Here a e: smApril all 1t est io: I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Select t he JNDI Ex plorer . Ex pan d ejb - > it sobank an d select t he Cu st om er I nf o b ean ( t h is is t he BMP ent it y b ean ) . Ex pan d t he hom e an d select t he findByPrimaryKey m et hod . Lear n about t he EJB 2 .0 specif icat ion Select t he par am et er and en t er 1 an d 101 as key v alues for infoID an d customerID. Click IDev n vok e. EJBs w it h St u dio App licat ion Dev eloper elop The BMP inst tance is Sp r ether r iev an d y ion ou can Deploy EJBs o W eb e ed Applicat Ser vclick er Wor k w it h Object t o add t h e r esult t o t h e Refer ences ( Figur e 13 - 18) . This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or trhe t of iap ion sPbased on tthest e Entcli er pr Figu e d1ev 3 elopm -1 8 . en Test ngplicat a BM i n t he enise t Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Ex pan d t he inst ance, t hen r un some of t he get t er m et hods t o see t he d at a. I n t he hom e r efer en ce, select t he create m et hod :
1.
1. En t er valu es f or t h e key f ields, f or exam p le, 102 an d 1. Click I n vok e. 2. A n ew inst ance is cr eat ed. 3. Click Wor k w it h Object . Ex pan d t he new in st an ce ref er ence: • •
Table of Content s
1. I nvI ndex oke t he getDescription an d getData m et hod s.
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
2. The d escr ipt ion field is an em pt y st r ing and t he d at a f ield is nu ll.
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Select t he setData m et hod : Pub lish er: I BM Pub Dat e: April 1 1, 20 03
1. Ex pan d t he par am et er .
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
2. Click Add t w ice t o add ind ividu al by t es. 3. En t er som e v alues ( nu m er ic 0- 1 27) 4. Click I n vok e ( Figur e 13 - 19) .
Lear n about t he EJB 2 .0 Figu specifricat e 1ion 3 -1 9 .
Usi ng a set t er m et hod
Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session Ruan nd t he setDescription m etusing hod t oWebSph add a der escr iput ion t explicat t. beans, m essag e- d r iv en beans e St dio Ap ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At br oweser w ind and EJB st opap t he serv ers. t o a WebSph er e App licat ion Serv er. t he enClose d, w et he d escrib how t o ow deploy plicat ion
Read-only methods I f you check t he Console view , y ou can see t est m essages fr om t he per sist er class. Fr om t h is out p ut you can see t h at ev en a get m et hod inv okes b ot h t he loadState an d t he storeState m et hod of t he p er sist er class, and t her ef or e t he t ab le is upd at ed f or ever y m et hod. •
Table of Content s
•For CMP ent itI ndex y beans, t he cont ainer is sm ar t enou gh t o u pdat e t he t ab le only w hen t he dat a has EJB 2 . 0mDod e ve been iflopm ied . e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
For BMP en t it y beans, w e hav e t o im plem ent t his ou rselves. Pub lish er: I BM Pub Dat e: April 1 1, 20 03
Making the BMP entity bean smarter I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Becau se we im p lem ent t he per sist ent logic, w e hav e t o add t he code t o m ak e t he BMP ent it y bean smar t er and n ot up dat e t he t able u nnecessar ily . We ad d a dir t y f ield t h at is set w henev er t he b ean dat a is up dat ed. I n t he ejbStore m et hod , w e t hen check t he dir t y f lag t o see if a t able u pdat e is r eq uired. about t he EJB 2 .0 specif icat ion Op en Lear t he nCustomerInfoBmpBean class: Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy Defin e aEJBs f ield:t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat Developer // ion dirty flag f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. boolean dirty = true; Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part we tinhe t r oduce EJBs ashod a ptart of Jav Ent yt erf lag p rise Ch1, ange ejbLoad m et o r eset t hae2dir : Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or persister.loadState(this); successful imp lem ent at ions of EJBs. dirty = false; I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he enCh d,ange w e dtescrib e how t omdeploy ap plicat WebSph he ejbStore et hod tEJB o t est and r ion esets tt ohea dir t y f lag:er e App licat ion Serv er.
if (dirty) persister.storeState(this); dirty = false;
Ch ange t he ejbCreate m et hod t o r eset t h e dir t y f lag :
persister.createState(key); dirty = false; return key;
• •
Table of Content s
Ch ange t he fou r set m et hods t o set t h e dir t y flag : I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli public Wah li, Wout er Denay er, Lar s Schu nk , Deborah{ Shaddon , Mart in Weiss void setXxxxxx(.....)
this.xxxxxxxxx Pub lish er: I BM
= ......;
Pub Dat e: April 1 1, 20 03
dirty = true;
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
}
I f you r eru n t he t est w it h t he u niv er sal t est clien t , y ou can see t hat t he storeState m et hod is not inv oked for get t ers. Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Summary As w e m ent ioned in t he int r odu ct ion :
•
Tr y t o use Table CMP of Content w henever s y ou can .
•
I ndex
evlopm er , et her arWeebSphe st ill sitr eu St atudio ionsAppl w her devDeeloper s EJB 2 . 0How D e ve nt weit h icaeti on v el oper By
w ill be f or ced t o develop BMP ent it y beans. Because t he on ly m ean in gf ul d if fer en ce bet w een BMP an d CMP com pon en t s is w ho Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss pr ov ides t he p er sist en ce log ic, BMPs off er quit e a good deal of f lex ibilit y in developing com ponent s w it h in t h e J2EE ar chit ect ur e, w hen f un ct ion alit y is not cov ered by CMP. Pub lish er: I BM Pub BMP Dat e:are Aprilquit 1 1, e20d03 if ficult
and long t o w r it e. Wit h abst r act ion s and helper classes t h at w e int r odu0-ced in26t09 he-1code ( env ir onm en t v ar iables fr om t h e deploy ment descript or, separ at ing I SBN: 73 84tPages: he JDBC 75 0 cod e in a per sist er helper class, sub classing a CMP 2. 0 bean class) , it is not t oo dif ficult .
Pros and cons of BMP entity beans t he EJB specif Aft er Lear seeinngabout how BMPs are2 b.0uilt , h ericat e arion e som e point s t hat cou ld h elp y ou m ak e a d ecision. Dev elop EJBs w it h St u dio App licat ion Dev eloper
Advantages Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Basically , y ou h ave any persist en tplicat st or e. App licat ion Developer f or access t he d evt oelopm en t of ap ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Op t imizat ion can b e don e for each access. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase.
Disadvantages
I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed You hav e t o wr it e a lot of code. per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful ent at ions of : EJBs. Dat a imp sourlem ce dependency I t is diff icu lt t o isolat e t h e bean fr om a sp ecific d at a sour ce ( un lik e CMP, w hich allow s y ou t o sw it ch f r om one D B t o anot h er an d ev ent ually f rom I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session v en dor t o v en dor ) . beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Access intent
Access int ent can b e specif ied for CMPs t o opt im ize t h e access in t h e EJB cont ainer . For BMPs, access int ent can also b e specif ied . How ev er, it is u p t o t he bean d ev eloper t o m ak e use of t h e access int ent specif icat ion .
Not e
From t he d ocu ment at ion of access in t en t :
• • EJB 2 . 0
How ev er , t her e ar e st ill sit u at ions w her e dev eloper s w ill be f or ced t o develop BMP ent it y b ean s. Because t h e only m eaningf ul d iff er ence bet w een BMP and CMP com ponent s is wh o p rov ides t he persist en ce log ic, BMPs should be ab le t o lever age access int ent hin t s ju st t he sam e as t he WebSph er e Ap plicat ion Ser ver does on behalf of CMPs. BMPs t hat use t h e access int ent mechan ism w ill of Content s parTable t icipat e in applicat ion pr ofiling; t hat is, t he v alue of t h e access int ent I ndex at t r ibut es m ay d iff er f r om r eq uest t o requ est , allow ing t h e BMP t o seam lessly D e vemlopm e ntitwsitper h Wsist ebSphe r e st Strat udio odify ence egAppl y. ica ti on De v el oper
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Dev eloper s m ay t her efor e app ly access int ent policies t o BMP m et hods as w ell as CMP m et hods. Th er e is, of cour se, n o ob lig at ion f or a BMP t o ex ploit access int ent Pub lish er: I BM hint s, as t hey ar e not cont r act ual in nat u re. I t is in f act ex pect ed t hat BMP Pub Dat e: April 1 1, 20 03 dev elop er s w ill not use ev er y access int ent at t r ibu t e, but only t h e ones t hat ar e I SBN: 0- 73 84- 26 09 -1 im por t ant t o a par t icular BMP. Pages: 75 0
The cu rr ent access int ent policy is b ound int o t he java:comp n am espace f or each par t icular BMP. That policy is cur r ent only for t he d ur at ion of t h e m et hod call du rin g w h ich t he access in t ent p olicy w as r et r iev ed. Th e developer w ill m ost likely cach e t he access t yp e dur ing ejb Load so t hat t h e appr opr iat e act ions can be t aken du rin g ej bSt or e. Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Chapter 14. Session bean development I n t his chapt er , w e d escrib e how t o dev elop session beans using App licat ion Developer . We d ev elop bTable ot h aof st at eless and a st at ef ul session for t he b ank in g scenar io. Bot h session b ean s • Content s use t h e CMP en t it y beans t hat we developed ear lier t o u pdat e t he dat abase. •
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Finally, w e develop a Web ser vice f rom a session bean. Th is Web serv ice can b e used in SOAP By Ueli sWah Wout er Denay Lar sof Schu Deborah Shaddon client t o li,p er f orm t he er, logic t hnk e ,session b ean . , Mart in Weiss Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Design We d ev elop a st at eless session b ean ( Banking) t o serv e as session facade for t h e I TSO b ank bu sin ess logic. This b ean ex poses sev er al m et hod s t hat allow a clien t t o per for m b ank in g t r ansact ions, such as ask ing f or t he b alance, deposit in g an d wit h dr aw ing fun ds, t r ansf er ing •fu nds fr om one Table of Content accoun t t o sanot h er , an d list ing t he t ran sact ion r ecor ds. •
I ndex
We 2also EJB . 0 D edve evlopm elop e nt a wstitat h ef W ebSphe ul session r e St udio beanAppl ( Reports) ica ti on De vfor el oper r epor t ing pu rp oses. This b ean enables a client t o ru n m u lt ip le repor t s wit h out losing t h e con t ext , t hat is t he cust om er and account By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss nu mb er s. Th is bean u ses EQB- QL qu er ies t o r et r iev e cust om er and accou nt in for m at ion. Pub lish er: I BM Thr oug hout t he book , sev eral client s w ill u se t hese session beans: Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
Serv let75clien t —" Dev elopin g a ser v let client " on p age 59 0 Pages: 0 St r ut s client —" Dev elopin g a St r ut s- based W eb client " on p age 60 9 Messag e- d riv en b ean —"Dev elopin g a m essage- dr iven bean client " on p age 62 4 GUI client —" GUI client using access beans" on p age 63 4 Lear n about t he EJB 2 .0 specif icat ion Web ser vice clien t —" Client usin g a Web ser vice" on p age 63 9 Dev elop EJBs w it h St u dio App licat ion Dev eloper Anot h er possib le client is a Jav a ap plet bu t w e d o not elabor at e on t his Deploy EJBs t o W eb Sp her e Applicat ion Ser v er The session bean m et h ods w ill only b e exp osed in t he r em ot e in t er f ace of t he session beans. This BM d et in for m at ion ton h ow t go aef local fect ively use St udio They I ar e,Redbook aft er all, pr ouovides r gat ew ayailed f or out side clien s. Addin int erf aceWebSp could here b e useful if a App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans m essage- dr iv en bean is used as an access p oint and bet t er per for m ance is r eq uir ed . ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Figur e 14 - 1 sh ows t he b asic d esign of t he session beans. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase.
Figu r e 1 4 -1 . Sessi on fa ca de de si gn
I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Home factory Befor e st ar t ing t his p art , mak e sur e y ou hav e comp let ed t he set up of t h e ut ilit y JAR file w it h t he hom e f act ory class as d escrib ed in " Ut ilit y pr oject " on p age 39 2. W e w ill mak e u se of t h e hom e fact or y in t he session bean.
Session bean exceptions Ther e ar e a n um ber of except ions t he session can possibly t hr ow t o it s clien t s. One is t he InsufficientFundException ( see " Cr eat ing an ex cep t ion class" on p age 41 2) and t he ot hers ar e EJB ex cept ions, f or exam ple w h en an accou nt is not f oun d or a t r ansact ion r ecord cann ot be creat ed. •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Developing a stateless session bean I n t his sect ion, w e develop t he Banking session bean. The seq uence of oper at ions is:
•
Table of Content s
Cr eat e aI ndex p r oject f or session EJBs. We could use t he sam e pr oj ect as for t he CMP ent it y beans, b ut usin sep ar at oject mica akties EJB 2 . 0 D e ve lopm e nt w itgh a W ebSphe r eeStpr udio Appl on it Deeasier v el opert o nav igat e t h e code in Applicat ion Dev eloper . •
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Cr eat e t he session bean using t he EJB w izar d. Pub lish er: I BM
Defin EJB1 1, refer ences Pub Dat e:eApril 20 03
t o access t h e CMP en t it y beans.
I SBN: 0- 73 84- 26 09 -1
I m plem ent t he b usiness log ic. Pages: 75 0
Creating an EJB project for the session bean We ad d t he session b ean t o a n ew EJB mod ule nam ed ItsoBank5SessionEJB. To cr eat e t h e EJB m odule anndabout lin k it t the o tEJB he ex ist in g ItsoBank5EAR ent er pr ise applicat ion, d o t h e follow ing: Lear 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Select - > tNew - >SpProj - > EJBion - > Ser EJBv er Proj ect . Click Next . DeployFile EJBs o W eb herect e Applicat Cr eat e 2. EJB Prdoject , anindfor click . h ow t o ef fect ively use WebSp here St udio This ISelect BM Redbook pr0ovides et ailed m atNext ion on App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans er it ItsoBank5SessionEJB asoft he ect namion e, sselect Ex ist ingerf or t h e licat en t erp ( EJB) En artch ect u re, an d d ep loym ent su pchrojap plicat t o a WebSph e App ionrise Serv er. app licat ion, and click Br ow se t o select ItsoBank5EAR. Click Next . Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Foring m odule d ep en dencies, un derly r elat ional dat ab ase. select t he ItsoBank5CmpEJB.jar an d t he ItsoBank5Utility.jar f iles. Th e session b ean r equir es access t o t h e CMP ent it y beans and t o 1, t hewe h ome f act or EJBs y. I n Part in t r oduce as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2.Click 0 spFin ecifish. icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or You get a pop - up m essage t h at t h e pr oject is ad ded t o t h e EjbbankServer ser ver successful imp lem ent at ions of EJBs. conf ig ur at ion ( see Figur e 10 - 16 on p age 39 4) . Click OK. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session The p r oject is cr eat ed. You can v er ify t he pr oj ect dependencies by op en in g t he pr oj ect beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also pr oper t ies. im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Creating the session bean for banking transactions I n t he J2 EE Hier ar ch y v iew ( J2EE per sp ect iv e) , select t h e ItsoBank5SessionEJB p r oject and New - > Ent er pr ise Bean ( con t ex t ) :
The p r oject is pr eselect ed , so click Next . Select Session b ean , ent er Banking as t he n ame, and itso.bank5.session as t he p ack age ( Figur e 14 - 2) . Click Next .
Figu r e 1 4 -2 . Cr ea t i ng a se ssi on b ea n
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion I n t he det ails pan el ( Figur e 14 - 3) : Dev elop EJBs w it h St u dio App licat ion Dev eloper
Figu r e 1 4 -3 . D et ai ls of t h e Ba nk in g se ssion be a n
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
1. Ch ange t he EJB bind in g nam e t o ou r conv en t ion: ejb/itsobank/Banking. 2. Select on ly Rem ot e clien t v iew ( we access t he session bean fr om client s) .
2. 3. Click Fin ish. Thr ee classes ar e generat ed int o t he itso.bank5.session p ackag e: BankingBean— The abst r act b ean , w it h callback m et hods ( ejbCreate, . . . ) • •
BankingHome— The r semot e hom e int er face w it h a create m et hod Table of Content I ndex
The e com en tAppl in tica er ftiace, y f or EJB 2 . 0Banking— D e ve lopm e nt w itrhemot W ebSphe r e pon St udio on Deem v elpt oper
now
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Accessing CMP entity homes Pub lish er: I BM Pub Dat e: April 1 1, 20 03
The Banking session bean r eq uires access t o t he CMP en t it y beans. I n t his r egar d, t h e session I SBN: 0- 73 84- 26 09 -1 bean is a clien t of t h e ent it y beans. For p er f orm ance r easons, t he session bean uses t he local Pages: 75 0 int er faces t o access t he ent it y b ean s. Session beans m ust acq uir e t he local h ome in t erf aces of t h e ent it y beans using t heir JNDI nam es. Ther e are a nu m ber of w ays t o im plement t his: JNDI e session b ean code. Lear naccess about tinhet hEJB 2 .0 specif icat ion The JNDI nam es of t he en t it y beans can be h ard coded or st ored in env ir onm ent v ar iables or pr oper t y f iles. Dev elop EJBs w it h St u dio App licat ion Dev eloper Access t hr ou gh a ut ilit y class, su ch as t he hom e f act ory . Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Since a J2EE 1. 2 client should access EJBs usin g EJB r efer en ces ( local JND I nam es) , t hese EJB rThis ef erences I BM Redbook ar e m apped pr ovides in t he d etdeploy ailed in mfor en m t dat escrip ion on t orh ow ext ensions t o ef fecttively o t h euse global WebSp JNDI here namStes udio used App in t he licat n amin ion Developer g serv er . f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he references book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Defining EJB un derly ing r elat ional dat ab ase. The Banking bean r eqas uires access t o at he Account CMP enttithe y beans. I n Part 1, wesession in t r oduce EJBs a p art of Jav 2 En t er p risean Edd itTransRecord ion ( J2 EE) and cover basic The Account b ean is u sed t o r u n t r an sact ions, an d t h e TransRecord b ean is u sed t o cr concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced eat w iteh new t he tEJB r ansact ionecif r ecord s. t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed 2. 0 sp icat ion per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or EJB r ef er ences ar eent defined deploy m en t d escr ip t or edit or : successful imp lem at ionsinoft he EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, e- d r iv endescr beans er e St u dio Ap plicat pion Dev.elop er . We also Opan end tm heessag deploym ent iptusing or of tWebSph he ItsoBank5SessionEJB r oject im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he enOn d, twhe e dRefer escrib how t oe,deploy plicat ion s t o an a WebSph er e. App licat ion Serv er. eneces pag select EJB t he ap Banking b ean d click Add I n t he Add Ref er ence w indow , select EJB local r eference an d click Next . The Ad d EJB Local Refer en ce win dow op en s ( Figur e 14 - 4) . Th e easiest w ay t o fill in all t he fields is t o click Br ow se f or Lin k. I n t h e Link Select ion w ind ow, select En t er p rise bean in diff er ent EJB pr oject an d locat e t he Account b ean . Click OK.
Figu r e 1 4 -4 . D ef in in g a n EJB r e f er e nce
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Click Fin ish t o add t h e r ef er ence t o t he Banking b ean . Repeat t his pr ocess t o def in e r ef er ences for t he TransRecord an d t he Customer b ean . Figur e 14 - 5 sh ows t he d ep loy m ent d escr ip t or w it h t he EJB r efer en ces. Lear n about t he EJB 2 .0 specif icat ion
eit1h 4St-5 . EJB r ef ion er eDev n ces in Dev elopFigu EJBsrw u dio App licat eloper
t h e dep loy me n t d escr ip t or
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Sav e t he d ep loym ent descr ipt or .
Using EJB references Using t h e EJB ref er ences, t he Bank ing session b ean can access t he CMP ent it y beans using t h is coding :
InitialContext initCtx = new InitialContext(); AccountLocalHome accthome = (AccountLocalHome)initCtx.lookup("java:comp/env/ejb/Account");
•
Table of Content s
•
I ndex
By using t h e hom e fact or y , t he cod e can b e w rit t en as: EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
import itso.bank5.utility.HomeFactory; Pub lish er: I BM
......
Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
AccountLocalHome accthome = Pages: 75 0
(AccountLocalHome)HomeFactory.singleton().getHome("ejb/Account");
Preparing methods for entity home access Lear n about t he EJB 2 .0 specif icat ion Op en Dev t he elop BankingBean t o App define t ilitDev y met h ods f or en t it y hom e access: EJBs w it h class St u dio licatuion eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er e t hr ee fields f or tdhe local in h ome in tion erf aces: This IDefin BM Redbook pr ovides et ailed for m at on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. // home fields Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an private accountHome = null; un derly ing r elatAccountLocalHome ional dat ab ase. private = null; I n Part 1, we inTransRecordLocalHome t r oduce EJBs as a p art oftrecordHome Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he CustomerLocalHome = fun null; EJB 2.private 0 sp ecif icat ion t hat pr ov id e, for excustomerHome am ple, en hanced ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part Defin 2, we e a in mt et r oduce hod t ot hret e sam r iev p e le t h bank e hom ing es:applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At void getHomes() { ap plicat ion s t o a WebSph er e App licat ion Serv er. t he enprotected d, w e d escrib e how t o deploy EJB try { // InitialContext initCtx = new InitialContext(); // accountHome // // trecordHome
= (AccountLocalHome)initCtx.lookup ("java:comp/env/ejb/Account"); = (TransRecordLocalHome)initCtx.lookup
//
("java:comp/env/ejb/TransRecord");
// customerHome = (CustomerLocalHome)initCtx.lookup // accountHome •
Table of Content s
•
I ndex
("java:comp/env/ejb/Customer"); = (AccountLocalHome)HomeFactory.singleton() .getHome("ejb/Account");
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
trecordHome
= (TransRecordLocalHome)HomeFactory.singleton()
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
.getHome("ejb/TransRecord"); Pub lish er: I BM
customerHome Pub Dat e: April 1 1, 20 03
= (CustomerLocalHome)HomeFactory.singleton()
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
.getHome("ejb/Customer");
} catch (NamingException ex) { ex.printStackTrace(); throw new EJBException("Error looking up Account home: " Lear n about t he EJB 2 .0 specif icat ion +ex.getMessage()); Dev elop EJBs w it h St u dio App licat ion Dev eloper } Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I}BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Not e t htat e m ,etwheodprin des as com s ton he acode t oleaccess t h eapp hom esion dir ect Thr oug hout het hbook ovclu id e exam plesment based simp bank ing licat w itly h .an How ev er , t he act u al code r elies on t he hom e fact or y class. un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic I nv oke t he getHomes m et hod in t he ejbCreate m et hod : concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or public void ejbCreate() throws javax.ejb.CreateException { successful imp lem ent at ions of EJBs. I n Part 2,getHomes(); we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also } ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At im plem t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Add t h ese im por t st at em ent s:
import itso.bank5.cmp.*;
// CMP entity beans
import itso.bank5.exception.*;
// Exception for withdraw failure
import javax.ejb.*;
// EJB exceptions
import javax.naming.*;
// JNDI access
•
import java.math.BigDecimal;
// Business logic parameters
import java.util.*;
// Vector, Collection
import itso.bank5.utility.HomeFactory;
// Home factory
Table of Content s
•
ndex Now w e ar e rI eady t o imp lem ent t he b usin ess log ic met hod s. EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Implementing the business logic Pub lish er: I BM Dat e: April 1 1, 20 03 ThePub Banking session bean is t he f acade t o hide t h e CMP ent it y beans and t he d at abase fr om t he I SBN: 073 84client app licat ions.26I09 n -1our d esign , client s sh ould not access en t it y beans d irect ly. Pages: 75 0
We im plem en t a num ber of m et hod s in t h e Banking b ean so t hat client s can issue business t r ansact ions. The session bean also t ak es car e of t r ansact ion m an agement , becau se by def ault , each m et hod inv oked in a session bean st ar t s an d end s a t r an sact ion ( in r egar d t o EJBs and t h e dat ab ase) . Here Lear is t he of m et hods hatspecif we im p lem n list about t he EJB 2t.0 icat ion ent : Dev elop EJBs w it h St u dio App licat ion Dev eloper getBalance— Ret riev e t he b alance of an accoun t . Deploy EJBs t o W eb Sp her e Applicat ion Ser v er deposit— Deposit fu nds in t o an account , cr eat e a t ran sact ion r ecor d, ret ur n t he balance. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio withdraw— Wit hdr aw f un ds f rom an account , creat e a t r ansact ion r ecor d, ret u rn t h e App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans balance. ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. transfer— Tran sf er fu nds f r om one accoun t t o anot her , cr eat e t w o t r ansact ion record s, Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an r et ur n t he balan ce of t h e fr om accoun t . un derly ing r elat ional dat ab ase. getTransrecords— Ret ur ns a vect or of st r ings of t ran sact ion r ecor d in for m at ion f or an I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic accou nt : concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or timestamp transactiontype transactionamount successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Tr ansact ion r ecor d s car r y a t imest am p, a t r ansact ion t y pe ( C f or cr ed it or d ep osit , D f or im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At debit or w it hdr aw ) , an d t he am oun t . t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. getCustomers— Ret ur ns a vect or of cust om ers t hat can access an accou nt :
customerID customername
listAccountsOfCustomer— Ret ur ns a vect or of inf or mat ion cont ainin g t he cust om er nam e, t h e accoun t s t h at can be accesses, an d t he t r ansact ion r ecord s of each account .
This m et hod r et ur ns a vect or of an ar ray [ 5 ] of st r in gs ( w h er e is blank ) :
• •
C
customerid
customername
A
accountType
accountType
balance
Overdraft|MinAmount
T
Debit|Credit
timestamp
amount
Table of Content s I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Ther Wah li,e Wout er Denay Schu nkent , Deborah Shaddon , Mart in Weiss is one custer, omLar ers elem ( C) , an account elem ent
( A) f or each accou nt follow ed by t he t ran sact ion r ecor d elem en t s ( T) of t hat accoun t . This in for m at ion can be u sed in a clien t at a repor t . Pubt o lishfor er:m I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Ret ur ns an ar r ay of st ring s w it h accountID, accountType, balance, getAccountInfo— Pages: 75 0 and interest.
addCustAcct an d removeCustAcct— Add and r em ov e accou nt for a cu st omer .
Retrieve the balance Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper public BigDecimal getBalance(String accountID) throws FinderException { Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Thistry I BM{Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans = accountHome.findByPrimaryKey(accountID); ( EJB) arAccountLocal ch it ect u re, an daccount d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. return Thr oug hout t he account.getBalance(); book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. } catch (FinderException ex) { I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic "+accountID+" not found"); concep tthrow s and t new h e arFinderException("Account ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed } catch (EJBException { dr iv en beans. We also pr ovid e best pr act ice guidelines f or per sist ence ent it y b ean s and mex) essagesuccessful imp lem ent at ions of EJBs. throw new EJBException("Account getBalance failed for "+accountID); I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, } an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t}he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Deposit funds
public BigDecimal deposit(String accountID, BigDecimal amount) throws FinderException {
try { AccountLocal account = accountHome.findByPrimaryKey(accountID); TransRecordLocal tr = trecordHome.create("C", amount, account); return account.deposit(amount); • •
Table of Content s
} catch (FinderException ex) { I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
throw new FinderException("Account "+accountID+" not found");
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
} catch (CreateException ex) { Pub lish er: I BM
throw EJBException Pub Dat e: Aprilnew 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
("Account "+accountID+" create Transrecord failed");
} catch (EJBException ex) { throw new EJBException("Account deposit failed for "+accountID); } }
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
This I BM Redbook Withdraw funds pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an public BigDecimal withdraw(String accountID, BigDecimal amount) un derly ing r elat ional dat ab ase. InsufficientFundException I n Part 1, we in t r oducethrows EJBs as FinderException, a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover{t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he { ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed EJB try 2. 0 sp per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or AccountLocal = accountHome.findByPrimaryKey(accountID); successful imp lem ent ataccount ions of EJBs. result = account.withdraw(amount); I n Part BigDecimal 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also = en trecordHome.create("D", amount, account); im plem TransRecordLocal ent fin der m et h ods, tr dif fer t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. return result; } catch (InsufficientFundException ex) { getSessionContext().setRollbackOnly(); // force rollback throw new InsufficientFundException ("Account "+accountID+": not enough funds"); } catch (FinderException ex) {
throw new FinderException("Account "+accountID+" not found"); } catch (CreateException ex) { throw new EJBException ("Account "+accountID+" create Transrecord failed"); •
Table of Content s
•
} catch I (EJBException ex) { ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
throw new EJBException("Account withdraw failed for "+accountID);
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM
} Dat e: April 1 1, 20 03 Pub I SBN: 0- 73 84- 26 09 -1
}
Pages: 75 0
Transfer funds Lear n about t he EJB 2 .0 specif icat ion DevBigDecimal elop EJBs w ittransfer(String h St u dio App licat ion Dev eloper String accountID2, public accountID1, Deploy EJBsamount) t o W eb Spthrows her e Applicat ion Ser v er BigDecimal FinderException, InsufficientFundException { This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio AccountLocal account1 = null; App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. AccountLocal account2 = null; Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an try { un derly ing r elat ional dat ab ase. = accountHome.findByPrimaryKey(accountID1); I n Part account1 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he { am ple, en hanced fun ct ion alit y for cont ain er - m anag ed EJB }2.catch 0 sp ecif(FinderException icat ion t hat pr ov id e, ex) for ex per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or throw FinderException("Account "+accountID1+" not found"); successful imp new lem ent at ions of EJBs. } 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session I n Part beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also try ent { fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At im plem t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. account2 = accountHome.findByPrimaryKey(accountID2); } catch (FinderException ex) { throw new FinderException("Account "+accountID2+" not found"); } try { account1.withdraw(amount);
account2.deposit(amount); TransRecordLocal tr1 = trecordHome.create("D", amount, account1); TransRecordLocal tr2 = trecordHome.create("C", amount, account2); return account1.getBalance(); • •
Table of Content s
} catch I (InsufficientFundException ex) { ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
getSessionContext().setRollbackOnly(); // force rollback By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
throw new InsufficientFundException Pub lish er: I BM Pub Dat e: April 1 1, 20 03
("Account "+accountID1+": not enough funds");
I SBN: 0- 73 84- 26 09 -1
} Pages: catch75 (CreateException ex) { 0 throw new EJBException("Account transfer create Transrecord failed"); } catch (EJBException ex) { throw new EJBException Lear n about t he EJB 2 .0 specif icat ion ("Account transfer for "+accountID1+"/"+accountID2); Dev elop EJBs w it h St u dio App licat ion failed Dev eloper } Deploy EJBs t o W eb Sp her e Applicat ion Ser v er } This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an List the transaction records un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he public java.util.Vector getTransrecords(String EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hancedaccountID) fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.throws FinderException { try 2,{ we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session I n Part beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also result = new Vector(); im plem Vector ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. AccountLocal account = accountHome.findByPrimaryKey(accountID); Collection coll = account.getTransrecords(); Iterator collit = coll.iterator(); while ( collit.hasNext() ) { TransRecordLocal tr = (TransRecordLocal)collit.next(); result.addElement (
((java.sql.Timestamp)tr.getPrimaryKey()).toString() +" "+tr.getTransType() +" "+tr.getTransAmount().toString() ); } • •
Table of Content s
return result; I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
} catch (FinderException ex) {
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
throw new FinderException("Account "+accountID+" not found"); Pub lish er: I BM Pub } Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
}
Pages: 75 0
List the customers Lear n about t he EJB 2 .0 specif icat ion Devjava.util.Vector elop EJBs w it h St u dio App licat ion Dev eloper accountID) public getCustomers(String Deploy EJBs t o W eb Sp her e Applicat ion Ser v er throws FinderException { This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio try { App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Vector result = new Vector(); Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an account un derlyAccountLocal ing r elat ional dat ab ase. = accountHome.findByPrimaryKey(accountID); coll = as account.getCustomers(); I n Part Collection 1, we in t r oduce EJBs a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he collit = ov coll.iterator(); EJB 2. 0Iterator sp ecif icat ion t hat pr id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or while successful imp ( lemcollit.hasNext() ent at ions of EJBs. ) { CustomerLocal custp le= bank (CustomerLocal)collit.next(); I n Part 2, we in t r oduce t h e sam ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent result.addElement fin der m et h ods, dif fer(en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. ((CustomerKey)cust.getPrimaryKey()).getCustomerID() +" "+cust.getName() ); } return result; } catch (FinderException ex) { throw new FinderException("Account "+accountID+" not found");
} }
List a customer with accounts and transaction records •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wahjava.util.Vector li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss public listAccountsOfCustomer(int customerID)
throws FinderException {
Pub lish er: I BM Pub Dat e: April 1 1, 20 03
tryI SBN: {
0- 73 84- 26 09 -1
Pages: 75 0
Vector result = new Vector();
CustomerLocal cust = customerHome.findByPrimaryKey (new CustomerKey(customerID) ); Lear n about t he EJB 2 .0 specif icat ion result.addElement( new String[] Dev elop EJBs w it hString.valueOf(customerID), St u dio App licat ion Dev eloper { "C", cust.getName(), "", "" } ); Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Collection coll1 = cust.getAccounts(); This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio coll1.isEmpty() { App licatif ion(Developer f or t he d ev)elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. result.addElement( new String[] Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an { ional "A",dat "No accounts", "", "", "" } ); un derly ing r elat ab ase. return result; I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0}sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or Iterator successful imp lem collit1 ent at ions =of coll1.iterator(); EJBs. I n Part while 2, we in(t rcollit1.hasNext() oduce t h e sam p le bank ) ing { applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent AccountLocal fin der m et h ods,account dif fer en t =m (AccountLocal)collit1.next(); app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. if (account instanceof CheckingLocal) { CheckingLocal checking = (CheckingLocal)account; result.addElement ( new String[] { "A", account.getAccountType(), (String)account.getPrimaryKey(), account.getBalance().toString(),
"Overdraft: "+checking.getOverdraft().toString() } ); } else if (account instanceof SavingsLocal) { SavingsLocal savings = (SavingsLocal)account; result.addElement ( new String[] •
Table of Content s
•
I ndex { "A", account.getAccountType(),
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
(String)account.getPrimaryKey(),
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
account.getBalance().toString(), Pub lish er: I BM Pub Dat e: April 1 1, 20 03"MinAmount:
"+savings.getMinAmount().toString() } );
I SBN: 0- 73 84- 26 09 -1 Pages: }75 else 0
{
result.addElement ( new String[] { "A", account.getAccountType(), Lear n about t he (String)account.getPrimaryKey(), EJB 2 .0 specif icat ion "" } ); Dev elop EJBs w itaccount.getBalance().toString(), h St u dio App licat ion Dev eloper } EJBs t o W eb Sp her e Applicat ion Ser v er Deploy This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Collection coll2 = account.getTransrecords() App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch itIterator ect u re, an collit2 d d ep loym=ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. coll2.iterator(); Thr oug houtwhile t he book , w e pr ov id e exam ples on a simp le bank ing app licat ion w it h an ( collit2.hasNext() ) based { un derly ing r elat ional dat ab ase. TransRecordLocal tr = (TransRecordLocal)collit2.next(); I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t if h e ar it ect ur e. I n par t icu lar w e point out )t he n ew fu nct ions int r od uced w it h t he ( ch tr.getTransType().equals("C") EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y result.addElement( b ean s and m essage- dr new iv en String[] beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. {"T", "Credit", I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also ((java.sql.Timestamp)tr.getPrimaryKey()).toString() im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. tr.getTransAmount().toString(), "" } ); else result.addElement( new String[] {"T", "Debit", ((java.sql.Timestamp)tr.getPrimaryKey()).toString(), tr.getTransAmount().toString(), ""} );
} } return result; } catch (FinderException ex) { •
Table of Content s
throw new FinderException("Customer "+customerID+" not found");
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
}
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
} Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
Retrieve Pages:account 75 0 information
public String[] getAccountInfo(String accountID) throws FinderException { try Lear{n about t he EJB 2 .0 specif icat ion Dev elop EJBsresult w it h St u=dio App licat ion Dev eloper String[] new String[4]; Deploy EJBs t o W eb Sp her e =Applicat ion Ser v er AccountLocal account accountHome.findByPrimaryKey(accountID); This I BM Redbook pr d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio result[0] = ovides accountID; App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) arresult[1] ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. = account.getAccountType(); Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an result[2] = account.getBalance().toString(); un derly ing r elat ional dat ab ase. result[3] = String.valueOf(account.getInterest()); I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he return result; EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or } catch (FinderException ex) { successful imp lem ent at ions of EJBs. FinderException("Account "+accountID+" notlem found"); I n Part throw 2, we innew t r oduce t h e sam p le bank ing applicat ion and t hen imp ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also } ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At im plem t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. }
Add and remove an account for a customer
public void addCustAcct(int custid, String accid) { try {
CustomerLocal cust = customerHome.findByPrimaryKey ( new CustomerKey(custid) ); AccountLocal
acct = accountHome. findByPrimaryKey( accid );
cust.addAccount(acct); • •
Table of Content s ndex } catch I (Exception e) { e.printStackTrace(); }
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
} By
Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
public void removeCustAcct(int custid, String accid) { Pub lish er: I BM Pub Dat e: April 1 1, 20 03
try {
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0 CustomerLocal
cust = customerHome.findByPrimaryKey ( new CustomerKey(custid) );
AccountLocal acct
= accountHome. findByPrimaryKey( accid );
Lear cust.removeAccount(acct); n about t he EJB 2 .0 specif icat ion elop (Exception EJBs w it h St ue) dio{App licat ion Dev eloper } Dev catch e.printStackTrace(); } }
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Promoting the methods
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an ingt line r elat ionalofdat ase. Iun n derly t he Ou v iew t heabBankingBean class, select all t h e business logic m et hods and En t er p rise Bean - > Prom ot e t o Rem ot e I n t erf ace ( con t ex t ) . I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep and t h e ar ch it is ectav urailable e. I n par lar applicat w e pointions. out t he n ew fu nct ions int r od uced w it h t he Now t h tes bu siness logic t ot icu client EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
Setting deployment properties
I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, We w ant aneach d m essag b usine-ess d r iv log enicbeans met h od using t o r WebSph un as a ter ran e St sact u dio ion Ap ( inplicat regar ion d tDev o t he elop dat erabase . We also or ot her rim esour plemces) ent . fin This derismt heteh defau ods, dif lt fer behav en t ior m app , butin g w est w r at ant egies, t o illust andr at sim e ph le er eclient w hats tset h att inugs se ar t he e EJBs. At t heailab av en d,le.w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Op en t he deploym ent descr ipt or of t he ItsoBank5SessionEJB p r oject :
Go t o t h e Assem bly Descr ipt or page. Now click Add u nder Con t ain er Tr an sact ions. I n t he Add Cont ainer Tr ansact ion w ind ow , select t h e Banking b ean an d click Next . Select Requ ir ed f or Con t ainer t r ansact ion t yp e.
A t ran sact ion is r eq uir ed ; eit her a t ran sact ion is alr ead y st art ed ( w hen called fr om anot h er EJB for ex am ple) or a new t r ansact ion is st ar t ed. Ot h er select ion s are Not su ppor t ed, Sup por t ed , Requ ir es New , Man dat or y, an d Never . Ex pan d t he bean and select t h e business logic m et hods fr om t h e list , t hen click Fin ish ( Figur e 14 - 6) . •
Table of Content s
•
I ndexFigu r e
1 4 -6 . Set t in g t he cont a in e r t r a nsa ct i on t yp e
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, dwe in tions r oduce EJBs inast he a pdart of Jav a 2d En t eript p rise Ed it ion ( J2 EE) The ef init ap pear ep loy m ent escr or ( Figur e 14 - 7) . and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Figu r eit y1b4ean -7s. and D epl oym e dr ntivde scr iptWe or also w it h cont ai neprr act t r ice a nsa ct i onsf or per sist ence ent m essageen beans. pr ovid e best guidelines successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Sav e t he d ep loym ent descr ipt or , t h en go t o t he Sour ce pag e t o b row se t h rou gh t he XML code an d f ind t he m et hod def in it ions.
Generate the deployed code All t h e definit ions are com plet e an d w e can gener at e t he deployed code:
• •
Select t he ItsoBank5SessionEJB m od ule and Gener at e - > Deploy and RM I C Code ( cont ex Table t ) . Select t he sBanking b ean an d click Fin ish. of Content I ndex
The d ep loyed code for t he Banking session bean is gener at ed int o t he itso.bank5.session p ackag e. I n add it ion , som e helper pack ages ar e generat ed int o t he Ueli pr Wah er Denay s Schu nk , Deborah Shaddon , Mart in Weiss p ackag es ar e g en er at ed for session oj li, ectWout . How ev erer, , nLar o itso.bank5.session.websphere beans.
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: the 0- 73 8426 09 -1 Testing business logic in the universal test client Pages: 75 0
St ar t t he EjbbankServer an d t he u niver sal t est client t o t est t he b usin ess log ic of t he session bean. Her e ar e a few hin t s on how t o pr oceed:
I n t he JNDI ex plor er,2 you f in d icat t he ion local hom es by ex pan ding ejb - > it sobank - > Bank in g . Lear n about t he EJB .0 specif Select t he Banking r em ot e hom e int er face. Dev elop EJBs w it h St u dio App licat ion Dev eloper Ex pan d t he EJB Refer en ces an d select t he create m et hod of BankingHome. Click I n vok e, tDeploy hen WorEJBs k w ittho Object or teh Applicat e r esult .ion An Ser instvan W eb Spfher er ce of Banking ap pear s und er EJB Ref erences. d t he Banking , select thm e getBalance hod , ent er use 101-1001 accoun t k ey, This IEx BMpan Redbook pr ovidesb ean d et ailed in for at ion on h owmt oetef fect ively WebSpas here St udio andion click I n vok e. fTh balance is display App licat Developer or et he d ev elopm en t ofed. ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. I nv oke t he deposit an d withdraw m et hod s for t he sam e accou nt ( Figur e 14 - 8) . Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elatFigu ional rdat e 1ab4ase. -8 . Run ni ng a ba nk in g w i t h dr aw t r an sa ct ion I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
I nv oke t he transfer m et hod an d t r an sf er 13.00 f r om accou nt 101-1002 t o account 1011001 ( Figur e 14 - 9) .
Figu r e 1 4 -9 . Run ni ng a ba nk in g t r an sfe r t r an sa ct ion
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper I nv oke t he getTransrecords m et hod f or accou nt 101-1001. Click Wor k w it h Cont ained Ob ject s EJBs t o see heeb cont enteofApplicat t he result v ectvor Deploy t o tW Sp her ion Ser er ( Figur e 14 - 10) . This I BM Redbook ailedi ng in fortm h ow tion o ef fect useof WebSp here St udio Figu r epr ovides 1 4 -1 0d.etList hat e ion t r aonnsact r e ively cor ds a n accoun t App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
You should see a list of all t he t ran sact ions t hat you inv ok ed f or t hat accou nt , in clud ing t he init ial d ep osit of 80.00 t hat was load ed int o t he t able.
I nv oke t he getCustomers m et hod f or accou nt 105-5001. Th is is t he on ly accoun t t h at h as m or e t han one cust om er at t ached. Click Wor k w it h Cont ained Object s t o see t he list of cust om er s. I f you click Wor k w it h Object f or a result vect or , y ou can inv oke t he m et hods of t he Vector class. • •
For exam ple, t h e size m et hod r et ur ns t h e nu mb er of elem en t s in t he v ect or , t he Table of Content s r et ur ns t h e fir st st r ing, and t he elementAt(x) m et hod r et ur ns t h e firstElement m et hod I ndex x t h st r ing ( n ot e t hat x st ar t s at 0) .
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
f ast w ayert Denay o seeer,allLart she cont is t oShaddon in vok e I n sp Field s By Ueli A Wah li, Wout Schu nk ,ent Deborah , Mart in ect Weiss
f or a result vect or , and t he
cont ent is list ed by inv ok ing toString on each element . Pub lish er: I BM
Close t he univ er sal t est clien t w h en don e and st op t he EjbbankServer. Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Developing a stateful session bean I n t his sect ion, w e develop t he st at efu l Reports session bean. This exam ple m ay n ot b e ver y r eal as a st at ef ul session bean, but it illust r at es som e of t h e concep t s. •
Table of Content s
•
I ndex
Design issues EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
For a st at ef ul session bean, y ou hav e t o d ecide wh at d at a shou ld b e persist ent , t h at is, t h e dat a it ems t hat are car r ied f rom one int er act ion t o t he next . These dat a it em s hav e t o be serializable so tPub h atlish t her: eyI BM can be sav ed in case t he session bean is passiv at ed b et ween in vocat ions of Pub Dat e: April 1 1, 20 03 ion an d act iv at ion, see " Act ivat ion and p assivat ion " on p age 18 6. m et hods. For passivat I SBN: 0- 73 84- 26 09 -1
Not e tPages: h at h ome 75 0 and EJB r efer ences ar e n ot serializable, so w e cann ot k eep hom e r efer en ces ov er passivat ion / act iv at ion.
Persistent state n about 2 .0 specif ion We w Lear ill k eep t heset he datEJB a it ems as t heicat p er sist en t st at e of t h e session b ean : Dev elop EJBs w it h St u dio App licat ion Dev eloper Vector customerList— List of cust om er s r et r iev ed u sin g EJB QL Deploy EJBs t o W eb Sp her e Applicat ion Ser v er int currentCustomer— Select ed cust om er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio String currentAccount— Select ed App licat ion Developer f or t he d ev elopm en taccount of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. int currentInterest— Select ed int er est r at e Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an java.math.BigDecimal un derly ing r elat ional dat ab ase.currentBalance— Select ed balan ce f or qu er ies I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep treferences s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he Home EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or The session bean r eq uires t h ese h ome ref er ences: successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session AccountLocalHome beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At CustomerLocalHome t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. CustomerHome (remote home interface)
Creating a stateful session bean Defin e t he Reports session bean using t he sam e seq uence as describ ed for t he Banking session bean in " Cr eat ing t he session bean for b ank ing t r ansact ion s" on p age 52 2:
Use t he ItsoBank5SessionEJB p r oject
Nam e t he b ean Reports Use t he itso.bank5.session p ackag e Select St at efu l f or t h e session t y pe ( Figur e 14 - 3 on p age 52 3) Set t h e JNDI n ame t o ejb/itsobank/Reports • •
Table of Content s
Select on ly t he r em ot e int er face I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
EJB references Pub lish er: I BM
Defin e t hr ee r ef er ences for t he Reports session bean in t he deploy m en t d escrip t or edit or ( see Pub Dat e: April 1 1, 20 03 " Defin in g EJB r efer en ces" on p age 52 4) : I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Local r efer en ce for Account Local r efer en ce for Customer Rem ot e r ef erence f or Customer ( one of t he EJB QL quer ies is on ly available in t he rem ot e int er face) . Select EJB Refer en ce t o def in e a r em ot e r ef er ence. Lear n about t he EJB 2 .0 specif icat ion The d ep loym ent descr ipt or edit or w it h t h e r ef er ences is show n in Figur e 14 - 11. Dev elop EJBs w it h St u dio App licat ion Dev eloper
r eSp1her 4 -1 1 . EJBion r eSer fe rveernce s Deploy EJBsFigu t o W eb e Applicat
f or Re por t s b ea n
This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Define the persistent fields Op en t he ReportsBean f ile and add t he persist en t f ield s:
public class ReportsBean implements javax.ejb.SessionBean {
// stateful fields
• •
private Vector
customerList
= null;
private String
currentAccount = null;
private int
currentCustomer = 0;
Table of Content s
private int
I ndex
currentInterest = 0;
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
private BigDecimal currentBalance = new BigDecimal (0.00);
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 Import statements I SBN: 0- 73 84- 26 09 -1 75por 0 t st at em ent s f or t he classes r equir ed in our cod e: Add t hPages: ese im
import itso.bank5.cmp.*; import Learitso.bank5.exception.*; n about t he EJB 2 .0 specif icat ion import Devjava.rmi.*; elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er import javax.rmi.*; This I BMjavax.ejb.*; Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio import App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (import EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. javax.naming.*; Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an import java.math.BigDecimal; un derly ing r elat ional dat ab ase. import java.util.*; I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he import itso.bank5.utility.HomeFactory; EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, Create methods we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im entejbCreate fin der m etm h ods, en t t mp app inet g ers st r atisegies, andd ef sim p le We client s th se et he A dplem ef ault et hoddifwfer it hou aram alr eady ined. wan t at t o uhav an EJBs. At tadd he itenion d,alw m e detescrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. hod w it h a customerID an d an accountID as par am et er s.
public void ejbCreateByAccount(int customerID, String accountID) throws javax.ejb.CreateException { setCurrentCustomer(customerID); setCurrentAccount(accountID);
}
Not e t h at w e d ef ine an ejbCreateByAccount m et hod w it h a suf fix , as allow ed b y EJB 2. 0. Pr om ot e t he m et hod t o t he h ome in t erf ace. •
Table of Content s
Handling • Iof ndexhome references EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Hom r ef li, er Wout ences cannot bes kSchu eptnkin, Deborah t h e perShaddon sist ent, Mart st atineWeiss of t he session bean. I f t he session bean is By UelieWah er Denay er, Lar passivat ed, w e lose t he hom e r efer en ces an d h ave t o r et riev e t hem ag ain at act iv at ion t im e. Pub lish er: I BM Pub Dat e: April 1 1, 20 03
DefineI SBN: the0-home references 73 84- 26 09 -1 Pages: 75 0
Defin e t hese fields f or hom e r eferences:
// home fields Lear transient n about t he AccountLocalHome EJB 2 .0 specif icat ion private
accountHome
Dev elop EJBs w itCustomerLocalHome h St u dio App licat ion Dev eloper private transient customerHome
= null; = null;
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er private transient CustomerHome custRemoteHome = null; This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. By m ak in g t he hom e r efer en ces transient, t h ey will n ot b e ser ialized at passiv at ion t ime. W e w illoug usehout t h e callback etehod s tid o eest ablish t h based e hom eonr efer en ces. Thr t he bookm , w pr ov exam ples a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Retrieve theinhome references concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB u2.se0 aspgetHomes ecif icat ionmt hat pr ov e, r for amhom ple, eenr hanced fun ct ion alit y for cont ain er - m anag ed We et hod t o idr et ieveext he efer en ces: per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. Iprotected n Part 2, wevoid in t r oduce t h e sam{p le bank ing applicat ion and t hen imp lem ent en t it y beans, session getHomes() beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem try ent { fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. accountHome = (AccountLocalHome) HomeFactory.singleton().getHome("ejb/Account"); customerHome = (CustomerLocalHome) HomeFactory.singleton().getHome("ejb/Customer"); custRemoteHome = (CustomerHome) HomeFactory.singleton().getHome("ejb/CustomerRemote");
} catch (NamingException ex) { ex.printStackTrace(); throw new EJBException("Error looking up homes: "+ex.getMessage()); } •
Table of Content s
• }
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Managing Pub lish er: Ithe BM home references Pub Dat e: April 1 1, 20 03
We h ave I SBN: t o 0-m73 anage 84- 26 09t he -1 h ome refer ences so t hat t hey ar e av ailab le at cr eat ion and act iv at ion t im e. Pages: Add t he f ollowin g code t o t h e callb ack m et hod s: 75 0
public void ejbActivate() { getHomes(); Lear n about t he EJB 2 .0 specif icat ion }
Dev elop EJBs w it h St u dio App licat ion Dev eloper
public voidEJBs ejbCreate() throws javax.ejb.CreateException { Deploy t o W eb Sp her e Applicat ion Ser v er ThisgetHomes(); I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans } ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. public void customerID, String accountID) Thr oug hout t heejbCreateByAccount(int book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. throws javax.ejb.CreateException { I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he getHomes(); EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or setCurrentCustomer(customerID); successful imp lem ent at ions of EJBs. setCurrentAccount(accountID); I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also } im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At tpublic he en d, void w e d escrib e how t o deploy ejbPassivate() { EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. accountHome
= null;
customerHome
= null
custRemoteHome = null; }
Implementing the business logic The Reports session bean is t he f acade bet w een t h e client applicat ions an d t he EJB qu er ies t hat w e im plement ed using EJB QL. We im plem en t a num ber of m et hod s in t h e Reports b ean f or r ep or t in g pur poses: •
Table of Content s
listCustomerNames— Ret riev e and st or e t he list of all cust om er s I ndex
•
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
getCustomerList— Ret ur n t he cust om er list
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
setCurrentCustomer— Set t he cur r ent cu st om er n um ber for repor t ing Pub lish er: I BM
setCurrentAccount— Pub Dat e: April 1 1, 20 03
Set t he cur r ent accoun t n um ber for repor t ing
I SBN: 0- 73 84- 26 09 -1
setCurrentBalance— Set cur rent balance f or gold cust om er / account r ep ort s Pages: 75 0
setCurrentInterest— Set t he in t er est r at e f or high int er est r ep ort listGoldAccounts— Ret ur n accoun t s w it h high balances listTransferAccounts— Ret ur n cu st om er 's account s valid for t r ansfer Lear n about t he EJB 2 .0 specif icat ion listLargestAccount— Ret ur n t he account w it h t he hig hest balance Dev elop EJBs w it h St u dio App licat ion Dev eloper listGoldCustomers— Ret ur n cu st om er s w it h high balances Deploy EJBs t o W eb Sp her e Applicat ion Ser v er listHighInterest— Ret ur n cu st om er s w it h high int er est rat es This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio listAllAccounts— Ret ur n a com plet e repor t of all accou nt s an d t heir t r ansact ion r ecor ds App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout Retrieve list t heofbook all customers , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. The list of cust om er s is r et r ieved using t he getAllCustomers f inder m et hod . Each en t r y con t ains Ian n Part t r oduce assta itpem art being of Jav at he 2 En t eromer p riselast Ed itnion EE) tand cover t he itbasic ar ray1,ofwe st rining s, wit hEJBs t h e fir cu st am(eJ2and he r em ain ing em s concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he being t he accoun t n um bers of t hat cu st omer . EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. public java.util.Vector listCustomerNames() throws FinderException { I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, tryan { d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d,customerList w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. = customerHome.getAllCustomers(); return customerList; } catch (FinderException ex) { throw new FinderException("List customer names find error "+ex.getMessage()); }
}
Return customer list •
Table of Content s
• I ndexgetCustomerList() { public Vector EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
return customerList; By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss }
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Set current customer
public void setCurrentCustomer(int customerID) { Lear n about t he EJB 2 .0 specif icat ion currentCustomer = customerID; Dev elop EJBs w it h St u dio App licat ion Dev eloper } Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App ion Developer Set licat current accountf or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. public void setCurrentAccount(String accountID) { I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic currentAccount accountID; concep t s and t h e ar ch=it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed } per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m balance essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Set current im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. public void setCurrentBalance(java.math.BigDecimal balance) { currentBalance = balance; }
Set current interest
public void setCurrentInterest(int interest) { currentInterest = interest; } •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Return list of gold accounts
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
BM account s is r et r ieved using t he findGoldAccounts f inder m et hod . The r esult is a ThePub listlishofer:gIold 1 1, 20 03 v ectPub or Dat w he:erApril e each element is an ar ray of st r ing s. The ar r ay has t h ree element s: accoun t t yp e, accou nt num , and b alan ce. I SBN: 0-ber 73 8426 09 -1 Pages: 75 0
public java.util.Vector listGoldAccounts() throws FinderException { try { Lear n about t he EJB .0 specif icat ion Vector result = 2new Vector(); Dev elop EJBs w it h St u dio App licat ion Dev eloper Collection coll1 = accountHome.findGoldAccounts(currentBalance); Deploy EJBs t o W eb Sp her e Applicat ion Ser v er if (coll1 == null) { This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio new String[] { "No accounts", "", ""Jav } aBeans ); App licat ionresult.addElement( Developer f or t he d ev elopm en t of ap plicat ion gold s based on t h e Ent er pr ise ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. return result; Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly}ing r elat ional dat ab ase. I n Part Iterator 1, we in t r oduce collit1 EJBs=as coll1.iterator(); a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0while sp ecif icat ( collit1.hasNext() ion t hat pr ov id e, for ex ) am { ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp AccountLocal lem ent at ionsaccount of EJBs. = (AccountLocal)collit1.next(); I n Part 2, we t r oduce t hinstanceof e sam p le bank ing applicat ion and if in(account CheckingLocal) { t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app=in g(CheckingLocal)account; st r at egies, and sim p le client s t h at u se t he EJBs. At CheckingLocal checking t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. result.addElement ( new String[] { "Checking", (String) account.getPrimaryKey(), account.getBalance().toString() } ); } else if (account instanceof SavingsLocal) { SavingsLocal savings = (SavingsLocal)account; result.addElement ( new String[] { "Savings", (String) account.getPrimaryKey(), account.getBalance().toString() } );
} else { result.addElement ( new String[] { "Account",(String) account.getPrimaryKey(), account.getBalance().toString() } ); } •
Table of Content s
}
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
return result; By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss } catch (FinderException ex) {
Pub lish er: I BM
Pub Dat e: Aprilnew 1 1, 20 03 throw FinderException("Gold
accounts find error "
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
+ex.getMessage());
} } Lear n about t he EJB 2 .0 specif icat ion Devlist elopofEJBs w it h Staccounts u dio App licat ion Dev eloper Return transfer Deploy EJBs t o W eb Sp her e Applicat ion Ser v er The list of t ran sf er accou nt s is r et r iev ed u sing t h e findTransferAccounts f inder m et hod , The rThis esultI BM is aRedbook v ect or ofpran arr ayd of st r ings witmhatt w o elem en t s: num b erWebSp an d balance. ovides et ailed in for ion on h ow t o account ef fect ively use here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. public java.util.Vector listTransferAccounts() throws FinderException Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w{it h an un derly ing r elat ional dat ab ase. try { I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic = ur new concep tVector s and t h result e ar ch it ect e. IVector(); n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Collection coll1 per sist ence ent it y b ean s and= maccountHome.findTransferAccounts essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. (currentCustomer, currentAccount); I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, if an d(mcoll1.isEmpty() essag e- d r iv en beans WebSph er e St u dio Ap plicat ion Dev elop er . We also ) using { im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w eresult.addElement( d escrib e how t o deploy EJB ap plicat ion s t o transfer a WebSph er e App licat ion"", Serv er. new String[] {"No accounts", ""}); return result; } Iterator collit1 = coll1.iterator(); while ( collit1.hasNext() ) { AccountLocal account = (AccountLocal)collit1.next();
result.addElement ( new String[] { (String)account.getPrimaryKey(), account.getAccountType(), account.getBalance().toString() } ); } •
Table of Content s
return result;
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
} catch (FinderException ex) {
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
throw new FinderException("Transfer accounts find error " Pub lish er: I BM Pub Dat e: April 1 1, 20 03
+ex.getMessage());
I SBN: 0- 73 84- 26 09 -1
}
Pages: 75 0
}
Return account Learcustomer n about t he EJB 2 .0 with specif highest icat ion balance Dev elop w ithhighest St u dio App ce licat eloper The accoun t wEJBs it h t he b alan ision ret rDev iev ed usin g t he findLargestAccount f inder m et hod , The r esult is a st r in g ar r ay w it h t hr ee elem ent s: account nu mb er , account t y pe, and b alan ce. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t ofthrows ap plicatFinderException ion s based on t h e Ent public String[] listLargestAccount() { er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. try { Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derlyAccountLocal ing r elat ional dat ab ase. = account I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic accountHome.findLargestAccount(currentCustomer); concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed if (account == null) { per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. return new String[] { "No largest account", "", "" }; I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session } else { beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At return new String[] { (String)account.getPrimaryKey(), t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. account.getAccountType(), account.getBalance().toString() }; } } catch (FinderException ex) { throw new FinderException("Largest account find error " +ex.getMessage()); }
}
Return list of gold customers •The list of g old Table of om Content cust er s sis r et r ieved using t he findGoldCustomers f inder m et hod . The r esult is •a v ect or w herI ndex e each elem ent is an ar r ay of st r in gs. The arr ay h as t w o elem ent s: cu st omer EJB 2 . 0er D eand ve lopm e nt w it h n Wam ebSphe nu mb cust omer e. r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM public java.util.Vector listGoldCustomers() throws FinderException { Pub Dat e: April 1 1, 20 03
tryI SBN: {
0- 73 84- 26 09 -1
Pages: 75 0
Vector result = new Vector(); Collection coll1 = customerHome.findGoldCustomers(currentBalance); if ( coll1.isEmpty() ) { Lear n about t he EJB 2 .0 specif icat ion result.addElement( new String[] { "No gold customers", "" } ); Dev elop EJBs w it h St u dio App licat ion Dev eloper return result; Deploy EJBs t o W eb Sp her e Applicat ion Ser v er } This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licatIterator ion Developer f or t he ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans collit1 = dcoll1.iterator(); ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. while ( collit1.hasNext() ) { Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing CustomerLocal r elat ional dat ab ase. cust = (CustomerLocal)collit1.next(); I n Part 1, we in t r oduce EJBs as a(p new art ofString[] Jav a 2 En { t erString.valueOf( p rise Ed it ion ( J2 EE) and cover t he basic result.addElement concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain), er - m anag ed ((CustomerKey)cust.getPrimaryKey()).getCustomerID() per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem cust.getName() ent at ions of EJBs. } ); I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session } beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At return result; t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. } catch (FinderException ex) { throw new FinderException("Gold customers find error " +ex.getMessage()); } }
Return list of customers with high interest rate The list of cust om er s w it h a high int er est r at e is r et rieved using t he findHighInterest f inder m et hod. The r esult is a v ect or w her e each elem ent is an ar r ay of st r in gs. The arr ay h as t hr ee elem ent s: cust om er nu mb er , cu st om er nam e, and int er est r at e. •
Table of Content s
•
I ndex
public listHighInterest() FinderException { EJB 2 . 0 D ejava.util.Vector ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De vthrows el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
try {
Pub lish er: I BM Vector
result = new Vector();
Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Collection coll1
= custRemoteHome.findHighInterest(currentInterest);
Pages: 75 0
if ( coll1.isEmpty() ) { result.addElement( new String[] { "No high interest customers", "", "", "" } ); Lear n about t he EJB 2 .0 specif icat ion return result; Dev elop EJBs w it h St u dio App licat ion Dev eloper } Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Iterator collit1 = coll1.iterator(); This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licatwhile ion Developer f or t he d ev elopm)en{t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( collit1.hasNext() ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Customer custRemote = (Customer)collit1.next(); Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing CustomerLocal r elat ional dat ab ase. cust = customerHome.findByPrimaryKey( I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and (CustomerKey)custRemote.getPrimaryKey() ); cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hatcoll2 pr ov id=e, cust.getAccounts(); for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Collection per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. Iterator collit2 = coll2.iterator(); I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session while ( collit2.hasNext() ) { beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At AccountLocal acct = (AccountLocal)collit2.next(); t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. if ( acct.getInterest() > currentInterest ) result.addElement( new String[] {String.valueOf( ((CustomerKey)cust.getPrimaryKey()).getCustomerID() ), cust.getName(), (String)acct.getPrimaryKey(), String.valueOf( acct.getInterest() ) } ); } }
return result; } catch (FinderException ex) { throw new FinderException("High interest customers find error " +ex.getMessage()); • •
Table of Content s I ndex } catch (java.rmi.RemoteException ex) {
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah throw li, Wout new er Denay FinderException("High er, Lar s Schu nk , Deborah Shaddon interest , Mart in Weiss customers
Pub lish er: I BM
remote error "
+ex.getMessage());
Pub Dat e: April 1 1, 20 03
} }
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Return list of all accounts with transaction records Lear n about t he EJB 2 .0 specif icat ion The list of accoun t s is r et r ieved using t he findAllAccounts f inder m et hod . Each accoun t is analy Dev zed elop t o finEJBs d t hewaccou t ypApp e inlicat t heion inhDev er it ance it h St nt u dio eloperh ier ar ch y. The t r ansact ion record s ar e r et riev ed by f ollow in g t he getAccounts r elat ionship. The r esult is a v ect or w her e each elem ent is Deploy W ebarSp herhas e Applicat ion Ser er d t here are en t ries f or accou nt s an d an ar ray of stEJBs r ing s.t oThe r ay f ive element s,van t r ansact ion r ecord s: This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans or itaccount accoun t y pe, n um , balance, ov er or min im amlicat ount ( EJB) Aarf ch ect u re, , an d d eptloym ent of ber su ch ap plicat ion s tdoraf a tWebSph er ue m App ion Serv er. T hout f or t r tansact ion, w recor debit or cr ed itbased , t im est amou nt , bing lankapp licat ion w it h an Thr oug he book e pr d, ov id e exam ples onam a p, simp le bank un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic public listAllAccounts() throws FinderException concep t sjava.util.Vector and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r{od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed tryence { ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or per sist successful imp lem ent at ions of EJBs. Vector result = new Vector(); I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, Collection an d m essag e-coll1 d r iv en =beans accountHome.findAllAccounts(); using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d,ifw e(coll1 d escrib== e how null) t o deploy { EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. result.addElement( new String[] {"A", "No accounts", "", "", ""}); return result; } Iterator collit1 = coll1.iterator(); while ( collit1.hasNext() ) {
AccountLocal account = (AccountLocal)collit1.next(); if (account instanceof CheckingLocal) { CheckingLocal checking = (CheckingLocal)account; result.addElement ( new String[] { "A", •
Table of Content s
•
I ndex
account.getAccountType(), (String)account.getPrimaryKey(),
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
account.getBalance().toString(),
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
"Overdraft: "+checking.getOverdraft().toString() } ); Pub lish er: I BM
else if Pub Dat e: }April 1 1, 20 03
(account instanceof SavingsLocal) {
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
SavingsLocal savings = (SavingsLocal)account; result.addElement ( new String[] { "A", account.getAccountType(), (String)account.getPrimaryKey(),
account.getBalance().toString(), Lear n about t he EJB 2 .0 specif icat ion "MinAmount: "+savings.getMinAmount().toString() } ); Dev elop EJBs w it h St u dio App licat ion Dev eloper } else { Deploy EJBs t o W eb Sp her e Applicat ion Ser v er result.addElement new String[] { t"A", This I BM Redbook pr ovides d et ailed (in for m at ion on h ow o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (String)account.getPrimaryKey(), ( EJB) ar ch it ect u re,account.getAccountType(), an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. account.getBalance().toString(), "" le} bank ); ing app licat ion w it h an Thr oug hout t he book , w e pr ov id e exam ples based on a simp un derly ing r elat ional dat ab ase. } I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Collection coll2 concep t s and t h e ar ch it ect ur e. I=n account.getTransrecords(); par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed null) { per sist enceif ent(coll2 it y b ean!= s and m essagedr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. Iterator collit2 = coll2.iterator(); I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d(r ivcollit2.hasNext() en beans using WebSph while ) {er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ion s t o a WebSph er e App licat ion Serv er. TransRecordLocal trap=plicat (TransRecordLocal)collit2.next(); if ( tr.getTransType().equals("C") ) result.addElement( new String[] {"T", "Credit", ((java.sql.Timestamp)tr.getPrimaryKey()).toString(), tr.getTransAmount().toString(), "" } ); else
result.addElement( new String[] {"T", "Debit", ((java.sql.Timestamp)tr.getPrimaryKey()).toString(), tr.getTransAmount().toString(), "" } ); } •
}
•
Table of Content s I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
}
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
return result; Pub lish er: I BM
} catch (FinderException Pub Dat e: April 1 1, 20 03
ex) {
I SBN: 0- 73 84- 26 09 -1
throw new FinderException("All accounts error "+ex.getMessage());
Pages: 75 0
} } Lear n about t he EJB 2 .0 specif icat ion
Promote theEJBs business Dev elop w it h Stmethods u dio App licat ion Dev eloper Deploy ebess Sp her e hod Applicat ere in t erf ace. Pr om ot e t he EJBs p ublict ob W usin m et s t o tion he Ser r emvot This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans Testing stateful ( EJB) ar ch itthe ect u re, an d d epsession loym ent of bean su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr e pr ov e exam on acode simp le bank w it h bean. an At toug his hout poin t ,t he y oubook ar e, rw eady t o id gener at eples t he based d ep loyed and t est ing t h e app st atlicat ef ul ion session un derly ing r elat ional dat ab ase. Wit h a sing le con t ainer and one st at ef ul session bean, y ou m ay not be successful in t est ing passivat ion and act iv at ion of t h e session b ean . How ev er , you can r un mu lt iple m et hods in Iseq n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic uence: concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence it y om b ean and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or Ret r ievent e cust erslist successful imp lem ent at ions of EJBs. Set cur r en t cust om er and cur r en t account I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session Set ce fore-gdold cust om ers and WebSph accoun t ser e St u dio Ap plicat ion Dev elop er . We also beans, anbdalan m essag r iv en beans using im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At int est r ateehow for high int er est t he enSet d, w e er d escrib t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Ru n t he bu sin ess m et hods
Creating a Web Service from a session bean App licat ion Developer m ak es it v er y easy t o d ep loy a session bean as a Web ser v ice an d m ak e it s m et hods available t o client s usin g SOAP. All t h e con figu r at ion is p er f orm ed in Ap plicat ion Dev eloper an d t he r esu lt in g W eb ser v ice is inst alled in a Web applicat ion. •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Creating the Web service
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
To cr eat e and inst all a Web serv ice fr om a session b ean , w e r un t hr oug h t he pag es of a w izar d: Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
I n t he J2 EE Hier ar ch y, select t he Banking session bean and New - > Ot her - > W eb Ser v ices Pages: 75 0 - > W eb Serv ice. I n t he w izar d ( Figur e 14 - 12) :
Figu r e 1 4 -1 2 . W eb se r v ice s w iz ar d Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
1. The W eb ser v ice t yp e is pr eselect ed as EJB Web ser vice. 2. We select t o hav e a Jav a pr ox y gener at ed an d w e w an t t o t est t he Web serv ice using t he pr oxy .
3. Click Next . I n t he deploy m en t set t ing s panel, select Ch oose ser ver f ir st an d select t he EjbbankServer. For t he Web pr oj ect , t he ItsoBank5Web p r oject is pr eselect ed . I n t he EJB conf ig ur at ion panel, all t h e defau lt s are fine.
I n t he Jav aBean id en t it y panel, ent er uri:Banking as t he Web ser v ice URI . All t he def ault s for t he WSD L g en er at ion ar e fin e. I n t he Jav aBean m et hod s panel, w e can select w hich met h ods t o t ur n in t o W eb ser v ice; by defau lt all m et hod s ar e select ed . Select Show ser ver t yp e m ap pings. I n t he Jav a t o XML m appin gs pan el, all en cod ings f or par am et ers an d result s ar e f in e. • •
Table of Content I n t he bin ding pr ox y sgener at ion p anel, select Gener at e pr ox y. Th e pr ox y is generat ed int o a I ndex nam ed ItsoBank5WebClient. Select Show m ap pings. Web pr oject
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
n t he XMLer tDenay o Javer, a Lar m appin gs, Deborah pan el, Shaddon all m apping ar e fine. By Ueli IWah li, Wout s Schu nk , Mart insWeiss I n t he SOAP bind in g map ping config ur at ion, all m ap pings are f ine. Pub lish er: I BM Pub e: April 20 03 I nDat t he Test1 1, pan el,
select Test t he gen er at ed pr oxy an d select Web ser vice sam p le JSPs. Set t he out0-pu f older I SBN: 73 t8426 09 -1as sample/Banking. Select Ru n t est on serv er . Pages: 75 0
Sk ip t h e pu blicat ion pan el ( UD DI ) and click Fin ish. The W eb ser v ice cod e is gener at ed an d t h e EjbbankServer is st ar t ed or r est ar t ed. W hen t h e ser v er is r ead y, t he gener at ed t est client is st ar t ed in a Web br ow ser . Lear n about t he EJB 2 .0 specif icat ion
Testing the Web service with the generated test client Dev elop EJBs w it h St u dio App licat ion Dev eloper The g en er at ed t est client can b e used t o execut e t he m et hods of t he Web ser vice t h rou gh t he EJBs generDeploy at ed p rox y . t o W eb Sp her e Applicat ion Ser v er This d etr ailed fortm at client ion on: h ow t o ef fect ively use WebSp here St udio FigurIeBM 14 Redbook - 13 sh owspraovides sam ple un of in t he est App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Figu r e 1 4 -1 3 . W eb se r v ice t est cli en t r un
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Select t he deposit m et hod , ent er an accou nt num ber an d an am oun t , click I n vok e, an d t he
r esult in g b alance is display ed . I nv oke som e of t h e ot her m et hods and see t h e r esult s of t he call. Not e t h at t h e r esult of t he listAccountsForCustomer m et hod is n ot f orm at t ed. The v ect or of an ar ray of st r ings is not for m at t ed b y t he sim ple t est clien t . •
Table of Content s
Testing the Web service with the universal test client • I ndex EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Anot er wli,ay t oert est a er, Web vice is t o select t he, Mart generat ed pr ox y class By Uelih Wah Wout Denay Lar sser Schu nk , Deborah Shaddon in Weiss ( proxy.soap.BankingProxy in t h e ItsoBank5WebClient p r oject ) and Lau nch Univ er sal Test Client ( con t ex t ) . Pub lish er: I BM
Datt s e: tApril 1 1,iv20 03 ThisPub st ar h e un ersal t est client and inst ant iat es a pr oxy ob ject . Ex pan d t he pr ox y obj ect , I SBN: 073 8426 09 -1 select on e of it s m et h ods, ent er suit able p ar amet er s, and click I n vok e. Pages: 75 0
Analyzing the generated code The W eb ser v ice is inst alled in t he ItsoBank5Web p r oject : Lear n about t he EJB 2 .0 specif icat ion I n t he deploy m en t d escr ip t or , y ou f ind t w o serv let s: rpcrouter an d messagerouter. Dev elop EJBs w it h St u dio App licat ion Dev eloper I n Web Content, y ou find t h e Web ser vice deploy m en t d escrip t or ( dds.xml) and t he Deploy W eb Sp her e Applicat ion Ser v er conf ig urEJBs at iont ofile ( soap.xml) . This IIBM Redbook pr ovides d et ailed in for find m at ion fect ively use WebSp Stat udio n Web Content\WEB-INF\isd, y ou t h e on isdhfow ile, ttoh eef deploy m en t bin ding here inf orm ion App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans t hat becom es p ar t of web.xml. ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. I n Web Content\WEB-INF\lib, y ou find t h e SOAP r u nt im e. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r Content\wsdl elat ional dat ab ase. I n Web y ou fin d t he WSDL f iles an d t h e XSD file. You m ay w an t t o st udy t he WSDL f iles: I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e.The I n int parert icu lardef w einpoint - Banking.wsdl— face it ion out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence -ent it y b ean s and m essage-The dr ivSOAP en beans. We e er best pr act ice guidelines f or BankingBinding.wsdl— bind in g ( also pointpr s ovid t o int face) successful imp lem ent at ions of EJBs. - BankingService.wsdl— The ser v ice locat ion ( poin t s t o b inding ) I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d -m essag e- d r iv en beans using e Stbuedio Ap plicat ionWeb DevServ elopices er . We also ion BankingEJB.wsdl— The EJB WebSph bind ing er ( can used b y t he I n vocat im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At Fram ew or k) t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Not e t h at t h e Banking.xsd f ile show s an er r or . I n Web Content\admin, y ou find a sm all ad min ist r at iv e app licat ion. Select t he index.html file an d Ru n on Ser ver . You can list , st ar t , an d st op Web ser v ices. The p r oxy an d t he t est clien t ar e gener at ed in t o t h e ItsoBank5WebClient p r oject : You can f ind t he BankingProxy p r oxy class in t he Java Source\proxy.soap p ackag e. You can f ind t he t est client in Web Content\sample\Banking. Select t he TestClient.jsp
file an d Ru n on Ser ver t o st ar t t h e t est client .
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Summary I n t his chapt er , w e d ev elop ed t w o session b ean s t hat w ill be used as a f acade bet w een EJB client s and t he ent it y beans. • of Content We also showTable ed how easy sit is t o t ur n a session bean int o a Web ser vice. •
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Chapter 15. EJB access beans I n t his chapt er , w e int rod uce access beans as a f acilit y t o sim plif y client pr ogr am min g w h en accessin g en t er pr ise beans. •
Table of Content s
Accessin g an ent er pr ise bean r equ ir es t he use of t he in it ial nam ing con t ext and basic k now led ge • I ndex abou t t h e w ay t he h om e and r em ot e in t er f aces ar e used. Th e dev eloper also h as t o pr ovide EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper im por t ant par am et er s, such as JNDI nam es and p rov ider URL. The m ost im por t ant By Ueli Wah li,ion Wout er Denay er,, Lar s Schu nk , Deborah Shaddon , Mart in considerat , how ever is p rob ably t he cost associat edWeiss w it h t he r epeat ed calling of r em ot e m et hods. Pub lish er: I BM
ConPub sider all 1t hese, Dating e: April 1, 20 03 w e need an ot her w ay t o w rit e EJB clien t s, a w ay t h at enab les d ev elopers w it h noI SBN: EJB0-k73 now ledge t o w r it e client s t hat can access ent er pr ise beans and display t h eir 84- 26 09 -1 pr oper t ies. Access b ean s ar e t he solut ion . Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
What are access beans? Access beans are Java com p onent s t h at ad here t o t he JavaBeans sp ecificat ion and ar e meant t o simp lif y t he developm ent of EJB clien t s. An access bean ad apt s an ent er pr ise bean t o t he Jav aBean p rog ram m ing m odel by hiding t he hom e and r em ot e in t er f aces f r om t he d ev eloper. •They pr ov ideTable Content f ast of access t os en t er p rise beans by let t ing you m aint ain a local cache of ent er pr ise •bean at t r ibutIes. ndexAccess beans mak e it possib le t o u se an ent er pr ise bean in m uch t he sam e w ay EJB D e ve lopm W ebSphe t hat2 .y0ou wou ld euntsew ait hJav aBeanr.e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Ther e ar e fou r t y pes of access beans: Pub lish er: I BM Pub Dat e: April EJB fact or y1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
Dat a class Pages: 75 0 ( f or an ent it y bean) Jav aBean w r apper ( f or a session or ent it y bean) Cop y helper ( for an ent it y bean) Lear n about t he EJB 2 .0 specif icat ion elop tEJBs IDev m por a nt w it h St u dio App licat ion Dev eloper t oatWing eb Sp herVisualAge e Applicat ion v ery ou w ill not ice t hat t he Row set access ifDeploy you arEJBs e m igr fr om for Ser Jav a, bean is no lon ger t her e. The copy help er and Jav aBean w r apper ar e st ill supp ort ed bu t This Idepr BM Redbook pr ovides ailed m at ionised on ht oow t o on ef fect ively WebSp ecat ed . For new prdojetect s, y in oufor are adv u se ly t he d atuse a class andhere t he St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans fact or y class t hat is gener at ed along w it h it . ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase.
EJB factory
I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he An EJB fact or y is an access bean t hat sim plifies t h e cr eat ing or f inding of an ent erp r ise b ean EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed inst ance. I t ex poses t he cr eat e and find er met h ods on t he rem ot e hom e int er f ace. Th e fact or y is per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or not av ailab le for local h ome in t er f aces. successful imp lem ent at ions of EJBs. The f act or y hides t h e looku p in t he nam e ser v ice an d t he necessar y IPortableRemoteObject.narrow: n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he enI td,u ses w e dt hescrib e how t o as deploy EJBinapt he plicat ion s t ion o a Dev WebSph er ed ep App licat ion descr Serv er. e JNDI n ame defined App licat eloper loy m ent ipt or edit or , bu t t h is can b e ch anged by calling t he setJNDIName m et hod . I t p er f orm s t h e looku p in t h e defau lt InitialContext, b ut t his can be cust om ized by callin g t h e setInitialContextFactoryName an d setInitialContextProviderURL m et hods. EJB fact or ies are generat ed w hen you cr eat e any of t he ot her access beans. Th ey cann ot b e gener at ed separ at ely .
I m por t a nt t he cr eat ed f act ory class uses t he en t er pr ise beans' global JNDI n ame t o look up t h e bean. This m ean s t hat t he applicat ion d ep loyer shou ld not chang e t he global JND I nam e at deploy t im e or t h e access bean w ill not be able t o fin d it s bean an ym or e. The sam e rem ar k is v alid f or t he w ay h om e int erf aces ar e look ed u p in copy h elp er s and Jav aBean w r apper s. •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Data class By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss A d at class access bean is an im plem en t at ion of w hat is of t en refer r ed t o as a v alue ob ject or Pubalish er: I BM dat aPub t r Dat ansfer ob1ject I t is a con t ainer t hat holds a local copy of select ed ent it y bean at t rib ut es. e: April 1, 20. 03 I SBN: 0- 73 84- 26 09 -1
Each at t r ibut e has a get t er and set t er m et hod . The d at a class k eeps t rack of w hat at t r ibut es Pages:ged 75 0 b y m ean s of a d ir t y f lag. Wh en u pdat in g, only t hese d irt y at t r ib ut es w ill be w r it t en hav e chan back t o t he ent it y bean. Mu lt ip le dat a classes can be d ef ined for an ent it y b ean , each w it h a d if fer en t set of cached pr oper t ies. Th is can b e useful t o r et r iev e only t h ose at t r ib ut es t h at y ou are really going t o use, esp ecially if t here are man y of t hem . Lear n about t he EJB 2 .0 specif icat ion The w izar d creat es t hr ee m et hods in t h e imp lem ent at ion class of t h e ent it y bean: Dev elop EJBs w it h St u dio App licat ion Dev eloper To cr eatEJBs e a dtat calle tApplicat he getXXXData Deploy o aWclass, eb Sp her ion Ser v m er et hod on t he en t it y bean, w h er e XXXData is t he nam e of t he d at a class ( t he default nam e is t h e en t it y bean nam e w it h Data as su ff ix, am ple, CustomerData) . in for m at ion on h ow t o ef fect ively use WebSp here St udio This Ifor BM ex Redbook pr ovides d et ailed App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans u pdat t he an entditdyepbean it h of t hesudat t h e ion datsa t class, inv okeersyncXXXData, w hich ( EJB) To ar ch it ecteu re, loymwent ch aapinplicat o a WebSph e App licat ion Serv er. copies all chang ed at t r ibu t es fr om t h e dat a class int o t he ent it y class. I t also r et ur ns a new dat a class it h t, he ent at e ofples t he based ent it y on bean ( andlecon sequ t ly licat w it hion all wdir flag s Thr oug hout t he w book w ecuprrrov id estexam a simp bank ingenapp it ht yan clear . ional dat ab ase. un derly inged) r elat The f ir EJBs st peras forampsart t heofsam t h teersyncXXXData etEE) hodand , bu tcover t h en tthe h row sa I n Part 1,setXXXData we in t r oduce Javea as 2 En p rise Ed it ion m ( J2 basic FieldChangedException if an ent it y b ean f ield has b een ch anged t hat w as not chang concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h ed t hein a icat class. EJB 2.t he 0 spdat ecif ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or When creatimp ing lem a dat a fEJBs. act ory is also cr eat ed if t h e bean has a rem ot e int er face. This successful entaatclass, ions of fact or y h id es t he look up of t he r em ot e hom e int er face an d g ives access t o t he cr eat e and fin der m hods. I netPart 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also The d atent a class canmbet e hconsid er fer ed en t he imapp pr ov of and t he sim copy h elp er . sUsin helper im plem fin der ods, dif t m in gedstvr ersion at egies, p le client t h atg ucop se yt he EJBs.for At new p roj not e recom ed . Let parion e tshe of t he a class ( Figur 15 - 1) wit h t he en d, ect w esdis escrib how mend t o deploy EJBusapcom plicat t o uasage WebSph er edat App licat ion Serve er. t hat of t he cop y helper ( Figur e 15 - 2) by m eans of an ex am ple. We w ill chan ge t h e firstname at t r ibut e of a cust om er.
Figu r e 1 5 -1 . Sam pl e code u si ng a da t a cl ass
CustomerFactory customerFactory = new CustomerFactory(); CustomerKey customerKey = new CustomerKey(999);
Customer customer = customerFactory.findByPrimaryKey(customerKey); CustomerData customerData = customer.getCustomerData(); customerData.setFirstName("Elke"); customer.setCustomerData(customerData); •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Figu r e 1 5 -2 . Sam pl e code u si ng a copy h el pe r By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss Pub lish er: I BM Pub Dat e: AprilcustomerKey 1 1, 20 03 CustomerKey = new CustomerKey(999); I SBN: 0- 73 84- 26 09 -1 Pages: 75 0 CustomerAccessBean customerAccessBean =
new CustomerAccessBean(customerKey); customerAccessBean.setFirstName("Elke"); customerAccessBean.commitCopyHelper(); Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This INot BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio e App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB)Aft arer ch ad it ect u re,a an ep loym ent ofbean su cht oapan plicat t o a, WebSph licat ione Serv ding datdadclass access ention it y sb ean y ou hav er e teo App r egenerat t he er. deploy ed code, b ecause of t h e changes m ade t o t h e bean. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic JavaBean concep t s and twrapper h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part we tinatnt r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session I m2,por beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem fin der maccess et h ods,bean dif ferisenhere t m app in g st r atback egies, simpat p leibilit client at uarsee t he EJBs. At Theent JavaBean m ainly f or w arand d com y . sI ft hyou t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. creat ing n ew cod e, con sid er u sin g t he dat a class access b ean inst ead ( see " Dat a class" on page 553 ) .
The JavaBean w rap per is desig ned t o allow eit her a session or ent it y bean t o be u sed like a st andar d Jav aBean , and it hides t he en t er pr ise bean hom e and com ponent int er faces fr om t h e dev elop er . Each Jav aBean w r app er t hat you creat e ex t ends eit her AbstractEntityAccessBean or AbstractSessionAccessBean A JavaBean w rap per access bean has t h e follow ing char act er ist ics:
You can on ly hav e one Jav aBean w r app er . Th e nam e of t h e w rap per class is t he bean nam e w it h AccessBean as a suf fix , for ex am ple CustomerAccessBean. I t cont ain s a n o- ar gum ent con st r u ct or , w h ich m aps t o eit her one of t h e creat e or on e of t he single ob ject fin der m et h ods.
• •
The access bean cont ain s one init_xxx property f or each p aram et er of t he cr eat e or fin der m et h od ( for ex am ple, init_customerID f or cr eat e or initKey_customerID f or Table of Content. s findByPrimaryKey) I ndex
EJB 2 . 0When D e ve lopm e ntclass w it h W e St udio ti on find De v el a k ey isebSphe u sed rin t he cr Appl eat eicaand eroper met h ods
f or a CMP ent it y b ean , t h e key k ey class. A- k ey field is n orm ally declared as a sim ple t y pe. Th is mak es it easier for v isual const r uct ion t ools t o u se an access bean. Pub lish er: I BM
ar e er u sed aser,t he p r opert ies ,inst of t he By Ueli fields Wah li, Wout Denay Lar sinit_xxx Schu nk , Deborah Shaddon Martead in Weiss
Pub Dat e: April 1, 20 When t he 1noar03 gum ent
con st ru ct or is used, t h e init_xxx p r opert ies m ust be set bef ore I SBN: 73 8426 09 any ot 0her calls t o-1 t he access bean. Th e act ual cr eat e or fin der m et h od is in vok ed at t h e fir st Pages: 75 0call t o t h e access bean ( lazy init ializat ion ) . m et hod The access bean m ay cont ain sev er al m ult iple- ar gum ent con st r u ct or s, each cor r espon ding t o one of t he cr eat e or f inder m et hods defined in t he bean hom e int er face. A d ef ault JNDI nam e is g en er at ed int o each access bean class. The code generat or reads tLear he deploym andicat passes n about ent t he descr EJB 2ipt .0 or specif ion t h e JNDI n am e t o t h e access bean. You can chang e t he JNDI nam e u sin g t h e setInit_JNDIName m et hod . I t is n ot ex pect ed t hat y ou will h ave tDev o chelop angeEJBs t he wJNDI Howev , inDev t h eeloper ev ent t hat an en t er pr ise bean is deploy ed int o a it h Stnam u dioe.App licater ion dif fer en t hom e, t h e adm inist r at or m ay add a p r ef ix t o t h e JNDI n am e t o ind icat e t he dif fer en EJBs t homt e. Deploy o W eb Sp her e Applicat ion Ser v er
Figur 15 Redbook - 3 sh ows pr sam ple code w henin tfor h emfindByPrimaryKey m et hod use is mWebSp app ed here t o t heStnudio oThis IeBM ovides d et ailed at ion on h ow t o ef fect ively ar gum ent ru ct or. f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans App licat ionconst Developer ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Figu r e 1 5 -3 . Ja va Be an w r app er w i t h l az y in it i al iz a t ion
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic CustomerAccessBean customerAccessBean = new CustomerAccessBean(); concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed customerAccessBean.setInitKey_customerID(999); per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. customerAccessBean.setFirstName("Elke"); // this triggers the find I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session customerAccessBean.commitCopyHelper(); beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Copy helper
I m por t a nt The cop y helper access bean is here main ly for b ack w ar d com pat ib ilit y . I f y ou ar e creat ing n ew cod e, con sid er u sin g t he Dat a class access b ean inst ead ( see " Dat a class" on page 553 ) .
A copy help er access bean has all of t h e char act er ist ics of a Jav aBean w r apper , bu t it also m aint ains a local copy of at t r ibut es f r om a r em ot e ent it y b ean . A clien t p rog ram can r et r iev e t he ent it y b ean at t r ibu t es fr om t his local copy , w h ich elim inat es t h e need f or accessin g t he at t r ibu t es fr om t he r emot e en t it y bean. •When y ou cr eat Table Content s er access bean, y ou can select all of t he b ean s' at t r ibu t es or only a e aof copy help •subset . The select I ndex ed at t r ibut es ar e sav ed in t he access b ean conf igur at ion file ( META-INF/ibmejb-access-bean.xmi) EJB 2 . 0 D e ve lopm e nt w it h W. ebSphe Th eser eselect St udio ions Applar icaetiron e- De display v el oper ed if you d ecide y ou w ant t o chan ge t he access bean. By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
The r em ot e int er face of t he en t it y bean is ch anged t o ex t end t he CopyHelper in t er f ace as w ell as Pub lish er: I BM in t er f ace. Th is int erf ace defines t w o m et h ods, _copyFromEJB an d _copyToEJB. t he EJBObject Pub m Dat 1,e20ad 03 ded t o t he bean im plem en t at ion class t o allow t he access b ean t o mov e t h e These ete:h April ods 1ar I SBN: 073 8426 09 bean at t r ibut es in and-1out of t he en t it y bean. Th is is done by using a hash t ab le t hat cont ains a nam e-Pages: v alu e75pair 0 f or each at t r ibu t e. The access bean h as a g et and set m et hod for each en t it y bean at t r ibut e. Th ese m et hods will get and set t o t h e local cop y of t he d at a. I n ord er t o sy nchr onize w it h t he r emot e inst ance pr oper t ies, y ou can u se t he f ollow ing t w o m et h ods: Lear n about t he EJB 2 .0 specif icatr efr ionesh es t he local copy dat a f rom t he r emot e en t erp rise The refreshCopyHelper m et hod bean. I t copies fr om t h e ent it y bean t o t he access bean. Dev elop EJBs w it h St u dio App licat ion Dev eloper The commitCopyHelper m et hod com m it s t he chan ges in t he local dat a t o t he r emot e Deploy EJBs t o W eb Sp her e Applicat ion Ser v er ent er pr ise b ean . Not ice t hat t his m et hod is sm art enoug h t o up dat e only t he m odif ied t r ibut es in tpr h eovides bean in stailed ance. in for m at ion on h ow t o ef fect ively use WebSp here St udio This Iat BM Redbook d et App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an Not un derly inge r elat ional dat ab ase. Aft 1, er we ad ding a cop yEJBs helper bean y b ean , ion y ou( hJ2ave r egenerat e t he I n Part in t r oduce as aaccess p art of Jav at o2an Enent t er pit rise Ed it EE) t oand cover t he basic deploy ed code, b ecause of t h e changes m ade t o t h e bean. concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
Access beans and relationships
I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also Relat ionships ar e d escr ib ed in " EJB cont ainer - man aged relat ionships ( CMR) " on p age 11 6. I f y ou im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At creat e an access b ean ( Jav aBean w r app er or copy helper ) f or an en t er p rise bean inv olved in a t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. r elat ion sh ip t h at h as been m ade nav ig able, t he n avig at ion m et hod of t he access bean r et ur ns an access bean corr esp ondin g t o t he en t er pr ise bean at t h e ot her side of t h e r elat ionsh ip . Ther ef or e, you hav e t o gener at e access beans f or all ent er pr ise beans r elat ed t h rou gh r elat ion sh ip s. Ot her w ise, y ou en cou nt er er ror s w h en follow ing r elat ion sh ip s t hr oug h access bean m et hods.
I m por t a nt
Dat a class access b ean s do not hav e r elat ionship nav igat ion m et hods g en er at ed; t hey only cont ain at t r ibu t es.
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Developing access beans To illust r at e access b ean s, we develop a dat a class f or t h e Customer b ean an d a Jav aBean w r apper f or t he Banking session bean. •
Table of Content s
•
I ndex
Developing a data class EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
We d ev elop a dat a class access b ean for t he Customer ent it y bean as follow s: Pub lish er: I BM Pub Dat e: April 1 1, 20 03
I nI SBN: t he 0-J273EE perspect iv e, d ouble- click t h e ItsoBank5CmpEJB p r oject t o open t he EJB 84- 26 09 -1 deploy m en t d escrip t or edit or . Pages: 75 0
On t he beans pan e, select t he Customer b ean . We d ef ined b ot h rem ot e and local int er faces for t he Customer b ean . We w ill m ak e an access f or t he r em ot e int er face only . Scroll n ttohet hEJB e Access Beanicat s sect Lear n dow about 2 .0 specif ionion and click Add . ( You can also select File - > New - > EJB - > Access Bean . ) Dev elop EJBs w it h St u dio App licat ion Dev eloper I n t he Add an Access Bean w in dow , select Dat a class an d click Next . Deploy EJBs t o W eb Sp her e Applicat ion Ser v er I n t he nex t p anel, select t h e Customer b ean of t h e ItsoBank5CmpEJB p r oject and click Next This I(BM Redbook Figur e 15 - 4) pr . ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loymrent ap plicat t o aaWebSph e App Figu e 1of5 su -4ch. Cr ea t i ion ngs an cce ss er bea n licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
I n t he Dat a Class Access Bean p anel, select t h e Rem ot e in t er f ace and en t er itso.bank5.access as t he p ack age n am e for b ot h t he f act ory and t he d at a class. Leave t he default nam e of CustomerData f or t h e access bean nam e, and leav e all at t rib ut es select ed ( Figur e 15 - 5) .
Figu r e 1 5 -5 . Specif yi ng t h e pa ck ag e an d t he a t t r i bu t e s of t h e a cce ss b ea n
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Click Fin ish an d t hen sav e t h e EJB deploym ent descr ipt or edit or . Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing- 6r sh elat ional abloy ase. Figur e 15 ows t hedat d ep m ent d escr ip t or af t er adding t h e dat a class access b ean . I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Figu r e 1 5 -6 . D epl oym e nt de scr ipt or w it h a ccess be an concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Generated access bean The D at a Access Bean Sm art Guide chan ges t he ex ist in g Customer b ean an d cr eat es t w o classes:
Thr ee m et hod are added t o t h e CustomerBean class:
getCustomerData, setCustomerData, syncCustomerData
• •
Table ofe, Content s St ud y t he cod especially t h e setCustomerData m et hod t h at checks if t h e bean w as I ndex m odified.
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
The sam e t h ree m et hods also appear in t he r em ot e com ponent int erf ace ( Customer) .
PubThe lish er: I BM itso.bank5.access Pub Dat e: April 1 1, 20 (03t he CustomerData
p ackag e con t ains t h e CustomerFactory ( t he f act or y ) an d t he access b ean ) classes.
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Customer factory The CustomerFactory class cont ains t he JNDI nam e fr om t h e deploy m en t d escrip t or ( ejb/itsobank/Customer) in t h e getDefaultJNDIName m et hod . I n addit Lear ion, n about it conttains he EJB t he2 create .0 specifm icat et hod ion an d t h e t w o f inder m et hod s ( findByPrimaryKey an d findHighInterest) of t he r emot e hom e int er face. These met h ods call t he Dev elop EJBs w it h St ion Dev _acquireCustomerHome muetdio hodApp t h licat at r et r iev es eloper t he h ome in t er f ace fr om t h e nam e ser v er. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er
Customer data class This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans The CustomerData ainsent copof iessuofcht hap e plicat at t r ibut a d irt y flag each. The por er. t ant ( EJB) ar ch it ect u re, class an d dcont ep loym iones s two itah WebSph er e App licat ionim Serv int er nal m et hod s ar e: Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. CustomerData(CustomerData.Store)— The const r uct or t h at is called f rom t he getCustomerData m et hod he of Customer b ean I n Part 1, we in t r oduce EJBs as ainp tart Jav a 2 En t er p. rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he heidate,t r for ib utex esam fr om bean t o fun t hectdat class. EJB 2.initialize— 0 sp ecif icat ionCopies t hat prtov ple,t he en hanced ion aalit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or copyTo— Copies if ied at t ribu t es fr om t h e dat a class t o t he b ean ( t h is m et h od is successful imp lem ent att he ionsmod of EJBs. called by syncCustomerData an d setCustomerData in t h e Customer b ean ) . I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent findeployed der m et h ods,code dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At Generate t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Becau se a dat a class access b ean ( an d a copy helper as w ell) chan ges t he com pon en t int er face, w e hav e t o r eg en er at e t h e deploy ed code of t h e bean. I n our case w e hav e t o regener at e t he deployed code f or t he Customer b ean .
Generating a JavaBean wrapper To d ev elop a Jav aBean w r apper f or t h e Banking session bean:
Op en t he deploym ent descr ipt or of t he ItsoBank5SessionEJB p r oject . Select t he Banking b ean ( on t h e Bean s pag e) , and click Add f or Access Beans. I n t he w ind ow, select Jav a b ean w r apper an d click Next . Select t he Banking b ean an d click Next . • •
Table of Content s ndex Select t Ihe cr eat e met hod an d click Fin ish.
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Sav e t he d ep loym ent descr ipt or .
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM
Generated classes Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
The BankingAccessBean is generat ed int o t he itso.bank5.session p ackag e ( you d o not hav e a Pages: 75 0 choice as w it h dat a classes) . I n addit ion , t he BankingFactory class is also gener at ed in t o t h e sam e packag e. Ther e is no need t o r eg en er at e t h e deploy ed code; a Jav aBean w r apper d oes not mod if y t h e or iginal b ean or an y of t he in t er f aces. Lear n about t he EJB 2 .0 specif icat ion
Testing theEJBs access Dev elop w it h St beans u dio App licat ion
Dev eloper
We can Deploy t est tEJBs he CustomerData t o W eb Sp her eaccess Applicat bean ion wSer it hv ter he univ er sal t est clien t : This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans he uEjbbankServer. ( EJB) St arar cht itt ect re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Sthout ar t t he iver ,sal client and ples selectbased t h e EJB do ning ot napp eedlicat t heion JNDwIitEx plor er ) . Thr oug t heunbook w et est pr ov id e exam on apage simp( w leebank h an un derly ing r elat ional dat ab ase. Ex pan d t he Ut ilit ies sect ion and select Load Class. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic Ent st erand itso.bank5.access.CustomerFactory as out t he t nhe ame click Load t huced en click Wor concep t h e ar ch it ect ur e. I n par t icu lar w e point n ewand fu nct ions int r,od w it h t hek w it h Object . EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or Ex pan d t he CustomerFactory class ( un der Class Refer ences) , select t he CustomerFactory successful imp lem ent at ions of EJBs. const r uct or m et hod, and click I n vok e, t h en Wor k w it h Object . I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session Ex pan d t he CustomerFactory ob ject ( u nder Ob ject Ref er ences) , select t he beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also findByPrimaryKey m et hod , select t h e CustomerKey(int) con st r uct or , ex pand t h e key , im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At ent er 10 1 as t he v alue, and click I n vok e. A Customer is ret ur ned; click Wor k w it h Object . t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Ex pan d t he Customer b ean ( u nder EJB Ref er ences) , select t he getCustomerData m et hod and click I n vok e, t h en click Wor k w it h Object . Ex pan d t he CustomerData ob ject ( u nder Ob ject Ref er ences) , and inv oke a few g et and set m et hods t o chang e t he d at a. Select t he syncCustomerData m et hod ( of t h e Customer b ean ) , select t he CustomerData in t he Ob ject s p ull- dow n, t hen click I n vok e. Select t he get met h ods of t h e Customer b ean an d v er if y t hat t he dat a was ch anged in t he EJB.
Ret r iev e a new CustomerData access bean. Use a set m et hod of t he Customer b ean t o chang e t he b ean d at a. Then inv oke t he setCustomerData m et hod w it h t he access b ean as t he par amet er ( Object s pu ll- d ow n) . You should get a FieldChangedException, b ecause t he bean d at a w as m od if ied and t h e access bean w as not m odified. To t est t he BankingAccessBean in t h e univ ersal t est client : • •
Table of Content s
Ex pan d I tndex he Ut ilit ies sect ion and select Load Class.
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
t er t he By Ueli En Wah li, itso.bank5.session.BankingAccessBean Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in as Weiss
n ame and click Load , t h en click
Wor k w it h Object . Pub lish er: I BM
Ex pan d t he BankingAccessBean class ( un der Class Refer ences) , select t he con st r uct or met hod , and click I n vok e, an d t hen Wor k w it h Object .
Pub Dat e: April 1 1, 20 03 BankingAccessBean I SBN: 0- 73 84- 26 09 -1
Ex pan d Pages: 75 t0he BankingAccessBean ob ject ( u nder Ob ject Ref er ences) , and select t he getBalance m et hod . En t er a v alid accountID as a par am et er ( for ex am ple, 101 - 10 01) and click I n vok e. At t his poin t , an inst ance of t he Banking session bean is creat ed and t he getBalance m et hod is inv oked ( lazy init ializat ion) . Th e result is displayed. Lear n about t he EJB 2 .0 specif icat ion You can r un ot h er met hod s on t he BankingAccessBean. Dev elop EJBs w it h St u dio App licat ion Dev eloper St op t he u niv er sal t est clien t an d t h e ser v er . Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Summary I n t his chapt er , w e d escrib ed t h e con cept of access beans and t he d if fer ent t yp es of access b ean s t hat can be gener at ed w it h App licat ion Developer . •Then w e creat Table of Content ed an accesss b ean f or t he Customer ent it y bean. We will u se t his access b ean in a •client GUI pr ogr I ndex am ( see " GUI client using access beans" on p age 63 4) . EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Chapter 16. Message-driven bean development •This chap t er Table of es Content s pr ov id det ailed inst r uct ions on how t o cr eat e a m essage- dr iv en b ean ( MD B) t hat •act s as a fr ont I ndex - end t o t he ex ist in g b ank ing applicat ion. EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
We d escrib e t he d esign of t he applicat ion, t h en im plement t he MDB, an d also d ev elop t wo By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss client s, a ser v let and a J2EE app licat ion client , t hat feed m essag es t o t h e MDB. lishd er: I BMib e t he set up of t he ser ver w it h a JMS q ueue f or t he MDB so t hat w e can t est t h e We Pub also escr Pub April 20 03 s. MDB w Dat it h e:t he t w1 1, o client I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Design We w ill pu t a m essage- dr iven bean ( MDB) called Transfer in f ron t of t h e Banking session bean ( f acad e) as an asyn ch ron ous ent r y p oin t t o t he sam e b usin ess logic ( Figur e 16 - 1) . •
Table of Content s
•
I ndex
Figu r e 1 6 -1 . M D B de sign ov er v ie w
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
To let t he Transfer d ist in guish bet w een t he d iff er ent met hod s t hat it can pr ocess, w e send a JMS st r ing pr oper t y along w it h t he m essage. Lear n about t he EJB 2 .0 specif icat ion For our ex am ple, w e imp lem ent only t he transfer m et hod . This m et hod r equir es t hr ee Dev elop EJBs w it h St u dio App licat ion Dev par am et er s ( fromAccount, toAccount, an eloper d amount) t hat we packag e int o a TransferDataObject, w hich is a ser ializable Jav aBean. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er We p ut t he TransferDataObject in t o a JMS m essag e and pu t t h e m essage on t he queue This ItBM pr ovides d et ailed forThis m at ion on h ow t o stefrat fect WebSp St udio in a hat Redbook t he Transfer MDB list ens in on. t echniqu e illu esively t h at use ob ject s canhere b e passed App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans JMS m essage. ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. The Transfer MDB in vok es t he Banking session bean t o p rocess t he t r an sf er . Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly elatprional dat abisase. Oning ce trhe ocessing don e, t he Transfer MDB sends a r eply on a secon d q ueue back t o t he client . Th is r ep ly will b e a st r ing cont aining a descr ipt ion of t he st at u s, in clu ding any I n Part oduce EJBs as ar ed p art err1, or swet hin att rm ay hav e occur . of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or Naming convention successful imp lem ent at ions of EJBs. IJMS n Part 2, wer equir in t r oduce t h nect e sam bank applicat ion requ and ires t henaimp en. t it y beans, queues e a con ionp le f act or y ing , and t h e MDB list lem enerent p ort We w ill usesession t he beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also follow ing nam es: im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. ItsoMdbListenerPort—List ener por t in t he ser v er ItsoMdbConnectionFactory—Fact ory for JMS qu eu es ItsoMdbQueue—Qu eu e for t he m essage t o t h e MDB ItsoResponseQueue—f or t he r esponse f rom t he MD B The JND I nam es for t he conn ect ion fact or y and t h e queues ar e jms/Xxxx ( wh er e Xxxx is t he nam e) an d w e use ref er ences ( jms/XxxxRef) in t h e applicat ion:
jms/ItsoMdbQueueRef ===points-to===> jms/ItsoMdbQueue
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Developing a message-driven bean Befor e st ar t ing t his p art , mak e sur e y ou hav e comp let ed t he set up described in " Ut ilit y pr oject " on page 392 w it h t he h ome fact or y. •
Table of Content s
•
I ndex
Creating the data transfer object EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
We w ill use t h is obj ect t o t r anspor t t he fromAccount, toAccount, an d amount p ar amet er s t o t he MDB. We w ould lik e all t he int er faces t hat our f acade ex poses t o be in t h e sam e p r oject . This Pub lish er: includes t h eI BM TransferDataObject class. Pub Dat e: April 1 1, 20 03
For simI SBN: p licit0-y73 , w e 26 w09 ill-1use t he ut ilit y pr oject ( ItsoBank5Utility) t o st or e t he 84TransferDataObject. Pages: 75 0
Re m a r k Lear t he EJBclasses, 2 .0 specif icat ion Put t inngabout all ex posed ex cept ion s and int er faces t oget her in one p roj ect can h elp us lat er w h en w e w ant t o dist ribu t e t hem . I f a t hird p art y w ant s t o access t he ser vices Dev elop EJBs w it h St u dio App licat ion Dev eloper exp osed by ou r facade, w e on ly hav e t o give t hem t he JAR f ile of t his pr oj ect . W it h t h is app roach w e do not hav e t o g ive t hem t he b yt e cod e of our f acade, w hich t hey cou ld Deploy EJBs t o W eb Sp her e Applicat ion Ser v er decom pile t o g ain insigh t int o t he im p lem ent at ion of our b usin ess log ic. Wit h ion Developer e ailed cannot sep e ton he hhow omet oand rem ot e use int erWebSp faces of ourSt udio This IApp BM licat Redbook pr ovides, dwet in for marat at ion ef fect ively here facad EJBs f rom ftor he tim en t at ion. Weapcan use ans based Ant scron ipt t h t oe accom plish t h is. App licat ione Developer heplem d ev elopm en t of plicat ion Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an To cr eating e t he TransferDataObject: un derly r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep andItsoBank5Utility t h e ar ch it ect ur e. I n t icu lar w et hpoint out t he n ew fu nct ions int r od w it h -t>he I nt st he p rpar oject , select e itso.bank5.utility p ackag e uced and New EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed Class ( con t ex t ) . per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. En t er TransferDataObject as t he n ame. Ad d java.io.Serializable t o t h e int erf aces. D o not select a main m et hod and a const r uct or . Click Fin ish. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, dm essag e-or d r:iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also I nan t he Jav a edit im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. 1. Add t h ree f ields f or t h e dat a ( fromAccount-string, toAccount-string, amountBigDecimal) .
2. Gener at e get t er m et hods for t h e t hr ee fields ( in t he Out line v iew , select t he field s and Gener at e Get t er and Set t er , an d d eselect t he set t er m et h ods in t he w in dow ) .
3. Cr eat e a con st r uct or t hat in it ializes t h e t hr ee fields. The f inal code is sh ow n in Figur e 16 - 2.
Figu r e 1 6 -2 . Tr a nsfe r Da t a O bje ct f or M DB
package itso.bank5.utility;
import java.io.Serializable; • •
Table of Content s
import I ndex java.math.BigDecimal;
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
public class TransferDataObject implements Serializable {
Pub lish er: I BM
Pub Dat// e: April 1 1, 20 03 data variables I SBN: 0- 73 84- 26 09 -1 Pages: 75 0 private
String fromAccount = null;
private String toAccount
= null;
private BigDecimal amount
= null;
Lear n about t he EJB 2 .0 specif icat ion Dev//constructor elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBsTransferDataObject(String t o W eb Sp her e Applicat ion Ser vfrom, er public String to, BigDecimal amt) { This I BM Redbook pr ovides=d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio fromAccount from; App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d=epto; loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. toAccount Thr oug hout tamount he book = , wamt; e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. } I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or public BigDecimal getAmount() { successful imp lem ent at ions of EJBs. return amount; I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also } im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. public String getFromAccount() { return fromAccount; } public String getToAccount() { return toAccount; }
}
Creating the message-driven bean •We d ev elop tTable of Content s arat e EJB p r oject in t he ex ist ing ent er pr ise applicat ion. h e MDB in a sep • I ndex EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
Create an EJB project for the MDB
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lisheer:an I BM To cr eat EJB pr oject in t he ex ist in g ent er pr ise applicat ion: Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Select File - > New - > EJB Proj ect . Select Cr eat e 2. 0 EJB Pr oject in t h e w izar d pag e, and click Next . En t er ItsoBank5MdbEJB as t he p roj ect nam e. Select Ex ist ing f or t h e en t erp rise app licat ion, click Br ow se an d select t he ItsoBank5EAR pr oj ect . Clickt he OKEJB t o r 2et.0urspecif n t o t hicat e wion izar d pag e. Lear n about Click NextEJBs t o gow it t oh tSt heu Mod ule licat Depend cies p age. Select ItsoBank5SessionEJB an d Dev elop dio App ion en Dev eloper ItsoBank5Utility an d click Fin ish. Deploy EJBs t o W eb Sp her e Applicat ion Ser v er The w izar d creat es t he new EJB pr oject in t he en t er pr ise app licat ion. This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (Creating EJB) ar ch the it ect uMDB re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an To ad d a m essagedr iv enabbean un derly ing r elat ional dat ase. t o t he pr oj ect : I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s andt he the ar ch it ect ur e. I n par t icu larand w eNew point- >out n ise ew Bean fu nct ions Select ItsoBank5MdbEJB p r oject Entt he er pr ( conint t exr tod ) . uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it yisb pr ean s and ed m,essagedr ivNext en beans. We also pr ovid e best pr act ice guidelines f or The p r oject eselect and click . successful imp lem ent at ions of EJBs. I n t he Cr eat e a 2. 0 Ent er pr ise Bean pan el, select Message- dr iven bean as t he EJB t y pe, I n Part in t r oduce e sam bank ing applicat ion and t hen impdlem ent packag en t it y beans, ent2,erwe Transfer as tthhe b eanp le nam e an d itso.bank5.mdb as t he ef ault e ( Figursession e 16 beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also 3) . Click Next . im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Figu r e 1 6 -3 . Cr ea t i ng an M D B
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
On t he Ent er pr ise Bean Det ails pan el, select Bean as t he t r ansact ion t yp e and Qu eu e f r om t he dest in at ion t y pe pull- dow n , and ent er ItsoMdbListenerPort as t he list en er por t n am e ( Figur e 16 - 4) . Click Next . Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBsFigu w it h St r eu dio 1 6 App -4 . licat Setion t inDev g teloper he M D B
det ai le d pr op er t i es
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Ther e is no su per class, so click Fin ish.
Not e
We d iscuss t h e Cont ain er t ran sact ion t y pe in " Tr ansact ion t y pes" on p age 58 4.
The w izar d creat es t he Transfer MDB. Figur e 16 - 5 sh ows t he ent r y t h at is ad ded t o t he deploy m en t d escrip t or.
Figu r e 1 6 Table -5 . D oym • of epl Content s e nt de scr ipt or of M D B •
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Transfer Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 itso.bank5.mdb.TransferBean Pages: 75 0
Bean
javax.jms.Queue Lear n about t he EJB 2 .0 specif icat ion
Dev elop EJBs w it h St u dio App licat ion Dev eloper
Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Not e
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r eelat ional abs ase. Messag d riv en bdat ean do not hav e hom e or r em ot e int er faces and w e do not hav e t o gener at e deploy ed code for t h em . I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or Implementing the MDB successful imp lem ent at ions of EJBs. IThe n Part 2, dwe in t res oduce t h e on sam p le bank ing applicat ion eand t hen imp en ttithe y beans, session w izar creat a sk elet Transfer MDB w it h som callback m etlem hodent s and onMessage beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also m et hod t he d oes t he pr ocessing . We hav e t o comp let e t he coding . im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At tThe he en d, wr eceiv e d escrib e how t o deploy ap plicat ion s t o at hWebSph er e bApp licat iont he Serv er.ing MDB es t he m essage fr omEJB a client , p rocesses e m essage y calling ex ist Banking session bean, and ret u rn s a r eply t h rou gh a JMS qu eu e.
Not e The cod e inclu des a n um ber of System.out.println st at em ent s. These should be com ment ed out for a r eal applicat ion befor e deploy m en t .
Import statements The MDB r equir es access t o JMS f or t he reply m essag e, t he session bean, an d t h e ut ilit y p ack age ( f or t he h om e fact or y and t he TransferDataObject) .
• Table of Content s import javax.jms.*; •
I ndex
EJB 2 . 0 D eitso.bank5.session.*; ve lopm e nt w it h W ebSphe r e St udio import
Appl ica ti on De v el oper
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
import itso.bank5.utility.*; Pub lish er: I BM
import java.math.BigDecimal; Pub Dat e: April 1 1, 20 03
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Variables To access t he session bean an d t he JMS queues, w e r equir e a few v ariab les: Lear n about t he EJB 2 .0 specif icat ion private static final String QUEUECONNECTIONFACTORY = Dev elop EJBs w it h St u dio App licat ion Dev eloper "jms/ItsoMdbConnectionFactoryRef"; Deploy EJBs t o W eb Sp her e Applicat ion Ser v er private static final String RESPONSEQUEUE = ively use WebSp here St udio This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of "jms/ItsoResponseQueueRef"; su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. private BankingHome bankingHome = anull; Thr oug hout t he book , w e pr ov id e exam ples based on simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. private QueueConnectionFactory qcf = null; I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par tsendQueue icu lar w e point= out t he n ew fu nct ions int r od uced w it h t he private Queue null; EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. To access a JMS queue, w e r equir e a QueueConnectionFactory an d a Queue. We use r esour ce Ir n Part 2, we t r oduce t h e sam p ler bank ing and t hen imp lem ent ent t dit escr y beans, ef erences for int his pu r pose. These efer en cesapplicat w ill b eion defined in t he deploy m en ip t or session lat er. beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At tInitialization he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. When t he MDB is cr eat ed , w e init ialize t he h om e and JMS access:
public void ejbCreate() { initialize(); }
private void initialize() { try { System.out.println("MDB initialize"); bankingHome = (BankingHome)HomeFactory.singleton() •
Table of Content s
•
I ndex
.getHome("ejb/Banking");
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
System.out.println("MDB found session bean home");
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
qcf = (QueueConnectionFactory)HomeFactory.singleton() Pub lish er: I BM Pub Dat e: April 1 1, 20 03
.getJMS(QUEUECONNECTIONFACTORY);
I SBN: 0- 73 84- 26 09 -1
System.out.println("MDB found QueueConnectionFactory");
Pages: 75 0
sendQueue = (Queue)HomeFactory.singleton().getJMS(RESPONSEQUEUE); System.out.println("MDB found ResponseQueue"); } catch (javax.naming.NamingException e) { Lear n about t he EJB 2 .0 specif icat ion System.out.println("MDB initialization failed: " + e.getMessage()); Dev elop EJBs w it h St u dio App licat ion Dev eloper e.printStackTrace(); Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This}I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans } ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. The getJMS m et hod in t he HomeFactory class is similar t o t he getHome m et hod : I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed public Object jmsRef) throws { pr act ice guidelines f or per sist ence ent it ygetJMS(String b ean s and m essagedr iv en beans.NamingException We also pr ovid e best successful imp lem ent at ions of EJBs. if (initialContext != null) { I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session nsObject beans, Object an d m essag e- d r iv en= beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w einitialContext.lookup( d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. new StringBuffer("java:comp/env/").append(jmsRef).toString()); System.out.println("class=" + nsObject.getClass()); return nsObject; } else { throw new NamingException("HomeFactory: no InitialContext");
} }
Add t h e getJMS m et hod t o t he HomeFactory class in t he ItsoBank5Utility p r oject . •
Table of Content s
•
I ndex
Implementing the EJB 2 . 0 D e ve lopm e nt w itonMessage h W ebSphe r e Stmethod udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
The onMessage m et hod is called by t he cont ain er f or each m essag e r eceiv ed on t he qu eu e t hat t he MDB is assigned t o. Pub lish er: I BM Dat e:log April 1 1,quit 20 03e sim ple: ThePub b asic ic is I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
We ex t r act t he m essag e t y pe ( w e on ly accept t ran sfer m essages) We ex t r act t he TransferDataObject f r om t he message We ex t r act t he p ar amet er s fr om t h e TransferDataObject We e a Banking Learcr n eat about t he EJB 2session .0 specifbean icat ion We okEJBs e t hewtransfer et hod e session Dev inv elop it h St u diomApp licatof iont hDev eloper b ean We cr eat Deploy EJBs e a rt esp o Wonse eb Spm her essage e Applicat an d ion r et ur Ser n vitert o t he client t hr ough JMS using a h elper m et hod ( sendResponseMessage) This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio Figur e 16 - 6 Developer sh ows t he fonMessage m et hod ion. App licat ion or t he d ev elopm en t im of plement ap plicatat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Figu r e 1 6 -6 . M D B on M essa ge m e t h od
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic public msg) out { t he n ew fu nct ions int r od uced w it h t he concep t svoid and t onMessage(javax.jms.Message h e ar ch it ect ur e. I n par t icu lar w e point EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed String messageID null; per sist ence ent it y b ean s=and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. String selector = null; I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session System.out.println("MDB onMessage"); beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he try en d, {w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. messageID = msg.getJMSMessageID(); selector
= msg.getJMSCorrelationID();
ObjectMessage objectMessage = (ObjectMessage)msg; // JMS property for business method String businessMethod = msg.getStringProperty("BUSINESSMETHOD");
if ( businessMethod.equalsIgnoreCase("transfer") ) { System.out.println("MDB got a transfer message: "+messageID); TransferDataObject transferMsg = (TransferDataObject)objectMessage.getObject(); •
Table of Content s
String fromAccount = transferMsg.getFromAccount();
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
String toAccount
= transferMsg.getToAccount();
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
BigDecimal amount
= transferMsg.getAmount();
Pub lish er: I BM Pub Dat e: System.out.println("MDB April 1 1, 20 03
got parms: "+
I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
fromAccount+"/"+toAccount+"/"+amount);
Banking bank = bankingHome.create(); System.out.println("MDB got session bean"); BigDecimal balance = bank.transfer(fromAccount,toAccount,amount); Lear n about t he EJB 2 .0 specif icat ion System.out.println("MDB did the transfer: " + balance); Dev elop EJBs w it h St u dio App licat ion Dev eloper String response = "Transfer OK: " + amount + " from/to " + Deploy EJBs t o W eb Sp her e Applicat ion Ser v er fromAccount + "/" toAccount + WebSp balance; This I BM Redbook pr ovides d et ailed in for + m at ion on h ow+t o" efBalance=" fect ively use here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans response); ( EJB) ar ch itsendResponseMessage(selector, ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. } else Thr oug hout t he {book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. System.out.println("MDB got a wrong message: "+businessMethod); I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t}s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed } catch (Exception e) m{essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or per sist ence ent it y b ean s and successful imp lem ent at ions of EJBs. System.out.println("MDB exception: " + e.getMessage()); I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, e.printStackTrace(); an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d,ifw e( dmessageID escrib e how != t o deploy null )EJB { ap plicat ion s t o a WebSph er e App licat ion Serv er. sendResponseMessage(selector, "ERROR: " + e.getMessage()); } } }
The sendResponseMessage m et hod sends a t ext m essage w it h t he reply t o t he response q ueue by cr eat ing a conn ect ion, a session, an d a sen der fr om t h e QueueConnectionFactory. A cor r elat ion I D is set u p fr om t h e incomin g m essag e so t hat t he client can r et riev e t he cor r ect m essage. Figur e 16 - 7 sh ows t he sendResponseMessage im plem en t at ion. •
Table of Content s
Figu r e 1 6 I-7 . M D B sen dResponse M essa ge m e t hod • ndex EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
private void sendResponseMessage(String corrid, String text) { Pub lish er: I BM Pub Dat e: April 1 1, 20 03 System.out.println("MDB
response " + corrid +": " + text);
I SBN: 0- 73 84- 26 09 -1
QueueConnection qc Pages: 75 0
= null;
QueueSession
session = null;
QueueSender
sender
= null;
try { Lear n about t he EJB 2 .0 specif icat ion //elop Create a itconnection Dev EJBs w h St u dio App licat ion Dev eloper qc = EJBs qcf.createQueueConnection(); Deploy t o W eb Sp her e Applicat ion Ser v er qc.start(); This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar// ch itCreate ect u re, a an session. d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on aSession.AUTO_ACKNOWLEDGE); simp le bank ing app licat ion w it h an session = qc.createQueueSession(false, un derly ing r elat ional dat ab ase. // Create a QueueSender I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep tSystem.out.println("MDB s and t h e ar ch it ect ur e. I n parcreating t icu lar w e queue point out t he n ew fu nct ions int r od uced w it h t he sender"); EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it=y session.createSender(sendQueue); b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or sender successful imp lem ent at ions of EJBs. // Create a message to send to the queue... I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, TextMessage an d m essag e- dmessage r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also = session.createTextMessage(text); im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d,//w eSet d escrib e how t o deploy EJB ap plicat ionmessage s t o a WebSph er e App licat ion Serv er. CorrelationID from the input and send System.out.println("MDB sending message to queue"); message.setJMSCorrelationID(corrid); sender.send(message); // Close the connection (close calls will cascade to other objects) sender.close();
session.close(); qc.close(); qc = null; System.out.println("MDB Send done"); • •
Table of Content s
} catch (JMSException e) { I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper
System.out.println("MDB JMSException - send message failed with "+ e);
By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Exception le = e.getLinkedException(); Pub lish er: I BM
ife: (le null) Pub Dat April 1!= 1, 20 03 I SBN: 0- 73 84- 26 09 -1
System.out.println("MDB linked exception " + le);
Pages: 75 0
} catch (Exception e) { System.out.println("MDB exception: " + e); } finally { // Ensure that the Connection always gets closed Lear n about t he EJB 2 .0 specif icat ion if (qc != null) { Dev elop EJBs w it h St u dio App licat ion Dev eloper try { qc.close(); } catch (JMSException e) {} Deploy EJBs t o W eb Sp her e Applicat ion Ser v er } Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio This I BM App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans } ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. ( EJB) Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. } I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs.
Tip I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, WebSph an d mer essag e Ente-erdpr r ivise en Edit beans ion using has ext WebSph end ed er m eessaging St u dio Ap su plicat ppor t ion , w hDev ich elop allows er . you We talso o im plem useent a sen fin der der m bean et h ods, t hat dif t urfer nsena t m met app h od in g call st rint at egies, o a JMS and m essag sim p le e. client W hiles m t hessageat u se t dr heiven EJBs. At t he en beans d, w emak d escrib e it e easy howt ot or eceiv deploy e mEJB essag ap plicat es, t he ionsen s to der a WebSph bean helps er e yApp ou licat t o sen iond Serv m essag er. es.
Setting deployment information Now w e p rov ide ad dit ional in for m at ion t o t he deploym ent descr ipt or for t h is MDB:
Dou ble- click t he Transfer b ean in t he J2EE Hier ar chy t o open t he d ep loy m ent d escr ip t or edit or ( Figur e 16 - 8) .
Figu r e 1 6 -8 . D epl oym e nt de scr ipt or of t h e Tr an sfe r be a n
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Set t h e t rEJBs ansact t yupe Bean d tDev he ack now led ge m ode t o Aut oAck now ledge . Dev elop w ition h St diot oApp licatan ion eloper The list ener t sh alre eady b e ion ItsoMdbListenerPort. Deploy EJBs por to W ebould Sp her Applicat Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans Adding references ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. The Transfer MDB accesses t he Banking session bean and r eq uir es an EJB r eference. I n Thr book , wt oe access pr ov id et he exam ples based on a simp le bank ionQueue. w it h an addoug it ionhout , t het he bean h as QueueConnectionFactory an d ing t he app r esplicat onse Th is is un derly ing r elat ional dat ab ase. don e using a r esou r ce r efer en ce and r esour ce env ir onm ent r ef er ences. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic On t he Refer en ces pag e in t he deploy m en t d escr ip t or : concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful Selectimp t helem Transfer ent at ions b ean of EJBs. an d click Add . I n Part 2, we t r oduce t h eansam p le Next bank.ing applicat t hen t it y. beans, Select EJBin Refer en ce d click Click t h e Brion ow and se b ut t on imp f or lem t he ent link en field Select session beans, an d m essag ed r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also En t er p rise bean in diff er ent EJB p r oject , select I t soBan k5 EAR - > I t soBan k5 Session EJB - > im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At Bank in g, an d click OK an d t hen Fin ish t o leave t he w izar d. t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Click Add ag ain. Th is t ime, select EJB r esour ce r ef erence an d click Next . Ent er jms/ItsoMdbConnectionFactoryRef as t he n ame, an d select javax.jms.QueueConnectionFactory f or t h e t y pe and Con t ain er f or Aut h en t icat ion . Click Fin ish. Select t he new ent ry an d set t he WebSp here binding s JNDI n am e t o jms/ItsoMdbConnectionFactory. Click Add , select Resour ce en vir onm en t r efer en ce . Ent er jms/ItsoResponseQueueRef as t he nam e and select javax.jms.Queue as t he t y pe. Click Fin ish. Select t he new ent ry an d set t he WebSp here binding s JNDI n am e t o
jms/ItsoResponseQueue. Figur e 16 - 9 sh ows t he d ep loy m ent d escr ip t or w it h t he t hr ee r efer en ces.
Figu r e 1 6 -9 . Ref er e n ces i n t h e dep loy m en t d escr ip t or •
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
Sav e and close t h e deploy m en t d escrip t or. Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Setting up the server To t est t he Transfer MDB, w e h ave t o config ur e t he EjbbankServer an d wr it e a client . Let 's st ar t w it h pr epar ing t he serv er . •
Table of Content s
•
I ndex
Server configuration EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
I n t he EjbbankServer, w e h ave t o conf ig ur e a list ener por t , a connect ion f act ory , an d t he send and r eceive queues. Pub lish er: I BM Pubt he Dat e: April 20 03 Op en ser ver1 1, conf igur at ion in t he Ser ver per spect iv e: I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
On t he EJB p age, click Add f or list en er por t s and com plet e t he w ind ow as show n in Figur e 16 - 10. Click OK.
Figu r e 1 6 -1 0 . Con fi gur i ng a ser v er li st en er por t Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep1. t s and e ar it ectes urmu e. Istn m par icutlar e point n ew ions intloy r odmuced it h ipt t heor Thet ht hr eechnam attch he w nam es w out e deft he in ed in tfu henct EJB d ep ent dwescr EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er m anag ed for t he MDB an d in t he refer ences. per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful impsur leme ent EJBs. 2. Be t o at setions t heof init ial st at e as START. I n Part3. 2, we t r oduce t h efield sam is p leset bank ing applicat ion and t hen imp lem ent en t it y beans, session TheinMax r et r ies t o 0. beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent I fin m et h ods, t m ed appt in g st r ations, egies, simlim p leitclient t h at he es EJBs. f wder e used cont aindif erfer - men anag r ansact w eand could t he nsum beru se of t im t he At t he en d, w sam e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. e message w ould be r ead aft er a rollback of t h e t r ansact ion b ecause of er ror s. The p or t app ear s in t h e list of list ener por t s ( Figur e 16 - 11) .
Figu r e 1 6 -1 1 . Se r ve r con f igu r at ion w it h list e ne r p or t
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages: 75 0
On t he JMS p age, un der Ser v er Set t ings, click Add f or JMS Con nect ion Fact ories ( WASQueueConnectionFactory ent ries) and com plet e t he win dow as show n in Figur e 16 12 . Click OK.
Figu r e 1 6 -1 2 . JM S conn ect ion f a ct or y
Lear n about t he EJB 2 .0 specif icat ion
Dev elop EJBs w it h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er.
Click Add f or JMS Dest inat ions ( WASQueue ent ries) and com plet e t he win dow as show n in Figur e 16 - 13. Click OK.
Figu r e 1 6 -1 3 . JM S dest i na t i on que ue
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1 Pages:is75 0 qu eu e t o w h ich t he list ener por t for w ar ds t h e messages and t he Transfer MDB This t he get s t h e m essages.
Repeat t he pr ocess t o def in e t he response q ueue ( ItsoResponseQueue) . The MDB sends a r eply t o t he r esp onse queue and t he clien t can r et r ieve t he r esp onse. Lear n about t he EJB 2 .0 specif icat ion The com plet ed d ef init ions ar e show n in Figur e 16 - 14. Dev elop EJBs w it h St u dio App licat ion Dev eloper
Figu r e 1 6 -1 4 . Se r ve r JM S con ne ct ion f act or y an d q ue ue s Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an un derly ing r elat ional dat ab ase. I n Part 1, we in t r oduce EJBs as a p art of Jav a 2 En t er p rise Ed it ion ( J2 EE) and cover t he basic concep t s and t h e ar ch it ect ur e. I n par t icu lar w e point out t he n ew fu nct ions int r od uced w it h t he EJB 2. 0 sp ecif icat ion t hat pr ov id e, for ex am ple, en hanced fun ct ion alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f or successful imp lem ent at ions of EJBs. I n Part 2, we in t r oduce t h e sam p le bank ing applicat ion and t hen imp lem ent en t it y beans, session beans, an d m essag e- d r iv en beans using WebSph er e St u dio Ap plicat ion Dev elop er . We also im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, w e d escrib e how t o deploy EJB ap plicat ion s t o a WebSph er e App licat ion Serv er. Scroll up t o Ser ver Set t ing s. Click Add f or q ueue nam es and ent er ItsoMdbQueue. Click Add ag ain an d en t er ItsoResponseQueue. Set t h e init ial st at e t o START. The com plet ed Ser v er Set t ing s ar e show n in Figur e 16 - 15.
Figu r e 1 6 -1 5 . Se r ve r set t i ngs w i t h qu eu es
•
Table of Content s
•
I ndex
EJB 2 . 0 D e ve lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 I SBN: 0- 73 84- 26 09 -1
WebSphere Pages: 75 0MQ installation directory The d irect or y of t he W eb Sp her e MQ in st allat ion m ust be set in t he serv er v ariab les. On t he Variab les p age of t he ser ver conf igur at ion: Lear n about t he EJB 2 .0 specif icat ion Find t he ent r y f or MQ_INSTALL_ROOT an d ch eck t h at t h e v alu e point s t o t h e WebSph er e MQ Dev elop ion EJBs w it horSt dio ex App licat inst allat d irect y,ufor amp le:ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er d:/WebSphere MQ Dev elop EJBs w itvalue="List h St u dio App licat ion Dev eloper Deploy EJBs t o W eb Sp her e Applicat ion Ser v er This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans (
EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er. Thr oug hout t he book , cellpadding="5"> w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an
Number | Type | Balance im plem ent fin der m et h ods, dif fer en t m app in g st r at egies, and sim p le client s t h at u se t he EJBs. At t he en d, |
w e d escrib e how deploy EJB ap plicat ion o a WebSph er e App ion Serv er. s t |
Go back
Here are a few ex planat or y n ot es about t he JSP code: Mu lt ip le useBean t ags ar e used t o access t h e dat a collect ed by t he serv let . A loop f orm at s each result dat a ob ject int o an HTML t able. •
Table of Content s
•
I ndex
Account EJB 2 . 0 D e velisting lopm e nt w it h W ebSphe r e St udio Appl ica ti on De v el oper By Ueli Wah li, Wout er Denay er, Lar s Schu nk , Deborah Shaddon , Mart in Weiss
The b ody of t h e ReportAccounts.jsp f ile is show n in Ex am ple 17 - 8. Th e r esult dat a is for m at t ed in t able. Pub lish er: I BM
Pub Dat e: April 1 1, 20 03
Exa mI SBN: pl e 0-173784-826. 09Accoun t li st in g JSP ( abb r ev ia t e d) -1 Pages: 75 0
Lear n about t he EJB 2 .0 specif icat ion Dev elop EJBs w it h St u dio App licat ion Dev eloper Accounts Deploy EJBs t o W eb Sp her e Applicat ion Ser v er Go back This I BM Redbook pr ovides d et ailed in for m at ion on h ow t o ef fect ively use WebSp here St udio App licat ion Developer f or t he d ev elopm en t of ap plicat ion s based on t h e Ent er pr ise Jav aBeans ( EJB) ar ch it ect u re, an d d ep loym ent of su ch ap plicat ion s t o a WebSph er e App licat ion Serv er.
Thr oug hout t he book , w e pr ov id e exam ples based on a simp le bank ing app licat ion w it h an
Account | EJB 2. 0 sp ecif icat ion t hat pr ov id e, forNumber | ex am ple, en hanced fun ct ionBalance | alit y for cont ain er - m anag ed per sist ence ent it y b ean s and m essage- dr iv en beans. We also pr ovid e best pr act ice guidelines f orTx | Amount | Timestamp | successful imp lem ent at ions of EJBs. I n Part
Pub lish er: I BM Pub Dat e: April 1 1, 20 03 | I SBN: 0- 73 84- 26 09 -1 | Pages: 75 0 | Lear n about t hedata[2] EJB 2 .0 specif icat ion |