About Enterprise JavaBeans (EJB)

climbmoujeanteaSoftware and s/w Development

Dec 13, 2013 (3 years and 3 months ago)


About Enterprise JavaBeans (EJB)

Top of Page

Enterprise JavaBeans (EJB)

technology is a specification framework tha
t supports the
development and deployment of
. You can build distributed object
oriented business
applications in Java using this standard server
side architecture. The framework

a complete deployment environment and a set of services for developing and executing
distributed applications. This enables you to focus on the application logic instead of the
application framework required when building server applications. An
EJB represents a
business object or service, such as a purchase requisition, in a Java application. You code
the business logic in reusable Java software components that reside in an application or
data server and can be accessed by the clients.

An enterp
rise bean can be developed once, and then deployed on multiple platforms
without recompilation or modification. It also provides inter
operability between
components fitting this standard.

Viewing the EJB Framework

When you create a distributed applicatio
n with Enterprise JavaBeans technology, you
code the business logic in reusable components called enterprise beans. Enterprise beans
reside in an EJB container that runs on an EJB server.

The EJB framework consists of components and services. In the diagr
am below you can
see the location of those components. The EJB server is the highest level component,
including all the others, and providing the EJB container the access to the services. The
home interface is returning a reference on the enterprise bean t
hat is invoked and
manipulated by the remote interface.

Figure 1 EJB Server

EJB Client

The EJB client is connects to the EJB server. It uses
Java Naming and Directory Interface

to locate the home interface object to create an EJB class instance. It then uses the
remote interface of the EJB class instance to invoke metho
ds on the server object. The
EJB client and the EJB server communicate using
Internet Inter
ORB Protocol (IIOP)

another protocol, such as
Remote Method Invocation (RMI)

Enterprise Bean

The enterprise bean is a Java class or collection of classes, provided as a
Java ARchive
(JAR) fil
. The enterprise bean component takes advantage of the Enterprise JavaBean
architecture extending classes aligned with the EJB specifications develops it.

EJB Container

The EJB container acts as the enterprise bean holder that manages the enterprise bea
classes and instances. EJB classes are deployed to the EJB container that manages the
startup and shutdown of the EJB class instances. The EJB container provides system
level services to the EJB classes through the standard EJB APIs. The EJB server, wh
provides the basic system services, manages the EJB containers.

Home interface

Home interface defines the methods for locating, creating, and removing instances
of the EJB classes. You must create and implement the required factory methods
so that the

EJB container can create instances of the EJB classes when a client
establishes a connection to the server. The factory is a mandatory component of
the distributed computing that returns a reference on an object.

Remote interface

EJB clients do not invok
e methods on the EJB class directly, but instead go
through the remote interface. Therefore, an EJB client can only define the
business methods that a client may call through the remote interface. The business
methods are implemented in the enterprise bean


EJB server

The EJB server manages EJB containers. It provides the underlying environment for an
enterprise bean. It provides access to the basic system services required by the Enterprise
JavaBeans architecture and any vendor specific features, suc
h as optimized database

Deployment descriptor

The deployment descriptor is a file that contains all the information needed by the EJB
container at runtime.

Services Provided by the EJB Container

The basic services provided by the EJB container ar
e transactions, naming, and
persistence. The transaction service allows multiple enterprise beans to participate in a
transaction without transaction code implemented explicitly into the client's source. The
naming service allows EJB clients to locate EJB
servers by name. The persistence service
allows the enterprise bean instances to be stored inside a database or in a file system.

This age EJB server provides an age to date calculation for clients. Multiple EJB clients
can connect to the age EJB server a
nd invoke a method to convert an age from years to
days. To develop the EJB server application, you create the age EJB class. Since the EJB
container assumes the server infrastructure and communication mechanism, you can
deploy to an EJB server without kno
wing anything about the infrastructure or
communication mechanism, as the EJB container assumes them.

View Example

Download Code

Comparing JavaBeans and EJBs

Both JavaBeans and Enterprise JavaBeans are written in Java. However, they reside in
different types of containers and are not interchangeable. There are other differences that
are dis
played in the following table:


Enterprise JavaBeans...

Contain a specification for creating and
reusing components.

Contain a specification for a services framework so
that you can deploy components.

Create the server framework.


framework is provided.

Contain EJB classes that must conform to the
JavaBean specifications.

Contain EJB classes that must conform to the EJB

Types of EJBs

There are two types of enterprise beans: session beans and entity beans. Th
ey mainly
differ in the way they manage data persistency, but are not mutually exclusive. A single
entity bean represents a data object and several entity beans are shared among multiple
clients. For example, an entity bean can contain the value of the col
umns of a row in the
database. Similar to database tables, entity beans cannot be used to store session state
information. Most applications use session beans to store the session state for multiple
clients, and use entity beans to encapsulate access to th
e data objects.

For each underlying data object, you have a one to one mapping between the data object
and the entity bean instance. Entity beans by definition are shared among multiple clients,
usually across multiple session beans.

The following table
provides an overview of the differences between entity beans and
session beans:

Entity Beans...

Session Beans...

Shared by multiple clients

Represents a single client inside the
EJB server.

Represents a business object in a persistent storage mechan
such as a database. The persistence of an entity bean can be
managed by either the entity bean itself, or by the EJB
container. Bean
managed persistence requires you to write the
data access code in the Bean

Not persistent. When the client

its session bean appears to
terminate and is no longer associated
with the client.

Represents a business entity. Entity beans are server
components where each instance of a bean maps to an
underlying data object such as a row in the database.

orms a task for a client. Session
beans are server
side components
where each instance of the bean maps
to a client connection to the server.

Creating Applications Using EJBs

Enterprise JavaBeans based applications consist of an EJB server and an EJB
client. The
EJB server implements the business logic and is deployed to an EJB server. The EJB
client can be any standard Java

or application that uses
Java Naming and Directory
Interface (JNDI)

to locate and connect to an EJB server.

To create an EJB server:


Implement the EJB class.


Implement the remote interface and the home interface.


Create the de
ployment descriptor.


Deploy the EJB class to an EJB server.