Enterprise Java Beans Overview
Download
Report
Transcript Enterprise Java Beans Overview
Enterprise
Java
Enterprise Java Beans
Overview
v10-17-2004
EJB Intro
1
Agenda
•
•
•
•
Enterprise
Java
Background
EJB Overview
Bean Types
EJB Interfaces and Classes
v10-17-2004
EJB Intro
2
Applications
Enterprise
Java
• 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
v10-17-2004
EJB Intro
3
What didn’t we address ?
•
•
•
•
•
•
•
Enterprise
Java
Could someone else deploy your application?
Component reusability
Security
Threading
Resource Management
Load-Balancing
Fault Tolerance (a little with activation)
v10-17-2004
EJB Intro
4
Paradigm Shift
Enterprise
Java
• 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
v10-17-2004
EJB Intro
5
Components Vs. Objects
Enterprise
Java
• Usually larger-grained
• Provide a complete capability
– Credit Card Verification
• Can be customized for deployment
v10-17-2004
EJB Intro
6
Component Architectures
Enterprise
Java
• Client-Side
– Applets
– Java Beans
– ActiveX
• Server Side
–
–
–
–
Servlets
Enterprise Java Beans (EJB)
CORBA Components
Microsoft .NET
v10-17-2004
EJB Intro
7
Distributed Objects
Enterprise
Java
• 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 serverside component support; results in “roll your own”
component models
v10-17-2004
EJB Intro
8
Server-Side Components
Enterprise
Java
• 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.
v10-17-2004
EJB Intro
9
Component Transaction Monitors
(CTMs)
Enterprise
Java
• 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 isn’t 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
v10-17-2004
EJB Intro
10
Enterprise JavaBeans (EJBs)
Enterprise
Java
• Standard server-side component model for Java
Enterprise Applications
• Enables large scale development
• Helps build portable applications
• 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
v10-17-2004
EJB Intro
11
EJB Server-side Component Model
Enterprise
Java
• Encapsulates application’s business logic
– manage terms of an account
– manage state of an order
– provide tax calculations
• Normally accesses database or other backend
systems
• EJB Clients
– implement only the presentation logic
– use EJBs for their business logic
v10-17-2004
EJB Intro
12
Enterprise
Java
EJB Enables Large System Development
• Distributed
communication
• Security
– RMI, RMI-IIOP, CORBAIDL
– class/method level, role
based
• Threading
• Transaction management
– scope, isolation, lifecycle
– container initiated
• Persistence
• Resource pooling
– container and bean
managed
– separation of bean state
from object state
v10-17-2004
EJB Intro
13
EJB Resource Pooling
Enterprise
Java
Bean Instance
Object State
• name
• address
Bean Pool
Bean Instance
Bean Instance
Bean Instance
v10-17-2004
Bean State
• connection
Object Methods()
• getCounty()
• getVotingDistrict()
Bean Methods()
• ejbCreate()
•ejbActivate()
• ejbPassivate()
• ejbRemove()
• setEJBContext()
• unsetEJBContext()
EJB Intro
Object State
Object State
Object State
Database
14
EJB Portability
Enterprise
Java
• Java
– platform independence
– “write once, run anywhere”
• EJB components
– platform/implementation independence
– write once, run in any Application Server complying
with the EJB spec
•
•
•
•
•
v10-17-2004
J2EE reference implementation
IBM’s Websphere
BEA’s Weblogic Server
Borland’s Enterprise AppServer
...
EJB Intro
15
EJB Architecture
Enterprise
Java
• Interfaces and Classes
– beans
• entity bean
• session session (no primary key)
– Stateless session bean
– Stateful session bean
• message driven bean (no primary key, home or object
interface)
– primary key (entity beans only)
– home interface (local and remote)
– EJB object interface (local and remote)
• Container
• Application Server
v10-17-2004
EJB Intro
16
Bean Types
Enterprise
Java
• 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 (Stateful 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
v10-17-2004
EJB Intro
17
Bean Types (cont.)
Enterprise
Java
• Message Driven beans
• models non-persistent state - this state will be lost between
message processing
• similar to Stateless Session Beans (which are invoked through
synchronous RMI calls)
• invoked through asynchronous JMS Messages
v10-17-2004
EJB Intro
18
Bean Usage
Enterprise
Java
• 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
v10-17-2004
EJB Intro
19
Bean Usage (cont.)
Enterprise
Java
• Message Driven Bean
– model business process invoked through asynchronous
messages
– are not persisted to a database
v10-17-2004
EJB Intro
20
Enterprise
Java
Stateful 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
v10-17-2004
EJB Intro
21
Stateless Session Bean Lifecycle
Enterprise
Java
Does Not Exist
bean
deployed/Class.newInstance();
setSessionContext(); ejbCreate()
bean undeployed/ejbRemove()
Method Ready Pool
business method
v10-17-2004
EJB Intro
22
Stateless Session Bean
Conceptual Class Model
JNDI
interface
HomeInterface
0..*
ContainerHomeInterfaceImpl
Enterprise
Java
one instance per type
+create: ObjectInterface
bean pool
uses to lookup
SessionEJB
uses to create
Client
interface
SessionContext
Container
-beanState:int
1
1
+_businessMethods:void
+_beanCallbacks_:void
uses business methods
interface
ObjectInterface
ContainerObjectInterfaceImpl
once instance per client invocation
+_businessMethods_:void
v10-17-2004
EJB Intro
23
Stateful Session Bean Lifecycle
remove/ejbRemove()
Does Not Exist
Enterprise
Java
timeout
create/Class.newInstance();
setSessionContext(); ejbCreate()
throws SystemException
resources low/ejbPassivate()
Method Ready
business method
v10-17-2004
EJB Intro
Passive
object needed/ejbActivate()
24
Stateful Session Bean
Conceptual Class Model
interface
HomeInterface
JNDI
Enterprise
Java
ContainerHomeInterfaceImpl
one instance per type
0..*
+create: ObjectInterface
uses to lookup
interface
SessionContext
Client
uses to create
SessionEJB
1 1
Container
-beanState:int
-_businessState_:int
+_businessMethods:void
+_beanCallbacks_:void
1
1
interface
ObjectInterface
ContainerObjectInterfaceImpl
uses business methods
one instance per client
+_businessMethods_:void
v10-17-2004
EJB Intro
25
Entity Bean Lifecycle
Enterprise
Java
Does Not Exist
unload bean
instance()/unsetEntityContext();
finalize()
bean instance
required/Class.newInstance();
setEntityContext()
Pooled
passivate object()/ejbPassivate
this.ejbSelect()
Home.find()/ejbFind()
Home.XXX()/ejbHomeXXX()
Home.create()/ejbCreate();
ejbPostCreate()
object.remove()/ejbRemove()
activate object()/ejbActivateObject
Ready
object.method()/ejbLoad();
business method(); ejbStore()
v10-17-2004
this.ejbSelect()
EJB Intro
26
Entity Bean
Conceptual Class Model
JNDI
0..*
interface
HomeInterface
Enterprise
Java
ContainerHomeInterfaceImpl
one per bean type
+create: ObjectInterface
+find: ObjectInterface
+remove:void
+_homeMethods_:void
Database
uses to lookup
EntityEJB
interface
EntityContext
1
Client
uses to create/find
interface
ObjectInterface
1
Container
-_objectState_:int
-_beanState_:int
+_businessMethods_:void
+_beanMethods_:void
ContainerObjectInterfaceImpl
uses business methods
10s to 100s of instances
conceptually millions
+_businessMethods_:void
v10-17-2004
EJB Intro
27
Message Driven Bean Lifecycle
Enterprise
Java
Does Not Exist
bean deployed/newInstance();
setMessageDrivenContext();
ejbCreate()
bean undeployed/ejbRemove()
Method Ready Pool
onMessage(msg)
v10-17-2004
EJB Intro
28
Message Driven Bean
Conceptual Class Model
interface
Destination
JNDI
Enterprise
Java
0..*
0..*
uses to lookup
publishes to
interface
MessageDrivenContext 1
beanEJB
Container
1
Client
+onMessage:void
10s instances per type
v10-17-2004
EJB Intro
29
Enterprise
Java
EJB Classes and Interfaces
Cabin Example from “Enterprise Java Beans, 3rd Edition”,
Monson-Haefel
v10-17-2004
EJB Intro
30
Remote Interface
Enterprise
Java
• 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
– stubs 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 EJB Intro
v10-17-2004
31
Remote Interface (cont.)
Enterprise
Java
• 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 reestablish 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
v10-17-2004
EJB Intro
32
Remote Interface Example:
Cabin
Enterprise
Java
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;
}
v10-17-2004
EJB Intro
33
Local Interface (EJB 2.0)
Enterprise
Java
• Like the Remote Interface, defines the “business”
methods that will be available to clients, but only
for local calls
– e.g. Account.debit(transactionRec)
– e.g. Teller.transfer(sourceAccount, targetAccount)
– Can only be used within the same JVM as the EJB
• Gets compiled by the ejb compiler to creates local
stubs for container to interpose transactions,
access control, etc. on invocations.
v10-17-2004
EJB Intro
34
Local Interface (EJB 2.0) (cont.)
Enterprise
Java
• Defined as a Java interface
– extends javax.ejb.LocalObject
• getEJBLocalHome() - returns the LocalHome object for the
bean
• getPrimaryKey() - returns the primary key for the object
• remove() - removes this EJBObject object (prior to eviction)
• isIdentical(EJBObject) - returns if both objects are same
– not applicable
• extends java.rmi.Remote - it is a local object
• getHandle() - unnecessary since client/EJB in same JVM
– methods do not throw RemoteException; only
EJBException
v10-17-2004
EJB Intro
35
Local Interface Example (EJB 2.0):
Cabin
Enterprise
Java
package com.titan.cabin;
import javax.ejb.EJBException;
public interface LocalCabin 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 getShip() throws EJBException;
public void setShip(int sp) throws EJBException;
public int getBedCount() throws EJBException;
public void setBedCount(int bc) throws EJBException;
}
v10-17-2004
EJB Intro
36
Remote Home Interface
Enterprise
Java
• 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
v10-17-2004
EJB Intro
37
Remote Home Interface (cont.)
Enterprise
Java
• 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
– defines
• create methods (create(<XXXX>))
• finder methods (findBy<XXXX>(<YYYY>))
• home methods (<XXXX>) //EJB 2.0
v10-17-2004
EJB Intro
38
Remote Home Interface Example:
CabinHome
Enterprise
Java
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;
public int getDeckCount(int deck)
throws RemoteException;//EJB2.0
}
v10-17-2004
EJB Intro
39
Local Home Interface (EJB 2.0)
Enterprise
Java
• Like the Remote Home 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()
Can only be used within the same JVM as the EJB
• Gets compiled by the ejb compiler to creates stubs
for container interpose on invocations
v10-17-2004
EJB Intro
40
Local Home Interface (cont.)
Enterprise
Java
• Defined as a Java interface
– extends javax.ejb.EJBLocalHome
• remove(primaryKey) - removes object identified by primary
key
– not applicable
• extends java.rmi.Remote
• getEJBMetaData() - inserted in Remote Home for visual tools
only
• remove(handle) - no need for handles in local JVM
– defines
• create methods (create(<XXXX>))
• finder methods (findBy<XXXX>(<YYYY>))
• home methods (<XXXX>)
v10-17-2004
EJB Intro
41
Local Home Interface Example
(EJB 2.0): CabinHome
Enterprise
Java
package com.titan.cabin;
import javax.ejb.EJBException;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
public interface LocalCabinHome
extends javax.ejb.EJBLocalHome {
public LocalCabin create(int id)
throws CreateException, EJBException;
public LocalCabin findByPrimaryKey(CabinPK pk)
throws FinderException, EJBException;
public int getDeckCount(int deck)
throws EJBException;
}
v10-17-2004
EJB Intro
42
Bean Class
Enterprise
Java
• 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 containerspecific skeletal code
• uses the Deployment Descriptor to help generate glue code
• similar in functionality to a C++ template or a CORBA TIE class
v10-17-2004
EJB Intro
43
Bean Class (cont.)
Enterprise
Java
• Clients never interact with bean classes
themselves
– always interact with the bean through home (create,
find, remove) and remote/local 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
v10-17-2004
EJB Intro
44
Bean Class (cont.)
Enterprise
Java
• implements javax.ejb.[Entity|Session|MessageDriven]Bean
– extends empty javax.ejb.EnterpriseBean; extends
java.io.Serializable
• set/unset[Entity|Session|MessageDriven]Context()
– 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 object’s state
v10-17-2004
EJB Intro
45
Bean Class (cont.)
Enterprise
Java
• ejbPassivate() [ Session and Entity Beans Only]
– container calls this method when the instance of the
bean class is either being returned to the pool (Entity)
or serialized/unloaded (Stateful)
• ejbActivate() [ Session and Entity Beans Only]
– container calls this method when the instance of the
bean class is taken from a pool of available instances
and associated with a particular EJB Object (Entity) or
loaded/de-serialized (Stateful)
• ejbRemove()
– container calls this method of an instance before
removing an EJB Object from the system
v10-17-2004
EJB Intro
46
Bean Class (cont.)
Enterprise
Java
• 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), preparation event (container
managed)
v10-17-2004
EJB Intro
47
Bean Class (cont.)
Enterprise
Java
• onMessage(Message msg) [ Message Driven Beans Only ]
– container calls bean method to handle message from
Destination (Topic or Queue)
v10-17-2004
EJB Intro
48
Bean Class Example (EJB 2.0):
CabinBean
Enterprise
Java
package com.titan.cabin;
import javax.ejb.EntityContext;
public abstract class CabinBean implements
javax.ejb.EntityBean {
public
public
public
public
public
public
public
public
public
public
v10-17-2004
abstract
abstract
abstract
abstract
abstract
abstract
abstract
abstract
abstract
abstract
void setId(Integer id);
Integer getId();
void setName(String name);
String getName();
void setDeckLevel(int level);
int getDeckLevel();
void setShipId(int ship);
int getShipId();
void setBedCount(int count);
int getBedCount();
EJB Intro
49
Bean Class Example (EJB 2.0):
CabinBean (cont.)
Enterprise
Java
public Integer ejbCreate(int id){
this.setId(new Integer(id));
return null;
}
public void ejbPostCreate(int id){
// Do nothing. Required.
}
//EJB 2.0 Home Method
public int ejbGetDeckCount(int deck) {
int count=0;
//implementation not shown
return count;
}
v10-17-2004
EJB Intro
50
Bean Class Example (EJB 2.0):
CabinBean (cont.)
public
}
public
}
public
}
public
}
public
}
public
}
public
}
void setEntityContext(
Enterprise
Java
// Not implemented.
void unsetEntityContext(){ // Not implemented.
void ejbActivate(){
// Not implemented.
void ejbPassivate(){
// Not implemented.
void ejbLoad(){
// Not implemented.
void ejbStore(){
// Not implemented.
void ejbRemove(){
// Not implemented.
}
v10-17-2004
EJB Intro
51
Bean Class Example (EJB 1.1):
CabinBean
Enterprise
Java
package com.titan.cabin;
import javax.ejb.EntityContext;
public class CabinBean implements javax.ejb.EntityBean
{
public
public
public
public
public
v10-17-2004
Integer id;
String name;
int deckLevel;
int shipId;
int bedCount;
EJB Intro
52
Bean Class Example (EJB 1.1):
CabinBean (cont.)
public
public
public
public
public
public
public
public
v10-17-2004
String getName(){
void setName(String str){
int getShipId(){
void setShipId(int sp) {
int getBedCount(){
void setBedCount(int bc){
int getDeckLevel(){
void setDeckLevel(int level){
EJB Intro
return name;
name = str;
return shipId;
shipId = sp;
return bedCount;
bedCount = bc;
return deckLevel;
deckLevel = level
Enterprise
Java
}
}
}
}
}
}
}
}
53
Bean Class Example (EJB 1.1):
CabinBean (cont.)
Enterprise
Java
public Integer ejbCreate(int id){
this.id = new Integer(id);
return null;
}
public void ejbPostCreate(int id){
// Do nothing. Required.
}
v10-17-2004
EJB Intro
54
Bean Class Example (EJB 1.1):
CabinBean (cont.)
public
}
public
}
public
}
public
}
public
}
public
}
public
}
void setEntityContext(
Enterprise
Java
// Not implemented.
void unsetEntityContext(){ // Not implemented.
void ejbActivate(){
// Not implemented.
void ejbPassivate(){
// Not implemented.
void ejbLoad(){
// Not implemented.
void ejbStore(){
// Not implemented.
void ejbRemove(){
// Not implemented.
}
v10-17-2004
EJB Intro
55
Primary Key Class
Enterprise
Java
• 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 bean’s primary key value(s).
v10-17-2004
EJB Intro
56
Primary Key Class Example:
CabinPK
Enterprise
Java
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);
}
}
v10-17-2004
EJB Intro
57
Containers
Enterprise
Java
• Manages interaction between the bean and its
server
• Provides a uniform interface to the bean and to the
server
v10-17-2004
EJB Intro
58
Containers (cont.)
Enterprise
Java
• Creates new instances of beans and manages their
persistence
– provides mapping between bean and container’s 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
v10-17-2004
EJB Intro
59
Deployment Descriptor
•
•
•
•
•
Enterprise
Java
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
•
•
•
•
•
•
v10-17-2004
bean class
remote interface
home interface
primary key (for entity beans)
deployment descriptor
RMI stub/skeleton
EJB Intro
60
Deployment Descriptor Example:
ejb-jar.xml (Cabin)
Enterprise
Java
<?xml version="1.0" ?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise
JavaBeans 2.0//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_2_0.dtd">
<ejb-jar>
<enterprise-beans>
<entity>
<description>
This Cabin enterprise bean entity represents a cabin on a cruise ship.
</description>
v10-17-2004
EJB Intro
61
Deployment Descriptor Example:
ejb-jar.xml (Cabin)
Enterprise
Java
<ejb-name>CabinBean</ejb-name>
<home>com.titan.cabin.CabinHome</home>
<remote>com.titan.cabin.Cabin</remote>
<local-home>com.titan.cabin.LocalCabinHome</local-home>
<local>com.titan.cabin.LocalCabin</local>
<ejb-class>com.titan.cabin.CabinBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Integer</prim-key-class>
<reentrant>False</reentrant>
v10-17-2004
EJB Intro
62
Deployment Descriptor Example:
(cont.)
Enterprise
Java
<cmp-version>2.x</cmp-version>
<abstract-schema-name>Cabin</abstract-schema-name>
<cmp-field> <field-name>id</field-name> </cmp-field>
-
<cmp-field> <field-name>name</field-name> </cmp-field>
-
-
<cmp-field> <field-name>deckLevel</field-name> </cmp-field>
<cmp-field> <field-name>shipId</field-name> </cmp-field>
-
<cmp-field> <field-name>bedCount</field-name> </cmp-field>
<primary-field>id</primary-field>
</entity>
</enterprise-beans>
...
</ejb-jar>
v10-17-2004
EJB Intro
63
Enterprise
Java
EJB Object class
• Written by the
container-specific
compiler after
processing the
deployment descriptor
• Implements methods
defined
in remote interface
by delegating them to
an instance of the bean
class
v10-17-2004
<<Interface>>
EJBObject
getEJBHome( )
getPrimaryKey(
getHandke()
remove( )
isIdentical( )
EntityBean
ejbPassivate( )
ejbActivate( )
ejbLoad( )
ejbStore( )
ejbRemove( )
setEntityContext( )
unsetEntityContext( )
<<extends>>
<<implements>>
<<Interface>>
Account
debit( )
credit( )
getBalance(
<<implements>>
Account_EJBObject
EJB Intro
AccountBean
id_
balance_
ejbCreate (balance)
ejbPostCreate (balance)
ejbPassivate ()
ejbActivate ()
ejbLoad ()
ejbStore ()
ejbRemove ()
setEntityContext ()
unsetEntityContext ()
64
EJB home class
Enterprise
Java
• 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
v10-17-2004
EJB Intro
65
Example Client
Enterprise
Java
Context jndiContext = getInitialContext();
Object obj = jndiContext.lookup("ejb/CabinHome");
System.out.println("found it! ="+ obj);
CabinHome home =
(CabinHome)javax.rmi.PortableRemoteObject.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);
v10-17-2004
EJB Intro
66
Example Client
Enterprise
Java
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());
v10-17-2004
EJB Intro
67
Enterprise
Java
Usage Scenarios
v10-17-2004
EJB Intro
68
Enterprise
Java
Creating an Account Object
AccountBean
database
3: select instance
8: return instance
5: insert
AccountBean
AccountBean
bean pool
1: createAccount(10.00)
AccountHome_EJBHome
4: ejbCreate(10.00)
6: ejbPostCreate(10.00)
7: ejbPassivate()
remote interface
2: new Account_EJBObject
Client
v10-17-2004
Account_EJBObject
EJB Intro
AccountBean
69
Accessing an Account Object
Enterprise
Java
AccountBean
database
3: select instance
10: return instance
2: select
8: update
AccountBean
AccountBean
bean pool
4: populate state
5: ejbLoad()
6: debit(10.00)
7: ejbStore()
Client
Account_EJBObject
AccountBean
1: debit(10.00)
v10-17-2004
EJB Intro
70
Enterprise
Java
Deleting an Account Object
AccountBean
database
2: select
7: delete
3: select instance
10: return instance
AccountBean
AccountBean
bean pool
1: remove(pKey)
AccountHome_EJBHome
4: populate state
5: ejbLoad()
6: ejbRemove()
8: remove()
Client
v10-17-2004
Account_EJBObject
EJB Intro
AccountBean
71
Questions to Ponder...
Enterprise
Java
• Entity bean, stateful session bean, stateless session,
message driven bean?
–
–
–
–
–
–
–
object which requires access by multiple clients over its lifetime
object whose work accepts and returns all information in the call
object whose work arrives through an asynchronous message
object that sends asynchronous messages
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 1:1 between instanced of beans
(EJB Objects) and instances of the bean class?
– What GoF Pattern does this depict?
v10-17-2004
EJB Intro
72