At home I have a few machines running several services that have my interest: an Apache HTTPD server, Tomcat 5.5 and Oracle XE. Both Apache and Tomcat run on a slow machine that is connected to my ADSL router, while the Oracle XE database runs on a more poweful machine. My "web server" is too old and slow to process large amounts of Entities quickly enough to get a web application that responds fast enough. Hence I decided to setup Glassfish on the faster database machine and have Tomcat connect to Glassfish. In order to do so, I had to connect to a Remote SessionBean running on the Glassfish server from a servlet or JSF managed bean running in Tomcat. It took a little while before I was able to put the pieces together, but I did it. This article tells you how. Please note I am using NetBeans 5.5 RC2 with the Enterprise Pack installed on it for this article. I am sure you can also do it with Eclipse or perhaps JDeveloper.
Before you try to follow the steps I am describing here, make sure to have installed Glassfish v2. Version 2 from after August 2006 is needed for some bugs to have been fixed. Please refer to https://glassfish.dev.java.net/public/downloadsindex.html for download and installation instructions. Also make sure to have installed NetBeans 5.5 RC2 and the NetBeans Enterprise Pack. NetBeans 5.5 RC2 can be downloaded from this page and the NetBeans Enterprise Pack QBuild from this page (make sure to select qbuild and not daily).
The Glassfish part
Once all the software is setup, start up NetBeans. In NetBeans select File -> New Project and then Enterprise and Enterprise Application.
Click Next. Put the name to e.g. "SessionBeansTest". This will be the application running on Glassfish and it will contain our SessionBean. In the Server drop down, make sure the Glassfish server is selected and that the Java EE Version is set to Java EE 5. Finally, unselect the checkbox in front of "Create Web Application Module" since we won’t be needing this.
To create the SessionBean, right click the SessionBeansTest-ejb project (the one with the Java bean icon) and select New -> Session Bean.
Give it the name MySessionBean and make sure the Remote interface checkbox is selected. Also provide a package name, e.g. "ejb".
This will create a Java interface calles MySessionRemote and a Java class called MySessionBean. Open the interface and add this line
public String getOkString();
In the MySessionBean class, make sure to implement the getOkString method. To do this, place the cursor on the class definition line and wait for the light bulb to show up in that line to the left of the "public" keyword. Next, hit Alt-Enter which will bring up a popup with actions that can be done. Select "Implement all abstract methods" and hit Enter again. Make sure the method returns a String, e.g.
return "Ok, you did it!";
That’s it for the Glassfish part. Next, right click the SessionBeansTest application and select Deploy. This will, you guessed it, deploy the application to Glassfish.
When the deployment succeeds, the Glassfish server log will tell you so.
The Tomcat part
Now that we have a Remote SessionBean that we can connect to, let’s connect to it. In NetBeans once more select File -> New Project and this time select Web -> Web Application.
Click Next. Give the application a name, e.g. "WebApplicationTest" and make sure both the embedded tomcat server and the Java EE Version J2EE1.4 are selected. Click Finish.
Right click the WebApplicationTest application and select New -> Servlet.
Name the servlet "MyServlet" and set the package name to "web". Click Finish. The wizard makes sure that the servlet is added to web.xml and will map to /MyServlet after deployment.
Next we need to modify the processRequest method to make the servlet do what we want it to. First, set the contentType to "text/plain". Next we need to add code to make the servlet connect to the Remote SessionBean that is waiting for us in Glassfish. According to the Glassfish EJB3.0 FAQ, we need to add these lines of code:
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
props.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
In case Glassfish is running on another host or in case you modified Glassfish to have it’s IIOP listen on a different port than 3700, you need to add these lines as well
props.setProperty("org.omg.CORBA.ORBInitialHost", <host>);
props.setProperty("org.omg.CORBA.ORBInitialPort", <port>);
where <host> is the host that Glassfish is running on and <port> the port at which the Glassfish IIOP is listening on. Next we need to do a JNDI lookup of the Remote SessionBean and invoke the getOkString() method on it:
try {
InitialContext ic = new InitialContext(props);
MySessionRemote mySessionRemote = (MySessionRemote)ic.lookup(MySessionRemote.class.getName());
out.println(mySessionRemote.getOkString());
} catch (NamingException ex) {
ex.printStackTrace();
}
When you have added this code, take care of the missing imports by pressing the Alt-Shift-F key combination. All imports will be fixed, apart from the MySessionRemote import. In order to successfully import t
his class as well, we need to add the SessionBeansTest-ejb project
to the CLASSPATH of the WebApplicationTest project. Right click the WebApplicationTest project and select Properties. In the new popup, select Libraries. Click the Add Project to the right and browse to the directory holding the SessionBeansTest-ejb project. This directory is inside the directory containing the SessionBeansTest project.
Now, the Glassfish EJB3.0 FAQ also tells us to make sure that certain Glassfish jar files are in the Tomcat CLASSPATH. This turned out to be the hardest part to do, until I read this message from Kenneth Saks. Kenneth says to import only four jar files from the Glassfish project, being
appserv-deployment-client.jar, appserv-ext.jar, appserv-rt.jar and javaee.jar. These jars can be found inside the lib directory in the Glassfish install directory. As Kenneth states, these jars need to be in the shared/lib directory in the Tomcat install directory. The Tomcat install that comes with NetBeans can be found in the enterprise3/apache-tomcat-5.5.17 dir in the NetBeans install dir.
However, a per user directory containing NetBeans specific stuff exists on your hard disk as well. For Windows users, this directory is located in C:\Documents And Settings\<username>\.netbeans\5.5rc2 and for Linux users, this directory is .netbeans/5.5rc2 in your home dir. This directory contains a directory called apache-tomcat-5.5.17_base which contains tomcat specific stuff. The shared/lib directory should be in here. As you can verify yourself, there is no directory name shared, so create it, create a lib directory in the newly created shared directory and copy the four Glassfish jars mentioned earlier to that directory.
Next, right click the WebApplicationTest application in NetBeans and select Run Project.
If all goes well, your browser should display the index.jsp page in the WebApplicationTest application. Modify the URL and append /MyServlet to it. If all goes well, you should see your message displayed in your browser.
I found this tutorial helpful but, it seems not applicable for manual deployment of a plain war file in Tomcat.
I am having some problems in what jar(s) file need to be copied in the %CATALINA_HOME%\shared\lib folder in order to run this successfully.
The errors I’ve encountered are “java.lang.NoClassDefFoundError: com/webage/ejbs/SimpleBean”, and others…
Any update on this. Any help would be highly appreciated. Thanks!
I have the same problem than Rob. I’m using Glassfish 2.1 b60e and Tomcat 6.0.18. After I copied the mentioned jars to the lib folder and renamed it (otherwise there would be a conflict in the class loader loading hierarchy), I still have the problem that Tomcat can not create the resource instance processing the global JNDI resources.
Has anyone an idea what’s wrong here? Any help would be great. Thanks.
Has anyone any help regarding this issue using Tomcat 6.0? There is no shared/lib folder in 6 and ading the jars to the lib folder causes Tomcat to fail to start. Any help would be appreciative.
Regards,
Rob
Thanks very much. I’ve been battling with this for such a long time. Just trying to understand how it all works was a problem.
Note for all, after copying the jars to the shared\lib directory remember to RESTART the tomcat server.
This tutorial looks great and helpful in testing ejb 3.0. espicially calling ejb thru a servlet is interesting. it is great to look such kind of tutorials on web.
Hi,
I tried the steps mentioned by you and it worked.
I was trying to figure out a way for this for a many days and your tips really helped me.
But I have a few issues,
The above mentioned tips works very well, if the project is not a JSF project.
But if you create a web project with the framework as JSF, then tomcat is failing to load the application properly.
Did you try this?
But it is perfectly working well if the project contains only jsps and servlets.
Eagerly awaiting your reply.
Please note that when you’d like to connect to the remote session bean from another machine than the one running Glassfish, you need to do some additional configuration of Glassfish.
Open the Glassfish admin console in your browser and expand Configuration -> ORB -> IIOP Listeners. The listener named orb-listener-1 is created by default when you install Glassfish and it is configured to listen to port 3700 on all IP addresses (0.0.0.0). If you try to connect to this listener remotely, it will throw an error saying it cannot resolve IP address 127.0.1.1.
In order to fix this, add another IIOP listener and copy all configuration settings of orb-listener-1 except for the port and IP address. Make sure the port is set to another, available, port, e.g. 3701. Also make sure that the listener is listening on the “external” IP address of the machine running Glassfish. So, if the IP address of the machine running Glassfish is 192.168.0.5 use this IP address.
Next, be sure to use the correct ORBInitialHost and ORBInitialPort settings. So, in this example use this
props.setProperty(“org.omg.CORBA.ORBInitialHost”, 192.168.0.1);
props.setProperty(“org.omg.CORBA.ORBInitialPort”, 3701);
After this, Tomcat should have no problems connecting to the remote session beans.
Please note that it is perfectly ok to modify the IP address that the orb-listener-1 listener is listening on to the “external” IP address. If you do this, local connections to your remote beans won’t work anymore.
Cool. I am pushing a spotlight to it in a short while. See http://blogs.sun.com/theaquarium/entry/invoking_ejbs_in_glassfish_from