The Enterprise JavaBeans specification defines an architecture for a
transactional, distributed object system based on components. The specification
mandates a programming model;
A D V E R T I S E M E N T
that is, conventions or protocols and a set of
classes and interfaces which make up the EJB API. The EJB programming model
provides bean developers and EJB server vendors with a set of contracts that
defines a common platform for development. The goal of these contracts is to
ensure portability across vendors while supporting a rich set of functionality.
The EJB Container
Enterprise beans are software components that run in a special environment
called an EJB container. The container hosts and manages an enterprise bean in
the same manner that the Java Web Server hosts a servlet or an HTML browser
hosts a Java applet. An enterprise bean cannot function outside of an EJB
container. The EJB container manages every aspect of an enterprise bean at
runtimes including remote access to the bean, security, persistence,
transactions, concurrency, and access to and pooling of resources.
The container isolates the enterprise bean from direct access by client
applications. When a client application invokes a remote method on an enterprise
bean, the container first intercepts the invocation to ensure persistence,
transactions, and security are applied properly to every operation a client
performs on the bean. The container manages security, transactions, and
persistence automatically for the bean, so the bean developer doesn't have to
write this type of logic into the bean code itself. The enterprise bean
developer can focus on encapsulating business rules, while the container takes
care of everything else.
Containers will manage many beans simultaneously in the same fashion that the
Java WebServer manages many servlets. To reduce memory consumption and
processing, containers pool resources and manage the lifecycles of all the beans
very carefully. When a bean is not being used, a container will place it in a
pool to be reused by another client, or possibly evict it from memory and only
bring it back when its needed. Because client applications don't have direct
access to the beans--the container lies between the client and bean--the client
application is completely unaware of the containers resource management
activities. A bean that is not in use, for example, might be evicted from memory
on the server, while its remote reference on the client remains intact. When the
client invokes a method on the remote reference, the container simply
re-incarnates the bean to service the request. The client application is unaware
of the entire process.
An enterprise bean depends on the container for everything it needs. If an
enterprise bean needs to access a JDBC connection or another enterprise bean, it
does so through the container; if an enterprise bean needs to access the
identity of its caller, obtain a reference to itself, or access properties it
does so through the container. The enterprise bean interacts with its container
through one of three mechanisms: callback methods, the EJBContext interface, or
the Java Naming and Directory Interface (JNDI).
Callback Methods
Every bean implements a subtype of the EnterpriseBean interface
which defines several methods, called callback methods. Each callback method
alerts the bean TO a different event in its lifecycle and the container will
invoke these methods to notify the bean when it's about to activate the bean,
persist its state to the database, end a transaction, remove the bean from
memory, etc. The callback methods give the bean a chance to do some housework
immediately before or after some event.
EJBContext
Every bean obtains an EJBContext object, which is a reference
directly to the container. The EJBContext interface provides
methods for interacting with the container so that that bean can request
information about its environment like the identity of its client, the status of
a transaction, or to obtain remote references to itself.
Java Naming and Directory Interface
Java Naming and Directory Interface (JNDI) is a standard extension to the
Java platform for accessing naming systems like LDAP, NetWare, file systems,
etc. Every bean automatically has access to a special naming system called the
Environment Naming Context (ENC). The ENC is managed by the container and
accessed by beans using JNDI. The JNDI ENC allows a bean to access resources
like JDBC connections, other enterprise beans, and properties specific to that
bean.
Enterprise Beans
To create an EJB server-side component, an enterprise bean developer provides
two interfaces that define a bean's business methods, plus the actual bean
implementation class. The client then uses a bean's public interfaces to create,
manipulate, and remove beans from the EJB server. The implementation class, to
be called the bean class, is instantiated at runtime and becomes a distributed
object.
Enterprise beans live in an EJB container and are accessed by client
applications over the network through their remote and home interfaces. The
remote and home interfaces expose the capabilities of the bean and provide all
the method needed to create, update, interact with, and delete the bean. A bean
is a server-side component that represents a business concept like a Customer or
a HotelClerk.
Remote and Home Interfaces
The remote and home interfaces represent the bean, but the container
insulates the beans from direct access from client applications. Every time a
bean is requested, created, or deleted, the container manages the whole process.
The home interface represents the life-cycle methods of the component (create,
destroy, find) while the remote interface represents the business method of the
bean. The remote and home interfaces extend the javax.ejb.EJBObject and
javax.ejb.EJBHome interfaces respectively. These EJB interface types define a
standard set of utility methods and provide common base types for all remote and
home interfaces.
Clients use the bean's home interface to obtain references to the bean's
remote interface. The remote interface defines the business methods like
accessor and mutator methods for changing a customer's name, or business methods
that perform tasks like using the HotelClerk bean to reserve a room at a hotel.
Below is an example of how a Customer bean might be accessed from a client
application.