Enterprise Javabeans Ejb
Enterprise Javabeans Ejb
Introduction
An Enterprise Java Bean (EJB) is a component the provides
reusable business logic functionality and/or a representation of a
persistent business entity
It is a server side component that can be deployed in a distributed
multi - tier environment
Client uses an interface to access the Bean indirectly
A deployment descriptor describes the structure of the Bean and
how to execute the Bean as part of an application
Example
An application needs to display a page that contains user Account
information
The J2EE application displays a web page that contains an HTML
form where the user can enter the identification number of the
Account
The browser then calls a JSP program
The JSP program parses the identification number from the query
string sent by the browser
If it is valid, then the number is passed to an EJB, which interacts
with a database to extract information about the account
This information is returned to the JSP program, which formats the
information into a web page and sends the web page to the browser
for display
Valid Request Data
B ID ID
R
O JSP EJB DATABASE
W
S Data Data
E
R Formatted
Output
EJB Container
An EJB container is a vendor-provided entity located on the EJB
server
It manages system-level services for EJB like threading, security,
and persistence
Each EJB must be installed in an EJB container
A client’s request for web service is made to a web server, which
forwards the client’s request to the appropriate server
Either the Java servlet server or the JSP server receives the client’s
request, depending on which is used by the J2EE application
The Java servlet or JSP uses JNDI to look up the EJB resource
EJB Classes
There are three kinds of EJB types:
Entity JavaBean class (Entity Bean)
Session JavaBean class (Session Bean)
Message – Driven JavaBean class (Message-Driven Bean)
An Entity Bean is used to represent the business data
A session bean is used to model a business process
A message – driven bean is used to receive messages from JMS
Distributed Objects : The foundation for
EJB
EJB components are based on distributed objects
A distributed object is an object that is callable from a remote
system
Distributed
Client Object
Remote Interface
Remote Interface
Stub Skeleton
Network
How client calls a distributed object
The client calls the stub which is a client side proxy object
The stub is responsible for masking network communication from the
client
The stub knows how to call over the network using sockets,
marshalling parameters as necessary into their network representation
Marshaling:
done by client (i.e., caller)
Unmarshaling:
done by receiver of message to extract parameters
Contd..
The stub calls over the network to a skeleton, which is a server-side
proxy object.
The skeleton masks network communication from the distributed
object
The skeleton understands how to receive calls on a socket
It also knows how to unmarshal the parameters from network
representation to the Java representation
The skeleton delegates the call to the distributed object
The distributed object does its work, and then returns control to the
skeleton, which returns to the stub which then returns the control to
the client
Contd..
The key point is that the stub and the distributed object both
implement the same interface (called the Remote interface)
A client who calls a method on the stub thinks he is calling the
distributed object directly; in reality, the client is calling an empty
stub that knows how to go over the network
This is called local / remote transparency
The Enterprise Bean class
This is simply a Java class that conforms to a well defined interface
and obeys certain rules
The EJB specification defines a few standard interfaces that our
bean class can implement:
javax.ejb.EnterpriseBean
javax.ejb.SessionBean
javax.ejb.EntityBean
javax.ejb.MessageDrivenBean
Javax.ejb.EnterpriseBean
The most basic interface
All bean classes must implement it
A marker interface
Implementing this interface indicates that the class is an enterprise
bean class
javax.ejb.EnterpriseBean itself extends java.io.Serializable
Interfaces
Session beans, entity beans and message driven bean each have
specific interfaces that extend the javax.ejb.EnterpriseBean
interface
All session beans must implement javax.ejb.SessionBean
All entity beans must implement javax.ejb.EntityBean
All message driven beans must implement
javax.ejb.MessageDrivenBean
EJB Interfaces
The session and entity beans must have two interfaces:
Home Interface
Remote Interface
Both these interfaces are declared by the developer of the EJB and
are implemented by the EJB container
The Home interface has methods to create and destroy EJB objects
The Remote interface is written using Java Remote Invocation
syntax and declares methods that can be accessed by a remote
client.
Hello.java
// Remote interface
import javax.ejb.*
import java.rmi.*;
The Home interface has methods to create and destroy EJB objects
The create() method is a factory method that client uses to reference to an
EJB object.
The create method is also used to initialize a bean
create() throws CreateException which indicates some ordinary problem (like
bad parameter) occurred during bean initialization