Enterprise Java Beans

Download Report

Transcript Enterprise Java Beans

Enterprise Java Beans
Tom Valesky
[email protected]
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 Client
EJB Server
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
– associated with a
particular client
– created and destroyed
by a client
– do not survive system
shutdown
• Entity
– shared by multiple
clients
– persist across multiple
invocations
– survive system
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
– no internal state
– do not need to be
passivated
– can be pooled to
service multiple clients
• Stateful
– possess internal state
– need to handle
passivation/activation
– one per client
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
– Container is
responsible for saving
state
– In deployment
descriptor, specify
container-managed
fields
– persistence
independent of data
source
• Bean-managed
– Bean is responsible for
saving its own state
– Container doesn’t need
to generate DB calls
– Less adaptable;
persistence is hardcoded
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
• Create home interface
–
–
must extend javax.ejb.EJBHome interface
create() methods, remove() methods
• Implement create methods
–
called by container at creation time
• Implement the SessionBean interface
–
–
–
–
ejbActivate() - called when bean is activated
ejbPassivate() - called when bean is passivated
ejbRemove() - called when bean is destroyed
setSessionContext(SessionContext ctx) - called by container to give bean a context
Session Bean Example
package tom.ejb.restaurant.server;
public class OrderBean implements SessionBean{
private transient SessionContext ctx;
private String order;
//can have many create methods
public void ejbCreate () throws Exception {
//initialization of class variables here
}
//business method
public boolean order(String order) {
this.order = order;
System.out.println("order received for " + order);
return true;
}
//these methods are required by the SessionBean interface
public void ejbActivate() throws Exception {
}
public void ejbDestroy() throws Exception {
}
public void ejbPassivate() throws Exception {
}
public void setSessionContext(SessionContext ctx) throws Exception {
this.ctx = ctx;
}
}
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
• Optionally implement create() methods
• Create remote interface
–
must extend javax.ejb.EJBObject interface
• Create home interface
Entity Bean example
package tom.ejb.entity.server;
public class ExampleBean implements EntityBean
{
private transient EntityContext ctx;
//notice: no finder method -- generated at deployment time by container provider
//can have multiple create methods
public void ejbCreate () throws Exception {
}
//business method
public boolean doSomething () {
}
//required methods for EntityBean interface
public void ejbActivate() throws Exception {
}
public void ejbDestroy() throws Exception {
}
public void ejbPassivate() throws Exception {
}
public void ejbLoad() throws Exception {
}
public void ejbStore() throws Exception {
}
public void setEntityContext (EntityContext ctx) throws Exception {
this.ctx = ctx;
}
public void unsetEntityContext () throws Exception {
this.ctx = null;
}
}
Deploying EJBs
• EJBs deployed as .SER files
– serialized instance
• Manifest file used to list EJBs
• Must also provide a “deployment
descriptor”
– serialized instance of EntityDescriptor or
SessionDescriptor
EJB-Jar manifest
• Sample entry
– Name: tom/RestaurantDeployment.ser
– Enterprise-Bean: True
• “Name” line
– describes a serialized deployment descriptor
• “Enterprise-Bean” line
– indicates whether the entry should be treated as
an EJB (not all entries need to be EJBs)
Deployment Descriptors
• Serialized instances of a class
• Used to pass info to container about bean’s
deployment needs and preferences
• Created by EJB provider
Auxiliary Systems
• JNDI
– Java Naming and
Directory Interface
– Used to allow clients to
find EJB beans
• JTS
– Java Transaction
Service
– Used to provide
transaction support in
EJB
EJB-to-CORBA bindings
• Uses COS naming for object lookup
• Uses OTS (object transaction service) for
transaction support
• Specifies IDL mappings for EJB objects
Who’s announced EJB support?
•
•
•
•
•
•
•
•
WebLogic
IBM
Oracle
GemStone
BEA
Borland
Netscape
Lotus
•
•
•
•
•
•
•
•
Forte
Progress
Novell
Novera
Borland
Informix
IONA
More...
For further information
• Sun’s Enterprise Homepage
– http://java.sun.com/products/ejb/index.html
• My EJB resources page:
– http://www.patriot.net/users/tvalesky/ejb.html
• WebLogic’s Tengah pages
– http://www.weblogic.com