Title: Overview of Enterprise JavaBeans EJB
1Overview of Enterprise JavaBeans (EJB)
- Source
- Enterprise JavaBeans, Richard Monson-Haefel
2Applications to Date
- Web Applications (Servlets/JSPs)
- Provided user interface
- access to database
- simple business logic
- no transactions except at the database level no
distributed transactions across business logic - Distributed Applications (RMI)
- Distributed access to business logic
- access to, but no direct support for database
- limited transactions
- We have to write a lot of plumbing code
3What didnt we address ?
- Could someone else deploy your application?
- Component reusability
- Security
- Threading
- Resource Management
- Load-Balancing
- Fault Tolerance (a little with activation)
4Paradigm Shift
- We need to start thinking of applications as a
set of reusable components and some business
logic that ties the components together - Deployed in a re-usable environment that handles
middleware and deployment requirements
5Components Vs. Objects
- Usually larger-grained
- Provide a complete capability
- Credit Card Verification
- Can be customized for deployment
6Component Architectures
- Client-Side
- Applets
- Java Beans (development components)
- ActiveX
- Server Side
- Servlets
- Enterprise Java Beans (EJB)
- CORBA Components
- Microsoft Distributed interNet Applications
Architecture (DNA)
7Distributed Objects
- Makes business objects more accessible
- permits more of an 3-tier architecture
- user interface at the first tier (Web
Applications) - business logic at the second tier (Business
Applications) - enterprise resources at the third tier
(Databases) - Key technologies include RMI, CORBA, and DCOM
- Provide for communication, but limited in
server-side component support results in roll
your own component models
8Server-Side Components
- Architecture for developing distributed business
objects - Separates the development from the assembly into
specific applications - allows for the sale of smaller/reusable
components rather than end-to-end systems - assembler determines actual transactional,
security, persistence behavior, etc. for the
component.
9Component Transaction Monitors (CTMs)
- Sophisticated distributed object Application
Servers - usually made up of web servers, ORBs, Messaging,
Databases, Naming, etc. - Hybrid of TP Monitors (e.g., CICS and Tuxedo)and
ORBs (e.g., CORBA and RMI) - Provide infrastructure for managing transactions,
object distribution, concurrency, security,
persistence, and resource management - the developer isnt left rolling their own
- the developer complies with the model and
basically implements a lot of callback event and
declarative programming - Analogy CD-player CTM, CD Server-side
Component
10Enterprise JavaBeans (EJBs)
- Standard server-side component model for Java
Enterprise Applications - security
- resource pooling
- persistence
- concurrency
- transactional integrity
- Has nothing to do with JavaBeans
- JavaBeans designed for intra-process purposes
- GUIs
- non-visual widgets
- Enterprise Java Beans (EJB) designed for
inter-process purposes
11EJBs (cont.)
- Java
- platform independence
- write once, run anywhere
- EJB components
- platform/implementation independence
- write once, run in any Application Server
complying with the EJB spec - J2EE reference implementation
- IBMs Websphere
- BEAs Weblogic Server and Weblogic Enterprise
- Gemstone/J
- ...
12EJB Architecture
- Interfaces and Classes
- beans
- entity bean
- session session
- Stateless session bean
- Statefull session bean
- primary key
- home interface
- remote interface (EJB Object)
- Container
- Application Server
13EJB Conceptual Class Overview
14Bean Class Instance Lifecycle
No State
1 newInstance() 2 setEJBContext()
Pooled State
3 bean associated to an EJB Object
5 bean disassociated from EJB Object
Ready State
4 service client requests
15Bean Types
- Entity beans
- models persistent state - this state is
maintained through all method and server
invocations - nouns of the domain
- real world objects (e.g. Owner, Account,
Transaction) - Session beans
- models non-persistent state - this state will be
lost between method invocations (stateless
session) or server invocations (statefull
session) - manage tasks performed on behalf of a single
client (e.g. Teller, Monthly Statement) - contains the business processes in which to use
entity beans - manages actions that may cross entity beans or go
outside the concern of an entity bean - e.g. Teller may authenticate the user and
transfer funds between accounts - e.g. Statement may include transactions from
multiple accounts
16Bean Usages
- Entity beans
- model state maintained across all client
interactions - represent a row of data in a database
- Session beans
- model business process being performed by a
single client involving one or more entity beans - it extends the actions of the client into the
server - simplifies the actions programmed by the client
- limits the number of distributed calls required
between the client and the entity beans - limits the number of stubs that have to be loaded
by the client - are not persisted to a database
17Stateful and Stateless session beans
- Stateful session bean
- maintain the conversational state between a
client and the session bean - may be serialized out and passivated to conserve
system resources - will be serialized in and activated when needed
in the future - e.g. Teller session bean who is logged into an
transfers funds between accounts - Stateless session bean
- do not maintain conversational state
- each method is independent of another and the
only information needed is is supplied in the
call parameters - e.g. Statement that is given a list of accounts
or an owner to generate a textual report for - consumes the least amount of resources among all
the bean types
18EJB Classes and Interfaces
19Remote Interface
- Called the EJB Object
- Defines the business methods that will be
available to clients in a distributed call - e.g. Account.debit(transactionRec)
- e.g. Teller.transfer(sourceAccount,
targetAccount) - Gets compiled by the ejb compiler to creates RMI
stubs and skeletons - clients are used by RMI to translate a method
invocation to wire format - skeletons are used by RMI to translate wire
format to a method invocation
20Remote Interface (cont.)
- Defined as a Java interface
- extends javax.ejb.EJBObject
- extends java.rmi.Remote
- getEJBHome() - returns the Home object for the
bean - getPrimaryKey() - returns the primary key for the
object - getHandle() - returns a handle to object that may
be used to re-establish communications at a later
time possibly in another server - remove() - removes this EJBObject object (prior
to eviction) - isIdentical(EJBObject) - returns if both objects
are same
21Remote Interface ExampleCabin
- package com.titan.cabin
- import java.rmi.RemoteException
- public interface Cabin 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 getShip() throws RemoteException
- public void setShip(int sp) throws
RemoteException - public int getBedCount() throws
RemoteException - public void setBedCount(int bc) throws
RemoteException
22Home Interface
- Defines the lifecycle methods that will be
available to clients in a distributed call - create new bean objects, locate or remove
existing bean objects - e.g. AccountHome.create(owner, initialBalance)
- e.g. AccountHome.findByPrimaryKey(accountId)
- e.g. TellerHome.create(login, pin)
- e.g. MonthlyStatementHome.create()
- Gets compiled by the ejb compiler to creates RMI
stubs and skeletons
23Home Interface (cont.)
- Defined as a Java interface
- extends javax.ejb.EJBHome
- extends java.rmi.Remote
- getEJBMetaData() - returns metadata about the
bean - remove(primaryKey) - removes object identified by
primary key - remove(handle) - removes object identified by its
EJBHandle
24Home Interface ExampleCabinHome
- package com.titan.cabin
- import java.rmi.RemoteException
- import javax.ejb.CreateException
- import javax.ejb.FinderException
- public interface CabinHome extends
javax.ejb.EJBHome - public Cabin create(int id)
- throws CreateException, RemoteException
- public Cabin findByPrimaryKey(CabinPK pk)
- throws FinderException, RemoteException
-
25Bean Class
- Implements the business methods defined in the
Object interface - does not inherit from the Object or Home
interfaces - must have methods that match signatures supplied
in all of the Object interface and portions of
the Home interface - Account.deposit(transactionRec) -
AccountBean.deposit(transarctionRec) - AccountHome.create(owner) - AccountBean.ejbCreate(
owner) - the connection between the Object/Home interface
calls and the Bean implementation is done by the
EJB compiler that creates the container-specific
skeletal code - uses the Deployment Descriptor to help generate
glue code - similar in functionality to a C template or a
C CORBA TIE class
26Bean Class (cont.)
- Clients never interact with bean classes
themselves - always interact with the bean through home
(create, find, remove) and remote interfaces
(business methods) - beans that interact with other beans are simply
clients of the other bean - stubs and skeletons are created from the
interfaces that glue the client, the database,
and the bean code together
27Bean Class (cont.)
- implements javax.ejb.EntitySessionBean
- extends empty javax.ejb.EnterpriseBean extends
java.io.Serializable - set/unsetEntity/SessionContext()
- provides callback into container to find caller
id, transaction information, etc. - ejbCreate()
- container calls this when the bean gets
associated with an object - ejbPostCreate() Entity Beans Only
- container calls this after persisting the
objects state
28Bean Class (cont.)
- ejbPassivate()
- container calls this method when the instance is
being dis-associated with a particular EJB Object
(pre serialization) - ejbActivate()
- container calls this method when the instance is
taken fro a pool of available instances and
associated with a particular EJB Object (post
de-serialization) - ejbRemove()
- container calls this method of an instance before
removing an EJB Object from the system
29Bean Class (cont.)
- ejbLoad() Entity Beans Only
- container calls this method to instruct the
instance to synchronize its state with the state
stored in the database - command (bean managed), completion event
(container managed) - ejbStore() Entity Beans Only
- container calls this method to instruct the
instance to synchronize the state store in the
database with its state - command (bean managed), completion event
(container managed)
30Bean Class ExampleCabinBean
- package com.titan.cabin
- import javax.ejb.EntityContext
- public class CabinBean implements
javax.ejb.EntityBean - public int id
- public String name
- public int deckLevel
- public int ship
- public int bedCount
31 Bean Class ExampleCabinBean (cont.)
- public void ejbCreate(int id)
- this.id id
-
- public void ejbPostCreate(int id)
- // Do nothing. Required.
-
- public String getName()
- return name
-
- public void setName(String str)
- name str
-
- public int getShip()
- return ship
-
32Bean Class ExampleCabinBean (cont.)
- public void setShip(int sp)
- ship sp
-
- public int getBedCount()
- return bedCount
-
- public void setBedCount(int bc)
- bedCount bc
-
- public int getDeckLevel()
- return deckLevel
-
33Bean Class ExampleCabinBean (cont.)
- public void setDeckLevel(int level )
- deckLevel level
-
- public void setEntityContext(EntityContext
ctx) - // Not implemented.
-
- public void unsetEntityContext()
- // Not implemented.
-
- public void ejbActivate()
- // Not implemented.
-
- public void ejbPassivate()
- // Not implemented.
-
34Bean Class ExampleCabinBean (cont.)
- public void ejbLoad()
- // Not implemented.
-
- public void ejbStore()
- // Not implemented.
-
- public void ejbRemove()
- // Not implemented.
-
-
35Primary Key Class
- Value that uniquely identifies the object in the
database - Implements java.io.Serializable
- Implemenets
- hashCode()
- equals()
- toString() helpful
- Contains identical public attributes from the
Bean Class for those that represent the beans
primary key value(s).
36Primary Key Class ExampleCabinPK
- package com.titan.cabin
- public class CabinPK implements
java.io.Serializable - public int id
- public int hashCode( )
- return id
-
- public boolean equals(Object obj)
- if(obj instanceof CabinPK)
- return (id ((CabinPK)obj).id)
-
- return false
-
- public String toString()
- return String.valueOf(id)
-
-
37Containers
- Manages interaction between the bean and its
server - Provides a uniform interface to the bean and to
the server
38Containers (cont.)
- Creates new instances of beans and manages their
persistence - provides mapping between bean and containers
underlying database - provides the skeletal class code for the home and
remote interfaces - e.g. AccountHome.findByPrimaryKey(accountPK)
- container supplies code to create row in
database, instantiate an EJB Object to represent
that instance of an account - e.g. AccountHome.remove(accountPK)
- container supplies code to remove row in database
and remove any existing EJB Objects - e.g. Account.debit(transactionRec)
- container supplies code obtain the EJB Object
from storage, locate a bean to take on its state,
and invoke behavior on the bean
39Deployment Descriptor
- Instructs the server on the type of bean (session
or entity) - Instructs the server how to apply services to the
bean - Described in XML
- Created from IDE or text source
- Supplied with other bean components in a jar
(Java archive) file - bean class
- remote interface
- home interface
- primary key (for entity beans)
- deployment descriptor
- rmi stub/skeleton
40Deployment Descriptor Exampleejb-jar.xml (Cabin)
-
- Inc.//DTD Enterprise JavaBeans 1.1//EN"
"http//java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd" -
-
-
- This Cabin enterprise bean
entity represents a cabin on a cruise
ship. - CabinBean
- com.titan.cabin.CabinHome
- com.titan.cabin.Cabin
- com.titan.cabin.CabinBean-class
41Deployment Descriptor Example (cont.)
- Containertype
- com.titan.cabin.CabinPKprim-key-class
- False
- id
- - name
- - deckLevelname
- - ship
- - bedCountame
-
-
- ...
-
42EJB Object class
- Written by the container-specific compiler after
processing the deployment descriptor - Implements methods definedin remote
interfaceby delegating them toan instance of
the beanclass
43EJB home class
- Written by the container-specific compiler after
processing the deployment descriptor - Implements methods defined in home interface
- locate
- create
- remove
- Handles interactions with resources pools,
persistence mechanism, and resource managers
44Example Client
- Context jndiContext getInitialContext()
- Object obj jndiContext.lookup("ejb/CabinHome")
- System.out.println("found it! " obj)
- CabinHome home (CabinHome)javax.rmi.PortableRemo
teObject.narrow(obj, CabinHome.class) - System.out.println("narrowed it! " home)
- Cabin cabin_1 home.create(1)
- System.out.println("created it! " cabin_1)
- cabin_1.setName("Master Suite")
- cabin_1.setDeckLevel(1)
- cabin_1.setShip(1)
- cabin_1.setBedCount(3)
45Example Client
- CabinPK pk new CabinPK()
- pk.id 1
- System.out.println("keyed it! " pk)
-
- Cabin cabin_2 home.findByPrimaryKey(pk)
- System.out.println("found by key! " cabin_2)
- System.out.println(cabin_2.getName())
- System.out.println(cabin_2.getDeckLevel())
- System.out.println(cabin_2.getShip())
- System.out.println(cabin_2.getBedCount())
46Usage Scenarios
47Creating an Account Object
AccountBean
AccountBean
3 select instance 8 return instance
AccountBean
5 insert
bean pool
4 ejbCreate(10.00) 6 ejbPostCreate(10.00) 7
ejbPassivate()
1 createAccount(10.00)
AccountHome_EJBHome
remote interface
2 new Account_EJBObject
Client
Account_EJBObject
AccountBean
48Accessing an Account Object
AccountBean
AccountBean
3 select instance 10 return instance
AccountBean
2 select 8 update
bean pool
4 populate state 5 ejbLoad() 6 debit(10.00) 7
ejbStore()
Client
Account_EJBObject
AccountBean
1 debit(10.00)
49Deleting an Account Object
AccountBean
AccountBean
3 select instance 10 return instance
AccountBean
2 select 7 delete
bean pool
4 populate state 5 ejbLoad() 6 ejbRemove()
1 remove(pKey)
AccountHome_EJBHome
8 remove()
Client
Account_EJBObject
AccountBean
50Questions to Ponder...
- Entity bean, stateful session bean, stateless
session bean? - object which requires access by multiple clients
over its lifetime - object whose work accepts and returns all
information in the call - object that must exist after server crash
- object that must exist between method invocations
- object that encapsulates a set of client actions
to multiple beans - Why is there not a ratio of 11 between instanced
of beans (EJB Objects) and instances of the bean
class? - What GoF Pattern does this depict?