Cover | Table of Contents | Colophon
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 business
object and skeleton reside on the middle tier, and the stub resides
on the client.
javax.ejb package, developers can create,
assemble, and deploy components that conform to the EJB
specification.
CruiseHomeRemote cruiseHome = ... use JNDI to get the home
// Get the cruise ID from a text field.
String cruiseID = textFields1.getText();
// Create an EJB primary key from the cruise ID.
Integer pk = new Integer(cruiseID);
// Use the primary key to find the cruise.
CruiseRemote 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.
ShipRemote ship = cruise.getShip();
// Set text field 3 to show the ship's name.
textField3.setText(ship.getName());
// Get all the cabins on the ship.
Collection cabins = ship.getCabins();
Iterator cabinItr = cabins.iterator();
// Iterate through the enumeration, adding the name of each cabin
// to a list box.
while( cabinItr.hasNext())
CabinRemote cabin = (CabinRemote)cabinItr.next();
listBox1.addItem(cabin.getName());
}
EntityBean,
SessionBean
, and
MessageDrivenBean
(EJB 2.0) interfaces are the bases of
this component model. As we learned earlier, these interfaces provide
callback methods that notify the bean class of life-cycle events. At
runtime, the container invokes the callback methods on the bean
instance when appropriate life-cycle 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 cleanup 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 contexts. This provides the bean developer
with a predictable runtime component model.
EJBContext, and the JNDI environment naming
context. The callback methods notify the bean class that it is
involved in a life-cycle event. The EJBContext and
JNDI environment naming context provide 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
.
http://www.oreilly.com/catalog/entjbeans3/workbooks.http://www.oreilly.com/catalog/entjbeans3/workbooks.package com.titan.cabin;
import java.rmi.RemoteException;
public interface CabinRemote 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 getShipId() throws RemoteException;
public void setShipId(int sp) throws RemoteException;
public int getBedCount() throws RemoteException;
public void setBedCount(int bc) throws RemoteException;
}
CabinRemote interface defines four properties:
the name, deckLevel,
shipId, and bedCount.
Properties
are attributes of an enterprise bean that can be accessed by public
set and get methods. The methods that access these properties are not
explicitly defined in the CabinRemote interface,
but the interface clearly specifies that these attributes are
readable and changeable by a client.
CabinRemote interface
a part of a new package named com.titan.cabin.
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 placed these packages in the
InitialContext, which it then used to get a
remote reference to the homes of the Cabin and TravelAgent EJBs. 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 EJBs. 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.