Title: Enterprise%20Java%20Beans%20-%20(EJB)
1Enterprise Java Beans - (EJB)
By Bharath Reddy Allam
By Bharath Reddy Allam
2EJB Overview
Enterprise Java Beans Technology is part of
larger Framework - The J2EE (Java 2 Enterprise
Edition) Component Architecture for Distributed
Systems Framework for creating middleware
3Why EJB?
- EJB Architecture simplifies the development of
distributed enterprise applications. - EJB Specification is a solution that is
operating-system independent. - Middleware Independence
- EJB defines a solution that eases the development
and deployment of distributed applications
4Why EJB?
- EJB enables the development of reusable and
portable components. - EJB has Broad Industry Support Server Vendors
and End Users.
5EJB Vs JavaBeans
- EJB component model logically extends the
JavaBeans component model to support server
components. - EJB components cannot be manipulated by a visual
Java IDE in the same way that JavaBeans can, They
are prepackaged pieces of application
functionality that are designed to run in an
application server.
- JavaBeans component model defines a standard
mechanism to develop portable, Java components,
such as widgets or controls. - JavaBeans technology can be used in any visual
Java technology integrated development
environment (IDE)
6EJB Architecture
- EJB Server
- EJB Container
- EJB Client
- Enterprise Java Beans
- Auxiliary Systems
- - JNDI, JTA,
- JavaMail
7Roles in EJB Development
EJB Developer - Person who provides
implementations of EJB classes EJB Deployer -
Responsible for Deploying EJBs in EJB Server EJB
Container Vendor - Provides software to
install an EJB into an EJB Server EJB Server
Vendor - Provides an Application Framework in
which to run EJB Containers Application
Developer - Who writes client applications
that uses EJB.
8EJB Server
Is a High Level Process or Application
that Manages and makes EJB Containers
Visible Provides Access to System
Services Provides Naming and Transaction Services
9EJB Container
Interface between Enterprise Java Bean and
outside world Accessing of EJB is done through
Container generated methods, which in turn call
the EJB methods
10EJB Clients
- Is an Application, Servlet, JSP or Applet that
- Find EJB Containers via JNDI
- Uses EJBHome to Create or Remove EJB from
Container - Uses EJBObject to Invoke Business Methods of EJB
11Enterprise Java Beans
Entity 1. Bean Managed 2. Container Managed
Session Bean 1. Stateless 2. Stateful
12Session Vs Entity Beans
Session 1. Performs a task for a Client 2.
Associated with a particular client 3. Non
Persistent and do not survive System Shutdown
Entity 1. Represents a business entity Object
that exists in a persistent Storage 2. Shared
by multiple clients 3. Persistent across
multiple Invocations and survives System Shutdown
13Passivation and Activation
Passivation - Saving the state of a Bean to a
persistent Storage Device and swapping it
out Activation - Restoring the state of Bean from
a persistent Storage Device and swapping it
in Depends upon Container Provider Applies to
both Session and Entity Beans
14Stateless Vs Stateful Session Beans
Stateful 1. Posses State during Client-Bean
Session 2. Is passivated 3. One Per Client
Stateless 1. No State during Client-Bean
Session 2. Is not passivated 3. Can be
shared by multiple clients
15SessionBean Interface
public abstract interface SessionBean extends
EnterpriseBean public void setSessionContext(Ses
sionContext ctx) throws EJBException,
java.rmi.RemoteException public void
ejbRemove() throws EJBException,
java.rmi.RemoteException public void
ejbActivate() throws EJBException,
java.rmi.RemoteException public void
ejbPassivate() throws EJBException,
java.rmi.RemoteException
16Writing a Session Bean-Step 1
Create a Remote Interface - Should extend
EJBObject - Provide the Business methods -
Arguments and Return types must be valid RMI
types - The throws Clause of the Business
methods must include java.rmi.RemoteException
17Writing a Session Bean-Step 2
Create Home Interface - Should extend EJBHome
- The argument and return types of create
method(s) must be valid RMI types - create
method(s) should return the Remote Interface -
Throws clause of create method(s) must include
java.rmi.RemoteException and javax.ejb.CreateExcep
tion
18Writing a Session Bean-Step 3
Writing the Bean Class (SessionBean) - should
implement SessionBean interface - Should be a
public class, and cannot be abstract or final
- contains public constructor with no Arguments
- should implement ejbCreate methods whose
arguments should correspond to create methods of
Home Interface - Return type of ejbCreate
methods should be void - throws clause of
ejbCreate methods should implement
javax.ejb.CreateException - implements
Business Methods of Remote Interface
19Writing an EJB Client
Locate the Home Interface Create an Enterprise
Bean Instance Invoke a Business Method upon the
Enterprise Bean Remove the Bean
20Life Cycle of Stateful SessionBean
21Life Cycle of Stateless SessionBean
22Container Vs Bean Managed EntityBean
Bean-Managed EntityBean 1. Bean is
responsible for saving its own state 2.
Entity Bean consists Code for DB calls 3.
Some times the code Contains DB specific calls
which makes non portable
Container-Managed EntityBean 1. Container is
responsible for Saving the state of Bean 2. In
DD, Specify the Container managed fields 3. Is
Independent of Data Store, Such as a Relational
Database
23EntityBean Interface
public abstract interface EntityBean extends
EnterpriseBean public void
setEntityContext(EntityContext ctx) throws
EJBException,
java.rmi.RemoteException public void
unsetEntityContext() throws EJBException,
java.rmi.RemoteException
public void ejbRemove() throws RemoveException,
EJBException,
java.rmi.RemoteException public void
ejbActivate() throws EJBException,
java.rmi.RemoteException public void
ejbPassivate() throws EJBException,
java.rmi.RemoteException public void
ejbLoad() throws EJBException, java.rmi.RemoteExce
ption public void ejbStore() throws
EJBException, java.rmi.RemoteException
24Writing a Entity Bean - Step 1
Create a Remote Interface - Should extend
EJBObject - Provide the Business methods -
Arguments and Return types must be valid RMI
types - The throws Clause of the Business
methods must include java.rmi.RemoteException
25Writing a Entity Bean - Step 2
Create Home Interface - Should extend EJBHome
- The argument and return types of create
method(s) must be valid RMI types - create
method(s) should return the Remote Interface -
Throws clause of create method(s) must include
java.rmi.RemoteException and javax.ejb.CreateExcep
tion
26Writing a Entity Bean - Step 3
Writing the Bean Class (EntityBean) - should
implement EntityBean interface - Should be a
public class, and cannot be abstract or final
- contains public constructor with no Arguments
- should implement ejbCreate and ejbPostCreate
method(s) whose arguments should correspond to
create method(s) of Home Interface - Return
type of ejbCreate method(s) is Primarykey -
Return type of ejbPostCreate method(s) is void
- throws clause of ejbCreate methods should
implement javax.ejb.CreateException -
implements Business Methods of Remote Interface
27Life Cycle of Entity Bean
28Deployment of EJBs in J2EE Server
29Transactions
- Transactions are units of work that can maintain
a reliable data source that is accessed by
several clients.
30Transactions
- Transactions have ACID Properties
- Atomicity A transaction either commits or aborts
- Consistency A transaction correctly manages the
changing state of a system - Isolation A transactions effects are isolated
from other transactions effects - Durability The result of a transaction persists
31Java Transaction Service (JTS)
- JTS is an implementation of an underlying
transaction service - javax.transaction.TransactionService allows the
application server to manage transaction
boundaries.
32EJB Transactions
- EJB Architecture
- Supports flat transactions
- Supports the two-phase commit protocol
- Prepare-ready
- Commit
- Supports access to transaction service using the
JTS interface.
33UserTransaction Interface
- javax.transaction.UserTransaction
- begin
- commit
- rollback
- setRollbackOnly
- setTransactionTimeOut
- getStatus
34Container-Managed Isolation
- Enterprise Bean can still participate via the
EJBContext - The setRollbackOnly method is used by a Bean to
force a rollback (usually due to an application
exception). - The getRollbackOnly method is used by a Bean to
test if the transaction is marked for a rollback.
35Bean-Managed Isolation
- Session Bean Can
- Gain Access to the transaction service
- Define transaction type as Bean in the deployment
descriptor - Ensure methods do not declare attributes that
conflict with Bean-Managed demarcation. - Use the javax.transaction.UserTransaction
interface. - Start a new transaction only after others complete
36Bean-Managed Isolation
- import javax.transaction.UserTransaction
- public class CartBean implements SessionBean
- EJBContext ic null // initialize some
where else -
- void someMethod()
- UserTransaction ex
ic.getUserTransaction() - tx.begin()
- // do work
- tx.commit()
-
37Bean-Managed Isolation
- Use the following methods
- UserTransaction.getStatus method obtains status
of a global transaction - UserTransaction.rollback method rolls back a
global transaction - Do not use the following methods
- setRollbackOnly()
- getRollbackOnly()
38Bean-Managed Demarcation
- Stateful Session Beans
- Methods can return in the middle of a transaction
without closing the transaction. - Subsequent calls can go to any Bean method
- Stateless and Entity Beans
- Methods must complete a transaction before
returning.
39Vendors Supporting EJB
- SUN
- IBM
- WebLogic
- Oracle
- Inprise
- BEA
- Novell
- Netscape
- IONA
- Gemstone
- Informix
- Allaire