Chapter 2. Architectural Overview

As you learned in Chapter 1, Enterprise JavaBeans is a component model for component transaction monitors, which are the most advanced type of business application servers available today. To effectively use Enterprise JavaBeans, you need to understand the EJB architecture, so this book includes two chapters on the subject. This chapter explores the core of EJB: how enterprise beans are distributed as business objects. Chapter 3 explores the services and resource-management techniques supported by EJB.

To be truly versatile, the EJB component design had to be smart. For application developers, assembling enterprise beans is simple, requiring little or no expertise in the complex system-level issues that often plague three-tier development efforts. While EJB makes the process easy for application developers, it also provides system developers (the people who write EJB servers) with a great deal of flexibility in how they support the EJB specification.

The similarities among different CTMs allow the EJB abstraction to be a standard component model for all of them. Each vendor’s CTM is implemented differently, but they all support the same primary services and similar resource-management techniques. These services and techniques are covered in more detail in Chapter 3, but some of the infrastructure for supporting them is addressed in this chapter.

The Enterprise Bean Component

Enterprise JavaBeans server-side components come in three fundamentally different types: entity, session, and message-driven beans. Both session and entity beans are RMI-based server-side components that are accessed using distributed object protocols. The message-driven bean, which is new to EJB 2.0, is an asynchronous server-side component that responds to JMS asynchronous messages.

A good rule of thumb is that entity beans model business concepts that can be expressed as nouns. For example, an entity bean might represent a customer, a piece of equipment, an item in inventory, or even a place. In other words, entity beans model real-world objects; these objects are usually persistent records in some kind of database. Our hypothetical cruise line will need entity beans that represent cabins, customers, ships, etc.

Session beans are an extension of the client application and are responsible for managing processes or tasks. A Ship bean provides methods for doing things directly to a ship but doesn’t say anything about the context under which those actions are taken. Booking passengers on the ship requires that we use a Ship bean, but it also requires a lot of things that have nothing to do with the ship itself: we’ll need to know about passengers, ticket rates, schedules, and so on. A session bean is responsible for this kind of coordination. Session beans tend to manage particular kinds of activities, such as the act of making a reservation. They have a lot to do with the relationships between different enterprise beans. A TravelAgent session bean, for example, might make use of a Cruise, a Cabin, and a Customer—all entity beans—to make a reservation.

Similarly, message-driven beans in EJB 2.0 are responsible for coordinating tasks involving other session and entity beans. The major difference between a message-driven bean and a session bean is how they are accessed. While a session bean provides a remote interface that defines which methods can be invoked, a message-driven bean does not. Instead, the message-driven bean subscribes to or listens for specific asynchronous messages to which it responds by processing the message and managing the actions other beans take in response to those messages. For example, a ReservationProcessor message-driven bean would receive asynchronous messages—perhaps from a legacy reservation system—from which it would coordinate the interactions of the Cruise, Cabin, and Customer beans to make a reservation.

The activity a session or message-driven bean represents is fundamentally transient: you start making a reservation, you do a bunch of work, and then it’s finished. The session and message-driven beans do not represent things in the database. Obviously, session and message-driven beans have lots of side effects on the database: in the process of making a reservation, you might create a new Reservation by assigning a Customer to a particular Cabin on a particular Ship. All of these changes would be reflected in the database by actions on the respective entity beans. Session and message-driven beans like TravelAgent and ReservationProcessor, which are responsible for making a reservation on a cruise, can even access a database directly and perform reads, updates, and deletes to data. But there’s no TravelAgent or ReservationProcessor record in the database—once the bean has made the reservation, it waits to process another.

What makes the distinction between the different types of beans difficult to understand is that it’s extremely flexible. The relevant distinction for Enterprise JavaBeans is that an entity bean has persistent state; session and message-driven beans model interactions but do not have persistent state.

Classes and Interfaces

A good way to understand the design of enterprise beans is to look at how you’d go about implementing one. To implement entity and session enterprise beans, you need to define the component interfaces,[7] a bean class, and a primary key:

Remote interface

The remote interface defines the bean’s business methods that can be accessed from applications outside the EJB container: the business methods a bean presents to the outside world to do its work. It enforces conventions and idioms that are well suited for distributed object protocols. The remote interface extends javax.ejb.EJBObject, which in turn extends java.rmi.Remote. It is used by session and entity beans in conjunction with the remote home interface.

Remote home interface

The home interface defines the bean’s life-cycle methods that can be accessed from applications outside the EJB container: the life-cycle methods for creating new beans, removing beans, and finding beans. It enforces conventions and idioms that are well suited for distributed object protocols. The home interface extends javax.ejb.EJBHome , which in turn extends java.rmi.Remote. It is used by session and entity beans in conjunction with the remote interface.

EJB 2.0: Local interface

The local interface for an enterprise bean defines the bean’s business methods that can be used by other beans co-located in the same EJB container: the business methods a bean presents to other beans in the same address space. It allows beans to interact without the overhead of a distributed object protocol, which improves their performance. The local interface extends javax.ejb.EJBLocalObject. It is used by session and entity beans in conjunction with the local home interface.

EJB 2.0: Local home interface

The local home interface defines the bean’s life-cycle methods that can be used by other beans co-located in the same EJB container: that is, the life-cycle methods a bean presents to other beans in the same address space. It allows beans to interact without the overhead of a distributed object protocol, which improves their performance. The local home interface extends javax.ejb.EJBLocalHome. It is used by session and entity beans in conjunction with the local interface.

Bean class

The session and entity bean classes actually implement the bean’s business and life-cycle methods. Note that the bean class for session and entity beans usually does not implement any of the bean’s component interfaces directly. However, it must have methods matching the signatures of the methods defined in the remote and local interfaces and must have methods corresponding to some of the methods in both the remote and local home interfaces. If this sounds perfectly confusing, it is. The book will clarify this as we go along. An entity bean must implement javax.ejb.EntityBean; a session bean must implement javax.ejb.SessionBean. The EntityBean and SessionBean extend javax.ejb.EnterpriseBean.

The message-driven bean in EJB 2.0 does not use any of the component interfaces, because it is never accessed by method calls from other applications or beans. Instead, the message-driven bean contains a single method, onMessage(), which is called by the container when a new message arrives. The message-driven bean does not have a component interface as does the session and entity beans; it only needs only the bean class to operate. The message-driven bean class implements the javax.ejb.MessageDrivenBean and javax.jms.MessageListener interfaces. The JMS MessageListener interface is what makes a message-driven bean specific to JMS, instead of some other protocol. EJB 2.0 requires the use of JMS, but future versions may allow other messaging systems. The MessageDrivenBean, like the EntityBean and the SessionBean, extends the javax.ejb.EnterpriseBean interface.

Primary key

The primary key is a very simple class that provides a pointer into the database. Only entity beans need a primary key. The only requirement for this class is that it implements java.io.Serializable.

EJB 2.0 adds the crucial distinction between remote and local interfaces. Local interfaces provide a way for beans in the same container to interact efficiently; calls to methods in the local interface don’t involve RMI; the methods in the local interfaces don’t need to declare that they throw RemoteException, and so on. An enterprise bean isn’t required to provide a local interface if you know when you’re developing the bean that it will interact only with remote clients. Likewise, an enterprise bean doesn’t need to provide a remote interface if you know it will be called only by enterprise beans in the same container. You can provide either a local or a remote component interface, or both.

The complexity—particularly all the confusion about classes implementing the methods of an interface but not implementing the interface itself—comes about because enterprise beans exist in the middle between some kind of client software and some kind of database. The client never interacts with a bean class directly; it always uses the methods of the entity or session bean’s component interfaces to do its work, interacting with stubs that are generated automatically. (For that matter, a bean that needs the services of another bean is just another client: it uses the same stubs, rather than interacting with the bean class directly.)

Although the local component interfaces (local and local home) in EJB 2.0 represent session and entity beans in the same address space and do not use distributed object protocols, they still represent a stub or a proxy to the bean class. While there is no network between co-located beans, the stubs allow the container to monitor the interactions between the beans and apply security and transactions as appropriate.

It’s important to note that EJB 2.0’s message-driven bean doesn’t have any component interfaces, but it may become the client of other session or entity beans and interact with those beans through their component interfaces. The entity and session beans with which the message-driven bean interacts may be co-located, in which case it uses their local component interfaces, or they may be located in a different address space and EJB container, in which case the remote component interfaces are used.

There are also many interactions between an enterprise bean and its server. These interactions are managed by a container, which is responsible for presenting a uniform interface between the bean and the server. (Many people use the terms “container” and "server” interchangeably, which is understandable because the difference between them isn’t clearly defined.) The container is responsible for creating new instances of beans, making sure they are stored properly by the server, and so on. Tools provided by the container’s vendor do a tremendous amount of work behind the scenes. At least one tool takes care of creating the mapping between entity beans and records in the database. Other tools generate code based on the component interfaces and the bean class itself. The code generated does things like create the bean, store it in the database, and so on. This code (in addition to the stubs) is what actually implements the component interfaces, and it is the reason the bean class doesn’t have to do so.

Naming conventions

Before going on, let’s establish some conventions. When we speak about an enterprise bean as a whole—its component interfaces, bean class, and so forth—we will call it by its common business name, followed by the acronym " EJB.” For example, an enterprise bean that is developed to model a cabin on a ship will be called the “Cabin EJB.” Notice that we don’t use a constant-width font for “Cabin.” This is because we are referring to all the parts of the bean (the component interfaces, bean class, etc.) as a whole, not just to one particular part, such as the remote interface or bean class. The term enterprise bean or bean denotes any kind of bean, including entity, session, and message-driven beans. Entity bean denotes an entity-type enterprise bean; session bean denotes a session-type enterprise bean; and message-driven bean denotes a message driven-type enterprise bean. It’s popular to use the acronym EJB for enterprise bean, a style adopted in this book.

We will also use suffixes to distinguish between local component interfaces and remote component interfaces. When we are talking about the remote interface of the Cabin EJB we will combine the common business name with the word Remote. For example, the remote interface for the Cabin EJB is called the CabinRemote interface. In EJB 2.0, the local component interface of the Cabin EJB would be the CabinLocal interface. The home interfaces follow the convention by adding the word Home to the mix. The remote and local home interfaces for the Cabin EJB would be CabinHomeRemote and CabinHomeLocal, respectively. The bean class is always the common business name followed by the word Bean. For example, the Cabin EJB’s bean class would be named CabinBean.

The remote interface

Having introduced the machinery, let’s look at how to build an entity or stateful enterprise bean with remote component interfaces. In this section, we will examine the Cabin EJB, an entity bean that models a cabin on a cruise ship. Let’s start with its remote interface.

We’ll define the remote interface for a Cabin bean using the CabinRemote interface, which defines business methods for working with cabins. All remote-interface types extend the javax.ejb.EJBObject interface:

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;
}

These are methods for naming the cabin and methods for setting the cabin’s deck level; you can probably imagine lots of other methods that you’d need, but this is enough to get started. All of these methods declare that they throw RemoteException, which is required of all methods on remote component interfaces, but not EJB 2.0’s local component interfaces. EJB requires the use of Java RMI-IIOP conventions with remote component interfaces, although the underlying protocol can be CORBA IIOP, Java Remote Method Protocol ( JRMP), or some other protocol. Java RMI-IIOP will be discussed in more detail in the next chapter.

The remote home interface

The remote home interface defines life-cycle methods used by clients of entity and session beans for locating enterprise beans. The remote home interface extends javax.ejb.EJBHome . We’ll call the home interface for the Cabin bean CabinHomeRemote and define it like this:

import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.FinderException;

public interface CabinHomeRemote extends javax.ejb.EJBHome {
    public CabinRemote create(Integer id) 
        throws CreateException, RemoteException;
    public CabinRemote findByPrimaryKey(Integer pk)
        throws FinderException, RemoteException;
}

The create() method is responsible for initializing an instance of our bean. If your application needs them, you can provide other create() methods with different arguments.

In addition to findByPrimaryKey(), you are free to define other methods that provide convenient ways to look up Cabin beans—for example, you might want to define a method called findByShip() that returns all the cabins on a particular ship. Find methods like these are used in entity beans but not in session beans and obviously not in message-driven beans.

EJB 2.0: The bean class

Now let’s look at an actual entity bean. Here’s the code for the CabinBean; it’s a sparse implementation, but it will show you how the pieces fit together:

import javax.ejb.EntityContext;

public abstract class CabinBean implements javax.ejb.EntityBean {

    public Integer ejbCreate(Integer id){
        setId(id);
        return null;
    }
    public void ejbPostCreate(Integer id){
        // do nothing
    }

    public abstract String getName();
    public abstract void setName(String str);

    public abstract int getDeckLevel();
    public abstract void setDeckLevel(int level);

    public abstract Integer getId();
    public abstract void setId(Integer id);

    public void setEntityContext(EntityContext ctx){
        // not implemented
    }
    public void unsetEntityContext(){
        // not implemented
    }
    public void ejbActivate(){
        // not implemented
    }
    public void ejbPassivate(){
        // not implemented
    }
    public void ejbLoad(){
        // not implemented
    }
    public void ejbStore(){
        // not implemented
    }
    public void ejbRemove(){
        // not implemented
    }
}

Notice that the CabinBean class is declared as abstract, as are several of its methods that access or update the EJB’s persistent state. Also notice that there are no instance fields to hold the state information these methods access. This is because we are working with a container-managed entity bean, which has its abstract methods implemented by the container system automatically—this will be explained in detail later in the book. EJB 2.0 container-managed entity beans are the only beans that are declared as abstract with abstract accessor methods. You won’t see abstract classes and methods with other types of entity beans, session beans, or message-driven beans.

EJB 1.1: The bean class

Here’s the code for the CabinBean in EJB 1.1:

import javax.ejb.EntityContext;

public class CabinBean implements javax.ejb.EntityBean {

    public Integer id;
    public String name;
    public int deckLevel;
 
    public Integer ejbCreate(Integer id){
        setId(id);
        return null;
    }
    public void ejbPostCreate(Integer id){
        // do nothing
    }

    public String getName(){
        return name;
    }    
    public void setName(String str){
        name = str;
    }

    public int getDeckLevel(){
        return deckLevel;
    }
    public void setDeckLevel(int level){
        deckLevel = level;
    }
    public Integer getId(){
        return id;
    }
    public void setId(Integer id){
        this.id = id;
    }
    public void setEntityContext(EntityContext ctx){
        // not implemented
    }
    public void unsetEntityContext(){
        // not implemented
    }
    public void ejbActivate(){
        // not implemented
    }
    public void ejbPassivate(){
        // not implemented
    }
    public void ejbLoad(){
        // not implemented
    }
    public void ejbStore(){
        // not implemented
    }
    public void ejbRemove(){
        // not implemented
    }
}

EJB 2.0 and 1.1: The bean class

The set and get methods for the cabin’s name and deck level are the CabinBean’s business methods; they match the business methods defined by the EJB’s remote interface, CabinRemote. The CabinBean class has state and business behavior that models the concept of a cabin. The business methods are the only methods visible to the client application; the other methods are visible only to the EJB container or the bean class itself. For example, the setId()/ getId() methods are defined in the bean class but not in the remote interface, which means they cannot be called by the entity bean’s client. The other methods are required by the EJB component model and are not part of the bean class’s public business definition.

The ejbCreate() and ejbPostCreate() methods initialize the instance of the bean class when a new cabin record is to be added to the database. The last seven methods in the CabinBean are defined in the javax.ejb.EntityBean interface. These methods are life-cycle callback methods. The EJB container invokes these callback methods on the bean class when important life-cycle events occur. The ejbRemove() method, for example, notifies an entity bean that its data is about to be deleted from the database. The ejbLoad() and ejbStore() methods notify the bean instance that its state is being read or written to the database. The ejbActivate() and ejbPassivate() methods notify the bean instance that it is about to be activated or deactivated, a process that conserves memory and other resources. setEntityContext() provides the bean with an interface to the EJB container that allows the bean class to get information about itself and its surroundings. unsetEntityContext() is called by the EJB container to notify the bean instance that it is about to be dereferenced for garbage collection.

All these callback methods provide the bean class with notifications of when an action is about to be taken, or was just taken, on the bean’s behalf by the EJB server. These notifications simply inform the bean of an event; the bean doesn’t have to do anything about it. The callback notifications tell the bean where it is during its life cycle, when it is about to be loaded, removed, deactivated, and so on. Most of the callback methods pertain to persistence, which can be done automatically for the bean class by the EJB container. Because the callback methods are defined in the javax.ejb.EntityBean interface, the entity bean class must implement them, but it isn’t required to do anything meaningful with the methods if it doesn’t need to. Our bean, the CabinBean, won’t need to do anything when these callback methods are invoked, so these methods are empty implementations. Details about these callback methods, when they are called, and how a bean should react to them are covered in Chapter 11.

The primary key

The primary key is a pointer that helps locate data that describes a unique record or entity in the database; it is used in the findByPrimaryKey() method of the home interface to locate a specific entity. Primary keys are defined by the bean developer and must be some type of serializable object. The Cabin EJB uses a simple java.lang.Integer type as its primary key. Its also possible to define custom primary keys, called compound primary keys , which represent complex primary keys consisting of several different fields. Primary keys are covered in detail in Chapter 11.

What about session beans?

CabinBean is an entity bean, but a session bean wouldn’t be all that different. It would extend SessionBean instead of EntityBean and would have an ejbCreate() method that would initialize the bean’s state, but no ejbPostCreate(). Session beans do not have an ejbLoad() or ejbStore() method, because session beans are not persistent. While session beans have a setSessionContext() method, they do not have an unsetSessionContext() method. Session beans do have ejbActivate() and ejbPassivate() methods, which are used by stateful session beans to manage conversational state. Finally, session beans would provide an ejbRemove() method, which would be called to notify the bean that the client no longer needs it. However, this method wouldn’t tell the bean that its data was about to be removed from the database, because a session bean doesn’t represent data in the database.

Session beans don’t have a primary key. That’s because session beans are not persistent themselves, so there is no need for a key that maps to the database. Session beans are covered in detail in Chapter 12.

EJB 2.0: What about message-driven beans?

Remote, local, and home interfaces would not be defined for a message-driven bean, because message-driven beans do not have component interfaces. Instead, the message-driven bean would define only a few callback methods and no business methods. The callback methods include the ejbCreate() method, which is called when the bean class is first created; the ejbRemove() method, called when the bean instance is about to be discarded from the system (usually when the container doesn’t need it any longer); the setMessageDrivenBeanContext(); and the onMessage() method. The onMessage() method is called every time a new asynchronous message is delivered to the message-driven bean. The message-driven bean doesn’t define the ejbPassivate()/ejbActivate() or ejbLoad()/ejbStore() methods because it doesn’t need them.

Message-driven beans don’t have a primary key, for the same reason that session beans don’t. They are not persistent, so there is no need for a key to the database. Message-driven beans are covered in detail in Chapter 13.

Deployment Descriptors and JAR Files

Much of the information about how beans are managed at runtime is not addressed by the interfaces and classes discussed previously. You may have noticed, for example, that we didn’t talk about how beans interact with security, transactions, naming, and other services common to distributed object systems. As you know from prior discussions, these types of primary services are handled automatically by the EJB container, but the EJB container still needs to know how to apply the primary services to each bean class at runtime. To do this, we use deployment descriptors.

Deployment descriptors serve a function very similar to property files. They allow us to customize the behavior of software (enterprise beans) at runtime without having to change the software itself. Property files are often used with applications, but deployment descriptors are specific to an enterprise bean. Deployment descriptors are also similar in purpose to property sheets used in Visual Basic and PowerBuilder. Where property sheets allow us to describe the runtime attributes of visual widgets (background color, font size, etc.), deployment descriptors allow us to describe runtime attributes of server-side components (security, transactional context, etc.). Deployment descriptors allow certain runtime behaviors of beans to be customized without altering the bean class or its interfaces.

When a bean class and its interfaces have been defined, a deployment descriptor for the bean is created and populated with data about the bean. Frequently, integrated development environments ( IDEs) that support development of Enterprise JavaBeans will allow developers to graphically set up the deployment descriptors using visual utilities like property sheets. After the developer has set all the properties for a bean, the deployment descriptor is saved to a file. Once the deployment descriptor is completed and saved to a file, the bean can be packaged in a JAR file for deployment.

JAR ( Java ARchive) files are ZIP files that are used specifically for packaging Java classes (and other resources such as images) that are ready to be used in some type of application. JARs are used for packaging applets, Java applications, JavaBeans, web applications (servlets and JSPs), and Enterprise JavaBeans. A JAR file containing one or more enterprise beans includes the bean classes, component interfaces, and supporting classes for each bean. It also contains one deployment descriptor, which is used for all the beans in the JAR file. When a bean is deployed, the JAR file’s path is given to the container’s deployment tools, which read the JAR file.

When the JAR file is read at deployment time, the container tools read the deployment descriptor to learn about the bean and how it should be managed at runtime. The deployment descriptor tells the deployment tools what kind of beans are in the JAR file (session, entity, or message-driven), how they should be managed in transactions, who has access to the beans at runtime, and other runtime attributes of the beans. The person who is deploying the bean can alter some of these settings, such as transactional and security access attributes, to customize the bean for a particular application. Many container tools provide property sheets for graphically reading and altering the deployment descriptor when the bean is deployed. These graphical property sheets are similar to those used by bean developers.

The deployment descriptors help the deployment tools add beans to the EJB container. Once the bean is deployed, the properties described in the deployment descriptors will continue to be used to tell the EJB container how to manage the bean at runtime.

When Enterprise JavaBeans 1.0 was released, serializable classes were used for the deployment descriptor. Starting with Enterprise JavaBeans 1.1, the serializable deployment descriptor classes used in EJB 1.0 were dropped in favor of a more flexible file format based on the eXtensible Markup Language (XML). The XML deployment descriptors are text files structured according to a standard EJB Document Type Definition (DTD) that can be extended so the type of deployment information stored can evolve as the specification evolves. Chapter 16 provides a detailed description of XML deployment descriptors. The following sections provide a brief overview of XML deployment descriptors.

EJB 2.0: Deployment descriptor

The following deployment descriptor might be used to describe the Cabin bean in EJB 2.0:

<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD EnterpriseJavaBeans 2.0//EN" 
"http://java.sun.com/dtd/ejb-jar_2_0.dtd">

<ejb-jar>
    <enterprise-beans>
        <entity>
            <ejb-name>CabinEJB</ejb-name>
            <home>com.titan.CabinHomeRemote</home>
            <remote>com.titan.CabinRemote</remote>
            <local-home>com.titan.CabinHomeLocal</local-home>
            <local>com.titan.CabinLocal</local>
            <ejb-class>com.titan.CabinBean </ejb-class>
            <persistence-type>Container</persistence-type>
            <prim-key-class>java.lang.Integer</prim-key-class>
            <reentrant>False</reentrant>
        </entity>
    </enterprise-beans>
</ejb-jar>

EJB 1.1: Deployment descriptor

The following deployment descriptor might be used to describe the Cabin bean in EJB 1.1:

<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD EnterpriseJavaBeans 1.1//EN" 
"http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd">

<ejb-jar>
    <enterprise-beans>
        <entity>
            <ejb-name>CabinEJB</ejb-name>
            <home>com.titan.CabinHomeRemote</home>
            <remote>com.titan.CabinRemote</remote>         
            <ejb-class>com.titan.CabinBean</ejb-class>            
            <persistence-type>Container</persistence-type>
            <prim-key-class>java.lang.Integer</prim-key-class>
            <reentrant>False</reentrant>
        </entity>
    </enterprise-beans>
</ejb-jar>

EJB 2.0 and 1.1: Elements of the XML deployment descriptor

The deployment descriptor for a real bean would have a lot more information. This example simply illustrates the type of information you’ll find in an XML deployment descriptor.

The first element in any XML document is <!DOCTYPE> . This element describes the organization that defined the DTD for the XML document, the DTD’s version, and a URL location of the DTD. The DTD describes how a particular XML document is structured.

All the other elements in the XML document are specific to EJB. They do not represent all the elements used in deployment descriptors, but they illustrate the types of elements that are used. Here’s what the elements mean:

<ejb-jar>

The root of the XML deployment descriptor. All other elements must be nested below this one. It must contain one <enterprise-beans> element and may contain other optional elements.

<enterprise-beans>

Contains declarations for all the enterprise beans described by this XML document. It may contain <entity>, <session>, or <message-driven> (EJB 2.0) elements, which describe entity, session, and message-driven enterprise beans, respectively.

<entity>

Describes an entity bean and its deployment information. There must be one of these elements for every entity bean described by the XML deployment descriptor. The <session> element is used in the same way to describe a session bean. The <message-driven> element is different as it does not define any component interfaces.

<ejb-name>

The descriptive name of the enterprise bean. This is the name we use for the enterprise bean in conversation, when talking about the bean component as a whole.

<home>

The fully qualified class name of the remote home interface. This interface defines the life-cycle behaviors (create, find, remove) of the enterprise bean to its clients outside the container system.

<remote>

The fully qualified class name of the remote interface. This interface defines the enterprise bean’s business methods to its clients outside the container system.

EJB 2.0: <local-home>

The fully qualified class name of the local home interface. This interface defines the life-cycle behaviors (create, find, remove) of the enterprise bean to other co-located enterprise beans.

EJB 2.0: <local>

The fully qualified class name of the local interface. This interface defines the enterprise bean’s business methods to other co-located enterprise beans.

<ejb-class>

The fully qualified class name of the bean class. This class implements the business methods of the bean.

<prim-key-class>

The fully qualified class name of the enterprise bean’s primary key. The primary key is used to find the bean data in the database.

The last two elements in the deployment descriptor, the <persistence-type> and <reentrant> elements, express the persistence strategy and concurrency policies of the entity bean. These elements are explained in more detail later in the book.

As you progress through this book, you will be introduced to the elements that describe concepts we have not covered yet, so don’t worry about knowing all of the elements you might find in a deployment descriptor at this time.

EJB Objects and EJB Home

The entity and session beans both declare the component interfaces that their clients will use to access them. In EJB 2.0 and 1.1, clients outside the container system, like servlets or Java applications, will always use the enterprise bean’s remote component interfaces. In EJB 2.0, clients that are other enterprise beans in the same container system will usually use local component interfaces to interact. This section explains in logical terms how the component interfaces are connected to instances of the bean class at runtime.

While this discussion will help you understand entity and session beans, it doesn’t apply to EJB 2.0’s message-driven beans at all, because they do not declare component interfaces. Message-driven beans are a very different kind of animal, and a full description of these beans is left to Chapter 13.

Now that you have a basic understanding of some of the enterprise beans parts (component interfaces, bean class, and deployment descriptor) it’s time to talk a little more precisely about how these parts come together inside an EJB container system. Unfortunately, we can’t talk as precisely as we’d like. There are a number of ways for an EJB container to implement these relationships; we’ll show some of the possibilities. Specifically, we’ll talk about how the container implements the component interface of entity and session beans, so that clients, either applications outside the container or other co-located enterprise beans can interact with and invoke methods on the bean class.

The two missing pieces are the EJB object itself and the EJB home. You will probably never see the EJB home and EJB object classes because their class definitions are proprietary to the vendor’s EJB implementation and are generally not made public. This is good because it represents a separation of responsibilities along areas of expertise. As an application developer, you are intimately familiar with how your business environment works and needs to be modeled, so you will focus on creating the applications and beans that describe your business. System-level developers, the people who write EJB servers, don’t understand your business, but they do understand how to develop CTMs and support distributed objects. It makes sense for system-level developers to apply their skills to the mechanics of managing distributed objects but leave the business logic to you, the application developer. Let’s talk briefly about the EJB object and the EJB home so you understand the missing pieces in the big picture.

The EJB object

This chapter has said a lot about a bean’s remote and local interfaces, which extend the EJBObject and, for EJB 2.0, the EJBLocalObject interfaces, respectively. Who implements these interfaces? Clearly, the stub does: we understand that much. But what about the server side?

On the server side, an EJB object is an object that implements the remote and/or local (EJB 2.0) interfaces of the enterprise bean. The EJB object wraps the enterprise bean instance—that is, an instance of the enterprise bean class you’ve created (in our example, the CabinBean)—on the server and expands its functionality to include javax.ejb.EJBObject and/or javax.ejb.EJBLocalObject behavior.

You will have noticed that “and/or” is used a lot when talking about which interface the EJB object implements. That’s because enterprise beans in EJB 2.0 can declare either the local interface, remote interface, or both! Local interfaces don’t apply to EJB 1.1, so if you are working with that version, ignore references to them; they are relevant only to EJB 2.0 container systems.

In EJB 2.0, regardless of which interfaces the bean implements, we can think of the EJB object as implementing both. In reality, there may be a special EJB object for the remote interface and another special EJB object for the local interface of each enterprise bean; that depends on the how the vendor chooses to implement it. For our purposes, the term EJB object will be used to talk about the implementation of either the local or remote interfaces, or both. The functionality of these interfaces is so similar from the EJB object’s perspective that discussing separate EJB object implementations isn’t necessary.

The EJB object is generated by the utilities provided by the vendor of your EJB container and is based on the bean classes and the information provided by the deployment descriptor. The EJB object wraps the bean instance and works with the container to apply transactions, security, and other system-level operations to the bean at runtime. Chapter 3 talks more about the EJB object’s role with regard to system-level operations.

A vendor can use a number of strategies to implement the EJB object. Figure 2-1 illustrates three possibilities using the CabinRemote interface. The same implementation strategies apply to the CabinLocal and javax.ejb.EJBLocalObject interfaces.

Three ways to implement the EJB object

Figure 2-1. Three ways to implement the EJB object

In Figure 2-1(a), the EJB object is a classic wrapper because it holds a reference to the bean class and delegates the requests to the bean. Figure 2-1(b) shows that the EJB object class actually extends the bean class, adding functionality specific to the EJB container. In Figure 2-1(c), the bean class is no longer included in the model. In this case, the EJB object has both the proprietary implementation required by the EJB container and bean class method implementations that were copied from the bean class’s definition.

The EJB object design shown in Figure 2-1(a) is perhaps the most common. Throughout this book, particularly in the next chapter, we will explain how EJB works with the assumption that the EJB object wraps the bean class instance as depicted in Figure 2-1(a). But the other implementations are used; it shouldn’t make a difference which one your vendor has chosen. The bottom line is that you never really know much about the EJB object: its implementation is up to the vendor. Knowing that the EJB object exists answers a lot of questions about how enterprise beans are structured. The information that a client (including other enterprise beans) needs to know about an enterprise bean is described by the remote and home interfaces.

The EJB home

The EJB home is a lot like the EJB object. It’s another class that’s generated automatically when you install an enterprise bean in a container. It implements all the methods defined by the home interfaces (local and/or remote) and is responsible for helping the container manage the bean’s life cycle. Working closely with the EJB container, the EJB home is responsible for locating, creating, and removing enterprise beans. This may involve working with the EJB server’s resource managers and instance pooling and persistence mechanisms, the details of which are hidden from the developer.

For example, when a create method is invoked on a home interface, the EJB home creates an instance of the EJB object that references a bean instance of the appropriate type. Once the bean instance is associated with the EJB object, the instance’s matching ejbCreate() method is called. In the case of an entity bean, a new record is inserted into the database. With session beans, the instance is simply initialized. Once the ejbCreate() method has completed, the EJB home returns a remote or local reference (i.e., a stub) for the EJB object to the client. The client can then begin to work with the EJB object by invoking business methods using the stub. The stub relays the methods to the EJB object; in turn, the EJB object delegates those method calls to the bean instance.

In EJB 2.0, how does the EJB home know which type of EJB object reference (local or remote) to return? It depends on which home interface is being used. If the client invokes a create() method on the remote home interface, the EJB home will return a remote interface reference. If the client is working with a local home interface, the EJB home will return a reference implementing the local interface. EJB 2.0 requires that the return type of remote home interface methods be remote interfaces and that the return type of local home interface methods be local interfaces:

// The Cabin EJB's remote home interface
public interface CabinHomeRemote extends javax.ejb.EJBHome {
    public CabinRemote create(Integer id) 
        throws CreateException, RemoteException;
    public CabinRemote findByPrimaryKey(Integer pk)
        throws FinderException, RemoteException;
}


// The Cabin EJB's local home interface
public interface CabinHomeLocal extends javax.ejb.EJBLocalHome {
    public CabinLocal create(Integer id) 
        throws CreateException;
    public CabinLocal findByPrimaryKey(Integer pk)
        throws FinderException;
}

Figure 2-2 illustrates the architecture of EJB with the EJB home and EJB object implementing the home interface and remote or local interface, respectively. The bean class is also shown as being wrapped by the EJB object.

Throughout this book, we will consider the EJB object and EJB home as constructs that support both the remote and local component interfaces. In reality, we have no idea how the vendor chose to implement the EJB object and EJB home, since they are only logical constructs and may not have equivalent software counterparts. It’s important to remember that “EJB object” and “EJB home” are simply terms to describe the EJB container’s responsibilities for supporting the component interfaces. We have chosen to give them a more concrete description in this book purely for instructional purposes; the EJB object and EJB home implementations discussed throughout this book are to be considered illustrative and not a true representation of how these terms may be implemented.

Deploying a bean

The EJB objects and EJB homes are automatically generated during the deployment process. After the files that define the bean (the component interfaces and the bean classes) have been packaged into a JAR file, the bean is ready to be deployed; that is, it can be added to an EJB container so it can be accessed as a distributed component. During the deployment process, tools provided by the EJB container vendor generate the EJB object and EJB home classes by examining the deployment descriptor and the other interfaces and classes in the JAR file.

EJB architecture

Figure 2-2. EJB architecture



[7] There are basically two kinds of component interfaces: remote and local. The remote interfaces are supported by both EJB 2.0 and 1.1, while the local component interfaces are new in EJB 2.0 and are not supported by EJB 1.1.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset