Top Banner

of 359

Ocejbcd6 Study Notes

Feb 26, 2018

Download

Documents

sureshbabubv
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 7/25/2019 Ocejbcd6 Study Notes

    1/358

    OCP JavaEE 6 EJB Developer

    Study Notes

    by

    Ivan A Krizsan

    Version July !" #$%%

    Copyri&'t #$%$(#$%% Ivan A Krizsan) All *i&'ts *eserved)

    %

  • 7/25/2019 Ocejbcd6 Study Notes

    2/358

    Table of Contents

    +able o, Contents )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))#Purpose ))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) -Stru.ture )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) -/i.ensin& )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) -Dis.lai0ers ))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) -Prere1uisites)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %$+'an2s3 )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %$

    %) Plan t'e Develop0ent o, a Business Servi.e 4odel))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))%#%)%)*easons ,or 5sin& EJBs))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %#%)#)*easons ,or Not 5sin& EJBs)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %#%))EJB Pro&ra0in& *estri.tions )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))%%)7)EJB )% /ite vs 8ull EJB )% API )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %9%)9)C'oosin& t'e +ype o, EJB))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %6%)6)Session Beans ( State,ul" Stateless or Sin&leton:)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %6

    %)6)%)State,ul Session Beans ))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %6%)6)#)Stateless Session Beans )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %!%)6))Sin&leton Session Beans)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %!

    %)!)Desi&nin& t'e Client Vie; o, Session Beans)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))%!%)

  • 7/25/2019 Ocejbcd6 Study Notes

    3/358

    7)!)Sin&leton Session Bean /i,e(Cy.le))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) 6!7)

    6)#)Inter.eptor +ypes))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %$!6)#)%)Business 4et'od Inter.eptors)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %$!6)#)#)+i0eout 4et'od Inter.eptors))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %$.ludeDe,aultInter.eptors Annotation )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %%%6)!)9)+'e ?Inter.eptor Annotation ))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %%%6)!)6)+'e ?Inter.eptors Annotation ))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %%#6)!)!)+'e ?Inter.eptorBindin& Annotation )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %%#

    6)

  • 7/25/2019 Ocejbcd6 Study Notes

    4/358

    !)%)Asyn.'ronous Session Bean E>a0ple Pro&ra0)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %%6!)#)Asyn.'ronous 4et'ods in Session Beans))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %#%

    !)#)%)Denote 4et'od Asyn.'roni.ity))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %#%!)#)#)*e1uire0ents on 4et'ods to be 4ade Asyn.'ronous ))))))))))))))))))))))))))))))))))))))))))))))))))) %##!)#))Asyn.'ronous 4et'ods and +ransa.tions ))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))%##!)#)7)Asyn.'ronous 4et'ods and Se.urity)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %##

    !))Client Vie; o, Asyn.'ronous Session Beans)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %#!))%)Asyn.'ronous 4et'ods and E>.eptions)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %#!))#)Asyn.'ronous 4et'od *eturn Values )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %#

  • 7/25/2019 Ocejbcd6 Study Notes

    5/358

    %$))%)E>a0ple o, an EJB ;it' Bean 4ana&ed +ransa.tions)))))))))))))))))))))))))))))))))))))))))))))))) %6!%$)7)Container(4ana&ed +ransa.tion De0ar.ation)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %!$

    %$)7)%)Session Syn.'ronization )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %!%%$)7)#)E>a0ple o, an EJB ;it' Container 4ana&ed +ransa.tions)))))))))))))))))))))))))))))))))))))))))%!#%$)7))+ransa.tion Attributes)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) %!7

    %$)9)J4S 4essa&in& and +ransa.tions))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))%!

    %#) S.'edule E>e.ution o, Business /o&i.)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #$-%#)%)EJB Business /o&i. S.'edulin& E>a0ple))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #$-

    %#)%)%)Create t'e [email protected])))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))#$-%#)%)#)Create t'e S.'eduled Stateless Session Bean)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #$-%#)%))*unnin& t'e E>a0ple Pro&ra0 De.larative S.'edulin&)))))))))))))))))))))))))))))))))))))))))))#%#

    %#)%)7)Pro&ra00ati. +i0er Creation E>a0ple )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #%%#)%)9)*unnin& t'e E>a0ple Pro&ra0 Pro&ra00ati. S.'edulin& ))))))))))))))))))))))))))))))))))))))#%6%#)%)6)*ollin& Ba.2 Pro&ra00ati.ally S.'eduled +i0ers))))))))))))))))))))))))))))))))))))))))))))))))))))) #%!

    %#)#)+i0eout Callba.2 4et'ods))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))#%-%#))+'e +i0er Inter,a.e))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) ##$

    %#))%)+i0er /i,e(Cy.le)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) ##%%#))#)+i0ers and Persisten.e ))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) ##%

    %#)7)De.larative S.'edulin&))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))###%#)7)%)De.larative S.'edulin& ;it' Annotations))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))###%#)7)#)De.larative S.'edulin& in t'e Deploy0ent Des.riptor))))))))))))))))))))))))))))))))))))))))))))))))##7

    %#)9)Pro&ra00ati. S.'edulin&))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))##!%#)9)%)+i0eout Callba.2 4et'ods ,or Pro&ra00ati.ally S.'eduled +i0ers))))))))))))))))))))))))) ##!%#)9)#)Creatin& Calendar +i0ers)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) ##-%#)9))Creatin& Interval +i0ers)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #$%#)9)7)Creatin& Sin&le(A.tion +i0ers)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #%%#)9)9)+i0er Con,i&uration )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))#%

    %#)6)+i0ers and +ransa.tions)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) ##%) Pa.2a&e and Deploy EJB Appli.ations)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #

    %)%)Java EE Appli.ation Asse0bly)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #%)#)Pa.2a&in& Options ))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #7

    %)#)%)EJB(JA* 8iles)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #9

    %)#)#)A* 8iles)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #!

    9

  • 7/25/2019 Ocejbcd6 Study Notes

    6/358

    %)#))JA* 8iles)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #-%)#)7)EA* 8iles )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))#-%)#)9)EJB(Client JA* 8iles))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #7$

    %))Pa.2a&in& *e1uire0ents)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #7%%)7)4odule Visibility))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #7%%)9)+'e e@b(@ar)>0l Deploy0ent Des.riptor)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #7%

    %7) Session Beans))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #77%7)%)Session Beans in t'e Deploy0ent Des.riptor)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #79%7)#)Session Bean +ypes))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))#7!%7))Session Bean 4etadata)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #7t))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #6$%9)7)#)/oo2up 5sin& EJBConte>t)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #6%

    %9)9)Si0ple Environ0ent Entries)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #6#

    %9)9)%)Si0ple Environ0ent Entry [email protected] 5sin& Annotations ))))))))))))))))))))))))))))))))))))))))))))#6%9)9)#)De,inin& Si0ple Environ0ent Entries in t'e Deploy0ent Des.riptor))))))))))))))))))))))))#6%9)9))Pro&ra00ati. /oo2up o, Si0ple Environ0ent Entires)))))))))))))))))))))))))))))))))))))))))))))))#6

    %9)6)EJB *e,eren.es)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #67%9)6)%)*e,eren.e [email protected] 5sin& Annotations ))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #67%9)6)#)De,inin& EJB *e,eren.es in t'e Deploy0ent Des.riptor )))))))))))))))))))))))))))))))))))))))))))) #69%9)6))Pro&ra00ati. /oo2up o, EJB *e,eren.es))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #6!%9)6)7)Overridin& an ?EJB Annotation ;it' t'e Deploy0ent Des.riptor )))))))))))))))))))))))))))))#6

  • 7/25/2019 Ocejbcd6 Study Notes

    7/358

    %9)%%)Persisten.e 5nit *e,eren.es))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #

    %9)%9)+i0erServi.e *e,eren.es))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #-$%9)%9)%)+i0erServi.e *e,eren.es in t'e Deploy0ent Des.riptor )))))))))))))))))))))))))))))))))))))))))))#-$

    %9)%9)#)+i0erServi.e *e,eren.e [email protected] 5sin& Annotations)))))))))))))))))))))))))))))))))))))))))))))))#-$%9)%9))Pro&ra00ati. *etrieval o, +i0erServi.e *e,eren.es)))))))))))))))))))))))))))))))))))))))))))))))) #-%

    %9)%6)EJBConte>t *e,eren.es))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #-%%9)%6)%)EJBConte>t *e,eren.es in t'e Deploy0ent Des.riptor)))))))))))))))))))))))))))))))))))))))))))))#-%%9)%6)#)EJBConte>t *e,eren.e [email protected] 5sin& Annotations))))))))))))))))))))))))))))))))))))))))))))))))) #-#%9)%6))Pro&ra00ati. *etrieval o, EJBConte>t *e,eren.es))))))))))))))))))))))))))))))))))))))))))))))))))#-#

    %6) EJBs and E>.eptions )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #-%6)%)Appli.ation E>.eptions))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))#-7

    %6)%)%)Appli.ation E>.eptions and +ransa.tions)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #-7%6)#)Syste0 E>.eptions))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #-9

    %6)#)%)Syste0 E>.eptions and +ransa.tions))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #-9

    %6))Session Bean E>.eption andlin&))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) #-9%6))%)Business Inter,a.e and No(Inter,a.e Vie; 4et'od E>.eption andlin& )))))))))))))))))))) #-6%6))#)eb Servi.e Client Vie; and EJB #)% Client Vie; 4et'od E>.eption andlin&)))))) #-!%6)))PostConstru.t and PreDestroy 4et'od E>.eption andlin&))))))))))))))))))))))))))))))))))))))) #- 8 Settin& 5p JDBC *esour.es in lass8is' v))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) 7$

    Create a JDBC Conne.tion Pool))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))7$Create a JDBC *esour.e)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) 7#

    Con,i&urin& Persisten.e Settin&s in eb Appli.ations))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) 7

    !

  • 7/25/2019 Ocejbcd6 Study Notes

    8/358

    Appendi> lass8is' Se.urity Con,i&uration))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))77Basi. Con.epts)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))77Creatin& 5sers in lass8is'))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) 76Se.urity *ole 4appin&))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))7 E0beddable EJB Container E>a0ple ))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))9$Creatin& t'e E>a0ple [email protected])))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) 9$

    5pdatin& t'e 4aven po0)>0l 8ile)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) 9Creatin& t'e EJB Class)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) 97Creatin& t'e Client Class))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) 97*unnin& t'e E>a0ple Pro&ra0 in E.lipse))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) 96Creatin& t'e Standalone Appli.ation))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) 9!*unnin& t'e Standalone Appli.ation))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) 9-

  • 7/25/2019 Ocejbcd6 Study Notes

    9/358

    Purpose

    +'ese are t'e notes I 0ade durin& t'e preparations ,or t'e OCP Java EE 6 EJB Developer.erti,i.ation) +'e ,o.us o, t'is boo2 is develop0ent o, Enterprise Java Beans a..ordin& to t'e EJB)% JS*(%a0ples related to develop0ent o, EJBs)

    8or instan.e" 'o; to set up t'e E.lipse develop0ent environ0ent" 'o; to .on,i&uredi,,erent aspe.ts o, t'e lass8is' appli.ation server and additional e>a0ples)

    Licensing

    +'is boo2 is li.ensed under a Creative Co00ons Attribution(Non.o00er.ial(No Derivative or2s)$li.ense) In s'ort t'is 0eans t'at

    Hou 0ay s'are t'is boo2 ;it' ot'ers)

    Hou 0ay not use t'is boo2 ,or .o00er.ial purposes)

    Hou 0ay not .reate derivate ;or2s ,ro0 t'is boo2)

    Disclaimers+'ou&' I 'ave done 0y best to avoid it" t'is boo2 0i&'t .ontain errors) I .annot be 'eldresponsible ,or any e,,e.ts .aused" dire.tly or indire.tly" by t'e in,or0ation in t'is boo2 youare usin& it on your o;n ris2)I .annot 0a2e any &uarantees .on.ernin& t'e .o0pleteness o, t'e in,or0ation .ontained in t'is

    boo2 i, you are preparin& ,or t'e .erti,i.ation" please do .onsult additional sour.es)Sub0ittin& any su&&estions" or si0ilar" t'e in,or0ation sub0itted be.o0es 0y property and you&ive 0e t'e ri&'t to use t'e in,or0ation in ;'atever ;ay I ,ind suitable" ;it'out .o0pensatin& youin any ;ay)All trade0ar2s are properties o, t'eir respe.tive o;ner and do not i0ply endorse0ent o, any 2ind)

    +'is boo2 'as been ;ritten in 0y spare ti0e and 'as no .onne.tion ;'atsoever ;it' 0ye0ployer)

    +'is boo2 ;as ;ritten prior to Ora.le listin& detailed in,or0ation about t'e OCPEJBD 6.erti,i.ation) +'is 0eans t'at t'is boo2 0ay not entirely .over all t'e .erti,i.ation [email protected])

    -

    http://creativecommons.org/licenses/by-nc-nd/3.0/http://creativecommons.org/licenses/by-nc-nd/3.0/http://creativecommons.org/licenses/by-nc-nd/3.0/http://creativecommons.org/licenses/by-nc-nd/3.0/
  • 7/25/2019 Ocejbcd6 Study Notes

    10/358

    Prerequisites

    'en developin& t'e e>a0ples in t'is boo2" I 'ave been usin& t'e lass8is' v appli.ation serverand E.lipse elios as t'e IDE)

    +'e version o, E.lipse I ;or2ed ;it' did not ,ind a lass8is' plu&in ;'en addin& a ne; server"

    instead I 'ad to install t'e lass8is' plu&in usin& t'e Install Ne; So,t;are))) 0enu alternative inE.lipse usin& t'e ,ollo;in& update site 'ttpdo;nload)@ava)net&lass,is'e.lipse'elios

    Thanks!

    +'an2s to t'e persons 'avin& &iven 0e ,eedba.2 on t'is boo2 and pointed out 0ista2es I 0ade3I a0 very &rate,ul ,or t'is invaluable 'elp I 'ave re.eived enablin& 0e to 0a2e t'is boo2 better)/isted in no parti.ular order

    ,ra&2a2is

    %$

    http://download.java.net/glassfish/eclipse/helioshttp://download.java.net/glassfish/eclipse/helios
  • 7/25/2019 Ocejbcd6 Study Notes

    11/358

    Part One

    Certi,i.ation +opi.s

    %%

  • 7/25/2019 Ocejbcd6 Study Notes

    12/358

    1. Plan the Development of a Business Service oel

    *e,eren.esJavaEE 6 +utorial" part IVEJB )% Spe.i,i.ation" .'apter #%)

    1.1. Reasons for Using EJBs

    So0e reasons ,or .'oosin& to i0ple0ent business servi.es as EJBs are

    S.alability is re1uired)

    EJBs .an be distributed a.ross 0ultiple .o0puters ;'ile t'eir lo.ation re0ain transparent to.lients)

    +ransa.tions are re1uired)

    Di,,erent 2inds o, .lients ;ill a..ess t'e appli.ation)

    EJBs .an be a..essed in 0ultiple ;aysL lo.al or re0ote EJBs" SOAP or *ES+ ;eb servi.e)

    4ana&e0ent o, .on.urren.y is desired)

    +'e EJB .ontainer .an 0ana&e .on.urrent a..ess to an EJB" allo;in& only a sin&le ite0 at ati0e to use t'e EJB)

    Se.urity is re1uired)

    EJBs o,,er a se.urity 0odel t'at enables se.urity to be .on,i&ured de.laratively" ;it'outa,,e.tin& t'e i0ple0entation) Pro&ra00ati. se.urity is also available)

    Portability a.ross di,,erent appli.ation servers is desired)

    Distributed deploy0ent I, re1uired" t'ere 0ust be a re0ote vie; o, t'e EJB or t'e EJB 0ust bee>posed as a ;eb servi.e" eit'er *ES+,ul or SOAP) An EJB .an also be e>posed as a 0essa&e.onsu0er)

    E>posin& an EJB as a ;eb servi.e does not re1uire any 0odi,i.ations to t'e .ode i0ple0entin& t'ebusiness lo&i. in t'e EJB)

    e ;ill loo2 .loser at transa.tions ;it' EJBs in .'apter %$and se.urity ;it' EJBs in .'apter %%)

    1.2. Reasons for Not Using EJBs

    *e,eren.es EJB )% Spe.i,i.ation" .'apter #% and ##)

    +'e ,ollo;in& reasons e>ists t'at 0ay .ause EJBs to be a less opti0al solution

    5se o, an EJB .ontainer is not desired)

    An EJB .ontainer is not available)

    One or 0ore o, t'e EJB pro&ra00in& restri.tions .annot be respe.ted)

    8or details on t'ese pro&ra00in& restri.tions" please see ne>t se.tion)

    I, t'e ,ull EJB API is .onsidered too 'eavy;ei&'t" t'ere is still t'e EJB /ite alternative" ;'i.' is asubset o, t'e EJB )% API) EJB /ite still re1uires an EJB .ontainer)

    +'ere is also an e0beddable EJB )% .ontainer" ;'i.' is only re1uired to support t'e EJB /itesubset o, t'e EJB )% API) +'e e0beddable .ontainer enables use o, t'e EJB pro&ra00in& 0odelin" ,or instan.e" des2top appli.ations)

    %#

  • 7/25/2019 Ocejbcd6 Study Notes

    13/358

    1.3. EJB Programing Restrictions

    *e,eren.es EJB )% Spe.i,i.ation" .'apter #%" se.tion #)

    +'ere are a nu0ber o, pro&ra00in& restri.tions t'at an EJB developer 0ust ,ollo;" in order toensure portability a.ross di,,erent .ontainers ad'erin& to t'e EJB )% standard) +'e ,ollo;in&restri.tions apply to bot' EJBs as ;ell as EJB inter.eptors

    Programming Restriction Motivation

    An EJB 0ust not use ;riteable stati. ,ields) Ensure .onsisten.y in a distributed environ0ent)

    An EJB 0ust not use t'read syn.'ronizationpri0itives to syn.'ronize e>e.ution o, 0ultipleinstan.es" e>.ept i, t'e EJB is a Sin&letonsession bean ;it' bean 0ana&ed .on.urren.y)

    ill not ;or2 in a distributed environ0ent)

    An EJB 0ust not use A+ ,un.tionality to&enerate output to a display or obtain input ,ro0a 2eyboard)

    Servers .o00only do not allo; a..ess to2eyboard or s.reen ,ro0 an appli.ation pro&ra0)

    An EJB 0ust not a..ess ,iles and dire.tories int'e ,ile syste0 dire.tly)

    Business .o0ponents s'ould use a resour.e0ana&er API" su.' as JDBC" to a..ess persistentdata)

    An EJB 0ust not a.t as a net;or2 server" listenon a so.2et" a..ept .onne.tions on a so.2et oruse a so.2et ,or 0ulti.ast)It 0ay a.t as a net;or2 so.2et .lient)

    Con,li.ts ;it' t'e duties o, t'e appli.ationserver)

    An EJB 0ust not atte0pt to .ir.u0vent t'ese.urity rules o, t'e Java pro&ra00in& lan&ua&e;'en a..essin& in,or0ation about ot'er .lassesor ;'en a..essin& ot'er .lasses)

    Could .o0pro0ise se.urity)

    An EJB 0ust not atte0pt to set t'e so.2et,a.tory used by ServerSocketand Socketoratte0pt to set t'e strea0 'andler ,a.tory used by5*/)

    Could .o0pro0ise se.urity) Inter,eres ;it' t'e.ontainerMs ability to 0ana&e t'e runti0eenviron0ent)

    An EJB 0ust not atte0pt to 0ana&e t'reads ort'read &roups)

    Inter,eres ;it' t'e .ontainerMs ability to 0ana&et'e runti0e environ0ent)

    An EJB 0ust not atte0pt to read or ;rite a ,iledes.riptor dire.tly)

    Could .o0pro0ise se.urity)

    An EJB 0ust not atte0pt to obtain t'e se.uritypoli.y in,or0ation ,or a parti.ular .ode sour.e)

    Could .o0pro0ise se.urity)

    An EJB 0ust not atte0pt to load a native library) Could .o0pro0ise se.urity)

    An EJB 0ust not atte0pt to .ir.u0vent t'e ruleso, t'e Java pro&ra00in& lan&ua&e ;'ena..essin& pa.2a&es and .lasses)

    Could .o0pro0ise se.urity)

    An EJB 0ust not atte0pt to de,ine a .lass in apa.2a&e)

    Could .o0pro0ise se.urity)

    An EJB 0ust not atte0pt to a..ess or 0odi,y

    se.urity .on,i&uration [email protected] Policy" Security"

    Could .o0pro0ise se.urity)

    %

  • 7/25/2019 Ocejbcd6 Study Notes

    14/358

    Provider" SignerandIdentityF)

    An EJB 0ust not atte0pt to use t'e sub.lass [email protected] substitution ,eatures o, t'e JavaSerialization Proto.ol)

    Could .o0pro0ise se.urity)

    An EJB 0ust not pass thisas a 0et'od ar&u0ent

    or result) Instead" use t'e result ,ro0 one o, t'egetXXXObject0et'ods in t'e SessionContextorEntityContext.lasses)

    +'e .ontainer 0ay use a pro>y 0e.'anis0 to"

    ,or instan.e" .ontrol .on.urrent a..ess o, t'eEJB instan.e) +'e re,eren.e [email protected] into .lientso, t'e EJB is a re,eren.e to t'e pro>y and not toan instan.e o, t'e EJB i0ple0entation .lass)

    %7

  • 7/25/2019 Ocejbcd6 Study Notes

    15/358

    1.4. EJB 3.1 Lite vs Full EJB 3.1 AP

    *e,eren.es EJB )% Spe.i,i.ation" .'apter #%" se.tion %)

    EJB /ite is a subset o, t'e ,ull EJB )% API" allo;in& .ontainer developers to i0ple0ent a EJB.ontainer o, redu.ed size and .o0ple>ity ;'ile still bein& .o0pliant ;it' t'e EJB )% spe.i,i.ation

    at least partiallyF)8or e>a0ple" t'e e0beddable EJB )% .ontainer is" as be,ore" only re1uired to support t'e EJB /iteAPI)

    +'e ,ollo;in& table lists ,eatures o, t'e EJB )% API and indi.ates t'eir availability in t'e ,ull EJB)% API and t'e EJB )% /ite API

    Feature Full EJB 3.1 API EJB 3.1 Lite API

    Java Persisten.e #)$ Available Available

    Session beans lo.alno inter,a.e.lient vie;)

    Available Available

    Session beans )$ re0ote .lientvie;)

    Available Not available

    Session beans #)> re0ote .lientvie;)

    Available Not available

    Session beans e>posed as JAG(S ;eb servi.e endpoints)

    Available Not available

    Session beans e>posed as JAG(*PC ;eb servi.e endpoints)

    Available Not available

    EJB ti0er servi.e) Available Not available

    Asyn.'ronous invo.ation o,session beans)

    Available Not available

    Inter.eptors) Available Available

    *4I(IIOP interoperability) Available Not available

    Bean and .ontainer 0ana&edtransa.tions)

    Available Available

    De.larative and pro&ra00ati.se.urity)

    Available Available

    E0beddable API) Available" but e0beddable.ontainer only re1uired tosupport EJB )% /ite)

    Available

    %9

  • 7/25/2019 Ocejbcd6 Study Notes

    16/358

    1.!. "#oosing t#e $%&e of EJB

    *e,eren.es JavaEE +utorial" .'apter %7)

    +'ere are t;o types o, EJBs

    Session beans)

    4essa&e(driven beans)

    Session beans are .o0ponents t'at .ontains business lo&i. t'at .an be invo2ed over lo.al" re0ote or;eb servi.e .lient vie;s)

    4essa&e(driven beans are .o0ponents t'at" upon re.eivin& an asyn.'ronous 0essa&e" e>e.utesso0e business lo&i.) 5sually 0essa&e(driven beans listen ,or J4S 0essa&es)

    Di,,erent reasons ,or .'oosin& to use session(driven beans are dis.ussed in t'e ne>tse.tion)

    *easons ,or usin& 0essa&e(driven beans are dis.ussed in a subse1uentse.tion)

    1.'. (ession Beans ) (tateful* (tateless or (ingleton++'ere are t'ree di,,erent types o, session beansL state,ul" stateless and sin&leton) +'e ,ollo;in&se.tions su00arizes so0e .'ara.teristi.s o, t'e di,,erent types and &ives reasons ,or .'oosin& a.ertain type)

    1.".1.Stateful Session Beans

    State,ul session beans ;ill retain state durin& t'e .onversation ;it' one sin&le .lient)

    *easons ,or .'oosin& state,ul session beans are

    +'e EJB needs to retain state related to a .lient a.ross 0ultiple 0et'od invo.ations) +'e EJB 0ediates bet;een a .lient and ot'er .o0ponents o, t'e appli.ation" presentin& a

    si0pli,ied vie; to t'e .lient)

    Be'ind t'e s.enes" t'e EJB 0ana&es t'e ;or2,lo; o, several EJBs)

    %6

  • 7/25/2019 Ocejbcd6 Study Notes

    17/358

    1.".#.Stateless Session Beans

    Stateless session beans 0ay e>ist in a pool ,ro0 ;'i.' t'e .ontainer 0ay .'oose any bean to servea re1uest) A stateless session bean 0ay 0aintain state in t'e ,or0 o, instan.e variables" but t'e state.annot be asso.iated ;it' a spe.i,i. .lient) +'e relation bet;een an instan.e o, a stateless session

    bean and a .lient only lasts ,or one sin&le 0et'od invo.ation)

    *easons ,or .'oosin& stateless session beans are

    +'e EJB does not need to retain .lient(spe.i,i. state)

    Better s.alability is desired)

    Better per,or0an.e is desired)

    No need to 0aintain state and 0ultiple instan.es bein& available to server re1uests &ivesbetter per,or0an.e)

    +'e ,un.tionality o, t'e EJB is to be e>posed as a ;eb servi.e)

    +'e EJB per,or0s one or 0ore &eneri. tas2s" ,or instan.e sendin& an e0ail" t'at .an be

    ,inis'ed durin& one sin&le 0et'od invo.ation)

    1.".$.Singleton Session Beans

    Sin&leton session beans are session beans ,or ;'i.' ea.' type t'ere is only one sin&le instan.edurin& t'e li,eti0e o, an appli.ation)

    *easons ,or .'oosin& sin&leton session beans are

    So0e state needs to be s'ared t'rou&'out t'e appli.ation)

    One sin&le EJB is to be a..essed by 0ultiple t'reads .on.urrently)

    +'e appli.ation re1uires an EJB to per,or0 startup or s'utdo;n tas2sF)

    +'e ,un.tionality o, t'e EJB is to be e>posed as a ;eb servi.e)

    (ingleton (ession Bean "oncurrent Access

    Sin&leton session beans .an be .on,i&ured to allo; 0ultiple t'reads to e>e.ute in an instan.e.on.urrently) 5nder su.' .ir.u0stan.es" spe.ial .are 'ave to be ta2en ;'en desi&nin& t'e sin&letonsession bean in order to" ,or instan.e" prote.t resour.es t'at 0ust not be a..essed .on.urrently)8or 0ore details" please re,er to .'apter ,our)

    1.,. -esigning t#e "lient ie/ of (ession Beans

    Co00on ,or all types o, session beans is t'at .lients ;ill see a business inter,a.e or a no(inter,a.evie; o, t'e session bean) +'e latter bein& .o0prised by t'e publi. 0et'ods in t'e .lassi0ple0entin& t'e session bean)Care s'ould be ta2en desi&nin& business inter,a.es and no(inter,a.e vie;s" sin.e any ,uture .'an&esin t'ese ;ill re1uire .orrespondin& .'an&e in all .lients) Properly desi&ned inter,a.es and no(inter,a.e vie;s ;ill also 'elp s'ieldin& .lients ,ro0 .o0ple>ities in t'e EJB tier)

    %!

  • 7/25/2019 Ocejbcd6 Study Notes

    18/358

    1.0. Remote or Local Access of (ession Beans

    *e,eren.es EJB )% Spe.i,i.ation" se.tion )#)

    One o, t'e ,irst desi&n(de.isions t'at 'as to be ta2en ;'en desi&nin& a Java EE appli.ation is ;'i.'type o, .lients you allo; to a..ess a session beanL lo.al" re0ote or ;eb servi.e)

    1.%.1.Choosing Client &ccess T'pe

    As be,ore" t'e ,ollo;in& t'ree types a..ess e>ist ,or session beans

    /o.al A..ess

    +'e session beans is a..essed ,ro0 ;it'in t'e sa0e appli.ation)Possible .lients are ;eb .o0ponents and ot'er EJBs)

    *e0ote A..ess

    +'e session bean is a..essed ,ro0 t'e sa0e or ,ro0 anot'er JV4) +'e JV4 in ;'i.' t'e.lient e>e.utes 0ay be deployed on a di,,erent .o0puter)

    Note t'at re0ote a..ess o, an EJB also is allo;ed ,or a .lient runnin& in t'e sa0e JV4 ast'e EJB)Possible .lients are ;eb .o0ponents" ot'er EJBs and appli.ation .lients)

    eb Servi.e A..ess

    +'e session bean is e>posed as a ;eb servi.e" SOAP or *ES+,ul" allo;in& any ;eb servi.e.lient to a..ess t'e EJB) Su.' .lients 0ay" ,or instan.e" be i0ple0ented in anot'erlan&ua&e) eb servi.e a..ess is 0ainly .'osen to provide interoperability ;it' e>ternalsyste0s)

    It s'ould be noted t'at lo.al a..ess does not e>.lude re0ote a..ess o, an EJB) An EJB 0ay allo;bot' lo.al and re0ote a..ess)

    %

  • 7/25/2019 Ocejbcd6 Study Notes

    19/358

    'et'er to sele.t lo.al or re0ote a..ess o, an EJB 0ay depend on one or 0ore o, t'e ,ollo;in&,a.tors

    +i&'t or loose .ouplin& bet;een an EJB and its .lients EJB operation &ranularity)

    I, t'e .lient .losely depends on t'e EJB to per,or0 its duties and o,ten invo2e t'e EJB" t'enit is 0ore ti&'tly .oupled to t'e EJB and ;ill bene,it ,ro0 lo.al a..ess" ;'i.' ;ill i0prove

    per,or0an.e)

    +ype o, .lients)

    I, t'e EJB is a..ess by appli.ation .lients" t'en it needs to provide re0ote a..ess" sin.e su.'.lients al0ost al;ays run on ot'er 0a.'ines)eb .o0ponents and ot'er EJBs 0ay a..ess t'e EJB lo.ally or re0otely" dependin& on 'o;t'e appli.ation is distributed)

    Co0ponent distribution)

    'en usin& JavaEE" server(side .o0ponents 0ay be distributed on several di,,erent nodesto ,a.ilitate better s.alability) 'en distributin& ;eb .o0ponent on one 0a.'ine and t'eEJBs i0ple0entin& t'e business ,un.tionality on anot'er 0a.'ine" t'e EJBs need to provide

    re0ote a..ess) /o.ation independen.e)

    An EJB providin& a re0ote vie; .an be deployed in t'e sa0e .ontainer as t'e .lient or on are0ote .ontainer" ;'ile an EJB providin& only a lo.al vie; are only a..essible by .lients int'e sa0e appli.ation as t'e EJB) An EJB ;it' a re0ote vie; is 0ore ,le>ible in t'at it 0ay

    be 0oved as desired)

    Para0eters and return values 0ust be serializable ,or EJBs ;it' a re0ote vie;)

    I, passin& lar&e a0ounts o, data bet;een an EJB and its .lients" serialization o, t'e data 0ay.ause over'ead) it' lo.al a..ess to an EJB" para0eters are passed by re,eren.e andserialization over'ead is t'us avoided t'e EJB .an operate on t'e data and t'e .lient"

    'oldin& a re,eren.e to t'e sa0e data" ;ill see t'e 0odi,i.ations) Additional error .ases due to re0ote .o00uni.ation)

    Per,or0an.e

    *e0ote .alls o, EJBs al;ays in.ur a per,or0an.e penalty" sin.e it 0eans a..ess over so0enet;or2) o;ever" distribution o, .o0ponents on several nodes 0ay i0prove t'e overall

    per,or0an.e o, t'e syste0) 4easurin& t'e per,or0an.e o, a syste0 in di,,erent.on,i&urations is t'e best ;ay o, learnin& about t'e .'ara.teristi.s o, a parti.ularappli.ation)

    1.%.#.&ccess T'pe an ParametersDependin& on ;'i.' type o, .lients ;ill a..ess an EJB" as dis.ussed in t'e previous session" so0e.are 0ay also need to be ta2en ;'en .onsiderin& para0eters and return data o, EJB 0et'ods)

    Please re,er to t'e se.tion on 4et'od Para0etersin t'e .'apter on Session Beans,or details)

    An EJB t'at is to provide a re0ote vie; to .lients are typi.ally desi&ned as to be 0ore .oarse(&rained ;it' ,e;er intera.tions bet;een .lient and EJB" ;'ile an EJB ;it' a lo.al vie; 0ay bedesi&ned as to be 0ore ,ine(&rained)

    %-

  • 7/25/2019 Ocejbcd6 Study Notes

    20/358

    1.. essage -riven Beans

    4essa&e(driven beans listen ,or 0essa&es" usually J4S 0essa&es" and" upon re.eipt o, a 0essa&e"e>e.utes so0e business ,un.tion) 4essa&e(driven EJBs s'are t'e ,ollo;in& .'ara.teristi.s ;it'stateless session beans

    *etains no .lient(spe.i,i. state) +'e .ontainer .an assi&n any instan.e o, an EJB to serve a .lient re1uest)

    Instan.es 0ay be pooled by t'e .ontainer)

    A sin&le EJB instan.e .an pro.ess re1uests ,ro0 0ultiple .lients)

    +'ou&' at 0ost one sin&le t'read at a ti0e .an e>e.ute on a 0essa&e(driven bean instan.e)

    *easons ,or .'oosin& 0essa&e(driven beans are

    Business ,un.tionality is to be invo2ed by asyn.'ronous 0essa&in&)

    A lon&(runnin& business pro.ess is to be per,or0ed)

    aitin& ,or a response ,ro0 su.' a pro.ess 0ay 'old up t'e .lient ,or a lon&er period o,ti0e" ;'i.' 0ay not be desirable) Syn.'ronous invo.ation ties up resour.es ,or t'e durationo, t'e .all" ;'i.' 0ay .ause de&radation in per,or0an.e and s.alability i, 0any" lon&(runnin&" tas2s are to be per,or0ed)

    4ore loose .ouplin& bet;een .lient and t'e EJB is desired)

    Clients does not see t'e EJB" instead it @ust sends a 0essa&e to so0e 0essa&e listener)

    1.(.1.)ueue or Topic

    +'ere are t;o di,,erent 0essa&in& 0odels t'at .an be e0ployed by 1ueues t'at 0essa&e drivenbeans are listenin& toL 1ueue or topi.)

    +'e 1ueue 0essa&in& 0odel is also re,erred to as t'e point(to(point 0essa&in& 0odel" ;'ile t'etopi. 0essa&in& 0odel is also re,erred to as t'e publis'(and(subs.ribe or pubsub 0essa&in& 0odel)

    Co00on ,or bot' t'ese 0essa&in& 0odels are

    4essa&es ,ro0 produ.er to .onsu0ersF are e>.'an&ed t'rou&' virtual .'annels)

    +'e type o, virtual .'annel depends on t'e 0essa&in& 0odel)

    Produ.ers and .onsu0ers are de.oupled)

    Produ.ers and .onsu0ers o, 0essa&es does not 2no; about ea.' ot'er" apart ,ro0 t'e ,a.tt'at t'ere are 0essa&es ,ro0 t'e ,or0er delivered to t'e latter)

    Produ.es and .onsu0ers o, 0essa&es .an be dyna0i.ally added at runti0e)

    4essa&es are pus'ed to 0essa&e driven beans re&ardless o, t'e 0essa&in& 0odel)*e&ular J4S 0essa&e .onsu0ers .an pull 0essa&es ,ro0 a 1ueue but 0essa&e driven beansdoes not 'ave t'is option)

    C'ara.teristi. o, t'e point(to(point 0essa&in& 0odel 1ueuesF are

    +'e virtual .'annel used to e>.'an&e 0essa&es is .alled a 1ueue)

    A 0essa&e sent to a 1ueue is delivered to e>a.tly one .onsu0er)

    4essa&es are delivered in t'e order in ;'i.' t'ey ;ere produ.ed)

    5sin& di,,erent 0essa&e priorities 0ay a,,e.t t'e order in ;'i.' 0essa&es are delivered)

    4essa&es sent to a 1ueue .an be persistent or non(persistent)

    Persistent 0essa&es are retained in t'e .ase o, J4S provider ,ailures)

    A 0essa&e sent to a 1ueue 0ay be assi&ned a priority)

    #$

  • 7/25/2019 Ocejbcd6 Study Notes

    21/358

    A 0essa&e sent to a 1ueue 0ay be assi&ned an e>piration ti0e)

    C'ara.teristi.s o, t'e publis'(and(subs.ribe 0essa&in& 0odel topi.sF are

    +'e virtual .'annel used to e>.'an&e 0essa&es is .alled a topi.)

    A 0essa&e sent to a topi. is delivered to all .onsu0ers o, t'e topi.)

    4essa&e ,ilterin& 0ay .ause so0e .onsu0ers not to re.eive so0e 0essa&es)

    #en $o Use ueues

    =ueues are suitable ;'en an appli.ation ;ant to" ,or instan.e" divide ;or2 a0on& a nu0ber o,;or2ers ( t'e ;or2ers all listen to t'e 1ueue but ea.' 0essa&e is only re.eived by one o, t'e;or2ers) +'e produ.er 2no;s t'at t'e unit o, ;or2 ;ill eventually be re.eived and pro.essed byone o, t'e ;or2ers)=ueues are also suitable ,or reliable asyn.'ronous .o00uni.ation bet;een t;o partiesL t'e ,irst

    party sends 0essa&es to a 1ueue ;'i.' are later delivered to t'e ot'er party) +'e ot'er party repliesby sendin& anot'er 0essa&e to anot'er 1ueue" ;'i.' t'e ,irst party is a .onsu0er o,)

    #en $o Use $o&ics

    +opi.s are suitable ;'en 0ultiple .onsu0ers are to re.eive a .opy o, ea.' 0essa&e produ.ed)Co00only topi.s are used ;'en t'e produ.er o, so0e in,or0ation ;ant to &ive t'e opportunity o,interested parties to re.eive t'e in,or0ation) +'e produ.er .o00only does not .are about ;'ore.eives t'e in,or0ation)A .o00only used e>a0ple o, ;'en to use a topi. is in a sto.2 1uote appli.ation)

    #%

  • 7/25/2019 Ocejbcd6 Study Notes

    22/358

    #. Create a Stateful Session Bean

    *e,eren.es EJB )% Spe.i,i.ation" .'apter 7)

    In t'is .'apter ;e loo2 at 'o; to .reate di,,erent 2inds o, state,ul session beans)'at .'ara.terizes a state,ul session bean is t'at it is tied to one sin&le .lient and only serves

    re1uests ,ro0 t'at .lient) +'e state,ul session bean 0ay" as i0plied by t'e na0e" 'old .lient(relatedstate) State,ul session beans 0ay also" as ;e ;ill see ;'en loo2in& at t'e li,e(.y.le o, state,ulsession beans" be te0porarily ta2en out o, servi.e passivatedF and later" ;'en need arises" ta2en

    ba.2 into servi.e a.tivatedF)Detailed in,or0ation applyin& to session beans .an be ,ound in .'apter %7) Clients to session beans;ill be dis.ussed in .'apter ,ive)

    2.1. "reate a (tateful (ession Bean /it# a Local No)interface ie/

    +'e state,ul session bean 'as one sin&le 0et'od t'at .reates a &reetin&) +'e EJB is developed in a;eb [email protected]" sin.e t'e session bean ;ill only 'ave a lo.al no(inter,a.eF vie; and .an t'us only be

    a..essed by .lients in t'e sa0e appli.ation) In t'is e>a0ple ;eMll also .reate a .lient to t'e sessionbean)

    Create a dyna0i. ;eb [email protected] in E.lipse" as des.ribed in appendi> A)

    I .all 0y [email protected] State,ulSession%eb)

    In t'e pa.2a&e com.ivan.scbcd6" .reate t'e session bean i0ple0entation .lass i0ple0ented

    as t'is

    package com.ivan.scbcd6;

    import java.util.Date;import java.util.concurrent.TimeUnit;import javax.annotation.PostConstruct;

    import javax.annotation.PreDestroy;import javax.ejb.LocalBean;import javax.ejb.Remove;import javax.ejb.Stateful;import javax.ejb.StatefulTimeout;import javax.ejb.TransactionAttribute;import javax.ejb.TransactionAttributeType;

    /*** Simplest possible stateful session bean exposing a local, no-interface view.*/@Stateful@LocalBean@StatefulTimeout(value=10, unit=TimeUnit.SECONDS)@TransactionAttribute(TransactionAttributeType.NEVER)public class StatefulSession1Bean{ private static int sCurrentInstanceNumber = 1;

    private int mInstanceNumber;

    @PostConstruct public void initialize() { mInstanceNumber = sCurrentInstanceNumber++; System.out.println("*** StatefulSession1Bean " + mInstanceNumber + " created."); }

    @PreDestroy public void destroy() { System.out.println("*** StatefulSession1Bean " + mInstanceNumber +

    " destroyed.");

    ##

  • 7/25/2019 Ocejbcd6 Study Notes

    23/358

    }

    @Remove public void remove() { System.out.println("*** StatefulSession1Bean " + mInstanceNumber + " remove."); }

    /** * Creates a greeting to the person with the supplied name. *

    * @param inName Name of person to greet. * @return Greeting. */ public String greeting(final String inName) { Date theCurrentTime = new Date();

    String theMessage = "Hello " + inName + ", I am stateful session bean " + mInstanceNumber + ". The time is now: " + theCurrentTime; return theMessage; }}

    Create t'e pa.2a&e com.ivan.scbcd6.clientand in it" .reate t'e .lient servlet i0ple0ented as

    ,ollo;s

    package com.ivan.scbcd6.client;

    import java.io.IOException;import java.io.PrintWriter;

    import javax.annotation.PostConstruct;import javax.ejb.EJB;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;

    import com.ivan.scbcd6.StatefulSession1Bean;

    /*** Servlet implementing a local EJB client.** @author Ivan A Krizsan*/@WebServlet(name = "StatefulSession1Servlet", urlPatterns = "/test.do")public class StatefulSession1Servlet extends HttpServlet{ /* Constant(s): */ private static final long serialVersionUID = 1L;

    /* Instance variable(s): */ @EJB

    private StatefulSession1Bean mStatefulSessionBean;

    /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ @Override protected void doGet(HttpServletRequest inRequest, HttpServletResponse inResponse) throws ServletException, IOException { PrintWriter theResponseWriter = inResponse.getWriter();

    String theRequestNameParam = inRequest.getParameter("name"); if (theRequestNameParam == null) { theRequestNameParam = "Anonymous Coward"; } String theResponse = mStatefulSessionBean.greeting(theRequestNameParam);

    theResponseWriter.println("Response from the EJB: " + theResponse);

    #

  • 7/25/2019 Ocejbcd6 Study Notes

    24/358

    }}

    Deploy t'e [email protected] to t'e lass8is' appli.ation server)

    *ead all o, t'e instru.tions belo;)

    +'is e>a0ple depends on t'e ti0in& bet;een re1uests so you s'ould be prepared)I, too 0u.' ti0e passes bet;een re1uests t'en re(deploy t'e appli.ation and start over)

    In a bro;ser" issue a re1uest to t'e ,ollo;in& 5*/

    'ttplo.al'ost

  • 7/25/2019 Ocejbcd6 Study Notes

    25/358

    Any subse1uent re1uests" despite bein& 0ade ;it'in %$ se.onds o, t'e prior re1uest" ;ill

    result in aNoSuchE!Exce"tionbein& t'ro;n)

    Note t'at

    +'e session bean i0ple0entation .lass is annotated ;it' t'e ?/o.alBean annotation)

    +'is annotation is used to annotate session beans t'at e>pose a no(inter,a.e vie;)I, t'e session bean does not e>pose any ot'er vie;" t'en t'e ?/o.alBean annotation isoptional and t'e session bean ;ill e>pose a no(inter,a.e vie;)

    +'e session bean i0ple0entation .lass is annotated ;it' t'e ?State,ul annotation)

    +'is annotation is used to annotate session beans t'at are to be state,ul)8or details about t'e options available ;it' t'e ?State,ul annotation" please see t'e se.tionon Session Bean 4etadatain t'e .'apter on session beans)

    +'e session bean i0ple0entation .lass is annotated ;it' t'e ?State,ul+i0eout annotation)

    +'is annotation is used to tell t'e .ontainer t'e idle ti0e a,ter ;'i.' an instan.e o, t'estate,ul session bean ;ill be eli&ible ,or re0oval)

    'en ;aitin& too lon&" %$ se.onds in t'e .ase o, t'is e>a0ple" t'e EJB instan.e is re0ovedby t'e .ontainer) Any subse1uent atte0pts to invo2e t'e EJB instan.e ;ill .ause aNoSuchE!Exce"tionto be t'ro;n)

    +'e session bean i0ple0entation .lass is annotated ;it' t'e

    ?+ransa.tionAttribute+ransa.tionAttribute+ype)NEVE*F annotation)+'is is to ensure t'at instan.es o, t'e EJB is never in t'e 4et'od *eady in +ransa.tion state"in ;'i.' .ase t'ey ;ill never ti0eout and be.o0e eli&ible ,or re0oval as des.ribed aboveF)

    As be,ore" ;'en a session bean 'as a no(inter,a.e vie;" t'e business 0et'ods are all t'e

    publi. 0et'ods in t'e bean i0ple0entation .lass)

    +'e re0ove 0et'od in t'e session bean i0ple0entation .lass is annotated ;it' t'e?*e0ove annotation)

    'en t'e servlet .alls t'e remove0et'od on an instan.e o, t'e state,ul session bean" it

    si&nals t'at it is done ;it' t'e state,ul session bean and t'e .ontainer disposes t'e EJBinstan.e" as seen in t'e .onsole lo&)

    #9

  • 7/25/2019 Ocejbcd6 Study Notes

    26/358

    2.2. "reate a (tateful (ession Bean /it# a Local Business nterface

    Co0pared to t'e state,ul session bean developed in t'e previous se.tion" t'e state,ul session beandeveloped in t'is se.tion" a session bean ;it' a lo.al business inter,a.e" di,,ers in t'e ,ollo;in&areas

    +'e session bean i0ple0entation .lass is not annotated ;it' t'e ?/o.alBean annotation) +'e session bean i0ple0entation .lass i0ple0ents an inter,a.e)

    +'e EJB is developed in a ;eb [email protected]" sin.e t'e session bean ;ill only 'ave a lo.al vie;" albeit;it' a lo.al business inter,a.e" and .an t'us only be a..essed by .lients in t'e sa0e appli.ation)

    Create a dyna0i. ;eb [email protected] in E.lipse" as des.ribed in appendi> A)

    IMll .all 0y [email protected] State,ulSession#eb)

    In t'e ne; [email protected]" .reate t'e session bean i0ple0entation .lass as belo;

    package com.ivan.scbcd6;

    import java.util.Date;

    import javax.annotation.PostConstruct;import javax.ejb.Stateful;

    /*** Simplest possible stateful session bean exposing a local business interface view.*/@Statefulpublic class StatefulSession2Bean implements StatefulSession2Local{ private static int sCurrentInstanceNumber = 1;

    private int mInstanceNumber;

    @PostConstruct public void initialize()

    { mInstanceNumber = sCurrentInstanceNumber++; System.out.println("*** StatefulSession2Bean " + mInstanceNumber + " created."); }

    /* (non-Javadoc) * @see com.ivan.scbcd6.StatefulSession2Local#greeting(java.lang.String) */ @Override public String greeting(final String inName) { Date theCurrentTime = new Date();

    String theMessage = "Hello " + inName + ", I am stateful session bean " + mInstanceNumber + ". The time is now: " + theCurrentTime;

    return theMessage; }}

    Create t'e lo.al business inter,a.e

    package com.ivan.scbcd6;

    import javax.ejb.Local;

    @Localpublic interface StatefulSession2Local{

    /**

    * Creates a greeting to the person with the supplied name.

    #6

  • 7/25/2019 Ocejbcd6 Study Notes

    27/358

    ** @param inName Name of person to greet.

    * @return Greeting. */ public String greeting(final String inName);

    }

    Create a .lient servlet as in t'eprevious e>a0ple)+'e .lient servlet is to 'ave an instan.e variable ;it' t'e type o, t'e lo.al business inter,a.eState#ulSession%&ocalF in ;'i.' a re,eren.e to an instan.e o, t'e state,ul session bean [email protected])

    Deploy t'e [email protected] to t'e lass8is' appli.ation server)

    In a bro;ser" issue a re1uest to t'e ,ollo;in& 5*/

    'ttplo.al'osttend any ot'er inter,a.e)

    it' EJB #)% and earlier" t'e lo.al business inter,a.e 'ad to e>tend t'ejavax.ejb.E!&ocalObject) +'is is no lon&er ne.essary)

    +'e State#ulSession$&ocalinter,a.e is annotated ;it' t'e ?/o.al annotation)

    +'e ?/o.al annotation .an be applied to bot' t'e session bean i0ple0entation .lass" in;'i.' .ase it also needs to spe.i,y ;'i.' inter,a.esF de,ines t'e lo.al vie;" or to t'einter,a.esF ;'i.' de,ines t'e lo.al vie;" in ;'i.' .ase no ,urt'er ele0ents need to bespe.i,ied)

    #!

    http://localhost:8080/StatefulSession2Web/test.do?name=Ivanhttp://localhost:8080/StatefulSession2Web/test.do?name=Ivan
  • 7/25/2019 Ocejbcd6 Study Notes

    28/358

    2.3. "reate a (tateful (ession Bean /it# a Remote Business nterface

    Co0pared to t'e state,ul session bean developed in t'e previous se.tion" t'e state,ul session beandeveloped in t'is se.tion" a session bean ;it' a re0ote business inter,a.e" di,,ers in t'e ,ollo;in&areas

    +'e inter,a.e i0ple0ented by t'e session bean i0ple0entation .lass is annotated ;it' t'e?*e0ote annotation)

    +'e session bean ;it' a re0ote vie; .an be .reated in an EJB [email protected]" sin.e it 0ay be a..essed by.lients outside o, t'e appli.ation in ;'i.' it is deployed)

    Create an EJB [email protected] in E.lipse" as des.ribed in appendi> B)

    In t'e ne; [email protected]" .reate t'e session bean i0ple0entation .lass as belo;

    package com.ivan.scbcd6;

    import java.util.Date;import javax.ejb.Stateful;

    /*** Simplest possible stateful session bean with a remote business interface view.*/@Statefulpublic class StatefulSession3Bean implements StatefulSession3Remote{ private static int sCurrentInstanceNumber = 1;

    private int mInstanceNumber;

    @PostConstruct public void initialize() { mInstanceNumber = sCurrentInstanceNumber++; System.out.println("*** StatefulSession3Bean " + mInstanceNumber + " created.");

    }/* (non-Javadoc)

    * @see com.ivan.scbcd6.StatefulSession3Remote#greeting(java.lang.String) */ @Override public String greeting(final String inName) { Date theCurrentTime = new Date();

    String theMessage = "Hello " + inName + ", I am stateful session bean " + mInstanceNumber + ". The time is now: " + theCurrentTime; return theMessage; }}

    Create t'e re0ote business inter,a.e

    package com.ivan.scbcd6;

    import javax.ejb.Remote;

    /*** Remote business interface of the stateful session bean.*/@Remotepublic interface StatefulSession3Remote{ /** * Creates a greeting to the person with the supplied name. *

    * @param inName Name of person to greet. * @return Greeting.

    #

  • 7/25/2019 Ocejbcd6 Study Notes

    29/358

    */ public String greeting(final String inName);

    }

    Create a .lient servlet as in t'eprevious e>a0ple)

    +'e .lient servlet is to 'ave an instan.e variable ;it' t'e type o, t'e re0ote business

    inter,a.e State#ulSession'(emoteF in ;'i.' a re,eren.e to an instan.e o, t'e state,ul sessionbean is [email protected])

    Deploy t'e [email protected] to t'e lass8is' appli.ation server)

    *e0ote .lients a..essin& t'e state,ul session bean ;it' t'e re0ote business inter,a.e ;ill bedeveloped in t'e se.ond se.tion o, t'e .'apter on session bean .lients)

    Note t'at

    +'e session bean i0ple0entation .lass is annotated ;it' t'e ?State,ul annotation)

    +'e session bean i0ple0entation .lass i0ple0ents an inter,a.e)

    +'is is t'e re0ote business inter,a.e)

    +'e re0ote business inter,a.e does not e>tend any ot'er inter,a.e)

    it' EJB #)% and earlier" t'e re0ote business inter,a.e 'ad to e>tend t'ejavax.ejb.E!Object) +'is is no lon&er ne.essary)

    +'e State#ulSession$(emoteinter,a.e is annotated ;it' t'e ?*e0ote annotation)

    +'e ?*e0ote annotation .an be applied to bot' t'e session bean i0ple0entation .lass" in;'i.' .ase it also needs to spe.i,y ;'i.' inter,a.esF de,ines t'e re0ote vie;" or to t'einter,a.esF ;'i.' de,ines t'e re0ote vie;" in ;'i.' .ase no ,urt'er ele0ents need to bespe.i,ied)

    #-

  • 7/25/2019 Ocejbcd6 Study Notes

    30/358

    2.4. "reate a (tateful (ession Bean /it#out Annotations

    In t'is se.tion ;e ;ill .reate a state,ul session bean ;it'out usin& annotations" .on,i&urin& it in t'ee@b(@ar)>0l deploy0ent des.riptor) +'e i0ple0entation o, t'e session bean is identi.al to t'at in t'e

    previous se.tions)+'e EJB is developed in a ;eb [email protected]" sin.e t'e session bean ;ill only 'ave a lo.al no(inter,a.eF

    vie; and .an t'us only be a..essed by .lients in t'e sa0e appli.ation) In se.tion one in .'apter ,ive;eMll i0ple0ent a servlet a.tin& as .lient to t'e EJB)

    Create a dyna0i. ;eb [email protected] in E.lipse" as des.ribed in appendi> A)

    I .all 0y [email protected] State,ulSession7eb)

    In t'e pa.2a&e com.ivan.scbcd6" .reate t'e session bean i0ple0entation .lass i0ple0ented

    as belo;

    package com.ivan.scbcd6;

    import java.util.Date;

    /*** Simplest possible stateful session bean without using annotations.*/public class StatefulSession4Bean{

    private static int sCurrentInstanceNumber = 1;

    private int mInstanceNumber;

    @PostConstruct public void initialize() { mInstanceNumber = sCurrentInstanceNumber++; System.out.println("*** StatefulSession4Bean " + mInstanceNumber + " created."); }

    /** * Creates a greeting to the person with the supplied name. *

    * @param inName Name of person to greet. * @return Greeting. */ public String greeting(final String inName) { Date theCurrentTime = new Date();

    String theMessage = "Hello " + inName + ", I am stateful session bean " + mInstanceNumber + ". The time is now: " + theCurrentTime; return theMessage; }}

    In t'e ebContentEB(IN8 dire.tory" .reate a ,ile na0ed e@b(@ar)>0l ;it' t'e ,ollo;in&

    .ontents

    $

  • 7/25/2019 Ocejbcd6 Study Notes

    31/358

    StatefulSession4Bean com.ivan.scbcd6.StatefulSession4Bean

    Stateful

    Create a .lient servlet in t'e sa0e ;ay as in t'e ,irst se.tion on .reatin& state,ul session

    beansbut ;it' t'e type o, t'e instan.e variable in ;'i.' t'e EJB re,eren.e is to be [email protected]& State#ulSession)!ean)

    Deploy t'e [email protected] to t'e lass8is' appli.ation server)

    In a bro;ser" issue a re1uest to t'e ,ollo;in& 5*/

    'ttplo.al'ost

  • 7/25/2019 Ocejbcd6 Study Notes

    32/358

    2.!. (tateful (ession Bean Life)"%cle

    *e,eren.es EJB )% Spe.i,i.ation" se.tion 7)6)

    +'e state,ul session bean li,e(.y.le is t'e 0ost .o0ple> a0on& all t'e EJBs" ;'i.' .an be seen int'e ,ollo;in& ,i&ure

    /i,e(.y.le o, a state,ul session bean)

    +'e red bo>es denote di,,erent states o, a state,ul session bean)+'e &reen bo>es denote .lient events t'at .auses a state,ul session bean to .'an&e state)

    +e>t t'at is not bo>ed and not .olored denote .allba.2F 0et'ods t'at are invo2ed on a state,ulsession bean in .onne.tion to a state transition)

    +'e ti0e a state,ul session bean 0ust be ;it'out re1uests be,ore bein& .onsidered ,or re0oval .anbe spe.i,ied usin& t'e ?State,ul+i0eout annotation) +'e ti0eout 0ay also be .on,i&ured so t'at asession bean ;ill never be re0oved due to ti0eout) Note t'at a state,ul session bean 0ay not beti0ed out ;'ile bein& in t'e 4et'od *eady in +ransa.tion state)

    #

  • 7/25/2019 Ocejbcd6 Study Notes

    33/358

    2.'. 5&erations Allo/e6 in et#o6s of a (tateful (ession Bean

    *e,eren.es EJB )% Spe.i,i.ation" se.tion 7)6)%)

    +'ere are .ertain restri.tions o, ;'at .an be done in di,,erent 2inds o, 0et'od o, a state,ul sessionbean) +'e ,ollo;in& table lists di,,erent 2inds o, 0et'ods and ;'at operations t'ey are allo;ed to

    per,or0)+'e notes B4+ only or C4+ only 0eans t'at t'ese operations are only available ;'en t'ebean 'as bean 0ana&ed" respe.tive .ontainer 0ana&ed" transa.tions)JNDI a..ess indi.ates ;'et'er t'e .onte>t @ava.o0penv is a..essible)

    I, a state,ul session bean atte0pts to per,or0 an ille&al operation" anIllegalStateExce"tion;ill bet'ro;n in t'e ,ollo;in& t;o .ases

    Invo2e a 0et'od in t'e SessionContextinter,a.e not per0itted)

    Invo2e a 0et'od in t'e *imerinter,a.e not per0itted)

    Bean Method(s Allo!ed "#erations

    Constru.tor None)

    Dependen.y [email protected] 0et'ods setter 0et'odsF) $ession%onte&t'&etEJBo0e"&etEJB/o.alo0e"loo2up)J)I Access' Available

    PostConstru.t" PreDestroy" PrePassivate"PostA.tivate 0et'ods li,e.y.le .allba.2

    0et'odsF)

    $ession%onte&t'

    &[email protected]"

    &etEJBo0e"&etEJB/o.alo0e"&etCallerPrin.ipal"isCallerIn*ole"&[email protected]"&etEJB/[email protected]"loo2up"&etConte>tData"&et5ser+ransa.tion B4+ onlyF)J)I Access' AvailableResource managers'A..essible)

    Entit*ManagerFactor*' A..essible)

    Business 0et'od ,ro0 any vie; or business0et'od inter.eptor 0et'od)

    $ession%onte&t'

    &[email protected]"&etEJBo0e"&etEJB/o.alo0e"&etCallerPrin.ipal"isCallerIn*ole"&[email protected]"&etEJB/[email protected]"loo2up"

    &etConte>tData"

  • 7/25/2019 Ocejbcd6 Study Notes

    34/358

    &etInvo2edBusinessInter,a.e";asCan.elCalled"&et5ser+ransa.tion B4+ onlyF"&et*ollba.2Only C4+ onlyF"set*ollba.2Only C4+ onlyF)J)I Access' Available

    Resource managers'A..essible)"ther EJBs'A..essible)Entit*ManagerFactor*' A..essible)Entit*Manager'A..essible)+imer methods'A..essible),ser+ransaction methods'A..essible B4+onlyF)

    a,terBe&in and be,oreCo0pletion;'en EJB i0ple0ents t'eSessionSyn.'ronization inter,a.eF or 0et'ods in

    t'e EJB annotated ;it' ?A,terBe&in and?Be,oreCo0pletion

    ON/H AVAI/AB/E +O C4+ EJBs3$ession%onte&t'

    &[email protected]"

    &etEJBo0e"&etEJB/o.alo0e"&etCallerPrin.ipal"isCallerIn*ole"&[email protected]"&etEJB/[email protected]"loo2up"&etConte>tData"&et*ollba.2Only"set*ollba.2Only)J)I Access' Available

    Resource managers'A..essible)"ther EJBs'A..essible)Entit*ManagerFactor*' A..essible)Entit*Manager'A..essible)+imer methods'A..essible)

    a,terCo0pletion ;'en EJB i0ple0ents t'eSessionSyn.'ronization inter,a.eF or 0et'odannotated ;it' ?A,terCo0pletion

    ON/H AVAI/AB/E +O C4+ EJBs3$ession%onte&t'

    &[email protected]"&etEJBo0e"&etEJB/o.alo0e"

    &etCallerPrin.ipal"isCallerIn*ole"&[email protected]"&etEJB/[email protected]"loo2up"&etConte>tData)J)I Access' Available)

    In addition" t'eget(ollbackOnlyandset(ollbackOnly0et'ods o, t'e SessionContextinter,a.e 0ayonly be invo2ed ,ro0 ;it'in a 0et'od e>e.utin& in a transa.tion .onte>t or else an

    IllegalStateExce"tion;ill be t'ro;n)

    7

  • 7/25/2019 Ocejbcd6 Study Notes

    35/358

    $. Create a Stateless Session Bean

    *e,eren.es EJB )% Spe.i,i.ation" .'apter 7)

    In t'is .'apter ;e loo2 at 'o; to .reate di,,erent 2inds o, stateless session beans) +'ese are si0ilarto state,ul session beans" e>.ept t'at stateless session beans are not tied to one sin&le .lient" but 0ay

    serve re1uests ,ro0 0ultiple .lients) +'e stateless session bean does not" as t'e na0e i0plies" 'oldany state related to a parti.ular .lient)Detailed in,or0ation applyin& to session beans .an be ,ound in .'apter %7)

    3.1. "reate a (tateless (ession Bean /it# a Local No)interface ie/

    +'e stateless session bean 'as one sin&le 0et'od t'at .reates a &reetin&) +'e EJB is developed in a;eb [email protected]" sin.e t'e session bean ;ill only 'ave a lo.al no(inter,a.eF vie; and .an t'us only bea..essed by .lients in t'e sa0e appli.ation)In t'is e>a0ple" ;e ;ill also .reate a .lient to t'e stateless session bean)

    Create a dyna0i. ;eb [email protected] in E.lipse" as des.ribed in appendi> A)

    I .all 0y [email protected] StatelessSession%eb)

    In t'e pa.2a&e com.ivan.scbcd6" .reate t'e session bean i0ple0entation .lass i0ple0ented

    as belo;

    package com.ivan.scbcd6;

    import java.util.Date;import javax.annotation.PostConstruct;import javax.ejb.LocalBean;import javax.ejb.Stateless;

    /*** Simplest possible stateless session bean exposing a local, no-interface view.*/@Stateless@LocalBeanpublic class StatelessSession1Bean{ private static int sCurrentInstanceNumber = 1;

    private int mInstanceNumber;

    @PostConstruct public void initialize() { mInstanceNumber = sCurrentInstanceNumber++; System.out.println("*** StatelessSession1Bean " + mInstanceNumber + " created."); }

    /** * Creates a greeting to the person with the supplied name. *

    * @param inName Name of person to greet. * @return Greeting. */ public String greeting(final String inName) { Date theCurrentTime = new Date();

    String theMessage = "Hello " + inName + ", I am stateless session bean " + mInstanceNumber + ". The time is now: " + theCurrentTime; return theMessage; }}

    9

  • 7/25/2019 Ocejbcd6 Study Notes

    36/358

    Create t'e pa.2a&e com.ivan.scbcd6.clientand in it" .reate t'e .lient servlet i0ple0ented as

    ,ollo;s

    package com.ivan.scbcd6.client;

    import java.io.IOException;import java.io.PrintWriter;

    import javax.ejb.EJB;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;

    import com.ivan.scbcd6.StatelessSession1Bean;

    /*** Servlet implementing a local EJB client.** @author Ivan A Krizsan*/@WebServlet(name = "StatelessSession1Servlet", urlPatterns = "/test.do")public class StatelessSession1Servlet extends HttpServlet

    { /* Constant(s): */ private static final long serialVersionUID = 1L;

    /* Instance variable(s): */ @EJB private StatelessSession1Bean mStatelessSessionBean;

    /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ @Override protected void doGet(HttpServletRequest inRequest, HttpServletResponse inResponse) throws ServletException, IOException { PrintWriter theResponseWriter = inResponse.getWriter();

    String theRequestNameParam = inRequest.getParameter("name"); if (theRequestNameParam == null) { theRequestNameParam = "Anonymous Coward"; } String theResponse = mStatelessSessionBean.greeting(theRequestNameParam); theResponseWriter.println("Response from the EJB: " + theResponse); }}

    Deploy t'e [email protected] to t'e lass8is' appli.ation server)

    In a bro;ser" issue a re1uest to t'e ,ollo;in& 5*/

    'ttplo.al'ost

  • 7/25/2019 Ocejbcd6 Study Notes

    37/358

    Note t'at

    +'e session bean i0ple0entation .lass is annotated ;it' t'e ?/o.alBean annotation)

    +'is annotation is used to annotate session beans t'at e>pose a no(inter,a.e vie;)I, t'e session bean does not e>pose any ot'er vie;" t'en t'e ?/o.alBean annotation isoptional and t'e session bean ;ill e>pose a no(inter,a.e vie;)

    +'e session bean i0ple0entation .lass is annotated ;it' t'e ?Stateless annotation)+'is annotation is used to annotate session beans t'at are to be stateless)8or details about t'e options available ;it' t'e ?Stateless annotation" please see t'e se.tionon Session Bean 4etadatain t'e .'apter on session beans)

    As be,ore" ;'en a session bean 'as a no(inter,a.e vie;" t'e business 0et'ods are all t'e

    publi. 0et'ods in t'e bean i0ple0entation .lass)

    !

  • 7/25/2019 Ocejbcd6 Study Notes

    38/358

    3.2. "reate a (tateless (ession Bean /it# a Local Business nterface

    Co0pared to t'e stateless session bean developed in t'e previous se.tion" t'e stateless session beandeveloped in t'is se.tion" a session bean ;it' a lo.al business inter,a.e" di,,ers in t'e ,ollo;in&areas

    +'e session bean i0ple0entation .lass is not annotated ;it' t'e ?/o.alBean annotation) +'e session bean i0ple0entation .lass i0ple0ents an inter,a.e)

    +'e EJB is developed in a ;eb [email protected]" sin.e t'e session bean ;ill only 'ave a lo.al vie;" albeit;it' a lo.al business inter,a.e" and .an t'us only be a..essed by .lients in t'e sa0e appli.ation)

    Create a dyna0i. ;eb [email protected] in E.lipse" as des.ribed in appendi> A)

    I .all 0y [email protected] StatelessSession#eb)A ;eb [email protected] is used" sin.e t'e session bean ;ill only 'ave a lo.al vie; and .an t'us only

    be a..essed by .lients in t'e sa0e appli.ation)

    In t'e ne; [email protected]" .reate t'e session bean i0ple0entation .lass as belo;

    package com.ivan.scbcd6;

    import java.util.Date;

    import javax.annotation.PostConstruct;import javax.ejb.Stateless;

    /*** Simplest possible stateless session bean exposing a local business interface view.*/@Statelesspublic class StatelessSession2Bean implements StatelessSession2Local{ private static int sCurrentInstanceNumber = 1;

    private int mInstanceNumber;

    @PostConstruct

    public void initialize() { mInstanceNumber = sCurrentInstanceNumber++; System.out.println("*** StatelessSession2Bean " + mInstanceNumber + " created."); }

    /* (non-Javadoc) * @see com.ivan.scbcd6.StatelessSession2Local#greeting(java.lang.String) */ @Override public String greeting(final String inName) { Date theCurrentTime = new Date();

    String theMessage = "Hello " + inName + ", I am stateless session bean " + mInstanceNumber + ". The time is now: " + theCurrentTime; return theMessage; }}

  • 7/25/2019 Ocejbcd6 Study Notes

    39/358

    Create t'e lo.al business inter,a.e

    package com.ivan.scbcd6;

    import javax.ejb.Local;

    @Local

    public interface StatelessSession2Local{

    /** * Creates a greeting to the person with the supplied name. *

    * @param inName Name of person to greet. * @return Greeting. */ public String greeting(final String inName);

    }

    Create a .lient servlet in t'e sa0e ;ay as in t'epreviousse.tion)

    Deploy t'e [email protected] to t'e lass8is' appli.ation server)

    In a bro;ser" issue a re1uest to t'e ,ollo;in& 5*/

    'ttplo.al'osttend any ot'er inter,a.e)

    it' EJB #)% and earlier" t'e lo.al business inter,a.e 'ad to e>tend t'ejavax.ejb.E!&ocalObject) +'is is no lon&er ne.essary)

    +'e StatelessSession%&ocalinter,a.e is annotated ;it' t'e ?/o.al annotation)

    +'e ?/o.al annotation .an be applied to bot' t'e session bean i0ple0entation .lass" in;'i.' .ase it also needs to spe.i,y ;'i.' inter,a.esF de,ines t'e lo.al vie;" or to t'einter,a.esF ;'i.' de,ines t'e lo.al vie;" in ;'i.' .ase no ,urt'er ele0ents need to bespe.i,ied)

    -

    http://localhost:8080/StatelessSession2Web/test.do?name=Ivanhttp://localhost:8080/StatelessSession2Web/test.do?name=Ivan
  • 7/25/2019 Ocejbcd6 Study Notes

    40/358

    3.3. "reate a (tateless (ession Bean /it# a Remote Business nterface

    Co0pared to t'e stateless session bean developed in t'e previous se.tion" t'e stateless session beandeveloped in t'is se.tion" a session bean ;it' a re0ote business inter,a.e" di,,ers in t'e ,ollo;in&areas

    +'e inter,a.e i0ple0ented by t'e session bean i0ple0entation .lass is annotated ;it' t'e?*e0ote annotation)

    +'e session bean ;it' a re0ote vie; .an be .reated in an EJB [email protected]" sin.e it 0ay be a..essed by.lients outside o, t'e appli.ation in ;'i.' it is deployed)

    Create an EJB [email protected] in E.lipse" as des.ribed in appendi> B)

    In t'e ne; [email protected]" .reate t'e session bean i0ple0entation .lass as belo;

    package com.ivan.scbcd6;

    import java.util.Date;import javax.ejb.Stateless;

    /*** Simplest possible stateless session bean with a remote business interface view.*/@Statelesspublic class StatelessSession3Bean implements StatelessSession3Remote{ private static int sCurrentInstanceNumber = 1;

    private int mInstanceNumber;

    @PostConstruct public void initialize() { mInstanceNumber = sCurrentInstanceNumber++; System.out.println("*** StatelessSession3Bean " + mInstanceNumber + " created.");

    }/* (non-Javadoc)

    * @see com.ivan.scbcd6.StatelessSession3Remote#greeting(java.lang.String) */ @Override public String greeting(final String inName) { Date theCurrentTime = new Date();

    String theMessage = "Hello " + inName + ", I am stateless session bean " + mInstanceNumber + ". The time is now: " + theCurrentTime; return theMessage; }}

    Create t'e re0ote business inter,a.e

    package com.ivan.scbcd6;

    import javax.ejb.Remote;

    /*** Remote business interface of the stateless session bean.*/@Remotepublic interface StatelessSession3Remote{ /** * Creates a greeting to the person with the supplied name. *

    * @param inName Name of person to greet. * @return Greeting.

    7$

  • 7/25/2019 Ocejbcd6 Study Notes

    41/358

    */ public String greeting(final String inName);

    }

    Deploy t'e [email protected] to t'e lass8is' appli.ation server)

    *e0ote .lients a..essin& t'e state,ul session bean ;it' t'e re0ote business inter,a.e ;ill bedeveloped in .'apter ,ive" se.tion #)

    Note t'at

    +'e session bean i0ple0entation .lass is annotated ;it' t'e ?Stateless annotation)

    +'e session bean i0ple0entation .lass i0ple0ents an inter,a.e)

    +'is is t'e re0ote business inter,a.e)

    +'e re0ote business inter,a.e does not e>tend any ot'er inter,a.e)

    it' EJB #)% and earlier" t'e re0ote business inter,a.e 'ad to e>tend t'ejavax.ejb.E!Object) +'is is no lon&er ne.essary)

    +'e StatelessSession'(emoteinter,a.e is annotated ;it' t'e ?*e0ote annotation)

    +'e ?*e0ote annotation .an be applied to bot' t'e session bean i0ple0entation .lass" in;'i.' .ase it also needs to spe.i,y ;'i.' inter,a.esF de,ines t'e re0ote vie;" or to t'einter,a.esF ;'i.' de,ines t'e re0ote vie;" in ;'i.' .ase no ,urt'er ele0ents need to bespe.i,ied)

    7%

  • 7/25/2019 Ocejbcd6 Study Notes

    42/358

    3.4. "reate a (tateless (ession Bean /it#out Annotations

    In t'is se.tion ;e ;ill .reate a stateless session bean ;it'out usin& annotations" .on,i&urin& it in t'ee@b(@ar)>0l deploy0ent des.riptor) +'e i0ple0entation o, t'e session bean is identi.al to t'at in t'e

    previous se.tions)+'e EJB is developed in a ;eb [email protected]" sin.e t'e session bean ;ill only 'ave a lo.al no(inter,a.eF

    vie; and .an t'us only be a..essed by .lients in t'e sa0e appli.ation) In se.tion one in .'apter ,ive;eMll i0ple0ent a servlet a.tin& as .lient to t'e EJB)

    Create a dyna0i. ;eb [email protected] in E.lipse" as des.ribed in appendi> A)

    I .all 0y [email protected] StatelessSession7eb)

    In t'e pa.2a&e com.ivan.scbcd6" .reate t'e session bean i0ple0entation .lass i0ple0ented

    as belo;

    package com.ivan.scbcd6;

    import java.util.Date;

    /*** Simplest possible stateless session bean without using annotations.*/public class StatelessSession4Bean{

    private static int sCurrentInstanceNumber = 1;

    private int mInstanceNumber;

    @PostConstruct public void initialize() { mInstanceNumber = sCurrentInstanceNumber++; System.out.println("*** StatelessSession4Bean " + mInstanceNumber + " created."); }

    /** * Creates a greeting to the person with the supplied name. *

    * @param inName Name of person to greet. * @return Greeting. */ public String greeting(final String inName) { Date theCurrentTime = new Date();

    String theMessage = "Hello " + inName + ", I am stateless session bean " + mInstanceNumber + ". The time is now: " + theCurrentTime; return theMessage; }}

    In t'e ebContentEB(IN8 dire.tory" .reate a ,ile na0ed e@b(@ar)>0l ;it' t'e ,ollo;in&

    .ontents

    7#

  • 7/25/2019 Ocejbcd6 Study Notes

    43/358

    StatefulSession1Bean com.ivan.scbcd6.StatelessSession4Bean

    Stateless

    Create a .lient servlet in t'e sa0e ;ay as in t'e ,irst se.tion on .reatin& stateless session

    beans)

    Deploy t'e [email protected] to t'e lass8is' appli.ation server)

    In a bro;ser" issue a re1uest to t'e ,ollo;in& 5*/'ttplo.al'ost

  • 7/25/2019 Ocejbcd6 Study Notes

    44/358

    3.!. (tateless (ession Bean Life)"%cle

    *e,eren.es EJB )% Spe.i,i.ation" se.tion 7)!)%)

    +'e stateless session bean 'as a si&ni,i.antly less .o0ple> li,e(.y.le" .o0pared to t'e state,ulsession bean)

    /i,e(.y.le o, a stateless session bean)

    +'e red bo>es denote di,,erent states o, a stateless session bean)+'e &reen bo>es denote .lient events t'at .auses a stateless session bean to .'an&e state)+e>t t'at is not bo>ed and not .olored denote .allba.2F 0et'ods t'at are invo2ed on a statelesssession bean in .onne.tion to a state transition)

    77

  • 7/25/2019 Ocejbcd6 Study Notes

    45/358

    3.'. 5&erations Allo/e6 in et#o6s of a (tateless (ession Bean

    *e,eren.es EJB )% Spe.i,i.ation" se.tion 7)!)#)

    +'ere are .ertain restri.tions o, ;'at .an be done in di,,erent 2inds o, 0et'od o, a stateless sessionbean) +'e ,ollo;in& table lists di,,erent 2inds o, 0et'ods and ;'at operations t'ey are allo;ed to

    per,or0)+'e notes B4+ only or C4+ only 0eans t'at t'ese operations are only available ;'en t'ebean 'as bean 0ana&ed" respe.tive .ontainer 0ana&ed" transa.tions)JNDI a..ess indi.ates ;'et'er t'e .onte>t @ava.o0penv is a..essible)

    I, a stateless session bean atte0pts to per,or0 an ille&al operation" anIllegalStateExce"tion;ill bet'ro;n in t'e ,ollo;in& t;o .ases

    Invo2e a 0et'od in t'e SessionContextinter,a.e not per0itted)

    Invo2e a 0et'od in t'e *imeror *imerServiceinter,a.es not per0itted)

    Bean Method(s Allo!ed "#erations

    Constru.tor None)

    Dependen.y [email protected] 0et'ods setter 0et'odsF) $ession%onte&t'&etEJBo0e"&etEJB/o.alo0e"loo2up)J)I Access' Available

    PostConstru.t" PreDestroy 0et'ods li,e.y.le.allba.2 0et'odsF)

    $ession%onte&t'

    &[email protected]"&etEJBo0e"&etEJB/o.alo0e"&[email protected]"&etEJB/[email protected]"loo2up"&etConte>tData"&et+i0erServi.e"&et5ser+ransa.tion B4+ onlyF)J)I Access' AvailableEntit*ManagerFactor*' A..essible)

    Business 0et'od ,ro0 any vie; or business0et'od inter.eptor 0et'od)

    $ession%onte&t'

    &[email protected]"

    &etEJBo0e"&etEJB/o.alo0e"&etCallerPrin.ipal"isCallerIn*ole"&[email protected]"&etEJB/[email protected]"loo2up"&etConte>tData"&etInvo2edBusinessInter,a.e";asCan.elCalled"&et+i0erServi.e"

    79

  • 7/25/2019 Ocejbcd6 Study Notes

    46/358

    &et5ser+ransa.tion B4+ onlyF"&et*ollba.2Only C4+ onlyF"set*ollba.2Only C4+ onlyF)J)I Access' AvailableResource managers'A..essible)"ther EJBs'A..essible)

    Entit*ManagerFactor*' A..essible)Entit*Manager'A..essible)+imer and +imer$ervice methods'A..essible),ser+ransaction methods'A..essible B4+onlyF)

    Business 0et'ods ,ro0 ;eb servi.e endpoint) $ession%onte&t'&[email protected]"&etEJBo0e"&etEJB/o.alo0e"&etCallerPrin.ipal"

    isCallerIn*ole"&[email protected]"&etEJB/[email protected]"loo2up"&etConte>tData"&et+i0erServi.e"&et4essa&eConte>t"&et5ser+ransa.tion B4+ onlyF"&et*ollba.2Only C4+ onlyF"set*ollba.2Only C4+ onlyF)Message%onte&t methods' Available

    J)I Access' AvailableResource managers'A..essible)"ther EJBs'A..essible)Entit*ManagerFactor*' A..essible)Entit*Manager'A..essible)+imer and +imer$ervice methods'A..essible),ser+ransaction methods'A..essible B4+onlyF)

    +i0eout .allba.2 0et'od) $ession%onte&t'&[email protected]"

    &etEJBo0e"&etEJB/o.alo0e"&etCallerPrin.ipal"isCallerIn*ole"&[email protected]"&etEJB/[email protected]"loo2up"&etConte>tData"&et+i0erServi.e"&et5ser+ransa.tion B4+ onlyF"&et*ollba.2Only C4+ onlyF"

    set*ollba.2Only C4+ onlyF)

    76

  • 7/25/2019 Ocejbcd6 Study Notes

    47/358

    J)I Access' AvailableResource managers'A..essible)"ther EJBs'A..essible)Entit*ManagerFactor*' A..essible)Entit*Manager'A..essible)+imer and +imer$ervice methods'A..essible)

    ,ser+ransaction methods'A..essible B4+onlyF)

    In addition to t'e .onstraints spe.i,ied in t'e above table" t'eget(ollbackOnlyandset(ollbackOnly0et'ods 0ay only be invo2ed ,ro0 ;it'in a transa.tion" ot'er;ise anIllegalStateExce"tion;ill bet'ro;n)

    7!

  • 7/25/2019 Ocejbcd6 Study Notes

    48/358

    *. Create a Singleton Session Bean

    *e,eren.es EJB )% Spe.i,i.ation" se.tion 7)ists)Su.' session beans 0ay be used to i0ple0ent" ,or instan.e" a .entral repository or appli.ation

    initialization t'at needs to be per,or0ed ;'en an appli.ation starts up)Sin&leton session bean instan.es 'as t'e ,ollo;in& .'ara.teristi.s

    One sin&le instan.e o, a sin&leton session bean e>ists per appli.ation)

    I, t'e appli.ation is distributed" t'en t'ere is one instan.e per JV4 in ;'i.' t'e appli.ation.ontainin& t'e sin&leton session bean is runnin&)

    Can be s'ared)

    Supports .on.urrent a..ess)

    An instan.e o, a sin&leton session bean is not destroyed until t'e appli.ation s'uts do;n)

    +'e instan.e even survives syste0 e>.eptions ,ro0 business 0et'ods and .allba.2 0et'ods

    invo2ed a,ter .reation and initialization o, t'e bean 'as .o0pleted) 4ay 'old state)

    Can 'old instan.e state t'at is preserved over 0ultiple .alls" but any state o, a sin&letonsession bean is not retained in t'e .ase o, .ontainer s'utdo;n or .ras')

    As ;it' stateless and state,ul session beans" sin&leton session beans .an also 'ave t'ree di,,erent2inds o, vie;sL lo.al no(inter,a.e vie;" lo.al business inter,a.e vie; and a re0ote businessinter,a.e vie;) E>a0ples on 'o; to .on,i&ure t'e di,,erent 2inds o, vie;s 'as been s'o;n in.'apters t;oand t'reeand ;ill not be repeated 'ere)

    7

  • 7/25/2019 Ocejbcd6 Study Notes

    49/358

    4.1. Basic (ingleton (ession Bean E7am&le

    In t'is se.tion ;e ;ill .reate t'e ,irst in.arnation o, t'e sin&leton session bean e>a0ple pro&ra0)+'is pro&ra0 ;ill be e>tended and 0odi,ied in subse1uent se.tions" as to de0onstrate di,,erent.apabilities o, sin&leton session beans)

    +'e e>a0ple pro&ra0 is developed in a ;eb [email protected]" sin.e t'e session beans ;ill only 'ave a lo.alno(inter,a.eF vie; and .an t'us only be a..essed by .lients in t'e sa0e appli.ation)In t'is e>a0ple" ;e ;ill also .reate a .lient to t'e sin&leton session beans)

    Create a dyna0i. ;eb [email protected] in E.lipse" as des.ribed in appendi> A)

    I .all 0y [email protected] Sin&letonSession%eb)

    In t'e pa.2a&e com.ivan.scbcd6" .reate t'e ,irst session bean i0ple0entation .lass a..ordin&

    to t'e ,ollo;in& listin&

    package com.ivan.scbcd6;

    import java.util.Date;import javax.annotation.PostConstruct;import javax.ejb.LocalBean;import javax.ejb.Singleton;

    /*** This class implements the first singleton session bean of this* example program.* This singleton session bean has the ability to hold state.*/@Singleton@LocalBeanpublic class SingletonSessionBeanA{ /* Constant(s): */ private final static String BEAN_NAME = "SingletonSessionBeanA";

    /* Instance variable(s): */ private String mStoredMessage = "[no message set]";

    @PostConstruct public void intialize() { System.out.println("*** " + BEAN_NAME + " - Initialized"); }

    @PreDestroy public void cleanUp() { System.out.println("*** " + BEAN_NAME + " - Destroyed"); }

    public String retrieveMessage() { Date theCurrentTime = new Date();

    return "Message from " + BEAN_NAME + " - " + mStoredMessage + " " + theCurrentTime; }

    public void storeMessage(final String inStoredMessage) { mStoredMessage = inStoredMessage; }}

    7-

  • 7/25/2019 Ocejbcd6 Study Notes

    50/358

    In t'e sa0e pa.2a&e" .reate t'e se.ond sin&leton session bean i0ple0entation .lass

    package com.ivan.scbcd6;

    import java.util.Date;import javax.annotation.PostConstruct;import javax.ejb.LocalBean;

    import javax.ejb.Singleton;

    /*** This class implements the second singleton session bean of this* example program.*/@Singleton@LocalBeanpublic class SingletonSessionBeanB{ private final static String BEAN_NAME = "SingletonSessionBeanB";

    @PostConstruct public void intialize() { System.out.println("*** " + BEAN_NAME + " - Initialized");

    }

    @PreDestroy public void cleanUp() { System.out.println("*** " + BEAN_NAME + " - Destroyed"); }

    public String retrieveMessage() { Date theCurrentTime = new Date();

    return "Message from " + BEAN_NAME + " - " + theCurrentTime; }}

    In t'e com.ivan.scbcd6.clientpa.2a&e" .reate t'e servlet t'at is to be t'e .lient o, t'e t;o

    sin&leton session beans ;e @ust i0ple0ented

    package com.ivan.scbcd6.client;

    import java.io.IOException;import java.io.PrintWriter;import javax.ejb.EJB;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;

    import com.ivan.scbcd6.SingletonSessionBeanA;

    import com.ivan.scbcd6.SingletonSessionBeanB;

    /*** Servlet acting as a client of the two singleton session beans.*/@WebServlet(name = "SingletonClientServlet", urlPatterns = "/test.do")public class SingletonClientServlet extends HttpServlet{ /* Constant(s): */ private static final long serialVersionUID = 1L; private final static String STORE_ACTION = "store"; private final static String CLEAR_ACTION = "clear";

    /* Instance variable(s): */ @EJB private SingletonSessionBeanA mSingletonBeanA;

    @EJB private SingletonSessionBeanB mSingletonBeanB;

    9$

  • 7/25/2019 Ocejbcd6 Study Notes

    51/358

    @Override protected void doGet(HttpServletRequest inRequest, HttpServletResponse inResponse) throws ServletException, IOException { System.out.println("**** Entering SingletonClientServlet");

    String theRequestNameParam = inRequest.getParameter("name"); String theRequestActionParam = inRequest.getParameter("action");

    /* Set default name if none provided. */ if (theRequestNameParam == null || theRequestNameParam.equals("")) { theRequestNameParam = "Anonymous Coward"; }

    /* Generate output from servlet using session beans. */ PrintWriter theResponseWriter = inResponse.getWriter(); String theMessage;

    theMessage = mSingletonBeanA.retrieveMessage(); theResponseWriter.println(theMessage); theMessage = mSingletonBeanB.retrieveMessage(); theResponseWriter.println(theMessage);

    /*

    * Store or clear data in one of the singleton session beans * if the supplied action so specifies. */ if (theRequestActionParam != null) { if (STORE_ACTION.equals(theRequestActionParam)) { mSingletonBeanA.storeMessage(theRequestNameParam); } if (CLEAR_ACTION.equals(theRequestActionParam)) { mSingletonBeanA.storeMessage("[CLEARED]"); } }

    System.out.println("**** Exiting SingletonClientServlet");

    theResponseWriter.println("Finished invoking singleton session beans!"); }}

    Deploy t'e [email protected] to t'e lass8is' appli.ation server)

    In a bro;ser" issue a re1uest to t'e ,ollo;in& 5*/

    'ttplo.al'ost

  • 7/25/2019 Ocejbcd6 Study Notes

    52/358

    8inally" re(issue t'e re1uest to t'e ,irst 5*/

    'ttplo.al'ost

  • 7/25/2019 Ocejbcd6 Study Notes

    53/358

    4.2. (ingleton (ession Bean nitiali8ation

    *e,eren.es EJB )% Spe.i,i.ation" se.tion 7)

  • 7/25/2019 Ocejbcd6 Study Notes

    54/358

    INFO: Loading application SingletonSession1Web at /SingletonSession1Web

    Note t'at

    I, t'e appli.ation 'as not been run" t'en you ;ill only see a 0essa&e about

    Sin&letonSessionBeanA 'avin& been destroyed" not Sin&letonSessionBeanB" sin.e t'e latter

    'as not been .reated) +'ere is a 0essa&e sayin& t'at Sin&letonSessionBeanA is initialized i00ediately on

    appli.ation startup t'is is ;'at ;e ;anted to a..o0plis' usin& t'e ?Startup annotation)

    4.3. (ingleton (ession Bean -estruction

    *e,eren.es EJB )% Spe.i,i.ation" se.tion 7)

  • 7/25/2019 Ocejbcd6 Study Notes

    55/358

    4.4. (ingleton (ession Bean nitiali8ation an6 -estruction-e&en6encies

    Not only .an ;e tell t'e .ontainer t'at ;e ;ant a sin&leton session bean ea&erly initialized" ;e .analso tell t'e .ontainer t'at one sin&leton session bean needs to be initialized prior to t'einitialization o, anot'er sin&leton session bean) +'is is a..o0plis'ed usin& t'e ?DependsOnannotation or t'e depends(on ele0ent in t'e e@b(@ar)>0l deploy0ent des.riptor)

    In t'e e>a0ple pro&ra0" 0odi,y t'e SingletonSession!ean,.lass addin& t'e ?DependsOn

    annotation

    ...@Singleton@LocalBean@Startup@DependsOn("SingletonSessionBeanB")public class SingletonSessionBeanA{...

    As a,ter 'avin& added t'e ?Startup annotation" navi&ate to t'e Appli.ations node in t'e

    lass8is' v ad0inistration .onsole and .li.2 *estart in t'e A.tion .olu0n ,or ourappli.ation)

    avin& restarted t'e appli.ation" you s'ould see t'e ,ollo;in& output in t'e lass8is' lo&

    ,ile or in t'e .onsole)Note t'at t'ere are no; 0essa&es ,ro0 bot' t'e sin&leton session beans sayin& t'at t'ey'ave been initialized and t'at Sin&letonSessionBeanB is initialized be,oreSin&letonSessionBeanA is initialized t'is is ;'at ;e ;anted to a..o0plis' usin& t'e?DependsOn annotation)

    INFO: *** SingletonSessionBeanA - DestroyedINFO: *** SingletonSessionBeanB - DestroyedINFO: Portable JNDI names for EJB SingletonSessionBeanA :[java:global/SingletonSession1Web/SingletonSessionBeanA!com.ivan.scbcd6.SingletonSessionBeanA,java:global/SingletonSession1Web/SingletonSessionBeanA]INFO: Portable JNDI names for EJB SingletonSessionBeanB :[java:global/SingletonSession1Web/SingletonSessionBeanB,java:global/SingletonSession1Web/SingletonSessionBeanB!com.ivan.scbcd6.SingletonSessionBeanB]INFO: *** SingletonSessionBeanB - InitializedINFO: *** SingletonSessionBeanA - InitializedINFO: Loading application SingletonSession1Web at /SingletonSession1WebINFO: Loading application SingletonSession1Web at /SingletonSession1WebINFO: Loading application SingletonSession1Web at /SingletonSession1Web

    I, ;e restart t'e appli.ation a&ain ,ro0 t'e lass8is' ad0inistration .onsole" ;e .an alsonote t'at Sin&letonSessionBeanA is destroyed prior to Sin&letonSessionBeanB)+'is is also .aused by Sin&letonSessionBeanA 'avin& a dependen.y onSin&letonSessionBeanB) +'e .ontainer ensures t'at all sin&leton session beans ;'i.' asin&leton session beans depend on are still present durin& destru.tion)

    I, Sin&letonSessionBeanA si0ply ;anted to invo2e Sin&letonSessionBeanB" t'en re&ulardependen.y [email protected] or JNDI loo2up ;ould 'ave been su,,i.ient and any ?DependsOnannotations are not ne.essary)Cy.li. dependen.ies are not all