Vous êtes sur la page 1sur 53

J2EE

Enterprise Java Beans


Version 1.0

September 3, 2009
EJB Defined
•The Enterprise JavaBeans architecture is a
component architecture for the development
and deployment of object-oriented
distributed enterprise-level applications.
Applications written using the Enterprise
JavaBeans architecture is scalable,
transactional and multi-user secure. These
applications may be written once, and
deployed on any server platform that
supports the Enterprise JavaBeans
specification
– Sun Microsystems Enterprise JavaBeans™
Specification, v1.1, Copyright 1999 by Sun
TCS Internal September 3, 2009 2
Enterprise Java Beans
•Enterprise Java Beans are components which
are deployed within a container.
•EJB component is a set of classes and
interfaces.
•The advantage of components over objects
is that only the business logic has to be
addressed and underlying middleware
services are taken care by containers.
•Containers are executables within server
environment which hold the beans.

TCS Internal September 3, 2009 3


EJB Advantages
•Provides a mechanism to store and retrieve
data in a persistent way.
•It is not necessary that all beans should
reside in the same machine. Beans from
different machine can talk to each other.
•The problem of transaction management is
automatically taken care.
•The data transfer between beans is secure
even though it is through network.
•Beans run in multithreaded environment.
User need not worry about concurrency
problems.

TCS Internal September 3, 2009 4


EJB Components
•Home Interface
– Extends javax.ejb.EJBHome
– Provides remote access to create, find and
remove beans.
•Remote Interface
– Extends javax.ejb.EJBObject
– Provides remote access to business
methods.
•Bean Class
– Extends javax.ejb.EnterpriseBean type
– Implements business logic and other
functionality.
TCS Internal September 3, 2009 5
EJB Architecture
Naming Service

Server

Home Interface Container

RMI

Client

RMI
Remote EJB Object
Interface (Wrapper)
Enterprise
You write this
Java Bean
Implements (Biz Logic)

Invokes

Creates / uses

TCS Internal September 3, 2009 6


Types of Enterprise Beans
•Session Bean
– Performs task for a client
•Entity Bean
– Represents business entity that exist in a
persistent storage
•Message-Driven Bean
– To process messages asynchronously
– Act like a listener for JMS API.

TCS Internal September 3, 2009 7


Session Bean
• Session means various activities done by
the user during one visit to the application.
• Represents a single client inside the J2EE
server.
• To access an application deployed in
server, client invokes session bean’s
methods.
• Session beans shields the client from
complexity of the application.
• Session beans are classified into two
types:
– Stateless Session Bean
TCS Internal September 3, 2009 8
Stateless Session Beans
•Does not maintain state across various
invocations..
•Instance variables have state but only for
one invocation.
•Can handle multiple clients and hence
provide better scalability for large clients.
•Better performance than stateful beans.

TCS Internal September 3, 2009 9


Example: SalaryBean

•Home Interface:
package simpleBean;
public interface SalaryHome extends
javax.ejb.EJBHome {
Salary create() throws java.rmi.RemoteException,
javax.ejb.CreateException;
}

Life
Cycle
Method

TCS Internal September 3, 2009 10


Example: SalaryBean
Business

•Remote Interface: Logic


Method
package simpleBean;
public interface Salary extends
javax.ejb.EJBObject {
double caculateSalary(int annualSalary,
double bonus)throws
java.rmi.RemoteException;
}

TCS Internal September 3, 2009 11


Example: SalaryBean
•Salary EJB: Business
package simpleBean; Logic
Method
import javax.ejb.*;
public class SalaryEJB implements
SessionBean {
double caculateSalary(int annualSalary,
double bonus)throws
Life java.rmi.RemoteException {
Cycle double monthly = annualSalary / 12;
Methods monthly = monthly + bonus;
return monthly;
}
public void ejbCreate(){}
public void ejbRemove(){}
public void ejbActivate(){}
public void ejbPassivate(){}
public void setSessionContext(SessionContext ctx){}
}

TCS Internal September 3, 2009 12


Example: SalaryBean

•Deployment Descriptor:
<?xml version=“1.0”?>
<!DOCTYPE ejb-jar PUBLIC ‘-//Sun Microsystems, Inc.
//DTD Enterprise Java Beans 1.1//EN’
http://java.sun.com/dtd/ejb-jar_2_0.dtd>
<ejb-jar>
<description>
Single stateless bean to calculate salary
</description>
<display-name>Salary Bean</display-name>

TCS Internal September 3, 2009 13


Example: SalaryBean
•Deployment Descriptor (Continued):
<enterprise-beans>
<session>
<description>
Simple bean to calculate salary
</description>
<display-name>Salary Bean</display-name>
<ejb-name>Salary</ejb-name>
<home>simpleBean.SalaryHome</home>
<remote>simpleBean.Salary</remote>
<ejb-class>simpleBean.SalaryEJB</ejb-class>
<session-type>Sateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>

TCS Internal September 3, 2009 14


Example: SalaryBean

•Directory Structure:
simpleBean
|---- Salary.class
|---- SalaryHome.class
|---- SalaryEJB.class
META-INF
|---- ejb-jar.xml

Keeping the files as shown in the directory structure, and


using deploytool, or any other tool,
the SimpleBean.jar can be created.

TCS Internal September 3, 2009 15


Life Cycle of the Stateless Bean

Container
Needs Instance created
a new session using no-args constructor
bean

setSessionContext
Method called

ejbCreate
Method called

Many clients
Component
Use
Added to the pool The bean

ejbRemove
called

TCS Internal September 3, 2009 16


Life cycle of Stateless Session
Bean

Does Not Exist

ejbRemove 1.setSessionContext
2.ejbCreate

Ready

TCS Internal September 3, 2009 17


When to use stateless session
beans?
•No data for a specific client
•Single method invocation for performing a
generic task of all clients
•Fetching and setting often used read-only
data from the database

TCS Internal September 3, 2009 18


Stateful Session Bean
•Instance variables represent the state of the
unique client-bean session.
•The state is retained for the duration of a
session.

TCS Internal September 3, 2009 19


Life Cycle of the Stateful Bean
Bean does
Not exist Instance created
Client calls using no-args constructor
Create()

setSessionContext
Method called
Container
Calls
ejbRemove ejbCreate
Method called
Timeout

One client
Container calls Component
Use
ejbPassivate Is ready The bean

Container calls Client calls


ejbPassivate ejbRemove

TCS Internal September 3, 2009 20


Life cycle of Stateful Session Bean

Does Not Exist

1.create
1.remove 2.setSessionContext
2.ejbRemove 3.ejbCreate

ejbActivate

Ready Passive
ejbPassivate

TCS Internal September 3, 2009 21


Example: SalaryBean
•Home Interface and Remote Interface
remains same as stateless bean.
•Inside the ejbCreate() method initialization is
done.

TCS Internal September 3, 2009 22


SalaryBean (stateful)
• Salary EJB: (stateful)
• package simpleBean;
• import javax.ejb.*;
• public class SalaryEJB implements SessionBean {
• String employee;
• double caculateSalary(int annualSalary,
• double bonus)throws
java.rmi.RemoteException {
• double monthly = annualSalary / 12;
• monthly = monthly + bonus;
• return monthly;
• }
• public void ejbCreate(String employee){
• this.employee = employee;
• }
• public void ejbRemove(){}
• public void ejbActivate(){}
• public void ejbPassivate(){}
• public void setSessionContext(SessionContext ctx){}
• }

TCS Internal September 3, 2009 23


When to use stateful session
beans?
•When the bean’s state represents
interaction between client and bean
•Bean needs to hold information across
method invocations.
•Bean mediates between client and other
components in application

TCS Internal September 3, 2009 24


Entity Bean
•Entity Bean represents a business object in a
persistent storage mechanism.
•Each entity bean has an underlying table in
a relational database.
•Each instance of a bean corresponds to a
row in that table.

TCS Internal September 3, 2009 25


Properties of Entity Bean
•Persistence
– States exist beyond the lifetime of
application
•Shared Access
– Can be shared by multiple clients
– Built-in transaction management
•Primary key
– Unique object identifier
•Relationships
– Between multiple entity beans, similar to
relationships in database.

TCS Internal September 3, 2009 26


Types of Persistence
•Bean-managed Persistence
– Bean directly access the database
– Database access code is written within
bean.
•Container-managed Persistence
– Container handles database access
– Bean is independent of underlying
persistence storage mechanism.
– No need to modify or recompile the code
when database is changed.

TCS Internal September 3, 2009 27


Abstract Schema
•Abstract schema is part of bean’s
deployment descriptor.
•Defines bean’s persistent fields and
relationships.
•Name of the abstract schema is referenced
by queries written in Enterprise Java Beans
Query Language.

TCS Internal September 3, 2009 28


Example of Abstract Schema

OrderEJB
ONE MANY

MANY ONE

LineItemEJB CustomerEJB

MANY

ONE

ProductEJB

TCS Internal September 3, 2009 29


Abstract Schema
•Persistent Fields
– Fields stored in the database
– Constitute the state of bean
– EJB container automatically synchronizes
with database fields.
– No instance variables, but only getter and
setter methods.
– In container managed persistence these
fields are virtual.

TCS Internal September 3, 2009 30


Abstract Schema
•Relationship Fields
– Similar to the foreign key in database
table
– These fields are virtual and defined in the
bean class with access methods
– Does not represent the bean’s state

TCS Internal September 3, 2009 31


Container Managed Relationships
Defines how one •One-to-One
instance of a bean is •One-to-Many
associated/related •Many-to-One
with an instance of
•Many-to-Many
another bean

TCS Internal September 3, 2009 32


Example: Container Managed
Bean(CMB)
• Home Interface:
package cmp;
import java.util.Collection;
import java.rmi.RemoteException;
import javax.ejb;
public interface TaxHome extends EJBHome {
public Tax create(String stateCode,
String taxRate)throws
RemoteException, CreateException;
public Tax findByPrimaryKey(String primaryKey)
throws FinderException, RemoteException;
}
Note:
14. Unlike session beans, create method has parameters passed to it.
These represents bean’s state and mapped to two columns in Tax
table.
15. finderMethods are used to find a record from the table.

TCS Internal September 3, 2009 33


Example: Container Managed
Bean(CMB)
• Remote Interface (Component Interface):
package cmp;
import javax.ejb;
public interface Tax extends EJBObject {
public void setTaxRate(float taxRate)throws
RemoteException;
public float getTaxRate()throws RemoteException;
}
Note:
10. Defines two business methods.

TCS Internal September 3, 2009 34


Example: Container Managed
Bean(CMB)
• Entity Bean Class:
package cmp;
import javax.ejb;
public interface TaxEJB implements EntityBean {
public String stateCode;
public float taxRate;
public void setTaxRate(float taxRate)throws
RemoteException{
this.taxRate = taxRate;
}
public float getTaxRate()throws RemoteException{
return this.taxRate;
}

TCS Internal September 3, 2009 35


Example: Container Managed
Bean(CMB)
• Entity Bean Class (Continued):
public String ejbCreate(String stateCode,
String taxRate)
throws CreateException {
if(stateCode == null){
throw new CreateException();
}
this.stateCode = stateCode;
this.taxRate = taxRate;
return null;
}
Note:
13. This method is invoked on behalf of create method defined in
home interface.

TCS Internal September 3, 2009 36


Example: Container Managed Bean
(CMB)
• Entity Bean Class (Continued):
public void ejbLoad();
public void ejbStore();
public void ejbRemove();
public void unsetEntityContext();
public void setEntityContext(EntityContext cxt){}
public void ejbActivate(){}
public void ejbPassivate(){}
Note:
10. Life cycle methods

TCS Internal September 3, 2009 37


Example: Container Managed Bean
(CMB)
• Deployment Descriptor:
<?xml version=“1.0”?>
<!DOCTYPE ejb-jar PUBLIC ‘-//Sun Microsystems, Inc.
//DTD Enterprise Java Beans 1.1//EN’
http://java.sun.com/dtd/ejb-jar_2_0.dtd>
<ejb-jar>
<description>
Persistant tax calculator
</description>
<display-name>TaxCMB</display-name>

TCS Internal September 3, 2009 38


Example: Container Managed Bean
(CMB)
• Deployment Descriptor (continued):
<enterprise-beans>
<entity>
<description>
Tax calculation business model.
</description>
<display-name>TaxBean</display-name>
<ejb-name>TaxBean</ejb-name>
<home>cmp.TaxHome</home>
<remote>cmp.Tax</remote>
<ejb-class>cmp.TaxEJB</ejb-class>
<persistance-type>Container</persistnace-type>
<transaction-type>Container</transaction-type>

TCS Internal September 3, 2009 39


Example: Container Managed Bean
(CMB)
• Deployment Descriptor (continued):
<prim-key-class>java.lang.String</prim-key-class>
<reentrant>false</reentrant>
<cmp-field>
<description>Tax rate</description>
<field-name>taxRate</field-bane>
<cmp-field>
<cmp-field>
<description>State Code</description>
<field-name>stateCode</field-bane>
<cmp-field>
<primary-field>stateCode</primary-field>
</entity>
</enterprise-beans>

TCS Internal September 3, 2009 40


Example: Container Managed Bean (CMB)

• Deployment Descriptor (continued):


<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>TaxBean</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>
Not Supported
<trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>

TCS Internal September 3, 2009 41


Bean Managed Persistence Beans(
BMP)
•BMP beans are similar to CMP beans.
•The only difference is that we have to write
the JDBC code to talk to the database. (In the
earlier case we didn’t do that).
•In the deployment descriptor is modified
accordingly.

TCS Internal September 3, 2009 42


Example: Container Managed Bean
(BMB)
• Deployment Descriptor:
<?xml version=“1.0”?>
<!DOCTYPE ejb-jar PUBLIC ‘-//Sun Microsystems, Inc.
//DTD Enterprise Java Beans 1.1//EN’
http://java.sun.com/dtd/ejb-jar_2_0.dtd>
<ejb-jar>
<description>
Persistant tax calculator
</description>
<display-name>TaxCMB</display-name>

TCS Internal September 3, 2009 43


Example: Container Managed Bean
(BMB)
• Deployment Descriptor (continued):
<enterprise-beans>
<entity>
<description>
Tax calculation business model.
</description>
<display-name>TaxBean</display-name>
<ejb-name>TaxBean</ejb-name>
<home>cmp.TaxHome</home>
<remote>cmp.Tax</remote>
<ejb-class>cmp.TaxEJB</ejb-class>
<persistance-type>Container</persistnace-type>
<transaction-type>Container</transaction-type>

TCS Internal September 3, 2009 44


Example: Container Managed Bean (BMB)

• Deployment Descriptor (continued):


• <prim-key-class>java.lang.String</prim-key-class>
• <reentrant>false</reentrant>
• <res-ref>
• <res-ref-name>
• JDBC/CloudSpace
• </res-ref-name>
• <res-type>javax.sql.DataSource</res-type>
• <res-auth>Container</res-auth>
• <res-ref>
• </entity>
• </enterprise-beans>

TCS Internal September 3, 2009 45


Example: Container Managed Bean
(BMB)
• Deployment Descriptor (continued):
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>TaxBean</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>
Not Supported
<trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>

TCS Internal September 3, 2009 46


When to use Entity Beans?
•When the bean represents entity and not
procedure
– CreditCardEJB is an entity bean, while
CriditCardValidateEJB is a session bean.
•When the bean’s state must be persistent.

TCS Internal September 3, 2009 47


Life cycle of Entity Bean

Does Not Exist

unsetEntityContext setEntityContext
1. remove
2.ejbRemove

ejbActivate

Ready Pooled
ejbPassivate
1.create
2.ejbCreate
3.ejbPostCreate

TCS Internal September 3, 2009 48


Message Driven Beans
•Message driven Bean allows applications to
process messages asynchronously.
•It acts like a listener for other J2EE
components such as
– An application client
– Another enterprise bean
– Web component
– Another non J2EE system

TCS Internal September 3, 2009 49


Message driven Bean
•Clients do not access these beans through
interfaces.
•Instances of message driven bean retain no
data / state.
•All instances are equivalent, allowing
container to assign an message to any
message-driven bean instance.
•A single bean can process messages from
multiple clients.

TCS Internal September 3, 2009 50


When to use Message Driven
Beans?
•Synchronous message transfer by session
and entity beans blocks system resources.
•To avoid this, message-driven beans can be
used.

TCS Internal September 3, 2009 51


Life cycle of Message Bean

Does Not Exist

ejbRemove 1.setMessageDrivenContext
2.ejbCreate

onMessage
Ready

TCS Internal September 3, 2009 52


Reference
•Stephanie Bodoff, et. al., The J2EE Tutorial,
Sun Microsystems.
•James Mc Govern, et. al., J2EE 1.4. Bible
•S Allamaraju., et. al., Professional Java
Server Programming. (J2EE 1.3 Ed).
•Rahim Adatia, et. al., Professional EJB

TCS Internal September 3, 2009 53

Vous aimerez peut-être aussi