5/29/2023 1
Understanding and Designing
with EJB
B.Ramamurthy
Based on j2eetutorial documentation.
http://java.sun.com/j2ee/tutorial/1_3-fcs/index.html
5/29/2023 2
Review
Request/Response Model
Distributed Objects: stubs and skeleton providing
location transparency
Naming and Lookup: registry and binding
Server-side technology: servlets (project1)
Web applications: can be written entirely using Java
Server Pages (static and dynamic content and data
access can be provided); JSP is wrapper on servlet
technology.
Concept of initial context:The starting point for
resolution of names for naming and directory
operations.
Data base access: using Java Data Base Connectivity
5/29/2023 3
When to use EJB
For large scale applications: where resources
and data are distributed.
When the application is run on servers at
many locations.
Where scalability is critical.
Where transactions are required to ensure
data integrity
When a variety of clients need to handled.
5/29/2023 4
Types of Enterprise Bean:
Session
Session bean: represents a single client
inside the J2EE server. Session represents an
interactive session. When a client terminates
the session bean terminates/is no longer
associated with the client.
Stateful session bean: maintains a
conversational state for the duration of a
session. Ex: items reviewed in a session at
some sites
Stateless session bean: does not maintain a
conversational state. Ex: computing a formula
for a given value
5/29/2023 5
Types of Enterprise Bean:
Entity
An entity bean represents a business object
in a persistent storage mechanism. Ex:
customers, orders, and products.
Each entity bean typically has an underlying
table in a relational database (business data),
and each instance of the bean corresponds to
a row in that table.
Transactional and recoverable on a server
crash.
5/29/2023 6
Types of Enterprise Bean:
Message-Driven
A message driven bean is an enterprise bean
that allows J2EE applications to process
messages asynchronously.
It acts as a JMS listener, which is similar to an
event listener except that it receives
messages instead of events.
The messages can be sent by any J2EE
component: an application client, another
enterprise bean, or a web component, or a
non-J2EE system using JMS.
Retain no data or conversational state.
5/29/2023 7
Contents of an Enterprise
Bean
Interfaces: The remote and home interface
for remote access. Local and local home
accesses for local access.
Enterprise bean class: Implements the
methods defined in the above interfaces.
Deployment descriptor: An XML file that
specifies information about the bean such as
its type, transaction attributes, etc.
Helper classes: non-bean classes needed by
the enterprise bean class such as utility and
exception classes.
5/29/2023 8
Naming Conventions
Item Syntax Example
Enterprise Bean
Name (DD)
<name>EJB AccountEJB
EJB JAR display
name (DD)
<name>EJB AccountJAR
Enterprise bean
class
<name>Bean AccountBean
Home interface <name>Home AccountHome
Remote interface <name> Account
Local home interface Local<name>Home LocalAccountHome
Local interface Local<name> LocalAccount
Abstract Schema
(DD)
<name> Account
5/29/2023 9
The life cycles of enterprise
beans
An enterprise bean goes through
various stages during its lifetime. Each
type has different life cycle.
5/29/2023 10
Session bean
Passive
Ready
Does not Exist
create remove
passivate
activate
Does not Exist
Ready
create remove
5/29/2023 11
Entity and Message-driven
Bean Lifecycle
Does not Exist
Ready
create remove
onMessage
Does not Exist
Pooled
setContext unsetContext
Ready
ejbActivate ejbPassivate
create remove
5/29/2023 12
Designing an application
Start with Remote interface methods.
For completion write the Home interface.
Implement these methods in a (session) bean class.
Update build.xml “ant” file and compile using ant
<target>
Use the deploy tool to deploy the application on your
j2EE server and set up the configuration.
Write a client (preferably a web client) to test your
enterprise application.
Lets go through converter application. Your
assignment is to make it a more meaningful and
useful converter.
5/29/2023 13
MidTerm Review
Web application design: n-tier design from
word problem. Represent using block
diagram, use case and class diagram.
Stepwise explanation; project 1
J2EE Application model: application model
Enterprise beans: Session, entity and
message-driven beans: characteristics and life
cycle
Enterprise integration: Ch.1-5 of your text
5/29/2023 14
Session Bean
Home interface
Remote interface
Bean class
Deployment descriptor
Can be stateless (ex: converter) or stateful (ex: cart)
Stateless: container can maintain a pool of instances
of session bean and reuse them for many client
requests.
Stateful: instance pooling can be done but what to
with the state? Activate and passivate the instances
by storing and restoring them from secondary
memory.
5/29/2023 15
Cart Stateful Session Bean
One or more ejbCreate methods (void
methods) providing flexibility of
instantiating the bean with various
starting states.
Business methods: addBook,
removeBook, getContents
Data (state): string customerId, string
CustomerName, Vector contents
5/29/2023 16
Client calls + bean and
container response
Client gets naming context.
Client looks up the remote object interface.
Narrows it to IIOP object and then casts it to
CartHome to set “home” variable.
Creates bean:
Cart shoppingCart = home.create(“Bina”, “1234”);
EJB container instantiates the enterprise bean.
EJB container then calls the appropriate ejbCreate
method in CartBean. Observe ejbCreate verifies the
ID using an IDVerifier object.
After successful creation and initialization of the bean
the client can invoke business methods as follows:
5/29/2023 17
Cart Business Method calls
shoppingCart.addBook(“J2EE in 21
days”);
shoppingCart.removeBook(“Java 2: inside
information”);
booklist = shoppingCart.getContents();
Now the client can print out the vector
booklist.
5/29/2023 18
Other Features
Stored in the enterprise bean’s deployment
descriptor, an environment entry is a name-
value pair that allows you to customize the
bean’s business logic without changing its
source code.
A enterprise bean that calculates discounts,
may have an environment variable named
discount percent.
Before deploying the bean’s application
one can use the deploy tool to assign the
proper value for discount percent.
Look at CheckerBean example.
5/29/2023 19
Entity Bean
Data is at the heart of most business
applications.
In J2EE applications, entity beans represent
the business objects that need persistence
(need to be stored in a database.)
You have choice of bean-managed
persistence (BMP) and container-managed
persistence (CMP).
In BMP you write the code for database
access calls. This may be additional
responsibility but it gives control to the bean
developer.
5/29/2023 20
Entity Bean with BMP:
Example: SavingsAccount
The state of the SavingsAccountEJB is stored
in the savingsAccount table of a relational
database.
savingsAccount table could be created by a
SQL statement as shown below:
CREATE TABLE savingsAccount
(id VARCHAR(3) CONSTRAINT
pk_savingsaccount PRIMARY KEY,
firstname VARCHAR(24),
lastname VARCHAR(24),
balance NUMERIC(10,2));
5/29/2023 21
SavingsAccountEJB
This contains:
Remote interface (SavingsAccount)
Home interface (SavingsAccountHome)
Entity bean class (SavingsAccountBean)
Utility class:
InsufficientBalanceException
And a client to test it:
SavingsAccountClient
5/29/2023 22
Entity Bean class
Implements EntityBean interface
Zero or more ejbCreate and
ejbPostCreate methods
Finder methods
Business methods
Home methods
5/29/2023 23
Entity Bean Methods
ejbCreate inserts the entity state into the database;
initializes the instance variables and returns the
primary key.
ejbRemove will delete the record corresponding to
the bean from the database.
ejbLoad and ejbStore methods synchronize instance
variables of an entity bean with the corresponding
values stored in a database. ejbLoad refreshes the
instance variables from the db and ejbStore writes
variables to the database. Container does this not the
client.
ejbFinder allows client to locate entity beans. Find
the collection of records with “Smith” as author.
Business methods and home methods.
5/29/2023 24
SQL statements in
SavingsAccountBean
Method SQL Statement
ejbCreate INSERT
ejbFindPrimaryKey SELECT
ejbFindByLastName SELECT
ejbFindInRange SELECT
ejbLoad SELECT
ejbRemove DELETE
ejbStore UPDATE

EJBDetailsFeb25.ppt

  • 1.
    5/29/2023 1 Understanding andDesigning with EJB B.Ramamurthy Based on j2eetutorial documentation. http://java.sun.com/j2ee/tutorial/1_3-fcs/index.html
  • 2.
    5/29/2023 2 Review Request/Response Model DistributedObjects: stubs and skeleton providing location transparency Naming and Lookup: registry and binding Server-side technology: servlets (project1) Web applications: can be written entirely using Java Server Pages (static and dynamic content and data access can be provided); JSP is wrapper on servlet technology. Concept of initial context:The starting point for resolution of names for naming and directory operations. Data base access: using Java Data Base Connectivity
  • 3.
    5/29/2023 3 When touse EJB For large scale applications: where resources and data are distributed. When the application is run on servers at many locations. Where scalability is critical. Where transactions are required to ensure data integrity When a variety of clients need to handled.
  • 4.
    5/29/2023 4 Types ofEnterprise Bean: Session Session bean: represents a single client inside the J2EE server. Session represents an interactive session. When a client terminates the session bean terminates/is no longer associated with the client. Stateful session bean: maintains a conversational state for the duration of a session. Ex: items reviewed in a session at some sites Stateless session bean: does not maintain a conversational state. Ex: computing a formula for a given value
  • 5.
    5/29/2023 5 Types ofEnterprise Bean: Entity An entity bean represents a business object in a persistent storage mechanism. Ex: customers, orders, and products. Each entity bean typically has an underlying table in a relational database (business data), and each instance of the bean corresponds to a row in that table. Transactional and recoverable on a server crash.
  • 6.
    5/29/2023 6 Types ofEnterprise Bean: Message-Driven A message driven bean is an enterprise bean that allows J2EE applications to process messages asynchronously. It acts as a JMS listener, which is similar to an event listener except that it receives messages instead of events. The messages can be sent by any J2EE component: an application client, another enterprise bean, or a web component, or a non-J2EE system using JMS. Retain no data or conversational state.
  • 7.
    5/29/2023 7 Contents ofan Enterprise Bean Interfaces: The remote and home interface for remote access. Local and local home accesses for local access. Enterprise bean class: Implements the methods defined in the above interfaces. Deployment descriptor: An XML file that specifies information about the bean such as its type, transaction attributes, etc. Helper classes: non-bean classes needed by the enterprise bean class such as utility and exception classes.
  • 8.
    5/29/2023 8 Naming Conventions ItemSyntax Example Enterprise Bean Name (DD) <name>EJB AccountEJB EJB JAR display name (DD) <name>EJB AccountJAR Enterprise bean class <name>Bean AccountBean Home interface <name>Home AccountHome Remote interface <name> Account Local home interface Local<name>Home LocalAccountHome Local interface Local<name> LocalAccount Abstract Schema (DD) <name> Account
  • 9.
    5/29/2023 9 The lifecycles of enterprise beans An enterprise bean goes through various stages during its lifetime. Each type has different life cycle.
  • 10.
    5/29/2023 10 Session bean Passive Ready Doesnot Exist create remove passivate activate Does not Exist Ready create remove
  • 11.
    5/29/2023 11 Entity andMessage-driven Bean Lifecycle Does not Exist Ready create remove onMessage Does not Exist Pooled setContext unsetContext Ready ejbActivate ejbPassivate create remove
  • 12.
    5/29/2023 12 Designing anapplication Start with Remote interface methods. For completion write the Home interface. Implement these methods in a (session) bean class. Update build.xml “ant” file and compile using ant <target> Use the deploy tool to deploy the application on your j2EE server and set up the configuration. Write a client (preferably a web client) to test your enterprise application. Lets go through converter application. Your assignment is to make it a more meaningful and useful converter.
  • 13.
    5/29/2023 13 MidTerm Review Webapplication design: n-tier design from word problem. Represent using block diagram, use case and class diagram. Stepwise explanation; project 1 J2EE Application model: application model Enterprise beans: Session, entity and message-driven beans: characteristics and life cycle Enterprise integration: Ch.1-5 of your text
  • 14.
    5/29/2023 14 Session Bean Homeinterface Remote interface Bean class Deployment descriptor Can be stateless (ex: converter) or stateful (ex: cart) Stateless: container can maintain a pool of instances of session bean and reuse them for many client requests. Stateful: instance pooling can be done but what to with the state? Activate and passivate the instances by storing and restoring them from secondary memory.
  • 15.
    5/29/2023 15 Cart StatefulSession Bean One or more ejbCreate methods (void methods) providing flexibility of instantiating the bean with various starting states. Business methods: addBook, removeBook, getContents Data (state): string customerId, string CustomerName, Vector contents
  • 16.
    5/29/2023 16 Client calls+ bean and container response Client gets naming context. Client looks up the remote object interface. Narrows it to IIOP object and then casts it to CartHome to set “home” variable. Creates bean: Cart shoppingCart = home.create(“Bina”, “1234”); EJB container instantiates the enterprise bean. EJB container then calls the appropriate ejbCreate method in CartBean. Observe ejbCreate verifies the ID using an IDVerifier object. After successful creation and initialization of the bean the client can invoke business methods as follows:
  • 17.
    5/29/2023 17 Cart BusinessMethod calls shoppingCart.addBook(“J2EE in 21 days”); shoppingCart.removeBook(“Java 2: inside information”); booklist = shoppingCart.getContents(); Now the client can print out the vector booklist.
  • 18.
    5/29/2023 18 Other Features Storedin the enterprise bean’s deployment descriptor, an environment entry is a name- value pair that allows you to customize the bean’s business logic without changing its source code. A enterprise bean that calculates discounts, may have an environment variable named discount percent. Before deploying the bean’s application one can use the deploy tool to assign the proper value for discount percent. Look at CheckerBean example.
  • 19.
    5/29/2023 19 Entity Bean Datais at the heart of most business applications. In J2EE applications, entity beans represent the business objects that need persistence (need to be stored in a database.) You have choice of bean-managed persistence (BMP) and container-managed persistence (CMP). In BMP you write the code for database access calls. This may be additional responsibility but it gives control to the bean developer.
  • 20.
    5/29/2023 20 Entity Beanwith BMP: Example: SavingsAccount The state of the SavingsAccountEJB is stored in the savingsAccount table of a relational database. savingsAccount table could be created by a SQL statement as shown below: CREATE TABLE savingsAccount (id VARCHAR(3) CONSTRAINT pk_savingsaccount PRIMARY KEY, firstname VARCHAR(24), lastname VARCHAR(24), balance NUMERIC(10,2));
  • 21.
    5/29/2023 21 SavingsAccountEJB This contains: Remoteinterface (SavingsAccount) Home interface (SavingsAccountHome) Entity bean class (SavingsAccountBean) Utility class: InsufficientBalanceException And a client to test it: SavingsAccountClient
  • 22.
    5/29/2023 22 Entity Beanclass Implements EntityBean interface Zero or more ejbCreate and ejbPostCreate methods Finder methods Business methods Home methods
  • 23.
    5/29/2023 23 Entity BeanMethods ejbCreate inserts the entity state into the database; initializes the instance variables and returns the primary key. ejbRemove will delete the record corresponding to the bean from the database. ejbLoad and ejbStore methods synchronize instance variables of an entity bean with the corresponding values stored in a database. ejbLoad refreshes the instance variables from the db and ejbStore writes variables to the database. Container does this not the client. ejbFinder allows client to locate entity beans. Find the collection of records with “Smith” as author. Business methods and home methods.
  • 24.
    5/29/2023 24 SQL statementsin SavingsAccountBean Method SQL Statement ejbCreate INSERT ejbFindPrimaryKey SELECT ejbFindByLastName SELECT ejbFindInRange SELECT ejbLoad SELECT ejbRemove DELETE ejbStore UPDATE