
 Accessing EJBs Remotely OpenEJB as a stand-alone server
 |
Short version An Example: Hello World!
Create the bean class
Create the EJB Home interface
Create the EJB Object interface
Create the ejb-jar.xml
Compile the EJB
Package the EJB
Deploy the EJB jar
Short version Using OpenEJB's default remote server implementation is pretty straight forward. You simply need to: - Deploy your bean.
- Start the server on the IP and Port you want, 25.14.3.92 and 4201 for example.
- Use that information in your client to create an initial context
- Add the right jars to your client's classpath
So, here it is in short. Deploy your bean: c:\openejb> openejb.bat deploy cont\default.openejb.conf beans\myBean.jar Start the server: c:\openejb> openejb.bat start -h 25.14.3.92 -p 4201 Create an initial context in your client as such:
Properties p = new Properties();
p.put("java.naming.factory.initial", "org.openejb.client.JNDIContext");
p.put("java.naming.provider.url", "25.14.3.92:4201");
p.put("java.naming.security.principal", "myuser");
p.put("java.naming.security.credentials", "mypass");
InitialContext ctx = new InitialContext(p);
| Add the following libraries to your clients classpath: |  | openejb-x.x.x.jar |  | openejb_client-x.x.x.jar | Both can be found in the lib directory where you installed OpenEJB. An Example: Hello World! This example assumes you have already downloaded and installed OpenEJB in the directory c:\openejb. Refer to the QuickStart Guide if you haven't yet installed OpenEJB. We also assume that you running your client from the directory c:\my\app. Create the bean class First create the package where we will place our ejb and application files. c:\my\app> mkdir org c:\my\app> mkdir org\acme In your favorite editor, create the file below. c:\my\app\org\acme\HelloBean.java |
package org.acme;
import java.rmi.RemoteException;
import javax.ejb.*;
public class HelloBean implements SessionBean {
private SessionContext sessionContext;
public void ejbCreate() {
}
public void ejbRemove() {
}
public void ejbActivate() {
}
public void ejbPassivate() {
}
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
public String sayHello() throws java.rmi.RemoteException {
return "Hello";
}
}
| Create the EJB Home interface c:\my\app\org\acme\HelloHome.java |
package org.acme;
import java.rmi.*;
import javax.ejb.*;
import java.util.*;
public interface HelloHome extends EJBHome {
public Hello create() throws RemoteException, CreateException;
}
| Create the EJB Object interface c:\my\app\org\acme\HelloObject.java |
package org.acme;
import java.rmi.*;
import javax.ejb.*;
import java.util.*;
public interface Hello extends EJBObject {
public String sayHello() throws RemoteException;
}
| Create the ejb-jar.xml c:\my\app> mkdir META-INF c:\my\app\META-INF\ejb-jar.xml |
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>Hello</ejb-name>
<home>org.acme.HelloHome</home>
<remote>org.acme.HelloObject</remote>
<ejb-class>org.acme.HelloBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>Hello</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
| Compile the EJB C:\my\app> javac ... Package the EJB C:\my\app> jar cvf myHelloEjb.jar org META-INF Deploy the EJB jar C:\my\app> cd C:\openejb C:\openejb> openejb.bat deploy -a -m c:\my\app\myHelloEjb.jar |