Enterprise Java Beans
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 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 is responsible for saving its own state
Container doesn’t need to generate DB calls
Less adaptable; persistence is hard-coded
Writing an EJB Client
Locate the bean container
Allocate a bean, if necessary
Use the bean
Dispose of the bean
Client Example
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
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
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