I am taking SCBCD5.0 certification soon. Right now, I am going over some EJB2.0 materials before taking on EJB3.0. I generally know EJB3.0 but never had grasped the fundamentals of EJB2.0. Mastering EJB2.0 would demystify EJB3.0.
I am writing a two-part blog about an EJB2.0 application running on a stand-alone OpenEJB server which also involves a remote Java client application running on a Linux machine. This is the first part.
Software required (or were used for this blog):
- OpenEJB 3.1.4
- JDK1.6 Update 21
- Eclipse Ganymede (optional)
- VMWare 3.0.1
- Ubuntu 10.10 (running as a virtual machine)
Create the interfaces
1 2 3 4 5 6 | package com.ejb2; import java.rmi.RemoteException; import javax.ejb.EJBObject; public interface Advice extends EJBObject { public String getAdvice() throws RemoteException; } |
1 2 3 4 5 6 7 8 | package com.ejb2; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; public interface AdviceHome extends EJBHome { public Advice create() throws CreateException, RemoteException; } |
Create the your session bean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.ejb2; import java.rmi.RemoteException; import javax.ejb.*; public class AdviceBean implements SessionBean { public void ejbActivate() throws EJBException, RemoteException { } public void ejbPassivate() throws EJBException, RemoteException { } public void ejbRemove() throws EJBException, RemoteException { } public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException {} public String getAdvice() { return "advice that"; } } |
NOTE: In order to successfully build the project in Eclipse, you ought to include javaee-api-5.0-3.jar in the class path.
Create ejb-jar.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?xml version="1.0" encoding="UTF-8"?> <!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> <home>com.ejb2.AdviceHome</home> <ejb-name>AdviceBean</ejb-name> <remote>com.ejb2.Advice</remote> <ejb-class>com.ejb2.AdviceBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Bean</transaction-type> <security-identity> <description></description> <use-caller-identity></use-caller-identity> </security-identity> </session> </enterprise-beans> </ejb-jar> |
Jar-up the classes and XML file and place the jar file in OpenEJB server.
Place jar file in OpenEJB Server.
You ought to modify OPENEJB_HOME\conf\ejbd.properties to change the bind-address to the current machine’s IP address which is 192.168.1.2.
NOTE: Your machine’s IP address may not be the same as mine.
Change bind address to current machines IP address.
Finally, start the OpenEJB Server from the command-line. Our remote Java application will use the EJB deployed in this machine (192.168.1.2).
Start the OpenEJB Server as a stand-alone