Académique Documents
Professionnel Documents
Culture Documents
InfoRep : EJB
1/64
Informatique Rpartie
Introduction aux EJB Alexandre Pauchet
INSA Rouen - Dpartement ASI
BO.B.RC.18, pauchet@insa-rouen.fr
INSA - ASI
InfoRep : EJB
2/64
Plan
Introduction
7
EJB Session avec tat EJB/JSP Persistance Les EJB message Rfrences
Architecture J2EE
8
EJB
9
4 5
11
INSA - ASI
InfoRep : EJB
3/64
Introduction
Histoire
(1/7)
INSA - ASI
InfoRep : EJB
4/64
Introduction
Histoire
(2/7)
Re
qu
te
sS QL
Requtes SQL
Re
qu
te
sS
QL
Base de Donnes
INSA - ASI
InfoRep : EJB
5/64
Introduction
Histoire
(3/7)
RP C
Primergy
Requtes SQL
RP
Base de Donnes
INSA - ASI
InfoRep : EJB
6/64
Introduction
Histoire
(4/7)
RM I/
CO RB A
Primergy
objet
Requtes SQL
objet
CO A RB
objet
RM
I/
objet
Base de Donnes
INSA - ASI
InfoRep : EJB
7/64
Introduction
Histoire
(5/7)
HTTP
Internet
HTTP
SQL
Base de Donnes
Navigateur
INSA - ASI
InfoRep : EJB
8/64
Introduction
Histoire
(6/7)
Aspect programmation
Annes 70-80 : Programmation procdurale
alors que les premiers langages objets datent de la n des annes 60 !
INSA - ASI
InfoRep : EJB
9/64
Introduction
J2EE
(7/7)
Volont de SUN
Cadre de dveloppement par composants avec services J2EE (Java 2 Edition Enterprise), qui propose des API :
Linvocation de mthodes distantes : RMI, CORBA, Web Services Laccs aux bases de donnes relationnelles : JDBC Laccs aux annuaires et services de nommage : JNDI Lutilisation du XML : DOM et SAX HTML dynamique et traitement de requtes HTTP : JSP et Servlet La gestion du Mail : Java Mail La gestion des messages : Java Message Service La gestion des composants : EJB La connection des ERP : Java Connector La gestion des droits daccs : Java Authentication and Authorization Service (JAAS)
INSA - ASI
InfoRep : EJB
10/64
Architecture J2EE
(1/4)
INSA - ASI
InfoRep : EJB
11/64
Architecture J2EE
(2/4)
La spcication J2EE fournit les lments suivants pour la conception et la ralisation dapplication Web :
sevlets Java et JSP
Les servlets et JSP constituent les blocs de construction du dveloppement dapplications web avec J2EE En terme J2EE, les servlets et pages JSP sont des composants web
application web
Collection de servlets et de pages JSP, dautres classes annexes ou de bibliothques de classes, ainsi que des ressources statiques telles que des documents HTML, XHTML ou XML, images, etc.
conteneur web
Essentiellement un environnement dexcution Java pour les applications web Responsable de linitialisation, de linvocation et de la gestion de la dure de vie des servlets Java et des pages JSP
INSA - ASI
InfoRep : EJB
12/64
Architecture J2EE
(3/4)
Application web Servlets Page JSP Classes Java Archives Description de dploiement
Application web Servlets Page JSP Classes Java Archives Description de dploiement
Conteneur web
INSA - ASI
InfoRep : EJB
13/64
Architecture J2EE
(4/4)
INSA - ASI
InfoRep : EJB
14/64
Les EJB
Gnralits
(1/11)
Il existe direntes API en Java pour crire des applications rparties Certaines fournissent des services :
De bas niveau : JDBC, JNDI De haut niveau : Servlets et JSP
INSA - ASI
InfoRep : EJB
15/64
Les EJB
Description
(2/11)
Un composant EJB est constitu dune collection de classes Java et dun chier XML, fusionns en une unit unique Les classes Java doivent respecter certaines rgles et fournir certaines mthodes callback Le composant EJB sexcute dans un conteneur EJB, qui prend en charge tout ce qui concerne le niveau systme
Rpartition des tches entre le programmeur de bean et le conteneur
Les EJB ne sont quune spcication Les composant EJB fonctionnent avec tout type de client :
servlets et JSP clients Java (via RMI) clients et serveur divers (via RMI/IIOP) ...
INSA - ASI
InfoRep : EJB
16/64
Les EJB
(3/11)
Peut tre vu comme une reprsentation oriente objet de donnes dans une base de donnes :
Les clients peuvent y accder en toute scurit simultanment La dure de vie du bean est exactement identique celle des donnes quil reprsente
La relation dun bean entit avec les donnes de la base de donnes peut tre gre par
le programmeur : persistance gre par le bean le conteneur : persistance gre par le conteneur
Cette distinction se rvlera souvent essentielle du point de vue de la productivit ou des performances Cependant on peut utiliser nimporte quel type de bean de manire interchangeable dans la conception
INSA - ASI
InfoRep : EJB
17/64
Les EJB
(4/11)
INSA - ASI
InfoRep : EJB
18/64
Les EJB
(5/11)
Permet de crer des EJB qui peuvent tre producteur ou consommateur de messages
Utilisation de JMS (Server dapplication est un JMS provider, et les beans sont des JMS client : JMS producer et JMS consumer) Permet lenvoi de message asynchrone Permet lenvoi multiple (1 n)
INSA - ASI
InfoRep : EJB
19/64
Les EJB
(6/11)
Le conteneur se trouve dans un serveur dapplications qui lui fournit un environnement dexcution
Conteneur web
Conteneur EJB
Serveur dapplications
INSA - ASI
InfoRep : EJB
20/64
Les EJB
(7/11)
Persistance
Possibilit pour le bean dtre persistant dans une base de donnes sans crire une seule ligne de code SQL
Transactions dclaratives
Possibilit de grer des transactions complexes sans la moindre ligne de code (sans utiliser lAPI JTA -Java Transaction- et JTS -Java Transaction Service-)
Mmoire cache
Possibilit damliorer les performances sans crire le moindre code
Scurit dclarative
Possibilit de grer laccs aux composants sans crire la moindre ligne de code
INSA - ASI
InfoRep : EJB
21/64
Les EJB
(8/11)
Gestion derreurs
La spcication EJB dnit la manire dont les erreurs aectent les transactions, les rsultats au niveau client, la connexion et la restauration des composants
Portabilit
EJB = Spcication = possibilit de porter un EJB sur un autre serveur (plus puissant utilisant par exemple le balancing, le clustering )
INSA - ASI
InfoRep : EJB
22/64
Les EJB
(9/11)
Contrat
Rpartition des responsabilits entre chaque couche du logiciel (Client, Conteneur, EJB, Gestionnaire de persistance ( 2.0))
Services orthogonaux
Le conteneur EJB fournit des services au programmeur Le programmeur de lEJB na plus qu respecter les rgles pour exploiter automatiquement ces services Ces rgles sont spcies dans le descripteur de dploiement
INSA - ASI
InfoRep : EJB
23/64
Les EJB
Interposition
(10/11)
Skeleton RMI
Le client excute un appel sur un Stub RMI Ce stub RMI assemble et envoie des informations au serveur Le skeleton dsassemble les paramtres et les transmets au conteneur EJB Le conteneur examine les rfrences de scurit de lappelant de la mthode Le conteneur dmarre ou rejoint toute transaction ncessaire Le conteneur excute tous les appels ncessaires aux fonctions de persistance Le conteneur dclenche diverses mthodes callback pour permettre au composant EJB dacqurir des ressources La mthode logique mtier est appele Le conteneur excute quelques autres tches relatives aux transactions, la persistance, aux mthodes callback Le conteneur renvoie le rsultat de la mthode mtier ou une exception au client
Votre EJB
Stub RMI
Rseau
Client
INSA - ASI
InfoRep : EJB
24/64
Les EJB
(11/11)
EJB3 : les annotations Intgres au JDK 1.5 Permet dajouter des Mta-informations sur le code Non prise en compte par la JVM (mais prsente dans le .class)
Annotation et EJB
Les EJB3 utilisent les annotations pour simplier le code produire : Moins de code crire Gnration automatique des descripteurs de dploiement
INSA - ASI
InfoRep : EJB
25/64
(1/6)
Le minimum
Compos dune ou deux interfaces et dune classe mtier : Interface de description du contrat pour accs distant (XXRemote.java)
Utilisation de lannotation @Remote (importation de javax.ejb.Remote) juste avant la denition de linterface
INSA - ASI
InfoRep : EJB
26/64
(2/6)
Dans XXXBean.java @PostConstruct suivi de la mthode postConstruct @PreDestroy suivi de la mthode preDestroy
a. extrait du tutoriel de SUN : http://java.sun.com/j2ee/tutorial/1_3-fcs/ doc/EJBConcepts9.html
INSA - ASI
InfoRep : EJB
27/64
(3/6)
HelloStatelessRemote.java
package HelloWorld ; import javax . ejb . Remote ; @Remote p u b l i c i n t e r f a c e HelloStatelessRemote { p u b l i c String sayHello ( String s ) ; }
HelloStatelessLocal.java
package HelloWorld ; import javax . ejb . Stateless ; @Stateless p u b l i c c l a s s H el l oS t at e le s sB e a n implements HelloStatelessLocal , H e l l o S t a t e l e s s R e m o t e { p u b l i c String sayHello ( String s ) { r e t u r n " Hello " + s + " ! " ; } }
INSA - ASI
InfoRep : EJB
28/64
(4/6)
HelloStatelessBean.java
package HelloWorld ; import javax . ejb . Stateless ; @Stateless p u b l i c c l a s s H el l oS t at e le s sB e a n implements HelloStatelessLocal , H e l l o S t a t e l e s s R e m o t e { p u b l i c String sayHello ( String s ) { r e t u r n " Hello " + s + " ! " ; } }
@Local et @Remote peuvent tre portes par la mme interface Il faut au moins une annotation @Local ou @Remote
INSA - ASI
InfoRep : EJB
29/64
(5/6)
Compilation et dploiement
A la compilation, inclure dans le classpath JBOSS_DIR/bin/classpath.sh -s Mettre dans un .jar Pour dployer, dposer dans le rpertoire JBOSS_DIR/server/JBOSS_MODE/deploy
INSA - ASI
InfoRep : EJB
30/64
(6/6)
Client.java
import javax . naming . InitialContext ; import HelloWorld . H e l l o S t a t e l e s s R e m o t e ; p u b l i c c l a s s Client { p u b l i c s t a t i c v o i d main ( String [] args ) { try { System . setProperty ( " java . naming . factory . initial " ," org . jnp . interfaces . NamingContextFactory "); System . setProperty ( " java . naming . provider . url " , " localhost :1099 " ) ; Init ialContext context = new InitialContext () ; H e l l o S t a t e l e s s R e m o t e obj = ( H e l l o S t a t e l e s s R e m o t e ) context . lookup ( " H el l oS t at e le s sB ea n / remote " ) ; System . out . println ( obj . sayHello ( args [0]) ) ; } c a t c h ( Exception e ) { System . out . println ( e ) ; e . p ri ntStackTrace () ; } } }
INSA - ASI
InfoRep : EJB
31/64
(1/4)
Interface : HelloSerializableRemote.java
package HelloWorld ; import javax . ejb . Remote ; @Remote p u b l i c i n t e r f a c e HelloSerializableRemote { p u b l i c String sayHello ( Guy g ) ; }
EJB : HelloSerializableBean.java
package HelloWorld ; import javax . ejb . Stateless ; @Stateless p u b l i c c l a s s H e l l o S e r i a l i z a b l e B e a n implements H e l l o S e r i a l i z a b l e R e m o t e { p u b l i c String sayHello ( Guy g ) { r e t u r n " Hello " + g . getName () + " ! " ; } }
INSA - ASI
InfoRep : EJB
32/64
(2/4)
Guy.java
package HelloWorld ; import import import import import java . io . Serializable ; java . io . O bje ctI npu tSt rea m ; java . io . Ob j ec t Ou t pu tS t re a m ; java . io . IOException ; java . rmi . RemoteException ;
p u b l i c c l a s s Guy implements Serializable { p r i v a t e String name ; p u b l i c Guy ( String name ) { t h i s . name = name ; } p u b l i c String getName () { r e t u r n t h i s . name ; } p u b l i c v o i d setName ( String name ) { t h i s . name = name ; } p r i v a t e v o i d writeObject ( O b je c tO u tp ut S tr e am s ) throws IOException { s . writeUTF ( t h i s . name ) ; } p r i v a t e v o i d readObject ( Obj ect Inp utS tre am s ) throws IOException { t h i s . name = s . readUTF () ; } }
INSA - ASI
InfoRep : EJB
33/64
(3/4)
INSA - ASI
InfoRep : EJB
34/64
(4/4)
Client.java
import javax . naming . InitialContext ; import HelloWorld . H e l l o S e r i a l i z a b l e R e m o t e ; import HelloWorld . Guy ; p u b l i c c l a s s Client { p u b l i c s t a t i c v o i d main ( String [] args ) { try { System . setProperty ( " java . naming . factory . initial " ," org . jnp . interfaces . NamingContextFactory "); System . setProperty ( " java . naming . provider . url " , " localhost :1099 " ) ; Init ialContext context = new InitialContext () ; H e l l o S e r i a l i z a b l e R e m o t e obj = ( H e l l o S e r i a l i z a b l e R e m o t e ) context . lookup ( " H e l l o S e r i a l i z a b l e B e a n / remote " ) ; System . out . println ( obj . sayHello (new Guy ( args [0]) ) ) ; } c a t c h ( Exception e ) { System . out . println ( e ) ; e . p ri ntStackTrace () ; } } }
INSA - ASI
InfoRep : EJB
35/64
Callbacks
Ct serveur
(1/6)
Interface : HelloCallbackRemote.java
package HelloWorld ; import javax . ejb . Remote ; @Remote p u b l i c i n t e r f a c e H e l l o C a l lb a c k R e m o t e { p u b l i c String sayHello ( OtherGuy g ) ; }
INSA - ASI
InfoRep : EJB
36/64
Callbacks
Ct serveur
(2/6)
EJB : HelloCallbackBean.java
package HelloWorld ; import javax . ejb . Stateless ; import java . rmi . RemoteException ; import java . rmi . R MI Se c ur i ty M an a ge r ; @Stateless p u b l i c c l a s s He llo Cal lb ack Bea n implements H e l l o C a l l ba c k R e m o t e { p u b l i c String sayHello ( OtherGuy g ) { String text = " " ; try { text = " Hello " + g . getName () + " ! " ; } c a t c h ( Exception e ) { System . out . println ( " Remote exception catched : " + e ) ; } r e t u r n text ; } }
INSA - ASI
InfoRep : EJB
37/64
Callbacks
Objet pass
(3/6)
OtherGuy.java
package HelloWorld ; import java . io . Serializable ; import java . rmi . Remote ; import java . rmi . RemoteException ; p u b l i c i n t e r f a c e OtherGuy e x t e n d s Remote , Serializable { p u b l i c String getName () throws RemoteException ; p u b l i c v o i d setName ( String name ) throws RemoteException ; }
GuyImpl.java
package HelloWorld ; import java . io .*; import java . rmi . RemoteException ; p u b l i c c l a s s GuyImpl implements OtherGuy { p u b l i c GuyImpl ( String name ) { t h i s . setName ( name ) ; } p r i v a t e v o i d writeObject ( O b je c tO u tp ut S tr e am s ) throws IOException { } p r i v a t e v o i d readObject ( O bje ctI npu tS tre am s ) throws IOException { }
...
INSA - ASI
InfoRep : EJB
38/64
Callbacks
Objet pass
(4/6)
...
p u b l i c String getName () { String name = " " ; String fichier = System . getProperty ( " user . dir " ) + " / GuyName " ; try { Buff eredReader br = new BufferedReader (new In put Str eam Rea der (new FileInputStream ( fichier ) ) ) ; name = br . readLine () ; br . close () ; } c a t c h ( IOException ioe ) { ioe . printStackTrace () ; } r e t u r n name ; } p u b l i c v o i d setName ( String name ) { String adr = System . getProperty ( " user . dir " ) + " / GuyName " ; try { Buff eredWriter output =new BufferedWriter (new FileWriter ( adr , f a l s e ) ) ; output . write ( name ) ; output . flush () ; output . close () ; } c a t c h ( IOException ioe ) { ioe . printStackTrace () ; } } }
INSA - ASI
InfoRep : EJB
39/64
Callbacks
Dploiement
(5/6)
INSA - ASI
InfoRep : EJB
40/64
Callbacks
Ct client
(6/6)
Client.java
import import import import import javax . naming . InitialContext ; java . rmi . server . U n i c a st R e m o t e O b j e c t ; HelloWorld . H e l l o C a l l ba c k R e m o t e ; HelloWorld . OtherGuy ; HelloWorld . GuyImpl ;
p u b l i c c l a s s Client { p u b l i c s t a t i c v o i d main ( String [] args ) { try { System . setProperty ( " java . naming . factory . initial " ," org . jnp . interfaces . NamingContextFactory "); System . setProperty ( " java . naming . provider . url " , " localhost :1099 " ) ; Init ialContext context = new InitialContext () ; H e l l o C a l lb a c k R e m o t e obj = ( H e l l o C a l lb a c k R e m o t e ) context . lookup ( " H ell oCa llb ack Be an / remote " ) ; OtherGuy aGuy = new GuyImpl ( args [0]) ; U n i c a s t R em o t e O b j e c t . exportObject ( aGuy ,0) ; System . out . println ( obj . sayHello ( aGuy ) ) ; } c a t c h ( Exception e ) { System . out . println ( e ) ; } } }
INSA - ASI
InfoRep : EJB
41/64
(1/5)
Le minimum
Compos dune ou deux interfaces et dune classe mtier : Interface de description du contrat pour accs distant (XXRemote.java)
Utilisation de lannotation @Remote (importation de javax.ejb.Remote) juste avant la denition de linterface
INSA - ASI
InfoRep : EJB
42/64
(2/5)
a
@PostConstruct suivi de la mthode postConstruct @Init suivi de la mthode init @PrePassivate suivi de la mthode prePassivate @PostActivate suivi de la mthode postActivate @Remove suivi de la mthode remove @PreDestroy suivi de la mthode preDestroy
INSA - ASI
InfoRep : EJB
43/64
(3/5)
CounterStatefulLocal.java
package Stateful ; import javax . ejb . Local ; @Local p u b l i c i n t e r f a c e CounterStatefulLocal { p u b l i c i n t count () ; }
CounterStatefulRemote.java
package Stateful ; import javax . ejb . Remote ; @Remote p u b l i c i n t e r f a c e CounterStatefulRemote { p u b l i c i n t count () ; }
INSA - ASI
InfoRep : EJB
44/64
(4/5)
CounterStatefulBean.java
package Stateful ; import javax . ejb . Stateful ; @Stateful p u b l i c c l a s s C o u n t e r St a t e f u l B e a n implements CounterStatefulLocal , C o u n t e r S t a t e f u l R e m o t e { p r i v a t e i n t counter = 0; p u b l i c i n t count () { r e t u r n ++ t h i s . counter ; } }
INSA - ASI
InfoRep : EJB
45/64
(5/5)
Client.java
...
p u b l i c c l a s s Client { p u b l i c s t a t i c v o i d main ( String [] args ) { try { System . setProperty ( " java . naming . factory . initial " ," org . jnp . interfaces . NamingContextFactory "); System . setProperty ( " java . naming . provider . url " , " localhost :1099 " ) ; Init ialContext context = new InitialContext () ; C o u n t e r S t a t e l e s s R e m o t e stateless = ( C o u n t e r S t a t e l e s s R e m o t e ) context . lookup ( " C o u n t e r S t a t e l e s s B e a n / remote " ) ; C o u n t e r S t a t e f u l R e m o t e stateful = ( C o u n t e r S t a t e f u l R e m o t e ) context . lookup ( " C o u n t e r S t at e f u l B e a n / remote " ) ; System . out . println ( " Decompte ( stateless ) : " + stateless . count () ) ; System . out . println ( " Decompte ( stateful ) : " + stateful . count () ) ; } c a t c h ( Exception e ) { System . out . println ( e ) ; e . p ri ntStackTrace () ; } } }
INSA - ASI
InfoRep : EJB
46/64
EJB/JSP
(1/9)
INSA - ASI
InfoRep : EJB
47/64
EJB/JSP
Ct serveur
(2/9)
WebCounterLocal.java
package EJBCounter ; import javax . ejb . Local ; @Local p u b l i c i n t e r f a c e WebCounterLoc al { p u b l i c i n t getCounter () ; }
WebCounterRemote.java
package EJBCounter ; import javax . ejb . Remote ; @Remote p u b l i c i n t e r f a c e WebCounterRemote { p u b l i c i n t getCounter () ; }
INSA - ASI
InfoRep : EJB
48/64
EJB/JSP
Ct serveur
(3/9)
WebCounterBean.java
package EJBCounter ; import javax . ejb . Stateless ; @Stateless p u b l i c c l a s s WebCounterBean implements WebCounterLocal , WebCounterRemote { p r i v a t e s t a t i c i n t counter = 0; p u b l i c i n t getCounter () { WebC ounterBean . counter ++; r e t u r n WebCounterBean . counter ; } }
INSA - ASI
InfoRep : EJB
49/64
EJB/JSP
Ct client
(4/9)
Client.java
...
System . setProperty ( " java . naming . provider . url " , " localhost :1099 " ) ; Init ialContext context = new InitialContext () ; W e b C o u n terRemote counter = ( WebCounterRemote ) context . lookup ( " WebCounter / WebCounterBean / remote " ) ; System . out . println ( " Decompte : " + counter . getCounter () ) ; } c a t c h ( Exception e ) { System . out . println ( e ) ; e . p ri ntStackTrace () ; } } }
INSA - ASI
InfoRep : EJB
50/64
EJB/JSP
Page JSP
(5/9)
index.jsp
<% @ page contentType = " text / html ; charset = UTF -8 " % > <! DOCTYPE html PUBLIC " -// W3C // DTD XHTML 1.0 Strict // EN " " http :// www . w3 . org / TR / xhtml1 / DTD / xhtml1 - strict . dtd " > < html > < head > < title > JSP Counter </ title > </ head > < body > <h1 > Une page JSP classique ... </ h1 > </ body > </ html >
INSA - ASI
InfoRep : EJB
51/64
EJB/JSP
(6/9)
web.xml
<? xml version = " 1.0 " encoding = " utf -8 " ? > <! DOCTYPE web - app PUBLIC " -// Sun Microsystems , Inc .// DTD Web Application 2.3// EN " " http :// java . sun . com / dtd / web - app_2_3 . dtd " > <web - app > < display - name > Premire archive J2EE </ display - name > < description > Partie Web de ma premire archive J2EE </ description > </ web - app >
Remarque
Appel par http://SERVEUR:PORT/JSPCounter/index.jsp
INSA - ASI
InfoRep : EJB
52/64
EJB/JSP
Appel externe
(7/9)
counter1.jsp
<% @ page contentType = " text / html ; charset = UTF -8 " % > <% @ page import = " EJBCounter . WebCounterRemote , javax . naming . InitialContext " % > <%! p u b l i c W e b C o unterRemote counter = n u l l ; p u b l i c v o i d jspInit () { try { System . setProperty ( " java . naming . factory . initial " ," org . jnp . interfaces . NamingContextFactory "); System . setProperty ( " java . naming . provider . url " , " localhost :1099 " ) ; Init ialContext context = new InitialContext () ; counter = ( WebCounterRemot e ) context . lookup ( " WebCounter / WebCounterBean / remote " ) ; } c a t c h ( Exception e ) { e . p ri nt S tackTrace () ; } } %> < html > < head > < title > JSP Counter </ title >
...
INSA - ASI
InfoRep : EJB
53/64
EJB/JSP
Appel externe
(8/9)
counter1.jsp
...
</ head > < body > <h1 > Hits : <%= counter . getCounter () % > </ h1 > </ body > </ html >
Remarque
Problme : appel de linterface distante alors que lon est en local
INSA - ASI
InfoRep : EJB
54/64
EJB/JSP
Appel interne
(9/9)
counter2.jsp
<% @ page contentType = " text / html ; charset = UTF -8 " % > <% @ page import = " EJBCounter . WebCounterLocal , javax . naming . InitialContext , javax . rmi . PortableRemoteObject "%> <%! p u b l i c W eb Co unterLocal counter = n u l l ; p u b l i c v o i d jspInit () { try { Init ialContext ctx = new InitialContext () ; counter = ( WebCounterLocal ) ctx . lookup ( " WebCounter / WebCounterBean / local " ) ; } c a t c h ( Exception e ) { e . p ri nt S tackTrace () ; } } %> < html > < head > < title > JSP Counter </ title > </ head > < body > <h1 > Hits : <%= counter . getCounter () % > </ h1 > </ body > </ html >
INSA - ASI
InfoRep : EJB
55/64
Persistance
Les EJB entit
(1/8)
peut tre la charge du conteneur (utilisation direct dun EntityManager via @PersistenceContext) ou de lapplication (cration dun EntityManager par lintermdiaire une fabrique -EntityManagerFactory- via @PersistenceUnit)
Le contexte de persistance :
ensemble dentits persistantes (par dfaut dans une base de donnes relationnelles)
INSA - ASI
InfoRep : EJB
56/64
Persistance
Les EJB entit
(2/8)
INSA - ASI
InfoRep : EJB
57/64
Persistance
Cycle de vie
(3/8)
INSA - ASI
InfoRep : EJB
58/64
Persistance
(4/8)
PersistentHelloBean.java
package helloWorld ; import import import import javax . ejb . Stateless ; javax . persistence .*; java . util . Collection ; java . util . Iterator ;
@Stateless p u b l i c c l a s s P e r s i s t en t H e l l o B e a n implements PersistentHelloLocal , P e r s i s t e n t H e l l o R e m o t e { @ P e r s i s t e n c e C o n t e x t ( unitName = " names " ) EntityManager em ; p u b l i c String sayHello ( String s ) { String hello = " Hello " + s + " ! " ; Collection < Name > nameCollection ; em . persist (new Name ( s ) ) ; nam eCollection = em . createQuery ( " from Name n " ) . getResultList () ; hello += " \ nListe des personnes dj passes : " ; f o r ( Iterator < Name > names = nameCollection . iterator () ; names . hasNext () ; ) { hello += " " + names . next () . getName () ; } r e t u r n hello ; } }
INSA - ASI
InfoRep : EJB
59/64
Persistance
EJB Entit
(5/8)
Name.java
package helloWorld ; import javax . persistence .*; import java . io . Serializable ; @Entity @Table ( name = " names " ) p u b l i c c l a s s Name implements Serializable { p r i v a t e i n t id ; p r i v a t e String name ; p u b l i c Name () { } p u b l i c Name ( String name ) { t h i s . name = name ; } @Id @ Ge ne r at ed V alue p u b l i c i n t getId () { r e t u r n t h i s . id ; } p u b l i c v o i d setId ( i n t id ) { t h i s . id = id ; } p u b l i c String getName () { r e t u r n t h i s . name ; } p u b l i c v o i d setName ( String name ) { t h i s . name = name ; } }
INSA - ASI
InfoRep : EJB
60/64
Persistance
(6/8)
INSA - ASI
InfoRep : EJB
61/64
Persistance
(7/8)
INSA - ASI
InfoRep : EJB
62/64
Persistance
Ct client
(8/8)
Client.java
import javax . naming . InitialContext ; import helloWorld . P e r s i s t e n t H e l l o R e m o t e ; p u b l i c c l a s s Client { p u b l i c s t a t i c v o i d main ( String [] args ) { try { System . setProperty ( " java . naming . factory . initial " ," org . jnp . interfaces . NamingContextFactory "); System . setProperty ( " java . naming . provider . url " , " localhost :1099 " ) ; Init ialContext context = new InitialContext () ; P e r s i s t e n t H e l l o R e m o t e obj = ( P e r s i s t e n t H e l l o R e m o t e ) context . lookup ( " P e r s i s t e n tH e l l o B e a n / remote " ) ; System . out . println ( obj . sayHello ( args [0]) ) ; } c a t c h ( Exception e ) { System . out . println ( e ) ; e . p ri ntStackTrace () ; } } }
INSA - ASI
InfoRep : EJB
63/64
INSA - ASI
InfoRep : EJB
64/64
Rfrences
Enterprise JavaBeans :
Richard Monson-Haefel OReilly 3me dition - ISBN 0-596-00226-2