Académique Documents
Professionnel Documents
Culture Documents
Tom Valesky
tvalesky@patriot.net
What is Enterprise JavaBeans?
• Not really JavaBeans
• Component architecture for distributed
systems
• Framework for creating middleware
Roles in EJB
• Enterprise Bean provider
– creates and sells EJBs
• Application assembler
– uses EJBs to build an application
• EJB Server provider
– creates and sells EJB server
• EJB Container provider
– creates and sells EJB containers
– server provider will likely provide containers
The EJB Architecture
• EJB servers
• EJB containers
• EJB clients
• Enterprise Java Beans
• Auxiliary systems
– JNDI
– JTS
EJB Architecture
EJB Server
EJB Client EJB Container
Enterprise
Bean
EJB Server
EJB Servers
• Analogous to CORBA ORB
– server software
– provides naming and transaction services
– makes containers visible
EJB Containers
• Interface between EJB Bean and outside
world
• Client never accesses bean directly; access
is done through container-generated
methods, which in turn call the bean’s
methods
EJB Clients
• Find EJB containers via JNDI
• Make use of EJB beans
Enterprise Java Beans
• Session beans
• Entity beans
Session vs. Entity Beans
• Session • Entity
– associated with a – shared by multiple
particular client clients
– created and destroyed – persist across multiple
by a client invocations
– do not survive system – survive system
shutdown shutdown
Passivation/Activation
• EJB server has the right to manage its
working set
• Passivation -- saves state of a bean to
persistent storage, then swaps it out.
• Activation -- restores state of a bean from
persistent storage, then swaps it in.
• Applies to both session and entity beans.
Stateless vs. Stateful Session
Beans
• Stateless • Stateful
– no internal state – possess internal state
– do not need to be – need to handle
passivated passivation/activation
– can be pooled to – one per client
service multiple clients
Persistent session beans
• Session Beans can be saved and restored
across client sessions.
• To save, call the session bean’s getHandle()
method; returns a Handle object
• To restore, call the Handle object’s
getEJBObject() method.
Entity Bean persistence
• Container-managed • Bean-managed
– Container is – Bean is responsible for
responsible for saving saving its own state
state – Container doesn’t need
– In deployment to generate DB calls
descriptor, specify – Less adaptable;
container-managed persistence is hard-
fields coded
– persistence
independent of data
source
Writing an EJB Client
• Locate the bean container
• Allocate a bean, if necessary
• Use the bean
• Dispose of the bean
Client Example
//An idealized EJB client
import tom.ejb.restaurant.*;
public class EJBClient{
public static void main(String[] argv){
//get JNDI naming context
javax.naming.Context initialContext = new javax.naming.InitialContext();
//use context to look up EJB home interface
RestaurantHome rh = initialContext.lookup(“RestaurantHome”);
//use home interface to create a session object
Restaurant r = rh.Create(“Burger Heaven”);
//invoke business methods
r.order(“cheeseburger”);
//remove session object
r.remove();
}
}
Writing a session bean
• Create remote interface
– must extend javax.ejb.EJBObject interface
– must give prototypes for business methods
– class needn’t say “implements”; this is handled by the container
}
Writing an Entity Bean
• Implement the EntityBean interface
– ejbActivate() -- called on activation
– ejbPassivate() -- called on passivation
– ejbLoad() -- tells bean to load state from database
– ejbStore() -- tells bean to store state in database
– ejbRemove() -- called when client calls remove()
– setEntityContext() -- called by container when instance has been created
– unsetEntityContext() -- called by container before removing the instance
– must also implement ejbFind() -- allows client to look up EJB objects