Java Reference
In-Depth Information
Note
Many of the preceding steps can be performed automatically by a variety of IDEs. Don't
write everything from scratch.
Although your favorite IDE may complete many steps for you, let's go through these steps manually in
the development of our stateless session bean example. To keep things less confusing, a naming
convention should be adopted for all the Java classes involved. A commonly accepted naming
convention is listed in Table 20-1 .
Table 20-1: EJB Name Convention
Item
Name
Example
Remote Interface
<name>
Customer
Home Interface
<name>Home
CustomerHome
Implementation Class
<name>Bean
CustomerBean
EJB Name
<name>EJB
CustomerEJB
EJB Jar Display Name
<name>Jar
CustomerJar
Remember the first program you have ever written in Java? Is it the " Hello, world" ? The first EJB
example developed in this chapter is a stateless session bean called HelloEJB . When it is invoked, a
welcome message is delivered to the calling client.
Although the business logic is defined in the implementation class, the client can never directly access
implementation-class instances. Instead, the client calls an EJB's remote interface to get its service. In
other words, the remote interface defines the business methods that a remote client may invoke. The
bean developer defines the types of the method arguments, the return type, and the exceptions the
methods throw. The signatures of these methods must be identical to the signatures of the
corresponding methods in the EJB implementation class.
Remote interface
Every EJB remote interface extends the java.ejb.EJBObject interface. Since EJBs are meant to
work in a distributed system, the remote interface is a valid remote interface for RMI-IIOP, so each
method must throw the java.rmi.RemoteException . The source code for the HelloEJB remote
interface is shown in Listing 20-1 . Three methods are defined by which a client can get all welcome
messages, a specific welcome message, or the number of messages.
Listing 20-1: Remote interface of HelloEJB
package java_database.ch20.HelloSLBean;
import javax.ejb.*;
import java.rmi.RemoteException;
public interface Hello extends EJBObject {
public String[] getAllWelcomeMsgs() throws RemoteException;
public String getWelcomeMsg(int i) throws RemoteException;
public int getNumberOfWelcomeMsgs() throws RemoteException;
}
Home interface
Search WWH ::




Custom Search