5. Enterprise JavaBeans 5.4 Session Beans Session beans Exécutent des traitements métiers (calculs, accès BD,…) � Peuvent accéder à des données dans la BD mais ne représentent pas � directement ces données. Sont non persistants (short-lived) � Peuvent être inclus dans une transaction � Associés à un seul client � Un flot d’exécution est créé pour � (en général) plusieurs appels de méthodes en provenance du même client et � un état transactionnel est maintenu • stateful session beans (avec état), ex: shopping cart chaque appel de méthode (sont des cas spéciaux des précédents) � • stateless session beans (sans état), pas de données interne, inutile de le rendre passif, peuvent être partagés par plusieurs clients, ex: fonctions mathématiques (tout ce dont la méthode a besoin est passé dans les paramètres) Détruits après un arrêt (ou une panne) du serveur EJB � Department of Informatics
5. Enterprise JavaBeans 5.4 Session Beans How a Client Uses a Session Bean JNDI Server lookup Container create Client method Home Bean Instance Component Department of Informatics
5. Enterprise JavaBeans 5.4 Session Beans Stateless Session Beans � Not tied to any particular client Can use instance variables only if they are not client related � � All Stateless Session Beans are equivalent A container can choose � • To serve the same instance of a Bean to multiple clients • To serve different Bean instances to the same client at different times A container may maintain a pool of Stateless Session Beans � • No necessary relation between the moment when a client creates the Bean and when the Container creates the Bean � Provide very high scalability Department of Informatics
5. Enterprise JavaBeans 5.4 Session Beans Stateful Session Beans � Assigned to a particular client Maintain per client state across multiple client requests � � May be “passivated” – allows a degree of pooling The container serializes the state of a Bean non currently � being used and writes state to secondary storage Frees JVM resources held for Bean � When a new request arrives for Bean, it must be activated � • State read from secondary storage and deserialized Can only passivate a Bean if it is not in a transaction � • More on transactions later Department of Informatics
5. Enterprise JavaBeans 5.4 Session Beans Lifecycle of a Stateful Session Bean Container Perspective create passivate Does Not Exist Ready Passive remove activate start commit or transaction rollback In Transaction timeout Department of Informatics
5. Enterprise JavaBeans 5.5 Message Driven Beans Message Driven Bean � Traitements métiers asynchrones � Messages JMS déclenchent le traitement � Files d'attente de messages � Supportent mieux la montée en charge � Stateless bean � Désanvantages : � Une seule méthode onMessage() � Très récents. Evoluent encore. Department of Informatics
5. Enterprise JavaBeans 5.5 Message Driven Beans Message Beans Executes upon receipt of a client JMS message � Asynchronous � No return value � Stateless and short lived � May access persistent data but does not represent persistent � data Not tied to a client � A single Message Bean can process messages from multiple clients � Has neither Home nor Component interface � Msg Bean Msg Bean Instance Msg Bean Instance Client Destination Instance Department of Informatics
5. Enterprise JavaBeans 5.3-5.5 Summary Characteristics of EJBs Contain business logic that operates on enterprise’s data � Bean provider defines a client view � Client view is independent of the container in which the bean is � deployed Beans are created and managed at runtime by a Container which � mediates client access Client never directly accesses the Bean � Since container involved in path between client and Bean instance it � can implement pragmatics and lifecycle functions If Bean uses only services defined by EJB Spec., it can be � deployed in any compliant Container Specialized containers with extended functionality can be defined � Can be assembled into an application without requiring source � code Department of Informatics
5. Enterprise JavaBeans 5.6 Implementation Putting It Alltogether 1. Show how to implement an entity bean (Cabin) 2. Show how to implement a client application using this entity bean 3. Show how to implement a session bean (TravelAgent) 4. Show how to implement a client application using these beans 5. Show how to deploy the bean: .jar file (packaging) � XML deployment descriptor � Department of Informatics
5. Enterprise JavaBeans 5.6 Implementation The Remote Interface package com.titan.cabin; import java.rmi.RemoteException; public interface CabinRemote extends javax.ejb.EJBObject { public String getName() throws RemoteException; public void setName(String str) throws RemoteException; public int getDeckLevel() throws RemoteException; public void setDeckLevel(int level) throws RemoteException; public int getShipId() throws RemoteException; public void setShipId(int sp) throws RemoteException; public int getBedCount() throws RemoteException; public void setBedCount(int bc) throws RemoteException; } Department of Informatics
5. Enterprise JavaBeans 5.6 Implementation The Local Interface package com.titan.cabin; import java.rmi.RemoteException; java.ejb.EJBException; CabinLocal public interface CabinRemote extends javax.ejb.EJBObject EJBLocalObject { public String getName() throws RemoteException; EJBException; public void setName(String str) throws RemoteException; EJBException; public int getDeckLevel() throws RemoteException; EJBException; public void setDeckLevel(int level) throws RemoteException; EJBException; public int getShipId() throws RemoteException; EJBException; public void setShipId(int sp) throws RemoteException; EJBException; public int getBedCount() throws RemoteException; EJBException; public void setBedCount(int bc) throws RemoteException; EJBException; } Department of Informatics
5. Enterprise JavaBeans 5.6 Implementation The Local Interface (2) package com.titan.cabin; import javax.ejb.EJBException; public interface CabinLocal extends javax.ejb.EJBLocalObject { public String getName() throws EJBException; public void setName(String str) throws EJBException; public int getDeckLevel() throws EJBException; public void setDeckLevel(int level) throws EJBException; public int getShipId() throws EJBException; public void setShipId(int sp) throws EJBException; public int getBedCount() throws EJBException; public void setBedCount(int bc) throws EJBException; } Department of Informatics
5. Enterprise JavaBeans 5.6 Implementation The Remote Home Interface package com.titan.cabin; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.FinderException; public interface CabinHomeRemote extends javax.ejb.EJBHome { public CabinRemote create(Integer id) throws CreateException, RemoteException; public CabinRemote findByPrimaryKey(Integer pk) throws FinderException, RemoteException; } Department of Informatics
5. Enterprise JavaBeans 5.6 Implementation The Local Home Interface package com.titan.cabin; import java.rmi.RemoteException; javax.EJB.EJBException; import javax.ejb.CreateException; import javax.ejb.FinderException; CabinHomeLocal javax.ejb.EJBLocalHome public interface CabinHomeRemote extends javax.ejb.EJBHome { public CabinRemote create(Integer id) throws CreateException, RemoteException; EJBException; public CabinRemote findByPrimaryKey(Integer pk) throws FinderException, RemoteException; EJBException; } Department of Informatics
5. Enterprise JavaBeans 5.6 Implementation The Local Home Interface (2) package com.titan.cabin; import java.rmi.EJBException; import javax.ejb.CreateException; import javax.ejb.FinderException; public interface CabinHomeLocal extends javax.ejb.EJBLocalHome { public CabinLocal create(Integer id) throws CreateException, EJBException; public CabinRemote findByPrimaryKey(Integer pk) throws FinderException, EJBException; } Department of Informatics
5. Enterprise JavaBeans 5.6 Implementation The Bean Class Life-cycle callback methods from EntityBean package com.titan.cabin; public void setEntityContext(EntityContext ctx) { import javax.ejb.EntityContext; // Not implemented. import javax.ejb.CreateException; } public void unsetEntityContext() public abstract class CabinBean { implements javax.ejb.EntityBean // Not implemented. { } public void ejbActivate() public Integer ejbCreate(Integer id) throws CreateException { methods { // Not implemented. Create this.setId(id); } return null; public void ejbPassivate() } { public void ejbPostCreate(Integer id) // Not implemented. { } } public void ejbLoad() { public abstract void setId(Integer id); // Not implemented. public abstract Integer getId(); } Business methods public void ejbStore() public abstract void setShipId(int ship); { public abstract int getShipId(); // Not implemented. } public abstract void setName(String name); public void ejbRemove() public abstract String getName(); { // Not implemented. public abstract void setBedCount(int count); } public abstract int getBedCount(); } public abstract void setDeckLevel(int level); public abstract int getDeckLevel(); Department of Informatics
Recommend
More recommend