Download - ILP J2EE Stream J2EE 05 Ejb v0.3
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
1/53
September 3, 2009
J2EEEnterprise Java Beans
Version 1.0
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
2/53
2September 3, 2009TCS Internal
EJB Defined
The Enterprise JavaBeans architecture is a
component architecture for the developmentand deployment of object-orienteddistributed enterprise-level applications.Applications written using the Enterprise
JavaBeans architecture is scalable,transactional and multi-user secure. Theseapplications may be written once, and
deployed on any server platform thatsupports the Enterprise JavaBeansspecification
Sun Microsystems Enterprise JavaBeans
Specification, v1.1, Copyright 1999 by Sun
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
3/53
3September 3, 2009TCS Internal
Enterprise Java Beans
Enterprise Java Beans are components which
are deployed within a container.EJB component is a set of classes andinterfaces.
The advantage of components over objectsis that only the business logic has to beaddressed and underlying middlewareservices are taken care by containers.
Containers are executables within serverenvironment which hold the beans.
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
4/53
4September 3, 2009TCS Internal
EJB AdvantagesProvides a mechanism to store and retrieve
data in a persistent way.It is not necessary that all beans shouldreside in the same machine. Beans fromdifferent machine can talk to each other.
The problem of transaction management isautomatically taken care.
The data transfer between beans is secureeven though it is through network.
Beans run in multithreaded environment.User need not worry about concurrencyproblems.
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
5/53
5September 3, 2009TCS Internal
EJB ComponentsHome Interface
Extends javax.ejb.EJBHomeProvides remote access to create, find and
remove beans.
Remote Interface
Extends javax.ejb.EJBObject
Provides remote access to businessmethods.
Bean Class
Extends javax.ejb.EnterpriseBean type
Implements business logic and other
functionality.
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
6/53
6September 3, 2009TCS Internal
EJB Architecture
Implements
Invokes
Creates / uses
Client
Server
Home Interface
EJB Object
(Wrapper)
Enterprise
Java Bean
(Biz Logic)
Remote
Interface
Container
RMI
RMI
Naming Service
You write this
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
7/53
7September 3, 2009TCS Internal
Types of Enterprise Beans
Session Bean
Performs task for a client
Entity Bean
Represents business entity that exist in a
persistent storageMessage-Driven Bean
To process messages asynchronously
Act like a listener for JMS API.
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
8/53
8September 3, 2009TCS Internal
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 inserver, client invokes session beansmethods.
Session beans shields the client from
complexity of the application.
Session beans are classified into twotypes:
Stateless Session Bean
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
9/53
9September 3, 2009TCS Internal
Stateless Session Beans
Does not maintain state across various
invocations..Instance variables have state but only forone invocation.
Can handle multiple clients and henceprovide better scalability for large clients.
Better performance than stateful beans.
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
10/53
10September 3, 2009TCS Internal
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
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
11/53
11September 3, 2009TCS Internal
Example: SalaryBean
Remote Interface:package simpleBean;
public interface Salary extends
javax.ejb.EJBObject {
double caculateSalary(int annualSalary,
double bonus)throwsjava.rmi.RemoteException;
}
Business
LogicMethod
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
12/53
12September 3, 2009TCS Internal
Example: SalaryBean
Salary EJB:package simpleBean;import javax.ejb.*;public class SalaryEJB implements
SessionBean {double caculateSalary(int annualSalary,
double bonus)throwsjava.rmi.RemoteException {double monthly = annualSalary / 12;monthly = monthly + bonus;return monthly;
}public void ejbCreate(){}
public void ejbRemove(){}public void ejbActivate(){}public void ejbPassivate(){}public void setSessionContext(SessionContext ctx){}
}
BusinessLogic
Method
Life
Cycle
Methods
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
13/53
13September 3, 2009TCS Internal
Example: SalaryBean
Deployment Descriptor:
Single stateless bean to calculate salary
Salary Bean
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
14/53
14September 3, 2009TCS Internal
Example: SalaryBean
Deployment Descriptor (Continued):Simple bean to calculate salary
Salary BeanSalarysimpleBean.SalaryHomesimpleBean.SalarysimpleBean.SalaryEJBSatelessContainer
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
15/53
15September 3, 2009TCS Internal
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, andusing deploytool, or any other tool,
the SimpleBean.jar can be created.
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
16/53
16September 3, 2009TCS Internal
Life Cycle of the Stateless Bean
Instance createdusing no-args constructor
setSessionContext
Method called
ejbRemove
called
ejbCreate
Method called
Component
Added to the pool
Container
Needsa new session
bean
Many clients
Use
The bean
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
17/53
17September 3, 2009TCS Internal
Life cycle of Stateless SessionBean
Does Not Exist
Ready
1.setSessionContext
2.ejbCreate
ejbRemove
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
18/53
18September 3, 2009TCS Internal
When to use stateless sessionbeans?No data for a specific client
Single method invocation for performing ageneric task of all clients
Fetching and setting often used read-only
data from the database
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
19/53
19September 3, 2009TCS Internal
Stateful Session Bean
Instance variables represent the state of the
unique client-bean session.The state is retained for the duration of asession.
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
20/53
20September 3, 2009TCS Internal
Life Cycle of the Stateful Bean
Instance createdusing no-args constructor
setSessionContext
Method called
ejbCreate
Method called
Component
Is ready
Bean does
Not existClient calls
Create()
One client
Use
The bean
Container calls
ejbPassivate
ContainerCalls
ejbRemove
Container calls
ejbPassivate
Client calls
ejbRemove
Timeout
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
21/53
21September 3, 2009TCS Internal
Life cycle of Stateful Session Bean
Does Not Exist
Ready Passive
1.create
2.setSessionContext
3.ejbCreate
1.remove
2.ejbRemove
ejbActivate
ejbPassivate
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
22/53
22September 3, 2009TCS Internal
Example: SalaryBean
Home Interface and Remote Interface
remains same as stateless bean.Inside the ejbCreate() method initialization isdone.
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
23/53
23September 3, 2009TCS Internal
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){}
}
Wh t t t f l i
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
24/53
24September 3, 2009TCS Internal
When to use stateful sessionbeans?When the beans state represents
interaction between client and beanBean needs to hold information acrossmethod invocations.
Bean mediates between client and othercomponents in application
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
25/53
25September 3, 2009TCS Internal
Entity Bean
Entity Bean represents a business object in a
persistent storage mechanism.Each entity bean has an underlying table ina relational database.
Each instance of a bean corresponds to arow in that table.
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
26/53
26September 3, 2009TCS Internal
Properties of Entity BeanPersistence
States exist beyond the lifetime ofapplication
Shared Access
Can be shared by multiple clients
Built-in transaction management
Primary key
Unique object identifier
RelationshipsBetween multiple entity beans, similar to
relationships in database.
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
27/53
27September 3, 2009TCS Internal
Types of PersistenceBean-managed Persistence
Bean directly access the databaseDatabase access code is written within
bean.
Container-managed Persistence
Container handles database access
Bean is independent of underlyingpersistence storage mechanism.
No need to modify or recompile the codewhen database is changed.
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
28/53
28September 3, 2009TCS Internal
Abstract Schema
Abstract schema is part of beans
deployment descriptor.Defines beans persistent fields andrelationships.
Name of the abstract schema is referencedby queries written in Enterprise Java BeansQuery Language.
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
29/53
29September 3, 2009TCS Internal
Example of Abstract Schema
OrderEJB
LineItemEJB
ProductEJB
CustomerEJB
ONE
MANY
ONE
ONE
MANY
MANY
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
30/53
30September 3, 2009TCS Internal
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 thesefields are virtual.
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
31/53
31September 3, 2009TCS Internal
Abstract Schema
Relationship Fields
Similar to the foreign key in databasetable
These fields are virtual and defined in the
bean class with access methodsDoes not represent the beans state
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
32/53
32September 3, 2009TCS Internal
Container Managed Relationships
Defines how one
instance of a bean isassociated/relatedwith an instance ofanother bean
One-to-One
One-to-Many
Many-to-One
Many-to-Many
Example: Container Managed
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
33/53
33September 3, 2009TCS Internal
Example: Container ManagedBean(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 beans state and mapped to two columns in Tax
table.15. finderMethods are used to find a record from the table.
Example: Container Managed
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
34/53
34September 3, 2009TCS Internal
Example: Container ManagedBean(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.
Example: Container Managed
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
35/53
35September 3, 2009TCS Internal
Example: Container ManagedBean(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;
}
Example: Container Managed
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
36/53
36September 3, 2009TCS Internal
Example: Container ManagedBean(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 inhome interface.
Example: Container Managed Bean
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
37/53
37September 3, 2009TCS Internal
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
Example: Container Managed Bean
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
38/53
38September 3, 2009TCS Internal
Example: Container Managed Bean(CMB)
Deployment Descriptor:
Persistant tax calculator
TaxCMB
Example: Container Managed Bean
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
39/53
39September 3, 2009TCS Internal
Example: Container Managed Bean(CMB) Deployment Descriptor (continued):
Tax calculation business model.
TaxBean
TaxBeancmp.TaxHome
cmp.Tax
cmp.TaxEJB
Container
Container
Example: Container Managed Bean
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
40/53
40September 3, 2009TCS Internal
Example: Container Managed Bean(CMB)
Deployment Descriptor (continued):java.lang.Stringfalse
Tax rate
taxRate
State Code
stateCode
stateCode
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
41/53
41September 3, 2009TCS Internal
Example: Container Managed Bean (CMB)
Deployment Descriptor (continued):
TaxBean
*
Not Supported
Bean Managed Persistence Beans(
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
42/53
42September 3, 2009TCS Internal
Bean Managed Persistence Beans(BMP)BMP beans are similar to CMP beans.
The only difference is that we have to writethe JDBC code to talk to the database. (In theearlier case we didnt do that).
In the deployment descriptor is modifiedaccordingly.
Example: Container Managed Bean
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
43/53
43September 3, 2009TCS Internal
Example: Container Managed Bean(BMB)
Deployment Descriptor:
Persistant tax calculator
TaxCMB
Example: Container Managed Bean
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
44/53
44September 3, 2009TCS Internal
Example: Container Managed Bean(BMB)
Deployment Descriptor (continued):
Tax calculation business model.
TaxBeanTaxBean
cmp.TaxHome
cmp.Tax
cmp.TaxEJB
Container
Container
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
45/53
45September 3, 2009TCS Internal
Example: Container Managed Bean (BMB)
Deployment Descriptor (continued): java.lang.String
false
JDBC/CloudSpace
javax.sql.DataSource
Container
Example: Container Managed Bean
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
46/53
46September 3, 2009TCS Internal
Example: Container Managed Bean(BMB)
Deployment Descriptor (continued):
TaxBean
*
Not Supported
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
47/53
47September 3, 2009TCS Internal
When to use Entity Beans?
When the bean represents entity and not
procedureCreditCardEJB is an entity bean, while
CriditCardValidateEJB is a session bean.
When the beans state must be persistent.
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
48/53
48September 3, 2009TCS Internal
Life cycle of Entity Bean
Does Not Exist
Ready Pooled
setEntityContextunsetEntityContext
ejbActivate
ejbPassivate
1.create
2.ejbCreate
3.ejbPostCreate
1. remove
2.ejbRemove
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
49/53
d i
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
50/53
50September 3, 2009TCS Internal
Message driven BeanClients do not access these beans through
interfaces.Instances of message driven bean retain nodata / state.
All instances are equivalent, allowing
container to assign an message to anymessage-driven bean instance.
A single bean can process messages frommultiple clients.
When to use Message Driven
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
51/53
51September 3, 2009TCS Internal
When to use Message DrivenBeans?Synchronous message transfer by session
and entity beans blocks system resources.To avoid this, message-driven beans can beused.
Lif l f M B
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
52/53
52September 3, 2009TCS Internal
Life cycle of Message Bean
Does Not Exist
Ready
1.setMessageDrivenContext
2.ejbCreate
ejbRemove
onMessage
R f
-
8/14/2019 ILP J2EE Stream J2EE 05 Ejb v0.3
53/53
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