The Directory Structure
|
Die folgende Verzeichnisstruktur unter irgendeinem logischem Elternteilverzeichnis, wie c:\ejb _example verursachen. Diese Verzeichnisstruktur ist nur zu den Entwicklung Zwecken; die Entwicklungakten werden in eine Glasakte in einem Moment Rei�verschluss zugemacht und entfaltet zu den 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.
|
package com.examples;
import javax.ejb.*;
import java.rmi.*;
import javax.rmi.*;
import javax.naming.*;
public class HelloClient
{
public static void main (String[] args) throws Exception
{
Context c = new InitialContext();
Object o = c.lookup ("Hello");
HelloHome home =
(HelloHome)PortableRemoteObject.narrow (o,HelloHome.class);
Hello hello = home.create();
System.out.println (hello.sayHello());
hello.remove();
}
}
|
ejb-jar.xml
|
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.
|
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise
JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>Hello</ejb-name>
<home>com.examples.HelloHome</home>
<remote>com.examples.Hello</remote>
<ejb-class>com.examples.HelloBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
|
jndi.properties
|
Create the jndi.properties file in target\client. This file defines properties that are required in order to use JNDI to find EJBs on the network.
|
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://localhost:1099
|
Keywords:
EJB Example,jboss ejb example,ejb client example,jboss example,ejb sample,j2ee example,xdoclet example,weblogic example,cmp example,jboss examples,ejb sample code,ejb examples,jboss sample,j2ee sample,sample j2ee application,j2ee examples,web services example,ejb jar xml example,ejb web services,sample weblogic xml,sample ejb jar xml
|