1 / 36

EJB: the details

EJB: the details. 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. Directory Machine.

addison
Download Presentation

EJB: the details

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EJB: the details

  2. 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. Directory Machine App server (container) Machine Client Machine NamingService HomeInterface Pool Istanza Client Trovami la Home interface Trova Dammi una istanza Crea o recupera una istanza Metodo() The logical architecture

  4. Session Beans-) stateless -) stateful

  5. Stateful Example Stateless Example: Euro Converter

  6. Stateful Example – suggested hierarchy App server client src deploy src xHome.class META-INF xHome.java client.java x.class x.java xHome.class xBean.class ejb-jar.xml xBean.java x.class

  7. 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. 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. 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=1936.27; public double convert(int lire) { return lire/ratio; } }

  10. Stateless example – The descriptor <?xml version="1.0" encoding="UTF-8"?> <!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>

  11. Stateless example – The descriptor <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>

  12. structure of the jar file • In this directory, run jar cvf converter.jar * this directory(ies) reflect the package hierarchy statelessDemo META-INF ConverterBean.class ConverterHome.class ejb-jar.xml Converter.class Watch out! Packages often make life complex. (it’s easy to make mistakes) Try first without packages.

  13. 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. Start the container • We assume you installed the j2ee sdk distribution provided by Sun in some %J2EE_HOME% directory. • Run %J2EE_HOME%/bin/j2ee.bat • Run %J2EE_HOME%/bin/deploytool.bat

  15. Start the container FileNew Application… FileAdd to Application… EJB-JAR (scegliere converter.jar)

  16. View the properties

  17. Start deployment ToolsDeploy Check this box  Crea ConverterApp.ear

  18. Start deployment  Choose a JNDI name

  19. Start deployment  Crea ConverterAppClient.jar

  20. 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 JNDI name Cast to correct type Get a bean instance from container

  21. 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. 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. Stateless example – execution java -classpath .;%J2EEHOME%\lib\j2ee.jar;…\ConverterAppClient.jar -Dorg.omg.CORBA.ORBInitialHost=localhost ConverterClient 100000 Lire = 51.64568990894865 Euro

  24. Stateful Example Stateful Example: Dollar Converter

  25. 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. Stateful example – The Home Interface import java.rmi.*; import javax.ejb.*; public interface DollarConverterHome extends EJBHome { public DollarConverter create() throws RemoteException, CreateException; }

  27. 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. 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. Stateful example – The descriptor <?xml version="1.0" encoding="UTF-8"?> <!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>

  30. Stateful example – The descriptor <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>

  31. 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 JNDI name Get a bean instance from container

  32. 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. Stateful example – execution 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

  34. Session Beans Lifecycle: client’s view Client invokes method (NoSuchObject Exception) Start Client releases reference Does not exist Is not referenced Does not exist Is referenced home.create() object.remove(), home.remove(), Crash, timeout Crash, Timeout Client obtains handle Exists Is not referenced Exists Is referenced Client invokes method Client releases reference

  35. Stateless session Beans Lifecycle Client invokes method (NoSuchObject Exception) Does not exist Client invokes create() Container executes: Client invokes remove() Container executes: • newInstance() • setSessionContext(sc) • ejbCreate() ejbRemoved() Exists In the pool Client invokes method

  36. Does not exist Client invokes remove(), or timeout is reached Container executes: ejbRemoved() • Client invokes create() • Container executes: • newInstance() • setSessionContext(sc) • ejbCreate() ejbPassivate() Exists, method ready in the pool Client invokes non TX method Passive Client invokes TX method AfterBegin() ejbActivate() Client invokes commit beforeCompletion() afterCompletion(true) Exists, method ready in TX Client invokes rollback afterCompletion(false) Client invokes TX method Stateful session Beans Lifecycle

More Related