Presentation is loading. Please wait.

Presentation is loading. Please wait.

J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento.

Similar presentations


Presentation on theme: "J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento."— Presentation transcript:

1 J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento EJB: the details

2 J0 2 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento 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

3 J0 3 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento NamingService Directory Machine Client Machine The logical architecture Pool App server (container) Machine HomeInterface Trovami la Home interface Dammi una istanza Istanza Crea o recupera una istanza Trova Metodo()

4 J0 4 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Session Beans -) stateless -) stateful

5 J0 5 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Stateful Example Stateless Example: Euro Converter

6 J0 6 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Stateful Example – suggested hierarchy App serverclient src META-INF ejb-jar.xml deploy xHome.java xHome.class client.java x.java xBean.java x.class xBean.class xHome.class x.class

7 J0 7 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Stateless example – The Component (Remote) Interface package statelessDemo; import java.rmi.*; import javax.ejb.*; public interface Converter extends EJBObject { public double convert(int lire); }

8 J0 8 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Stateless example – The Home Interface package statelessDemo; import java.rmi.*; import javax.ejb.*; public interface ConverterHome extends EJBHome { public Converter create() throws RemoteException, CreateException; }

9 J0 9 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Stateless example – The bean 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= ; public double convert(int lire) { return lire/ratio; }

10 J0 10 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Stateless example – The descriptor A simple demo of a stateless Bean An Euro Converter Bean A converter from Lire to Euro EuroConverter statelessDemo.ConverterHome statelessDemo.Converter statelessDemo.ConverterBean Stateless Container

11 J0 11 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Stateless example – The descriptor EuroConverter * Required

12 J0 12 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento structure of the jar file META-INF ejb-jar.xml statelessDemo Converter.class In this directory, run jar cvf converter.jar * ConverterBean.classConverterHome.class this directory(ies) reflect the package hierarchy Watch out! Packages often make life complex. (its easy to make mistakes) Try first without packages.

13 J0 13 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Compile the sources, generate jar file cls javac Converter.java move Converter.class statelessDemo\ javac ConverterHome.java move ConverterHome.class statelessDemo\ javac ConverterBean.java move ConverterBean.class statelessDemo\ cd..\deployment del *.jar del statelessDemo\*.class move..\src\statelessDemo\* statelessDemo\ jar cvf Converter.jar * cd..\src

14 J0 14 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Start the container 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

15 J0 15 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Start the container File New Application… File Add to Application… EJB-JAR (scegliere converter.jar)

16 J0 16 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento View the properties

17 J0 17 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Start deployment Tools Deploy Check this box Crea ConverterApp.ear

18 J0 18 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Start deployment Choose a JNDI name

19 J0 19 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Start deployment Crea ConverterAppClient.jar

20 J0 20 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Stateless example – The client import javax.ejb.*; import javax.naming.InitialContext; public class ConverterClient { public ConverterClient() { } public static void main(String[] args) { try { InitialContext ctx=new InitialContext(); Object objref=ctx.lookup("L2EConverter"); statelessDemo.ConverterHome home = (statelessDemo.ConverterHome) javax.rmi.PortableRemoteObject.narrow( objref,statelessDemo.ConverterHome.class); statelessDemo.Converter bean=home.create(); Get naming context and object reference Cast to correct type Get a bean instance from container JNDI name

21 J0 21 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Stateless example – The client int lire=100000; 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); } Do your business

22 J0 22 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Compile the client cls cd client\statelessDemo\ copy..\server\deployment\statelessDemo\Converter.class. copy..\server\deployment\statelessDemo\ConverterHome.class. cd.. javac ConverterClient.java Questa è la directory che contiene i sorgenti del client

23 J0 23 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Stateless example – execution java -classpath.;%J2EEHOME%\lib\j2ee.jar;…\ConverterAppClient.jar -Dorg.omg.CORBA.ORBInitialHost=localhost ConverterClient Lire = Euro

24 J0 24 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Stateful Example Stateful Example: Dollar Converter

25 J0 25 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento 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; }

26 J0 26 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Stateful example – The Home Interface import java.rmi.*; import javax.ejb.*; public interface DollarConverterHome extends EJBHome { public DollarConverter create() throws RemoteException, CreateException; }

27 J0 27 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Stateful example – The bean 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; }

28 J0 28 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Stateful example – The bean private double euro_dollar_ratio=1; 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; }

29 J0 29 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Stateful example – The descriptor A simple demo of a stateful Bean An Euro to Dollar Converter Bean A converter from Euro to Dollar DollarConverter DollarConverterHome DollarConverter DollarConverterBean Stateful Container

30 J0 30 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Stateful example – The descriptor DollarConverter * Required

31 J0 31 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Stateful example – The client 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; DollarConverter bean=null; try { InitialContext ctx=new InitialContext(); objref=ctx.lookup("EDC"); home =(DollarConverterHome)PortableRemoteObject.narrow( objref,DollarConverterHome.class); bean=home.create(); Get naming context and object reference Cast to correct type Get a bean instance from container JNDI name

32 J0 32 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Stateful example – The client 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(); } Do your business

33 J0 33 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Stateful example – execution java -classpath.;%J2EEHOME%\lib\j2ee.jar ;…\DollarConverterAppClient.jar -Dorg.omg.CORBA.ORBInitialHost=localhost ConverterClient Euro = Dollar Dollar = Euro

34 J0 34 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Session Beans Lifecycle: clients view Does not exist Is not referenced Exists Is not referenced Exists Is referenced Does not exist Is referenced Start home.create() Client releases reference Crash, Timeout Client releases reference Client obtains handle object.remove(), home.remove(), Crash, timeout Client invokes method Client invokes method (NoSuchObject Exception)

35 J0 35 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Stateless session Beans Lifecycle Exists In the pool Does not exist ejbRemoved() Client invokes method (NoSuchObject Exception) 1)newInstance() 2)setSessionContext(sc) 3)ejbCreate() Client invokes create() Container executes: Client invokes remove() Container executes:

36 J0 36 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Stateful session Beans Lifecycle Passive ejbPassivate() ejbActivate() Exists, method ready in the pool Does not exist Client invokes remove(), or timeout is reached Container executes: ejbRemoved() Client invokes create() Container executes: 1)newInstance() 2)setSessionContext(sc) 3)ejbCreate() Client invokes non TX method Exists, method ready in TX Client invokes TX method AfterBegin() Client invokes commit beforeCompletion() afterCompletion(true) Client invokes rollback afterCompletion(false)


Download ppt "J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento."

Similar presentations


Ads by Google