Enterprise Java Beans (EJB)
By
Janaranjani K
Outline
Introduction of EJBs.
EJB as a Business Tier Component.
EJB fundamentals.
Enterprise Beans.
Foundation of EJB.
What constitutes Enterprise Bean?
Session beans.
Components of a Session beans.
The Session EJB Lifecycle (includes stateful and stateless).
An example of stateless session bean (which includes both
remote and local interface).
Introduction to EJBs
Introduction to EJBs
EJB is a standard for building server side component in java. It
defines a contract between the component and application server
that enables the component to run in any application server.
EJB components (called as enterprise beans) are
deployable
imported
loaded
into an application server, which host all these components.
Continued…
Common Service includes:
Transaction Management
Resource Management
Connection pooling
Security
Networking
Persistant Management
Concurrency
Location transparency
Load Balancing etc.
EJB as a Business Tier
Component
Enterprise beans are intended for server side component
development not for client side (which lies in presentation tier
and components like thick clients, web service clients etc are
used for its development). So it lies in Business Tier.
EJB component can perform the following tasks:
Perform business logic.
Access to the database.
E.g., Submitting an order for books.
Enterprise bean can achieve database access using JDBC.
Access another system.
E.g., Calling some legacy system written in COBOL.
EJB Fundamentals
Enterprise Beans
An enterprise bean is a server side software component that
can be deployed in a distributed environment.
It compose of one or more java objects.
The client to an enterprise bean can be one of the following:
A servlet
An applet
An enterprise bean (this results in chain of beans).
Continued…
Types of Beans:
Session beans.
Stateful session beans.
Stateless session beans.
Entity beans.
Message Driven beans.
Diagrammatic
Representation
Enterprise java
beans
Session Beans MDBs Entity beans
Stateful Stateless CMP BMP
Session Beans
Session beans model business process.
They are perform actions. Like,
Adding numbers
Accessing database
Unlike entity beans, they are non-persistant.
Types:
Stateful session beans.
Stateless session beans.
Stateful Session Beans
A stateful session bean maintains a conversational state
The state is relevant only for a single client
Cannot be seen by other clients
The state is not persistent
Expires after a certain timeout
It has performance hit due to resource pooling.
Canonical example: Shopping cart
Stateless Session Beans
Conceptually, the same as stateful session beans.
No need to save the conversational state, so called Stateless.
Can have fields, but they are not unique to any client
Since the container knows the bean has no state, it can:
Use a single bean instance (While each client thinks it has its
own copy)
Destroy/re-instantiate on the fly
Redirect requests to different instances (load balancing)
Client does not care on which server the bean is stored
Example: Currency conversion bean
Entity Beans
Entity beans model Business Data.
Object-oriented view of entities stored in persistent storage
Normally, each instance represents a row in a relational DB
table
Persistence code can be written manually (bean-managed
persistence, BMP) or automatically (container-managed
persistence, CMP)
A single bean instance (on the server) can be accessed by
multiple clients
Unlike stateful session beans.
Message Driven Beans
New in EJB 2.0 standard
Built on top of JMS to send messages to clients, which in
turn would be received by MDB.
It is invoked by the container upon arrival of message at
the destination that is serviced by the message-driven
bean.
Have no client visibility,
Foundation of EJB
Distributed Architecture
EJB components are based on Distributed Objects
A distributed object is an object that is callable from a remote
system.
The client can be in-process, out-of-process or a client located
elsewhere on the network.
It does the invocation with the help of the followings:
Stubs (client side proxy object):
It masks the network communication from the client.
It knows how to call over the network using the sockets, massaging
parameters as necessary into their network representation.
Skeleton (server side proxy object):
It masks the network communication from the distributed object.
It understands how to receive calls on a network.
Also knows how to massage parameters from the network
representation to their java representations.
It then delegates the call to the appropriate implementation object.
What constitutes Enterprise
Bean?
Continued…
Enterprise Bean class:
It contains the implementation of your business logic.
It can be session or entity or MDB.
Component Interface:
Remote Interface:
It is the java interface that enumerates the business
methods of your bean class.
Client code always go through the remote or local interface
and never interacts with the bean directly.
It obeys the rules for java RMI-IIOP.
Continued…
Local Interface:
It’s a high performing version of remote version.
Used when you are calling the bean that presents in the
same process.
Unlike remote interface, it will not undergo stubs, skeleton,
marshaling/demarshaling of parameters.
EJB object:
It is container generated implementations of the remote
interface.
All client invoications go through [Link] delegates the call to
bean and implements the remote interface
Local home object:
High performing version of home object.
Implements the local home interface.
Continued…
Home interface:
Java interface, acts as a EJB factory.
Provides a handle of bean class to client
Local Home interface:
High version of Home interface.
Home object:
Container generated implementation of home interface.
Obeys RMI-IIOP rules.
Local home object:
High performing version of home object.
Implements the local home interface.
Continued…
Deployment Descriptor:
An XML file, specifies the middleware requirements of your
bean to the container.
Vendor specific files:
Enables you to take the advantage of vendor specific features
EJB-jar file:
A complete zip of the above said files.
Given to the application server.
Application server unpacks the jar file and loads the bean into the
container.
Session Beans
Components of a Session
Beans
To define session bean X, you must create three classes:
The remote (local) component interface, called X (or XLocal)
Extends EJBObject (or EJBLocalObject)
(All extended types are from [Link])
The home interface, called XHome (or XLocalHome)
Extends EJBHome (or EJBLocalHome)
The bean class itself, called XBean
Extends SessionBean
Should implement [Link] (for stateful beans)
Implements business methods from the component interface
Implements lifecycle methods (ejbXXX)
The class names are (strong) conventions
Stateful Session Bean’s
Lifecycle
Lifecycle Methods
Lifecycle methods are defined in the XBean class
They are named ejbXXX
ejbCreate, ejbActivate, etc. plus setSessionContext
No other method name should begin with ‘ejb’
Some lifecycle methods also appear in the XHome interface
For session beans, only the create methods appear in the
home interface
In the home interface, the ‘ejb’ prefix is not used
i.e., method should be named ‘create’
Lifecycle methods are invoked by the container when changing
state during the bean’s life
ejbCreate Methods in
Stateful Session Beans
A stateful session bean can contain several ejbCreate
methods
The methods can have zero or more parameters
They can be called as ejbCreate(...) or ejbCreateXXX(...),
for more descriptive names
e.g., ejbCreateByName(String name)
Must be public void
Should throw [Link] if creation fails
for whatever reason (invalid arguments, etc.)
Continued…
Must be reflected in the XHome interface (local or remote) by a
method with identical parameters, that is called ‘create’ or
‘createXXX’
Returns the bean component interface type (X)
For remote homes, throws [Link]
e.g., public X createByName(String name) throws
...
Activation and Passivation
During the bean’s lifecycle, the container may decide to
passivate it
Normally done to free up memory and other resources
Bean is notified immediately before passivation by lifecycle
method ejbPassivate()
In this method, the bean should:
Release any resources it might be holding
Sockets, Open files, database connections, etc.
Nullify fields that should not be serialized (cache, etc.)
If the bean is referenced by client while passivated, the container
activates it.
Bean is notified by ejbActivate() immediately after de-
serialization
Its chance to restore all field values, resources, etc.
The Lifecycle of Stateless
Session
EJBs
Session EJBs have no state, so activation and passivation are
meaningless
The container simply destroys instances when low on memory,
and creates new ones as needed
Still, ejbActivate and ejbPassivate must be implemented
in XBean (as empty methods)
The resulting lifecycle diagram is somewhat simplified:
ejbCreate Methods in
Stateless Session Beans
For stateless session beans, there can be only one ejbCreate
method, accepts zero parameters
Reflected normally in the home interface as create.
Must be public void
Should throw [Link] if creation fails for
whatever reason (invalid arguments, etc).
Must be reflected in the XHome interface (local or remote) by a
method with identical parameters, that is called ‘create’.
Returns the bean component interface type (X)
For remote homes, throws [Link]
Sample Session Bean
Deployment Descriptors
…. <enterprise-beans>
<session>
<ejb-name>HelloWorldLocalBean</ejb-name>
<home>[Link].localHome_Object.HelloWo
rldHome</home>
<remote>[Link].localHome_Object.HelloW
orldRemote</remote>
<ejb-
class>[Link].localHome_Object.HelloWorl
dLocalBean</ejb-class>
<session-type>Stateless(stateful)</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans> ….
An example of stateless
session bean.
Session Bean Class
package [Link].localHome_Object;
import [Link];
import [Link];
import [Link];
public class HelloWorldLocalBean implements SessionBean{
private SessionContext ctx;
public void ejbCreate() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void ejbRemove() {}
public void setSessionContext(SessionContext ctx) {
[Link] = ctx;
}
public String sayHelloMethod(int num, String s) {
[Link]("sayHello in [Link]
webservice has "+
"been invoked with arguments " + s + " and " + num+" the
context is : "+ctx);
String returnValue = "HelloWorldLocalBean....:This message
brought to you by the "+
"letter "+s+" and the number "+num+" the context is : "+ctx;
return returnValue;
}
}
Home Interface
package [Link].localHome_Object;
import [Link];
import [Link];
import [Link];
public interface HelloWorldHome extends EJBHome{
HelloWorldRemote create() throws CreateException,
RemoteException ;
}
Remote Interface
package [Link].localHome_Object;
import [Link];
import [Link];
public interface HelloWorldRemote extends EJBObject {
public String sayHelloMethod(int num, String s) throws
RemoteException;
}
Local Home Interface
package [Link].localHome_Object;
import [Link];
import [Link];
public interface HelloWorldLocalHome extends EJBLocalHome{
HelloWorldLocal create() throws CreateException;
}
Local Interface
package [Link].localHome_Object;
import [Link];
public interface HelloWorldLocal extends EJBLocalObject {
public String sayHelloMethod(int num, String s);
}
Deployment Descriptor
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD
Enterprise JavaBeans 2.0//EN" "[Link]
jar_2_0.dtd">
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>HelloWorldLocalBean</ejb-name>
<home>[Link].localHome_Object.HelloWo
rldHome</home>
<remote>[Link].localHome_Object.HelloW
orldRemote</remote>
Continued…
<local-
home>[Link].localHome_Object.HelloWorl
dLocalHome</local-home>
<local>[Link].localHome_Object.HelloWor
ldLocal</local>
<ejb-
class>[Link].localHome_Object.HelloWorl
dLocalBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
Q&A
Thank you all