Skip Headers
Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide
10g (10.1.3.5.0)

Part Number E13981-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

Implementing an EJB 2.1 Stateless Session Bean

Table 11-1 summarizes the important parts of an EJB 2.1 stateless session bean and the following procedure describes how to implement these parts. For a typical implementation, see "Using Java". For more information, see "What is a Stateless Session Bean?".

Table 11-1 Parts of an EJB 2.1 Stateless Session Bean

Part Description

Home Interface (remote or local)

Extends javax.ejb.EJBHome and javax.ejb.EJBLocalHome and requires a single create() factory method, with no arguments, and a single remove() method.

Component Interface (remote or local)

Extends javax.ejb.EJBObject for the remote interface and javax.ejb.EJBLocalObject for the local interface. It defines the business logic methods, which are implemented in the bean implementation.

TimedObject Interface

Optionally implements the javax.ejb.TimedObject interface. For more information, see "Understanding EJB Timer Services").

Bean implementation

Implements SessionBean. This class must be declared as public, contain a public, empty, default constructor, no finalize() method, and implements the methods defined in the component interface. Must contain a single ejbCreate method, with no arguments, to match the create() method in the home interface. Contains empty implementations for the container service methods, such as ejbRemove, and so on.


  1. Create the home interfaces for the bean (see "Implementing the Home Interfaces").

    The remote home interface defines the create method that a client can invoke remotely to instantiate your bean. The local home interface defines the create method that a collocated bean can invoke locally to instantiate your bean.

    1. To create the remote home interface, extend javax.ejb.EJBHome (see "Implementing the Remote Home Interface").

    2. To create the local home interface, extend javax.ejb.EJBLocalHome (see "Implementing the Local Home Interface").

  2. Create the component interfaces for the bean (see "Implementing the Component Interfaces").

    The remote component interface declares the business methods that a client can invoke remotely. The local interface declares the business methods that a collocated bean can invoke locally.

    1. To create the remote component interface, extend javax.ejb.EJBObject (see "Implementing the Remote Component Interface").

    2. To create the local component interface, extend javax.ejb.EJBLocalObject (see "Implementing the Local Component Interface").

  3. Implement the stateless session bean as follows:

    1. Implement a single ejbCreate method with no parameter that matches the home interface create method.

    2. Implement the business methods that you declared in the home and component interfaces.

    3. Implement the javax.ejb.SessionBean interface to implement the container callback methods it defines (see "Configuring a Life Cycle Callback Method for an EJB 2.1 Session Bean").

    4. Implement a setSessionContext method that takes an instance of SessionContext (see "Implementing the setSessionContext Method").

      For a stateless session bean, this method usually does nothing (does not actually add the SessionContext to the session bean's state).

  4. Configure your ejb-jar.xml file to match your bean implementation (see "Using Deployment XML").

Using Java

Example 11-1 shows a typical implementation of a stateless session bean.

Example 11-1 EJB 2.1 Stateless Session Bean Implementation

package hello;
import javax.ejb.*;

public class HelloBean implements SessionBean {

    /* ----------------------------------------
    * Begin business methods. The following methods
    * are called by the client code.
    * -------------------------------------- */

    public String sayHello(String myName) throws EJBException {
        return ("Hello " + myName);
    }

    /* ----------------------------------------
    * Begin private methods. The following methods
    * are used internally
    * -------------------------------------- */

...

    /* ------------------------------------------------------
    * Begin EJB-required methods. The following methods are called
    * by the container, and never called by client code
    * ------------------------------------------------------- */

    public void ejbCreate() throws CreateException {
        // when bean is created
    }

    public void setSessionContext(SessionContext ctx) {
    }

// Life Cycle Methods

    public void ejbActivate() {
    }

    public void ejbPassivate() {
    }

    public void ejbCreate() {
    }

    public void ejbRemove() {
    }
}

Using Deployment XML

Example 11-2 shows the ejb-jar.xml session element corresponding to the stateless session bean shown in Example 11-1.

Example 11-2 ejb-jar.xml For a Stateless Session Bean

...
    <enterprise-beans>
        <session>
            <ejb-name>Hello</ejb-name>
            <home>hello.HelloHome</home>
            <remote>hello.Hello</remote>
            <ejb-class>hello.HelloBean</ejb-class>
            <session-type>Stateless</session-type>
            <transaction-type>Container</transaction-type>
        </session>
    </enterprise-beans>
...

For more information on deployment files, see "Configuring Deployment Descriptor Files".