Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide 10g (10.1.3.1.0) Part Number B28221-02 |
|
|
View PDF |
Table 11-2summarizes the important parts of an EJB 2.1 stateful 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 Stateful Session Bean?".
Table 11-2 Parts of an EJB 2.1 Stateful Session Bean
Part | Description |
---|---|
Home Interface (remote or local) |
Extends |
Component Interface (remote or local) |
Extends |
Bean implementation |
Implements |
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.
To create the remote home interface, extend javax.ejb.EJBHome
(see "Implementing the Remote Home Interface").
To create the local home interface, extend javax.ejb.EJBLocalHome
(see "Implementing the Local Home Interface").
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.
To create the remote component interface, extend javax.ejb.EJBObject
(see "Implementing the Remote Component Interface").
To create the local component interface, extend javax.ejb.EJBLocalObject
(see "Implementing the Local Component Interface").
Implement the stateless session bean as follows:
Implement the ejb<METHOD>
methods that match the home interface create
methods.
For a stateful session bean, provide ejbCreate
methods with corresponding argument lists for each create
method in the home interface.
Implement the business methods that you declared in the home and component interfaces.
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").
Implement a setSessionContext
method that takes an instance of SessionContext
(see "Implementing the setSessionContext Method").
For a stateful session bean, this method usually adds the SessionContext
to the session bean's state.
Configure your ejb-jar.xml
file to match your bean implementation (see "Using Deployment XML").
Example 11-3 shows a typical implementation of a stateful session bean.
Example 11-3 EJB 2.1 Stateful Session Bean Implementation
package hello; import javax.ejb.*; public class HelloBean implements SessionBean { /* ---------------------------------------- * State * -------------------------------------- */ private SessionContext ctx; private Collection messages; private String defaultMessage = "Hello, World!"; /* ---------------------------------------- * Begin business methods. The following methods * are called by the client code. * -------------------------------------- */ public String sayHello(String myName) throws EJBException { return ("Hello " + myName); } public String sayHello() throws EJBException { return defaultMessage; } /* ---------------------------------------- * 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 ejbCreate(String message) throws CreateException { this.defaultMessage = message; } public void ejbCreate(Collection messages) throws CreateException { this.messages = messages; } public void setSessionContext(SessionContext ctx) { this.ctx = ctx; } // Life Cycle Methods public void ejbActivate() { } public void ejbPassivate() { } public void ejbCreate() { } public void ejbRemove() { } }
Example 11-4 shows the ejb-jar.xml session element corresponding to the stateful session bean shown in Example 11-3.
Example 11-4 ejb-jar.xml For a Stateful 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>Stateful</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> ...
For more information on deployment files, see "Configuring Deployment Descriptor Files".