Marco Ronchetti - [email protected] 
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
J0
1
EJB: the details
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Actors:
Component Interface:
Same role as in RMI
Component Implementation:
Same role as in RMI
Home Interface:
Implemented by the server, plays the role of a
factory class for the component
JNDI service:
Plays the same role as the register in RMI
J0
2
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
The logical architecture
App server (container)
Machine
NamingService
Client
HomeInterface
Pool
Trovami la
Home interface
Trova
Dammi una
istanza
Metodo()
J0
3
Directory
Machine
Client
Machine
Crea o recupera
una istanza
Istanza
Marco Ronchetti - [email protected] 
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
J0
4
Session Beans
-) stateless
-) stateful
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Stateful Example
J0
5
Stateless Example:
Euro Converter
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Stateful Example – suggested hierarchy
J0
6
App
server
src
client
deploy
src
xHome.java
x.java
xBean.java
xHome.class
x.class
xBean.class
META-INF
ejb-jar.xml
client.java
xHome.class
x.class
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Stateless example – The Component (Remote)
Interface
Marco Ronchetti - [email protected] 
package statelessDemo;
J0
7
import java.rmi.*;
import javax.ejb.*;
public interface Converter extends EJBObject {
public double convert(int lire);
}
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Stateless example – The Home Interface
J0
8
package statelessDemo;
import java.rmi.*;
import javax.ejb.*;
public interface ConverterHome extends EJBHome {
public Converter create() throws RemoteException,
CreateException;
}
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Stateless example – The bean
J0
9
package statelessDemo;
import java.rmi.*;
import javax.ejb.*;
public class ConverterBean implements SessionBean {
private SessionContext sessionContext;
public void ejbCreate() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext sessionContext)
{ this.sessionContext = sessionContext; }
private static double ratio=1936.27;
public double convert(int lire) {
return lire/ratio;
}
}
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Stateless example – The descriptor
Marco Ronchetti - [email protected] 
<?xml version="1.0" encoding="UTF-8"?>
J0
10
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise
JavaBeans 1.1//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd">
<ejb-jar>
<description>A simple demo of a stateless Bean</description>
<display-name>An Euro Converter Bean</display-name>
<enterprise-beans>
<session>
<description>A converter from Lire to Euro</description>
<ejb-name>EuroConverter</ejb-name>
<home>statelessDemo.ConverterHome</home>
<remote>statelessDemo.Converter</remote>
<ejb-class>statelessDemo.ConverterBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Stateless example – The descriptor
J0
11
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>EuroConverter</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
<ejb-client-jar />
</ejb-jar>
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
structure of the jar file
J0
12
In this directory, run
jar cvf converter.jar *
META-INF
ejb-jar.xml
statelessDemo this directory(ies) reflect
the package hierarchy
ConverterBean.class
ConverterHome.class
Converter.class
Watch out! Packages often make life complex.
(it’s easy to make mistakes)
Try first without packages.
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Compile the sources, generate jar file
J0
13
cls
javac
move
javac
move
javac
move
Converter.java
Converter.class statelessDemo\
ConverterHome.java
ConverterHome.class statelessDemo\
ConverterBean.java
ConverterBean.class statelessDemo\
cd ..\deployment
del *.jar
del statelessDemo\*.class
move ..\src\statelessDemo\* statelessDemo\
jar cvf Converter.jar *
cd ..\src
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Start the container
J0
14
We assume you installed the j2ee sdk distribution provided by
Sun in some %J2EE_HOME% directory.
1) Run %J2EE_HOME%/bin/j2ee.bat
2) Run %J2EE_HOME%/bin/deploytool.bat
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Start the container
Marco Ronchetti - [email protected] 
FileNew Application…
J0
15
FileAdd to Application… EJB-JAR
(scegliere converter.jar)
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
View the properties
J0
16
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Start deployment
Marco Ronchetti - [email protected] 
ToolsDeploy
J0
17
Check this box

Crea ConverterApp.ear
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Start deployment
J0
18
 Choose a JNDI name
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Start deployment
J0
19
 Crea
ConverterAppClient.jar
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Stateless example – The client
J0
20
import javax.ejb.*;
import javax.naming.InitialContext;
public class ConverterClient {
public ConverterClient() {
}
Get naming context
public static void main(String[] args) {
and object reference
try {
InitialContext ctx=new InitialContext();
JNDI name
Object objref=ctx.lookup("L2EConverter");
statelessDemo.ConverterHome home =
Cast to correct type
(statelessDemo.ConverterHome)
javax.rmi.PortableRemoteObject.narrow(
objref,statelessDemo.ConverterHome.class);
statelessDemo.Converter bean=home.create();
Get a bean instance from container
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Stateless example – The client
J0
21
int lire=100000;
Do your business
System.out.println(lire+" Lire = "+
bean.convert(lire)+" Euro");
} catch (javax.naming.NamingException ex) {
System.out.println("NamingException: "+ex);
} catch (ClassCastException cc) {
System.out.println(" ClassCastException : "+cc);}
catch (javax.ejb.CreateException ce) {
System.out.println("CreateException: "+ce);
} catch (java.rmi.RemoteException re) {
System.out.println("RemoteException: "+re);
}
}
}
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Compile the client
J0
22
Questa è la directory che
contiene i sorgenti del client
cls
cd client\statelessDemo\
copy ..\server\deployment\statelessDemo\Converter.class .
copy ..\server\deployment\statelessDemo\ConverterHome.class .
cd ..
javac ConverterClient.java
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Stateless example – execution
J0
23
java -classpath .;%J2EEHOME%\lib\j2ee.jar;…\ConverterAppClient.jar
-Dorg.omg.CORBA.ORBInitialHost=localhost ConverterClient
100000 Lire = 51.64568990894865 Euro
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Stateful Example
J0
24
Stateful Example:
Dollar Converter
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Stateful example – The Component (Remote) Interface
import java.rmi.*;
import javax.ejb.*;
public interface DollarConverter extends EJBObject {
public double convertInEuro(double dollar) throws
java.rmi.RemoteException;
public double convertInDollar(double euro) throws
java.rmi.RemoteException;
public void setRate(double euro_dollar_ratio) throws
java.rmi.RemoteException;
}
J0
25
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Stateful example – The Home Interface
J0
26
import java.rmi.*;
import javax.ejb.*;
public interface DollarConverterHome extends EJBHome {
public DollarConverter create() throws RemoteException,
CreateException;
}
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Stateful example – The bean
J0
27
import java.rmi.*;
import javax.ejb.*;
public class DollarConverterBean implements SessionBean {
private SessionContext sessionContext;
public void ejbCreate() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext sessionContext)
{ this.sessionContext = sessionContext; }
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Stateful example – The bean
Marco Ronchetti - [email protected] 
private double euro_dollar_ratio=1;
J0
28
public double convertInEuro(double dollar) throws
java.rmi.RemoteException{
return dollar/euro_dollar_ratio;
}
public double convertInDollar(double euro) throws
java.rmi.RemoteException{
return euro*euro_dollar_ratio;
}
public void setRate(double euro_dollar_ratio) throws
java.rmi.RemoteException{
this.euro_dollar_ratio=euro_dollar_ratio;
}
}
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Stateful example – The descriptor
Marco Ronchetti - [email protected] 
<?xml version="1.0" encoding="UTF-8"?>
J0
29
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise
JavaBeans 1.1//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd">
<ejb-jar>
<description>A simple demo of a stateful Bean</description>
<display-name>An Euro to Dollar Converter Bean</display-name>
<enterprise-beans>
<session>
<description>A converter from Euro to Dollar</description>
<ejb-name>DollarConverter</ejb-name>
<home>DollarConverterHome</home>
<remote>DollarConverter</remote>
<ejb-class>DollarConverterBean</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Stateful example – The descriptor
J0
30
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>DollarConverter</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
<ejb-client-jar />
</ejb-jar>
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Stateful example – The client
J0
31
import javax.ejb.*;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
public class ConverterClient {
public ConverterClient() {}
public static void main(String[] args) {
Object objref=null;
DollarConverterHome home=null;
Get naming context
DollarConverter bean=null;
and object reference
try {
InitialContext ctx=new InitialContext();
objref=ctx.lookup("EDC");
Cast to correct type
JNDI name
home =(DollarConverterHome)PortableRemoteObject.narrow(
objref,DollarConverterHome.class);
bean=home.create();
Get a bean instance from container
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Stateful example – The client
Marco Ronchetti - [email protected] 
bean.setRate(0.978);
double euro=1000;
System.out.println(euro+" Euro = "
+bean.convertInDollar(euro)+" Dollar");
double dollar=1000;
System.out.println(dollar+" Dollar = "
+bean.convertInEuro(dollar)+" Euro");
} catch (Exception e) {
e.printStackTrace();
}
}
}
J0
32
Do your business
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Stateful example – execution
J0
33
java -classpath .;%J2EEHOME%\lib\j2ee.jar;…\DollarConverterAppClient.jar
-Dorg.omg.CORBA.ORBInitialHost=localhost ConverterClient
1000.0 Euro = 978.0 Dollar
1000.0 Dollar = 1022.4948875255624 Euro
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Session Beans Lifecycle: client’s view
J0
34
Start
Does not exist
Is not referenced
Crash,
Timeout
Exists
Is not referenced
Client
releases
reference
home.create()
Client
obtains
handle
Client
releases
reference
Client invokes method
(NoSuchObject Exception)
Does not exist
Is referenced
object.remove(),
home.remove(),
Crash, timeout
Exists
Is referenced
Client
invokes
method
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Stateless session Beans Lifecycle
J0
35
Client invokes method
(NoSuchObject Exception)
Does not exist
Client invokes create()
Container executes:
1) newInstance()
2) setSessionContext(sc)
3) ejbCreate()
Client invokes remove()
Container executes:
ejbRemoved()
Exists
In the pool
Client invokes method
“Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento
Stateful session Beans Lifecycle
Marco Ronchetti - [email protected] 
Client invokes create()
Container executes:
1) newInstance()
2) setSessionContext(sc)
3) ejbCreate()
J0
36
Does not exist
Client invokes remove(),
or timeout is reached
Container executes:
ejbRemoved()
ejbPassivate()
Client invokes
non TX method
Exists,
method ready
in the pool
Client invokes
TX method
AfterBegin()
Client invokes commit
beforeCompletion()
afterCompletion(true)
Client invokes
TX method
Passive
ejbActivate()
Exists,
method ready
in TX
Client invokes rollback
afterCompletion(false)