Java Reference
In-Depth Information
 
Since the ejbCreate is called by the container, there is nothing to return so its return type is
void .
 
The create method returns the remote interface.
The bean class extends the java.ejb.SessionBean interface, which declares the ejbRemove,
ejbActivate, ejbPassivate , and setSessionContext methods. The HelloBean class does
not use these methods, but it must implement them (as empty functions). Later sections on stateful
session beans and entity beans explain the use of these methods.
EJBExceptions
The primary purpose of a session bean is to run business tasks for the client. The client invokes
business methods on the remote object reference that the create method returns. From the client's
perspective, the business methods appear to run locally, although they actually run remotely in the
application server's EJB container. All the business methods declared in the remote interface need to
be implemented. The signatures of these business methods are the same as those defined in the
remote interface. However, since the bean object is running inside of the container, it does not need to
throw the java.rmi.RemoteException .
To indicate a system-level problem, such as the inability to connect to a database, a business method
should throw javax.ejb.EJBException . When a business method throws an EJBException , the
container wraps it in a RemoteException , which is caught by the client. Since EJBException is a
subclass of RuntimeException , you do not need to explicitly include it in the throws clause of the
business method. The HelloBean class is shown in Listing 20-3 . It should be noted that the method
getWelcomeMsg(int) is coded defensively to prevent the index from going out of range.
Listing 20-3: HelloBean class
package java_database.ch20.HelloSLBean;
import javax.ejb.*;
import java.util.*;
public class HelloBean implements SessionBean
{
// instance variables
private SessionContext ctx;
private String[] msgList = new String[3];
// default constructor - different from ejbCreate()
public HelloEJBBean() {
}
// Life cycle methods called by EJB container
public void setSessionContext(SessionContext c) {
System.out.println("setSessionContext called.");
ctx = c;
}
public void ejbCreate() {
System.out.println ("ejbCreate() called.");
Search WWH ::




Custom Search