Professional Documents
Culture Documents
DO YOU WANT
TO ANSWER MOST
OF THE
EJB QUESTIONS ?
THEN LET ME
HELP YOU!
EJB
Kantimahanti Prasad
Kanti.Prasad@pramerica.ie or Kanti1@donegal.net
Copyright 2004 Kanti Prasad All rights reserved
1. Learning objectives
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
A STAR or a
different color
represents
important
points to NOTE
4. Component Technology
Components are deployable units that provide a
business service to their clients.
5. Component Characteristics
Components Characteristics include
Properties
Reusability
Operations
Shareable
Events
Distributable
Deployable
Self Containment
Self description
6. Component P O E
Components should have a mechanism for exporting
Properties
Operations
Events
7. Deployable Components
Hardware
The underlying operating system
Their Application Server
The network protocol they use
8. Reusable Components
9. Shareable Components
EJB Container
EJB
Possible
Remote
CLIENTS
Local
CLIENTS
Remote
CLIENTS
Remote
CLIENTS
Remote
CLIENTS
Directory Services
Distributed transaction management
Security management
Concurrent access management
Persistence management
Resource pooling (e.g. DB connections)
Administration interface
Load Balancing
Fault tolerance
J2EE
based
solutions
Non-J2EE
solutions
(PHP, Cold Fusion,
Perl, etc.)
Microsoft Solutions
(COM, ASP.Net
VB.Net,C# etc.)
App Server
EJB Container
EJB
EJB
CLIENT
Network
CLIENT
Copyright 2004 Kanti Prasad All rights reserved
Pool
EJB
EJB
EJB
EJB
DB
16. Container
Container means a
pre developed Software
A Container :
&
To increase capability.
clustering is taken care
by the vendor
Transaction
Persistence
Security
Connection
Pooling
Threading
App Server
EJB Container
EJB
STUB
CLIENT
Network
Container
intercepts
requests
EJB
EJB
Container knows what to do, as you describe everything in a special descriptor file
Copyright 2004 Kanti Prasad All rights reserved
Enterprise Components
services
standards
component-based
J2EE
Container/Server
J2EE Interfaces
Management Services
Deployment/Configuration
J2SE Language/Interfaces
J2SE Runtime
Operating System Platform
Hardware Platform
Deploy
Tools
Data
Web Container
JSP
Application Client
Container
HTTP/S
X C B S
R C S
JAF
JMX A D N B
I
C
EJB
JAX- J J
RPC A A
X C
SAAJ
R C
W J
E M
B S
S
Java Mgmt J
J
J
C
N
D
Mail
T
O
D
B
JAF JMX A
N
J2SE
Client
RMI/
IIOP
J2SE
4 TIER
3 TIER
JAX- J
W Mgmt
J
J
C
RPC A
E
D
M
O
X
B JMX
B
SAAJ
S
N
R
S
C
EJB Container
Servlet
RMI/
IIOP
EIS
DAO
DB
2 TIER
J2SE
MainFrames
EnterpriseJava Beans
EJBs need a Container
EJBs are deployable components
EJBs are assembled to form a complete Appl
EJBs are based on RMI IIOP and JNDI Technologies
Copyright 2004 Kanti Prasad All rights reserved
!=
Java Beans
JBs do not need a container
JBs are development components
JBs are Classes with no argument constructor
JBs have a get and a set method on them
EJB Client
LocalHome
Client Process
EJB
Client
create
remove
find
STUB
Home
create
remove
find
Remote
invoke
Network
Local
invoke
STUB
EJB Impl
create timeout
remove passivate
activate
find
load
invoke
store
Delegate
Delegate
Delegate
Delegate
EJB
Pool
DNS
NDIS
LDAP
X.500
Implemented by the javax.naming package and three other packages
below it
javax.naming.InitialContext is the entry point to the EJB Server
EJB Provider
Application Assembler
Deployment Specialist
deploys apps and understands architecture issues
builds application
supplies tools
Tools Provider
Application Assembler
develops EJBs
EJB Provider
Copyright 2004 Kanti Prasad All rights reserved
deploys system
Deployer
System Administrator
maintains deployment
Session-oriented approach
Interface
javax.ejb.EntityBean
extends
All SessionBeans
implements
All MessageDrivenBeans
Interface
javax.ejb.SessionBean
implements
Interface
javax.ejb.MessageDrivenBean
extends
extends
javax.ejb.EnterpriseBean interface
extends
It is only a marker Interface
java.io.Serializable
and there are no methods to
Implement.
Serialization is the reason
EJB is distributable.
Service such
as LDAP
Naming
Server B
App Server
JNDI
Initial Context
H
1
H
Home
Obj
Stub
Application
Client
EJB
Obj
Stub
EJB Container
8
Home
Interface
Home Object
(FACTORY)
10
5
6
creates
RMI/IIOP
Remote
Interface
EJB Object
(Wrapper)
EIS
EJB
DB
MainFrames
delegates request
11
Container implements/autogenerates code for the Interfaces
we have written
We will write only this Container manages TRANSACTIONS, PERSISTENCE, SECURITY & POOLING
Copyright 2004 Kanti Prasad All rights reserved
Start
End
App Server
EJB Container
EJB Home or
EJB Local Home
Application
Client
RMI/
IIOP
EJB Object or
EJB Local Object
JMS Destination
e.g
Bank teller
Credit Card authorization
Session Bean
instance
Entity Bean
instance
e.g
Bank account balance
Purchase order
Message Driven
Bean instance
e.g
Stock trade messages
Work flow messages
SESSION BEANS are like VERBS as they represent action or business process related logic.
MESSAGE DRIVEN BEANS have message oriented logic.
If all the processing is done in the same Application server then we use
EJB Local(Object & Home) interfaces. Using local interfaces are optional.
Local interfaces pass by REFERENCE and EJB (Object/Home) pass by VALUE.
Using local interfaces avoids stubs, skeletons, network and so it is faster.
EJB Client
EJB
Client
Home
Remote
Logic
LocalHome Local
Client Process
Network
input
EJB Pool
output
Container decided it
needs more instances
in the pool to service clients
Bean Instance
does not exiist
Class.newInstance()
setSessionContext()
ejbRemove()
ejbCreate()
Pool of equivalent
method ready instances
Client called another
Transactional business method
on the EJB Object.
Transactional
Business Method
EJB Client
EJB
Client
Home
Remote
Logic
State
LocalHome Local
Client Process
Network
input
EJB Pool
output
Bean Instance
does not exiist
Class.newInstance()
setSessionContext()
Client called a
non-transactional business
method on the EJBObject
ejbPassivate()
ejbActivate()
beforeCompletion()
afterBegin()
ejbCreate(args)
Non-Transactional
Business Method
If transaction ended
in a commit..
ejbRemove()
Containers limit of
instantiated beans
are reached, so it must
swap your bean out.
afterCompletion(true)
afterCompletion(false)
Transactional
Business Method
Bean instance is in
the passive state
Implemented class
If transaction ended
in an abort.
Stateless Bean
e.g
An EJB that unzips 100 bytes of data
An EJB that checks to see if a stock symbol is valid
e.g
An EJB that books a flight and rents a car at a travel agents
web site.
EJB Client
LocalHome Local
Client Process
EJB
Client
Hand
Coded
Logic
Home
Remote
Network
EJB Pool
input
output
data
Implements java.io.Serializable
all the members should be public
It should implement hashCode() and equals(.. ) methods
does not implement java.rmi.remote
App Server
EJB Container
CLIENT
EJB
Network
CLIENT
Copyright 2004 Kanti Prasad All rights reserved
EJB
DB
Container decided it
needs another
Entity Bean insinstance
newInstance()
unsetEntityContext()
JVM will garbage
collectt and call finalize()
setEntityContext()
Client called instance
independent ejbHome()
Business method
Container determined
that the database is out of
synch with the bean. The
Bean needs to load the
new Database data.
ejbFind()
Pooled
ejbCreate()
ejbActivate()
ejbStore()\
ejbPostCreate()
ejbLoad()
ejbPassivate()
ejbLoad()
Client called a business
method on a EJBObject
Container decided it
doesnt need the
Entity Bean instance
anymore
Ready
Business Method
ejbRemove()
ejbStore()
Container determined
that the database is out of
synch with the bean. The
Bean needs to store its
Data into the Database
EJB Client
LocalHome Local
Client Process
EJB
Client
Generated
Logic
Home
Remote
Network
EJB Pool
input
output
data
Persistence
Persistence
DDs
DDs
Container decided it
needs another
Entity Bean in
newInstance()
unsetEntityContext()
JVM will garbage
collectt and call finalize()
setEntityContext()
Client called instance
independent ejbHome()
Business method
ejbHome()
Client called create() on
the home interface (this will
create new database data)
Container determined
that the database is out of
Synch with the bean. The
Bean needs to load the
new Database data.
ejbFind() or ejbSelect()
Pooled
ejbCreate()
ejbActivate()
ejbStore()\
ejbPostCreate()
ejbLoad()
ejbPassivate()
ejbLoad()
Client called a business
method on a EJBObject
Container decided it
doesnt need the
Entity Bean instance
anymore
Ready
Business Method
Or ejbSelect()
ejbRemove()
ejbStore()
Container determined
that the database is out of
Synch with the bean. The
Bean needs to store its
Data into the Database
We should start developing CMP beans, unless we require some kind of special bean, like multi-tables, that
cannot be completely realized with a single bean. Then when we realize that we need something more or that
we prefer handling the persistence (performance issue are the most common reason), we can change the bean
from a CMP to a BMP
Messaging
Messaging
Service
Message
Network
API
Message-Driven Bean
Message
Queue
Handler Logic
input
Message
Message
Listener
EJB Pool
Asynchronous messaging
EJB Impl Developer View
gets async requests via messaging paradigm-specific interface
An instance can service multiple clients over time
EJB Client Developer View
Messages sent to endpoint & handled by messaging service
Specific to particular messaging paradigm used
EJB Container View
pulls instance from pool and delivers message
Container decided it
needs another
Message DrivenBean
Instance
newInstance()
ejbRemove()
setMessageDrivenContext()
ejbCreate()
Pooled
onMessage()
51. Deployment
Deployment involves taking an EJB compliant bean and
Declaring without programming helps the application assembler to change the XML file easily.
BEAN Provider declares components middleware service requirements in a DEPLOYMENT DESCRIPTOR File.
Bean Provider describes how the Container should perform the LifeCycle Management, Persistence, Transaction and Security.
javax.ejb.EJBLocalHome
javax.ejb.EJBLocalObject
E.g.:
Inter component calls
Servlet invocation
53. Exceptions
EXAMPLE 1
STATELESS BEAN
Start
1
5
2
3
Connect to your EJB by writing a test
7 Client .java file compile it and run it .
4
End
Please download the trial Application Server software from the links provided on the Reference slide at the end.
java.io.Serializable
Javax.ejb.EJBHome
Javax.ejb.EnterpriseBean
Javax.ejb.SessonBean
We need
to CODE
only Interface
this block
Hello world Remote Interface
Hello
World Home
Generated for us by the Container verdors tools
Hello world EJB Object
No Network so
LOCAL IMROVES
PERFORMANCE
Javax.ejb.EJBLocalHome
java.io.Serializable
Javax.ejb.EnterpriseBean
Javax.ejb.SessonBean
This method creates/manufactures the EJBObject and returns it . This create() method corresponds to the
ejbCreate() method in HelloBean.
As for every Interface we need implementation,Note
so Container
implement
code) for Remote
this Home
Interface
In Home will
Interface
we are(autogenerate
throwing 2 exceptions
and
Create .for us, which is the EJBHome Object
NOTE : Rules of Interfaces states that there should be no implementation so you will find only method signatures.
package examples ; Rules of Java says package declaration should be at the beginning and ends with a semi colon
import javax.ejb.EJBLocalHome;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import java.util.Collection;
Our interface HelloLocalHome is extending EJBLocalHome means it has all the behaviour of EJBLocalHome
package examples ; Rules of Java says package declaration should be at the beginning and ends with a semi colon
import java.util.*;
Importing the required files
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface Hello extends javax.ejb.EJBObject
Our interface Hello is extending EJBObject means it has all the behaviour of EJBObject
{
public String hello() throws java.rmi.RemoteException;
As our requirement wants a business logic to greet Hello World a single method would be sufficient .
}
Interface methods should end with a semicolon ;
As Remote interface is used to interact with the Bean we need to mirror all the method signatures what
are there in a Bean class so that is why we are writing only one method in this Remote interface.
Note EJB specification states that all remote calls should throw RemoteException so we are throwing it.
As for every Interface we need implementation, so Container will implement (autogenerate code) for this Remote Interface for us, which is the EJBObject
NOTE : Rules of Interfaces states that there should be no implementation so you will find only method signatures.
Copyright 2004 Kanti Prasad All rights reserved
package examples ; Rules of Java says package declaration should be at the beginning and ends with a semi colon
import javax.ejb.EJBLocalObject;
public interface HelloLocal extends javax.ejb.EJBLocalObject
Our interface Hello is extending EJBLocalObject means it has all the behaviour of EJBLocalObject
{
Interface methods should end with a semicolon ;
public String hello() ;
As our requirement wants a business logic to greet Hello World a single method would be sufficient .
}
As Local interface is used to interact with the Bean we need to mirror all the method signatures what
are there in a Bean class so that is why we are writing only one method in this Local interface.
Note As there is no remote calls no Remote Exception is thrown
As for every Interface we need implementation, so Container will implement (autogenerate code) for this Local Interface for us, which is the EJB Local Object
NOTE : Rules of Interfaces states that there should be no implementation so you will find only method signatures.
package examples ; Rules of Java says package declaration should be at the beginning and ends with a semi colon
import java.util.*;
Importing the required files
import javax.ejb.*;
public class HelloBean implements javax.ejb.SessionBean
NOTE : Note
If it is: Entity
this isBean
a class
weand
will not
implement
an Interface
javax.ejb.EntityBean
like the other two
and
(Home
if it is /Message
Remote).driven
This isbean
implementing
we will implement
SessionBean
javax.ejb.MessagedrivenBean
so it is a Session Bean
As per the below note in red we need to write all the methods what are in javax.ejb.SessionBean .
{
private SessionContext
This ctx;
ejbCreate() corresponds to the Homeobject Create(). NOTE : in Stateless Bean no arguments are sent
public void ejbCreate() { System.out.println(ejbCreate()); }
There is nothing
much
to clean up.
public void ejbRemove() { System.out.println(ejbRemove());}
These
are called
Management or
In Statelss Bean ejbActivate
public void ejbActivate() { System.out.println(ejbActivate());}
Call back methods
and ejbPassivate Do not apply
public void ejbPassivate() { System.out.println(ejbPassivate()); }
public void setSessionContext(javax.ejb.SessionContext ctx) {
Storing the Context in a variable so that it can be queried later .
this.ctx = ctx; }NOTE : Because it is a Session Bean we are using setSessionContext for Entity Bean we
use setEntityContext and for Message Driven Bean we use setMessageDrivenContext.
public String hello() { System.out.println(Hello());
return Hello World! ; }
Our Business method which matches
} our Remote Interface method signature
Java does not support multiple inheritance so you can extend only one class. To overcome
Copyright 2004 Kanti Prasad All rights reserved
this they had given us the Implementation option . So when we use implement we need to
implement or write all the methods what are there in the interface we implemented.
As a Bean provider we need to specify the Middleware needs through this descriptor.
<!DOCTYPE ejb-jar PUBLIC -//Sun Microsystems, Inc. // DTD Enterprise JavaBeans 2.0 //
EN http://java.sun.com.dtd/ejb-jar_2_0.dtd>
XML Standard requirement
<ejb-jar>
<enterprise-beans>
<session>
The nickname for this particular Bean
<ejb-name> Hello </ejb-name>
The fully qualified name of the Home Interface
<home>examples.HelloHome</home>
<remote>examples.Hello</remote>
The fully qualified name of the Remote Interface
<local-home>examples.HelloLocalHome</local-home>
The fully qualified name of the Local HomeInterface
The fully qualified name of the Local Interface
<local>examples.HelloLocal</local>
<ejb-class>examples.HelloBean</ejb-class>
The fully qualified name of the EJB Class
Whether the session bean is stateful or stateless
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
Copyright 2004 Kanti Prasad All rights reserved
NOTE : To call Local Interface you need not cast with the PortableRemoteObject. So the
block in red will be changed to
Object obj = ctx.lookup(java:comp/env/HelloLocalHome); ( to get a ref to LocalHome Object)
HelloLocalHome home = (HelloLocalHome)obj;
Properties
File
Remote/Local
Interface
HomeLocalHome
Interface
Enterprise
Bean Class
Deployment
Descriptor
Jar
Manifest
Jar Creator
Jar
file
DEPLOYED
.JAR
Deployment
Descriptor
.WAR
Deployment
Tool
WEB ARCHIEVE (.war)
WEB.XML
Enterprise Archive
(.EAR Files)
(Application.XML)
HTML
JSP
Servlets
EXAMPLE 2
STATEFUL BEAN
Remote Cart is extending EJBObject means Cart has all the behaviour of it
public interface Cart extends EJBObject
Our bean class CartBean methods are mirrored here
{
public void addBook(String title) throws RemoteException;
EJB spec says all remote interface methods need to throw remote exception
There will not be any implementation in Interfaces
EJB spec says all Home interface methods need to throw remote & create exceptions
There will not be any implementation in Interfaces
import java.util.*;
import javax.ejb.*;
import samples.ejb.stateful.simple.tools.BookException;
import samples.ejb.stateful.simple.tools.IdVerifier;
CartBean Class is implementing sessionbean means this bean class is a session bean
String customerName;
String customerId;
Vector contents;
We definedThis
this also
method
in the Cart
interface. person) method
represents
the remote
Home create(String
this is the Bean class we will implement the business logic here
NOTE : AS we are implementing and not extending we need to write all the methods that are there in the Sessionbean
Copyright 2004 Kanti Prasad All rights reserved
a vector
Rules of implement states we need to implement all methods that are there in the implemented interface
Copyright 2004 Kanti Prasad All rights reserved
package samples.ejb.stateful.simple.ejb;
import java.util.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
public class CartClient {
Client class
a class
shoppingCart.removeBook("Alice in Wonderland");
shoppingCart.remove(); Removing a particular book
System.exit(0);
77. ID Verifier
package samples.ejb.stateful.simple.tools;
public class IdVerifier {
/**
* Default constructor.
*/
public IdVerifier() {
}
public boolean validate(String id) {
boolean result = true;
for (int i = 0; i < id.length(); i++) {
if (Character.isDigit(id.charAt(i)) == false)
result = false;
}
return result;
}
}
ALL EXAMPLES
CAN BE SEEN AT
http://www.weblogic.com/docs/examples/ejb/Package-examples.ejb.html
http://my.execpc.com/~gopalan/java/entity.html
http://java.sun.com/developer/onlineTraining/Beans/EJBTutorial/
http://developers.sun.com/prodtech/javatools/jsstandard/reference/techart/entitybeans.html
ActiveX
Transactions, database management, legacy integration have to be coded
into the logic of the component. Security policy can be inherited from
NTLM. Transactions can be aided through MTS
CORBA
CORBAServices have different APIs. CORBAServices can be complicated
to code with. CORBAServices are not supported by all ORBs
Copyright 2004 Kanti Prasad All rights reserved
ActiveX
uses Microsoft's inprocess/out-of-process architecture for sharing
multiple ActiveX components and Client components.
CORBA
ORB makes multiple instances of CORBA objects. Clients contexts are
managed by ORB but can behave differently based upon vendors
Implementation.
Copyright 2004 Kanti Prasad All rights reserved
87. Conclusion
We successfully completed a Tour on EJB . We looked at how an EJB is identified. How many
different types of EJBs are there and what each EJB type implements. Why
LocalInterfaces were introduced. We disected the EJB and went deeper into each part i.e. BEAN
Class, Remote Interface, EJB Object, Local Object, Local Interface, Home Object. We looked
Into the deployment descriptor and also seen all the files in EJBJar. We looked into each
Beans
Methods and also learned why we needed those methods. We had done 2 different Beans
examples and we were directed to the Web links where all the examples are demonstrated.
Once
you can get an idea of all the Interfaces, methods & exceptions raised, you can attempt to
try
some examples to get familiar and master EJB. There are nearly 100s of Application servers
but in this presentation you can see a few majorly used AppServers sample
implementation
web links. Just download any one of them and try out for yourself.
CONGRATULATIONS!
Now you know
as much as he
know of EJB
88. References
Application Server Information , EJB Documentation & Tutorials are found here
Notes on Deploying on different Application Servers.
The Art of EJB Deployment http://www.javaworld.com/javaworld/jw-08-2001/jw-0803-ejb.html
Most of EJBs related information can be seen at http://www.volantec.biz/ejb.htm
http://www.theserverside.com/ Mastering Enterprise Java Beans
by Ed Roman , Scott Ambler & Tyler Jewel.
EJB Documentation http://java.sun.com/products/ejb/javadoc-2_0-fr/
http://my.execpc.com/~gopalan/java/java_tutorial.html
Application Servers download information . Most of them are either free or have a 60 day trial version
For JBOSS Open Source go to http://www.jboss.org/downloads/index
http://www.kevinboone.com/jboss_tut_1.html
SUNs downloads and examples
http://developers.sun.com/prodtech/javatools/jsstandard/reference/docs/s1s5/partsuppliers.html
http://java.sun.com/j2se/1.4.2/download.html ,http://java.sun.com/j2ee/1.4/docs/tutorial/doc/
http://java.sun.com/developer/onlineTraining/EJBIntro/EJBIntro.html
BEA WebLogic downloads and examples
http://commerce.bea.com/index.jsp , http://www.weblogic.com/docs/examples/index.html
http://dev2dev.bea.com/codelibrary/code/examples_ejb20.jsp
WebSphere downloads and examples
http://www-306.ibm.com/software/webservers/appserv/doc/v20dcadv/doc/howto/ep001.html
REDHAT Application Server and Examples
http://www.redhat.com/docs/manuals/rhaps/jonas-guide/
Copyright 2004 Kanti Prasad All rights reserved
Covers
90 % of the
MARKET