Session beans Excutent des traitements mtiers (calculs, accs BD,) - - PowerPoint PPT Presentation

session beans
SMART_READER_LITE
LIVE PREVIEW

Session beans Excutent des traitements mtiers (calculs, accs BD,) - - PowerPoint PPT Presentation

5. Enterprise JavaBeans 5.4 Session Beans Session beans Excutent des traitements mtiers (calculs, accs BD,) Peuvent accder des donnes dans la BD mais ne reprsentent pas directement ces donnes. Sont non persistants


slide-1
SLIDE 1

Department of Informatics

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
  • 5. Enterprise JavaBeans

5.4 Session Beans

slide-2
SLIDE 2

Department of Informatics

How a Client Uses a Session Bean

Home Client Component JNDI Server Bean Instance Container lookup create method

  • 5. Enterprise JavaBeans

5.4 Session Beans

slide-3
SLIDE 3

Department of Informatics

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

  • 5. Enterprise JavaBeans

5.4 Session Beans

slide-4
SLIDE 4

Department of Informatics

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
  • 5. Enterprise JavaBeans

5.4 Session Beans

slide-5
SLIDE 5

Department of Informatics

Lifecycle of a Stateful Session Bean Container Perspective

Passive Ready Does Not Exist create In Transaction commit or rollback start transaction timeout remove passivate activate

  • 5. Enterprise JavaBeans

5.4 Session Beans

slide-6
SLIDE 6

Department of Informatics

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.

  • 5. Enterprise JavaBeans

5.5 Message Driven Beans

slide-7
SLIDE 7

Department of Informatics

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

Client Destination

Msg Bean Instance Msg Bean Instance Msg Bean Instance

  • 5. Enterprise JavaBeans

5.5 Message Driven Beans

slide-8
SLIDE 8

Department of Informatics

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

  • 5. Enterprise JavaBeans

5.3-5.5 Summary

slide-9
SLIDE 9

Department of Informatics

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
  • 5. Enterprise JavaBeans

5.6 Implementation

slide-10
SLIDE 10

Department of Informatics

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; }

  • 5. Enterprise JavaBeans

5.6 Implementation

slide-11
SLIDE 11

Department of Informatics

The Local 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; } java.ejb.EJBException; EJBLocalObject EJBException; EJBException; EJBException; EJBException; EJBException; EJBException; EJBException; EJBException;

  • 5. Enterprise JavaBeans

5.6 Implementation

CabinLocal

slide-12
SLIDE 12

Department of Informatics

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; }

  • 5. Enterprise JavaBeans

5.6 Implementation

slide-13
SLIDE 13

Department of Informatics

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; }

  • 5. Enterprise JavaBeans

5.6 Implementation

slide-14
SLIDE 14

Department of Informatics

The Local 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; }

  • 5. Enterprise JavaBeans

5.6 Implementation

CabinHomeLocal javax.ejb.EJBLocalHome javax.EJB.EJBException; EJBException; EJBException;

slide-15
SLIDE 15

Department of Informatics

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; }

  • 5. Enterprise JavaBeans

5.6 Implementation

slide-16
SLIDE 16

Department of Informatics

The Bean Class

package com.titan.cabin; import javax.ejb.EntityContext; import javax.ejb.CreateException; public abstract class CabinBean implements javax.ejb.EntityBean { public Integer ejbCreate(Integer id) throws CreateException { this.setId(id); return null; } public void ejbPostCreate(Integer id) { } public abstract void setId(Integer id); public abstract Integer getId(); public abstract void setShipId(int ship); public abstract int getShipId(); public abstract void setName(String name); public abstract String getName(); public abstract void setBedCount(int count); public abstract int getBedCount(); public abstract void setDeckLevel(int level); public abstract int getDeckLevel(); public void setEntityContext(EntityContext ctx) { // Not implemented. } public void unsetEntityContext() { // Not implemented. } public void ejbActivate() { // Not implemented. } public void ejbPassivate() { // Not implemented. } public void ejbLoad() { // Not implemented. } public void ejbStore() { // Not implemented. } public void ejbRemove() { // Not implemented. } }

Business methods Life-cycle callback methods from EntityBean Create methods

  • 5. Enterprise JavaBeans

5.6 Implementation

slide-17
SLIDE 17

Department of Informatics

Using the Remote Client API

package com.titan.clients; import com.titan.cabin.CabinHomeRemote; import com.titan.cabin.CabinRemote; import javax.naming.InitialContext; import javax.naming.Context; import javax.naming.NamingException; import javax.rmi.PortableRemoteObject; import java.rmi.RemoteException; public class Client_1 { public static void main(String [] args) { try { Context jndiContext = new InitialContext(); Object ref = jndiContext.lookup("CabinHomeRemote"); CabinHomeRemote home = (CabinHomeRemote) PortableRemoteObject.narrow(ref,CabinHomeRemote.class); CabinRemote cabin_1 = home.create(new Integer(1)); cabin_1.setName("Master Suite"); cabin_1.setDeckLevel(1); cabin_1.setShipId(1); cabin_1.setBedCount(3); CabinRemote cabin_2 = home.findByPrimaryKey(new Integer(1)); System.out.println(cabin_2.getName()); System.out.println(cabin_2.getDeckLevel()); System.out.println(cabin_2.getShipId()); System.out.println(cabin_2.getBedCount()); } catch (java.rmi.RemoteException re){re.printStackTrace();} catch (javax.naming.NamingException ne){ne.printStackTrace();} catch (javax.ejb.CreateException ce){ce.printStackTrace();} catch (javax.ejb.FinderException fe){fe.printStackTrace();} } }

  • 5. Enterprise JavaBeans

5.6 Implementation

slide-18
SLIDE 18

Department of Informatics

Using the Local Client API

package com.titan.clients; import com.titan.cabin.CabinHomeRemote; import com.titan.cabin.CabinRemote; import javax.naming.InitialContext; import javax.naming.Context; import javax.naming.NamingException; import javax.rmi.PortableRemoteObject; import java.rmi.RemoteException; public class Client_1 { public static void main(String [] args) { try { Context jndiContext = new InitialContext(); Object ref = jndiContext.lookup("java:comp/env/ejb/CabinHomeLocal"); CabinHomeLocal home = (CabinHomeLocal)ref; CabinLocal cabin_1 = home.create(new Integer(1)); cabin_1.setName("Master Suite"); cabin_1.setDeckLevel(1); cabin_1.setShipId(1); cabin_1.setBedCount(3); Integer pk = new Integer(1); CabinLocal cabin_2 = home.findByPrimaryKey(pk); System.out.println(cabin_2.getName()); System.out.println(cabin_2.getDeckLevel()); System.out.println(cabin_2.getShipId()); System.out.println(cabin_2.getBedCount()); } catch (javax.naming.NamingException ne){ne.printStackTrace();} catch (javax.ejb.CreateException ce){ce.printStackTrace();} catch (javax.ejb.FinderException fe){fe.printStackTrace();} } }

The same as in the remote client, except that home.findByPrimaryKey(pk) returns a CabinLocal instead of a CabinRemote

  • 5. Enterprise JavaBeans

5.6 Implementation

slide-19
SLIDE 19

Department of Informatics

Entity Beans creation and removal

  • 5. Enterprise JavaBeans

5.6 Implementation Image tirée du livre Enterprise Java Beans, R. Monson-Haefel, O'Reilly

slide-20
SLIDE 20

Department of Informatics

The Remote Interface

package com.titan.travelagent; import java.rmi.RemoteException; import javax.ejb.FinderException; public interface TravelAgentRemote extends javax.ejb.EJBObject { // String elements follow the format "id, name, deck level" public String [] listCabins(int shipID, int bedCount) throws RemoteException; }

  • 5. Enterprise JavaBeans

5.6 Implementation

  • The changes for the local interface are analogous to the example

with the entity bean (extends javax.ejb.EJBLocalObject, throws EJBException)

slide-21
SLIDE 21

Department of Informatics

The Remote Home Interface

package com.titan.travelagent; import java.rmi.RemoteException; import javax.ejb.CreateException; public interface TravelAgentHomeRemote extends javax.ejb.EJBHome { public TravelAgentRemote create() throws RemoteException, CreateException; }

  • 5. Enterprise JavaBeans

5.6 Implementation

  • The changes for the local home interface are analogous to the

example with the entity bean (extends javax.ejb.EJBLocalHome, throws EJBException)

slide-22
SLIDE 22

Department of Informatics

The Bean Class

package com.titan.travelagent; import com.titan.cabin.CabinRemote; import com.titan.cabin.CabinHomeRemote; import java.rmi.RemoteException; import javax.naming.InitialContext; import javax.naming.Context; import javax.ejb.EJBException; import java.util.Vector; public class TravelAgentBean implements javax.ejb.SessionBean { public void ejbCreate() { // Do nothing. } // String elements follow the format "id, name, deck level" public String [] listCabins(int shipID, int bedCount) { try { javax.naming.Context jndiContext = new InitialContext(); Object obj = jndiContext.lookup("java:comp/env/ejb/CabinHomeRemote"); CabinHomeRemote home = (CabinHomeRemote) javax.rmi.PortableRemoteObject.narrow(obj,CabinHomeRemote.class); Vector vect = new Vector(); for (int i = 1; ; i++) { Integer pk = new Integer(i); CabinRemote cabin = null; try { cabin = home.findByPrimaryKey(pk); } catch(javax.ejb.FinderException fe) { System.out.println("Caught exception: "+fe.getMessage()+" for pk="+i); break; } // Check to see if the bed count and ship ID match. if (cabin != null && cabin.getShipId() == shipID && cabin.getBedCount() == bedCount) { String details = i+","+cabin.getName()+","+cabin.getDeckLevel(); vect.addElement(details); } } String [] list = new String[vect.size()]; vect.copyInto(list); return list; } catch(Exception e) { throw new EJBException(e); } } public void ejbRemove(){} public void ejbActivate(){} public void ejbPassivate(){} public void setSessionContext(javax.ejb.SessionContext cntx){} }

Life-cycle callback methods for session bean Create method

The single business method

Beans are clients to other beans, just like applications and must interact with other beans in the same way that the client applications interact with beans. (i.e. first locate and obtain a reference to the bean's EJB home using JNDI)

  • 5. Enterprise JavaBeans

5.6 Implementation

slide-23
SLIDE 23

Department of Informatics

Client Application

package com.titan.clients; import com.titan.travelagent.TravelAgentHomeRemote; import com.titan.travelagent.TravelAgentRemote; import javax.naming.InitialContext; import javax.naming.Context; import javax.naming.NamingException; import javax.ejb.CreateException; import javax.rmi.PortableRemoteObject; import java.rmi.RemoteException; public class Client_3 { public static int SHIP_ID = 1; public static int BED_COUNT = 3; public static void main(String [] args) { try { Context jndiContext = new InitialContext(); Object ref = jndiContext.lookup("TravelAgentHomeRemote"); TravelAgentHomeRemote home = (TravelAgentHomeRemote) PortableRemoteObject.narrow(ref,TravelAgentHomeRemote.class); TravelAgentRemote travelAgent = home.create(); // Get a list of all cabins on ship 1 with a bed count of 3. String list [] = travelAgent.listCabins(SHIP_ID,BED_COUNT); for(int i = 0; i < list.length; i++) { System.out.println(list[i]); } } catch(java.rmi.RemoteException re) { re.printStackTrace(); } catch(Throwable t) { t.printStackTrace(); } } }

  • The changes for a local client are analogous to the example with

the entity bean (no need to narrow() and no RemoteExceptions)

  • 5. Enterprise JavaBeans

5.6 Implementation

slide-24
SLIDE 24

Department of Informatics

Session Beans creation and removal

  • 5. Enterprise JavaBeans

5.6 Implementation Image tirée du livre Enterprise Java Beans, R. Monson-Haefel, O'Reilly

slide-25
SLIDE 25

Department of Informatics

Deploying the bean

Before deploying an entity bean you need

to define (XML deployment descriptor file) your abstract persistence model, which links:

the bean to a database table; and the properties to attributes (columns)

The table should either already exist, or

it is generated by the container.

  • 5. Enterprise JavaBeans

5.6 Implementation

slide-26
SLIDE 26

Department of Informatics

The JAR file

  • Package all the classes and interfaces

associated with a bean, including the deployment descriptor, into one file.

  • The deployment descriptor file must be

called ejb-jar.xml and must be in a META-INF subfolder.

  • The name of the jar file is not relevant.
  • It is this jar file that is actually deployed

in the container, following the instructions

  • f the container vendor.
  • Note that the Cabin_Object,

CabinRemote_Stub, CabinRemoteHome_Stub, TravelAbent_Object, TravelAgentRemote_stub, TravelAgentHomeRemote_stub are all generated by the container at deployment time.

titan.jar CabinRemote.class CabinBean.class CabinHomeRemote.class travelagent TravelAgentRemote.class TravelAgentBean.class TravelAgentHomeRemote.class META-INF ejb-jar.xml

  • 5. Enterprise JavaBeans

5.6 Implementation com titan cabin jboss.xml