EJB Container
Download
Report
Transcript EJB Container
Enterprise Java Beans - (EJB)
By Bharath Reddy Allam
By Bharath Reddy Allam
EJB 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
Why 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
Why EJB?
EJB enables the development of reusable
and portable components.
EJB has Broad Industry Support: Server
Vendors and End Users.
EJB Vs JavaBeans
EJB component model
JavaBeans component
logically extends the
model defines a standard
JavaBeans component
mechanism to develop
model to support server
portable, Java
components.
components, such as
EJB components cannot be
widgets or controls.
manipulated by a visual Java
JavaBeans technology
IDE in the same way that
can be used in any visual
JavaBeans can, They are
Java technology
prepackaged pieces of
integrated development
application functionality that
environment (IDE)
are designed to run in an
application server.
EJB Architecture
•EJB Server
•EJB Container
•EJB Client
•Enterprise Java
Beans
•Auxiliary Systems
- JNDI, JTA,
JavaMail
Roles in EJB Development
EJB Developer
- Person who provides implementations of EJB classes
EJB Deployer
- Responsible for Deploying EJB’s 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.
EJB 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
EJB 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
EJB 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
Enterprise Java Beans
Session Bean
1. Stateless
2. Stateful
Entity
1. Bean Managed
2. Container Managed
Session 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
Passivation 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
Stateless Vs Stateful Session Beans
Stateless
1. No State during ClientBean Session
2. Is not passivated
3. Can be shared by multiple
clients
Stateful
1. Posses State during
Client-Bean Session
2. Is passivated
3. One Per Client
SessionBean Interface
public abstract interface SessionBean extends EnterpriseBean{
public void setSessionContext(SessionContext 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
}
Writing 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
Writing 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.CreateException
Writing 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
Writing an EJB Client
Locate the Home Interface
Create an Enterprise Bean Instance
Invoke a Business Method upon the Enterprise Bean
Remove the Bean
Life Cycle of Stateful SessionBean
Life Cycle of Stateless SessionBean
Container Vs Bean Managed
EntityBean
Bean-Managed EntityBean
Container-Managed EntityBean
1. Bean is responsible for saving
its own state
1. Container is responsible for
Saving the state of Bean
2. Entity Bean consists Code for
DB calls
2. In DD, Specify the Container
managed fields
3. Some times the code Contains
DB specific calls which makes non
portable
3. Is Independent of Data Store,
Such as a Relational Database
EntityBean 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.RemoteException
public void ejbStore() throws EJBException, java.rmi.RemoteException
}
Writing 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
Writing 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.CreateException
Writing 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
Life Cycle of Entity Bean
Deployment of EJB’s in J2EE
Server
Transactions
Transactions are units of work that can
maintain a reliable data source that is
accessed by several clients.
Transactions
Transactions have ACID Properties
- Atomicity: A transaction either commits or
aborts
- Consistency: A transaction correctly
manages the changing state of a system
- Isolation: A transaction’s effects are isolated
from other transactions’ effects
- Durability: The result of a transaction
persists
Java Transaction Service
(JTS)
JTS is an implementation of an underlying
transaction service
javax.transaction.TransactionService
allows the application server to manage
transaction boundaries.
EJB Transactions
EJB Architecture:
Supports flat transactions
Supports the two-phase commit protocol
- Prepare-ready
- Commit
Supports access to transaction service using
the JTS interface.
UserTransaction Interface
javax.transaction.UserTransaction
begin
commit
rollback
setRollbackOnly
setTransactionTimeOut
getStatus
Container-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.
Bean-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
Bean-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();
}
}
Bean-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()
Bean-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.
Vendors Supporting EJB
•SUN
•Novell
•IBM
•Netscape
•WebLogic
•IONA
•Oracle
•Gemstone
•Inprise
•Informix
•BEA
•Allaire