Professional Documents
Culture Documents
00-Ejbcodecamp Ch3of6
00-Ejbcodecamp Ch3of6
1
Session Bean Tutorial Agenda:
2
What Are Session Beans?
3
What Is a Session Bean?
4
When to Use Session Beans?
5
Session vs. Entity Beans
6
2 Types of Session Beans
7
Stateless Session Beans
oStateless beans:
o Do not retain client information
from one method invocation to the
next.
o Client passes any needed information
as parameters to the business
methods.
oUsed mainly to provide a pool of beans to
handle frequent but brief requests.
oThe EJB™ server transparently reuses
instances of the bean to service
different clients.
© Copyright 2000 Sun Microsystems, Inc., All rights reserved.
8
Stateless vs Stateful
Cart items
9
Stateless May Use Fewer
Resources:
oNumber of Stateless Session Beans
needed < Number of Clients:
o Can be recycled.
o Number of instances to create is
minimized.
o Shorter life (no client session) optimizes
resource usage.
o Improved performance due to fewer
connections across the network.
oBut may require the client to maintain
state information on the client side which
can mean more complex client code.
Note: The difference in use of resources for stateless and stateful is application
server specific.
10
When to use Stateless Session
Beans?
12
Example Scenario: Use Case
Transfer
Money
Customer
13
Example Scenario: Classes
ATM Account
balance
0…* 0…*
transfer() withdraw()
accesses
deposit()
Checking Savings
Account Account
14
Example Scenario: Sequence
Diagram
1: transfer()
2: debit()
3: credit())
15
Example Scenario: EJB
debit Account
Entity Bean
ATM
Client account1
Session bean
Transfer account2
credit Account
Entity Bean
16
Session Bean Implementation
17
EJB™ API Review
<<Interface>> <<Interface>>
java.RMI.Remote java.io.serializable JDK
<<Interface>>
EnterpriseBean
<<Interface>> <<Interface>>
EJBHome EJBObject javax.ejb
<<Interface>>
SessionBean
xxx
Container
xxxatm
EJBHome
xxxatm
EJBObject
xxx
AtmBean provider
© Copyright 2000 Sun Microsystems, Inc., All rights reserved.
The tools provided by xxx Corporation are responsible for the following:
•Generate the class (xxxRemoteAtm) that implements the session bean’s
remote interface. The tools also generate the classes that implement the
communication protocol specific artifacts for the remote interface.
•Generate the implementation of the session Bean class suitable for the xxx
container (xxxAtmBean). xxxAtmBean includes the business logic from the
AtmBean class mixed with the services defined in the xxxBean class. xxx tools
can use inheritance, delegation, and code generation to achieve a mix-in of
the two classes.
•Generate the class (xxxAtmHome) that implements the session bean’s
home interface.
•Generate the class (xxxAtmMetaData) that implements the
javax.ejb.EJBMetaData interface for the Atm Bean.
18
javax.EJB™ Client Interfaces
<<Interface>>
java.RMI.Remote
extends
<<Interface>>
<<Interface>>
EJBHome
EJBObject
getEJBHome()
remove()
getEJBMetaData()
remove()
getHomeHandle()
getHandle()
isIdentical()
The EJBHome interface is extended by all enterprise Bean's home interfaces. An enterprise Bean's home interface
defines the methods that allow a client to create, find, and remove EJB™ objects. Each enterprise Bean has a home
interface. The home interface must extend the javax.ejb.EJBHome interface, and define the enterprise Bean type
specific create and finder methods (session Beans do not have finders). The home interface is defined by the enterprise
Bean provider and implemented by the enterprise Bean container. Method Summary:
EJBMetaData getEJBMetaData()
•Obtain the EJBMetaData interface for the enterprise Bean.
HomeHandle getHomeHandle()
•Obtain a handle for the home object.
Void remove(Handle handle)
•Remove an EJB™ object identified by its handle.
Void remove(java.lang.Object primaryKey)
•Remove an EJB™ object identified by its primary key.
The EJBObject interface is extended by all enterprise Bean's remote interface. An enterprise Bean's remote interface
provides the client's view of an EJB™ object. An enterprise Bean's remote interface defines the business methods
callable by a client. Each enterprise Bean has a remote interface. The remote interface must extend the
javax.ejb.EJBObject interface, and define the enterprise Bean specific business methods. The enterprise Bean's remote
interface is defined by the enterprise Bean provider and implemented by the enterprise Bean container. Method
Summary
EJBHome getEJBHome()
•Obtain the enterprise Bean's home interface.
Handle getHandle()
•Obtain a handle for the EJB™ object.
java.lang.Object getPrimaryKey()
•Obtain the primary key of the EJB™ object.
Boolean isIdentical(EJBObject obj)
•Test if a given EJB™ object is identical to the invoked EJB™ object.
void remove()
•Remove the EJB™ object.
19
javax.EJB™ Server Interfaces
<<Interface>>
java.io.serializable
<<Interface>>
EnterpriseBean
<<Interface>>
<<Interface>>
EntityBean
SessionBean
setEntityContext()
setSessionContext()
unsetEntityContext
ejbRemove()
ejbRemove()
ejbActivate()
ejbActivate()
ejbPassivate()
ejbPassivate()
ejbLoad()
ejbStore()
20
javax.EJB™ Server Interfaces
Cont.
<<Interface>>
EJBContext
getEJBHome()
getEnvironment()
getCallerIdentity()
isCallerInRole()
getUserTransaction()
setRollBackOnly()
<<Interface>> <<Interface>>
SessionContext EntityContext
getEJBObject() getEJBObject()
getPrimaryKey()
21
Differences From Entity Bean
Session Entity
• Implements • Implements
SessionBean EntityBean interface
interface
• Does not use primary • Uses primary key
key object object
• Uses create method • Uses create, and
to create instance, callback methods to
stateful to initialize create,store/update
local data data in database
Session EJB™ objects are created, associated with a specific client, and then
removed as needed, whereas entity EJB™ objects represent permanent
data in a data storage that can be uniquely identified with a primary key.
Because the instance data for session beans is not persistent, the session bean
class does not have callback methods for storing data to and loading data
from a data source.
22
Session Bean Implementation
23
1) Create the Remote Interface
<<Interface>>
EJBObject extends
<<Interface>>
Atm
transfer()
24
Session Bean Implementation
25
2) Create the Home Interface
<<Interface>>
EJBHome
<<Interface>>
AtmHome
create()
Returns Atm
remote interface
26
Session Bean Implementation
27
3) AtmBean Implementation
<<Interface>>
SessionBean
implements
setSessionContext()
ejbRemove()
ejbActivate()
ejbPassivate() AtmBean
<<Interface>>
Atm
transfer()
transfer() ejbCreate()
setSessionContext()
must match ejbRemove()
for container ejbActivate()
“glue” ejbPassivate()
<<Interface>>
AtmHome
create()
The following are the requirements for the session bean class:
• must implement the javax.ejb.SessionBean interface.
• must be defined as public, must not be final, and must not be abstract.
•must have a public constructor that takes no parameters. The Container uses
this constructor to create instances of the session bean class.
• must not define the finalize() method.
• must implement the business methods and the ejbCreate methods.
•If the class is a stateful session bean, it may optionally implement the
javax.ejb.SessionSynchronization interface.
•The session bean class may have superclasses and/or superinterfaces. If the
session bean has superclasses, then the business methods, the ejbCreate
methods, the methods of the SessionBean interface, and the methods of the
optional SessionSynchronization interface may be defined in the session bean
class, or in any of its superclasses.
•The session bean class is allowed to implement other methods (for example
helper methods invoked internally by the business methods) in addition to the
methods required by the EJB™ specification.
28
Lifecycle of a Stateless Session
Bean
does not
exist
1) newInstance()
ejbRemove()
2) setSessioncontext()
3)ejbCreate()
method-ready
pool
business method
EJB
Instance
29
Invocation of a Business Method
business method
business method
30
3) AtmBean: Implement Atm Interface
Business Methods
public class AtmBean implements SessionBean {
31
Creating a Stateless Session Bean
Instance
bean
client EJBHome EJBObject context instance
create()
new
32
Adding Instance of Stateless Session
Bean to a Method-ready Pool
new
new
setSessionContext()
ejbCreate()
33
3) AtmBean: Implement Home
Interface Create Method
Returns void
// implement atmHome interface create
private static AccountHome accountHome = null;
public void ejbCreate (){
try {
Context ic = new InitialContext();
Bean create methods must match the methods defined in this EJB's home interface, AtmHome.
Container tools generate the implementation of the AtmHome class and the client stub and
server skeleton classes. Calls on those classes will be forwarded to the AtmBean.
A stateless session bean must have only one ejbCreate method, which must return void and
contain no arguments. In a stateless session bean, none of the methods depend on the values
of variables set by any other method, except for the ejbCreate, setSessionContext methods
which set the initial (identical) state of each bean instance. The term “stateless” signifies that
an instance has no state for a specific client. However, the instance variables of the stateless
session bean can contain non-client specific state across client-invoked method calls.
Examples of such states include an open database connection or an object reference to an
EJB™ object.
ejbCreate method
The stateless session bean class must define one ejbCreate(...) methods whose signature must
follow these rules:
• The method name must be ejbCreate.
• The method must be declared as public.
• The method must not be declared as final or static.
• The return type must be void.
• The methods arguments must be legal types for RMI/IIOP.
•The throws clause may define arbitrary application exceptions, possibly including the
javax.ejb.CreateException.
34
Removing Instance of Stateless
Session Bean From Ready Pool
ejbRemove()
35
Removal of a Stateless Session
Bean Instance
bean
client EJBHome EJBObject context instance
remove()
36
3) AtmBean: Implement Session
Interface Container Callback Methods
// save the session context in an instance variable
public void setSessionContext(SessionContext sc) {
this.context= sc;
}
37
Lifecycle of a Stateless Session
Bean
does not
exist
1) newInstance()
ejbRemove()
2) setSessioncontext()
3)ejbCreate()
method-ready
pool
business method
EJB
Instance
38
Session Bean Implementation
39
4) Compile the Remote & Home Interfaces and
Implementation Class.
40
Session Bean Implementation
41
XML Short Intro
42
EJB descriptor composition
<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC '-//SunMicrosystems Inc.//DTD>
<ejb-jar>
<enterprise-beans>
<session> Description of all EJBs in the
. . .
</session>
jar archive
</enterprise-beans>
<assembly-descriptor>
. . .
</assembly-descriptor> Assembly data
</ejb-jar>
43
5) Create Deployment Descriptor
44
5) Create Deployment Descriptor
This example sets the EJB™ reference for referencing the Account Bean.
Example code in the AtmEJB™ for referencing the Account Bean:
45
5) Create Deployment Descriptor
46
5) Create Deployment Descriptor
<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems Inc.//DTD.. . >
<ejb-jar>
<description>no description</description>
<display-name>Atm</display-name>
<enterprise-beans>
<session>
<description>no description</description>
<display-name>AtmBean</display-name>
<ejb-name>AtmBean</ejb-name>
<home>AtmHome</home>
<remote>Atm</remote>
<ejb-class>AtmEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
<ejb-ref>
<description>no description</description>
<ejb-ref-name>ejb/Account</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<home>AccountHome</home>
<remote>Account</remote>
</ejb-ref>
</session>
© Copyright 2000 Sun Microsystems, Inc., All rights reserved.
47
5) Create DD Cont.
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>AtmBean</ejb-name>
<method-intf>Remote</method-intf>
<method-name>transfer</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
48
Session Bean Implementation
49
6) Package in an ejb-jar File.
packager –ejbJar
Atm.class:AtmEJB.class:AtmHome.class
Atm-ejb-jar.xml Atm.jar
Atm AtmEJB
AtmHome bean
Interfaces XML DD
Deployment
Descriptor
50
6) Package in an ejb-jar File.
51
© Copyright 2000 Sun Microsystems, Inc., All rights reserved.
52
Session Bean Implementation
53
7) Deploy the Enterprise
Application
AtmEJB.jar, Application jar: Atm, Account
EJBHome EJBObject interfaces,
Account.EJBjar
Atm, Account beans
Deployment tool
Deployment tool
54
© Copyright 2000 Sun Microsystems, Inc., All rights reserved.
55
Create a Client
56
ATM Client Code
57
Session Bean Accessing Entity Bean
JNDI name
service
EJBHome find
find
transfer EJBObject Account
EJBObject
Bean
Atm withdraw
Client Bean
deposit
EJBObject Account
Bean
server
Example of Sequence of events for Client calling the Transfer method on the
Atm remote interface stub.
58
Exercise: Design and Implement
Catalog Stateless Session Bean
Use Case Scenarios
customer
List Books in Store
customer
Search for books by subject…
customer
Get Book Details (by ISBN)…
© Copyright 2000 Sun Microsystems, Inc., All rights reserved.
59
Stateless Services:
Catalog Session EJB
product CatalogEJB
ISBN
g e tBooks()
title
g e tBookDetails()
author findBooksBySubject()
description ejbCreate()
s e tSessionContext()
e j b R e m o ve()
ejbActivate()
ejbPassivate()
60
CatalogEJB
<<Interface>>
Catalog
getBooks()
getBookDetails()
findBooksBySubject()
BookDetails
CatalogEJB ISBN
title
author
getBooks() publisher
getBookDetails() subject
findBooksBySubject() description
<<Interface>> price
ejbCreate()
CatalogHome setSessionContext()
getISBN()
ejbRemove() getTitle()
create() ejbActivate() getAuthor()
ejbPassivate() getPrice()
getDescription()
getPublisher()
61