Create the following directory structure under some logical parent directory,
A D V E R T I S E M E N T
such as c:\ejb_example. This directory structure is for development purposes
only; the deployment files will be zipped into a jar file in a moment and
deployed to jboss.
src/
|__client/
| |___com/
| |___examples/
| |___client java files
|__server/
| |__com/
| |___examples/
| |___server (bean) java files
|__shared
| |__com/
| |___examples/
| |___remote and home java files
|
assemble/
|___client and server jars
|
target/
|___client/
| |___com/
| | |___examples/
| | |___client, remote and home java classes
| |___jndi.properties
|___server/
|__com/
| |___examples/
| |___server (bean), remote and home java classes
|___META-INF/
|___ejb-jar.xml
The Bean Class
create the bean class as src\server\com\examples\HelloBean.java.
This class is a stateless session bean and contains our business logic. In this
case it prints out the exciting and always useful "Hello! World".
package com.examples;
import javax.ejb.*;
public class HelloBean implements SessionBean
{
public void ejbCreate() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void ejbRemove() {}
public void setSessionContext(SessionContext sc){}
public String sayHello ()
{
System.out.println ("Someone called sayHello()");
return �Hello! World�;
}
}
The Remote Interface
Create the remote interface as src\shared\com\examples\Hello.java. This is the interface that remote clients talk to instead of talking directly to the bean class, HelloBean. Notice that its only method is sayHello(), which is the business method in HelloBean.
package com.examples;
import javax.ejb.*;
import java.rmi.*;
public interface Hello extends EJBObject
{
public String sayHello() throws RemoteException;
}
The Home Interface
Create the home interface as src\shared\com\examples\HelloHome.java. This
interface will be used to create an instance of the Hello interface when we want
to execute the sayHello() business logic.
package com.examples;
import javax.ejb.*;
import java.rmi.*;
public interface Hello extends EJBObject
{
public String sayHello() throws RemoteException;
}
The Client
create the client as src\client\com\examples\HelloClient.java. This is the
"remote" client that will use our session bean. First, it uses the
InitialContext to get a handle to HelloHome. It then uses the HelloHome
interface to create hello. hello is a remote interface representing our bean
class. After hello is used to execute the business logic (sayHello), the bean is
released via the remove method.
Create the ejb-jar.xml file in target\server\META-INF. This deployment descriptor contains information that the jboss EJB container needs in order to deploy and run our EJB.