Cover | Table of Contents
The Enterprise JavaBeans architecture is a component architecture for the development and deployment of component-based distributed business applications. Applications written using the Enterprise JavaBeans architecture are scalable, transactional, and multi-user secure. These applications may be written once, and then deployed on any server platform that supports the Enterprise JavaBeans specification.
Enterprise JavaBeans is a standard server-side component model for component transaction monitors.
Person would have matching
Person_Stub and Person_Skeleton
classes. As shown in Figure 1.2, the object server
and skeleton reside on the middle tier, and the stub resides on the
client.javax.ejb
packages, developers can create, assemble, and deploy components that
conform to the EJB specification.java.beans package in the core Java API, is also a
component model, but it's not a server-side component model
like EJB. In fact, other than sharing the name
"JavaBeans," these two component models are completely
unrelated. A lot of the literature has referred to EJB as an
extension of the original JavaBeans, but this is a misrepresentation.
Other than the shared name, and the fact that they are both Java
component models, the two APIs serve very different purposes. EJB
does not extend or use the original JavaBeans component model.java.beans package) is
intended to be used for intraprocess purposes,
while EJB (the javax.ejb package) is designed to
be used for interprocess components. In other
words, the original JavaBeans was not intended for distributed
components. JavaBeans can be used to solve a variety of problems, but
is primarily used to build
clients by assembling visual (GUI) and
nonvisual widgets. It's an excellent component model, possibly
the best component model for intraprocess
development ever devised, but it's not a server-side component
model. EJB is designed to address issues involved with managing
distributed business objects in a three-tier architecture.CruiseHome cruiseHome = ... getCruiseHome();
// Get the cruise id from a text field.
String cruiseID = textFields1.getText();
// Create an EJB primary key from the cruise id.
CruisePrimaryKey pk = new CruisePrimaryKey(cruiseID);
// Use the primary key to find the cruise.
Cruise cruise = cruiseHome.findByPrimaryKey(pk);
// Set text field 2 to show the cruise name.
textField2.setText(cruise.getName());
// Get a remote reference to the ship that will be used
// for the cruise from the cruise bean.
Ship ship = cruise.getShip();
// Set text field 3 to show the ship's name.
textField3.setText(ship.getName());
// Get a list of all the cabins on the ship as remote references
// to the cabin beans.
Cabin [] cabins = ship.getCabins();
// Iterate through the enumeration, adding the name of each cabin
// to a list box.
for (int i = 0; i < cabins.length; i++){
Cabin cabin = cabins[i];
listBox1.addItem(cabin.getName());
}EntityBean and
SessionBean interfaces are the bases of this
component model. As we learned earlier, these interfaces provide
callback methods that notify the bean class of state management
events in its life cycle. At runtime, the container invokes the
callback methods on the bean instance when appropriate state
management events occur. When the container is about to write an
entity bean instance's state to the database, for example, it
first calls the bean instance's ejbStore()
method. This provides the bean instance with an opportunity to do
some clean up on its state just before it's written to the
database. The
ejbLoad() method is called just after the
bean's state is populated from the database, providing the bean
developer with an opportunity to manage the bean's state before
the first business method is called. Other callback methods can be used by the bean class in a
similar fashion. EJB defines when these various callback methods are
invoked and what can be done within their context. This provides the
bean developer with a predictable runtime component model.EJBObject and EJBHome stubs,
which are connected to the EJB object and EJB homes respectively. The
EJB object implements the remote interface and expands the bean
class's functionality. The EJB home implements the home
interface and works closely with the container to create, locate, and
remove beans.EJBContext, and the JNDI environment context (EJB
1.1 only). The callback methods notify the bean class that it is
involved in state management event. The EJBContext
and JNDI environment context provides the bean instance with
information about its environment. The container-server contract is
not well defined and remains proprietary at this time. Future
versions of EJB may specify the container-server
contract.javax.ejb and for EJB 1.0
javax.ejb and for EJB 1.0
javax.ejb.deploymentpackage com.titan.cabin;
import java.rmi.RemoteException;
public interface Cabin extends javax.ejb.EJBObject {
public String getName() throws RemoteException;
public void setName(String str) throws RemoteException;
public int getDeckLevel() throws RemoteException;
public void setDeckLevel(int level) throws RemoteException;
public int getShip() throws RemoteException;
public void setShip(int sp) throws RemoteException;
public int getBedCount() throws RemoteException;
public void setBedCount(int bc) throws RemoteException;
}Cabin interface defines four properties: the
name, deckLevel,
ship, and bedCount.
Properties are attributes of a bean that can be
accessed by public set and get methods. The methods that access these
properties are not explicitly defined in the Cabin
interface, but the interface clearly specifies that these attributes
are readable and changeable by a client.Cabin interface a
part of a new package named com.titan.cabin. In
this book, we place all the classes and interfaces associated with
each type of bean in a package specific to the bean. Because our
beans are for the use of the Titan cruise line, we place these
packages in the InitialContext, which it then used to get a remote
reference to the homes of the Cabin and TravelAgent beans. The
InitialContext is part of a larger API called the
Java Naming and Directory Interface ( JNDI). We use JNDI to look up
an EJB home in an EJB server just like you might use a phone book to
find the home number of a friend or business associate.InitialContext, which it then used to get a remote
reference to the homes of the Cabin and TravelAgent beans. The
InitialContext is part of a larger API called the
Java Naming and Directory Interface ( JNDI). We use JNDI to look up
an EJB home in an EJB server just like you might use a phone book to
find the home number of a friend or business associate.Handle or HomeHandle objects, or some other proprietary pointer type, which can be used to preserve the bean reference in the database. The container will manage this conversion from remote reference to persistent pointer and back automatically.ejbLoad() and
ejbStore() appropriately. In addition, you must
explicitly develop the find methods defined in the bean's home
interface.ShipBean
class and the deployment descriptor.
EntityBean
interface. Bean instances must
implement the EntityBean interface, which means
that invocations of the
callback methods are
invocations on the bean instance itse