Connecting to an EJB 3.0 Remote SessionBean from Tomcat

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.

Connecting to an EJB 3.0 Remote SessionBean from Tomcat CreateEntApp

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.

 Connecting to an EJB 3.0 Remote SessionBean from Tomcat CreateEntAppStep2

To create the SessionBean, right click the SessionBeansTest-ejb project (the one with the Java bean icon) and select New -> Session Bean.

Connecting to an EJB 3.0 Remote SessionBean from Tomcat CreateSessionBean

Give it the name MySessionBean and make sure the Remote interface checkbox is selected. Also provide a package name, e.g. "ejb".

Connecting to an EJB 3.0 Remote SessionBean from Tomcat CreateSessionBeanStep2 

This will create a Java interface calles MySessionRemote and a Java class called MySessionBean. Open the interface and add this line

public String getOkString();

Connecting to an EJB 3.0 Remote SessionBean from Tomcat EditingInterface 

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!";

Connecting to an EJB 3.0 Remote SessionBean from Tomcat ReturningAString 

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.

Connecting to an EJB 3.0 Remote SessionBean from Tomcat HowToDeploy 

When the deployment succeeds, the Glassfish server log will tell you so.

Connecting to an EJB 3.0 Remote SessionBean from Tomcat DeploymentLog

 

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.

Connecting to an EJB 3.0 Remote SessionBean from Tomcat CreateWebApp 

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.

Connecting to an EJB 3.0 Remote SessionBean from Tomcat CreateWebAppPart2 

Right click the WebApplicationTest application and select New -> Servlet.

Connecting to an EJB 3.0 Remote SessionBean from Tomcat CreateServlet 

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.

Connecting to an EJB 3.0 Remote SessionBean from Tomcat CreateServletPart2

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();
}

Connecting to an EJB 3.0 Remote SessionBean from Tomcat EditServlet 

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.

Connecting to an EJB 3.0 Remote SessionBean from Tomcat ModifyLibraries

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.

Connecting to an EJB 3.0 Remote SessionBean from Tomcat RunApplication 

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.

Connecting to an EJB 3.0 Remote SessionBean from Tomcat OkYouDidIt 

8 Comments

  1. Daniel April 1, 2009
  2. Martin February 23, 2009
  3. Rob December 5, 2008
  4. Firoz December 3, 2008
  5. sudhir October 18, 2007
  6. James June 24, 2007
  7. Wouter van Reeven November 1, 2006
  8. Eduardo Pelegri-Llopart October 20, 2006