6/20/2011

Jetty to GlassFish Interoperability: Remote EJB Invocation

Jetty-to-GlassFish EJB invocation is similar to Tomcat-to-GlassFish case. I tested with Jetty Hightide 7.4.2 and GlassFish 3.1.

Jetty configuration (in csh/tcsh syntax):

setenv JETTY_HOME $HOME/tools/jetty
setenv JETTY_PORT 7070 # avoid port conflict
mkdir $JETTY_HOME/lib/ext/glassfish
The following commands copy 32 GlassFish jars needed for this test to Jetty directory:
cd $GLASSFISH_HOME/modules
cp javax.ejb.jar ejb-container.jar deployment-common.jar dol.jar glassfish-corba-csiv2-idl.jar glassfish-corba-codegen.jar ssl-impl.jar security.jar ejb.security.jar management-api.jar gmbal-api-only.jar gmbal.jar glassfish-corba-asm.jar glassfish-corba-newtimer.jar glassfish-corba-orbgeneric.jar config-types.jar kernel.jar config.jar config-api.jar glassfish-corba-omgapi.jar glassfish-corba-orb.jar orb-connector.jar orb-enabler.jar orb-iiop.jar glassfish-api.jar auto-depends.jar hk2-core.jar internal-api.jar common-util.jar glassfish-corba-internal-api.jar glassfish-naming.jar bean-validator.jar $JETTY_HOME/lib/ext/glassfish
I had to slightly modify TestServlet to make it work in Jetty:

1, declare TestServlet with web.xml instead of using @javax.servlet.annotation.WebServlet, probably because servlet 3.0 support is not there yet in Jetty 7.4.2;

2, change @PostConstruct method to servlet init method. When using @PostConstruct to look up TestBean, it is not initialized and produced NullPointerException at request time. So I changed it to servlet init() method, which is a lifecycle method since early days of servlet. The complete TestServlet.java and web.xml:
package test;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
import javax.annotation.*;

public class TestServlet extends HttpServlet {
private static final String glassfishJndiPropertiesPath = "/glassfish-jndi.properties";
private static final String testBeanJndi = "java:global/test-ejb/TestBean";
private TestIF testBean;

private Properties getGlassFishJndiProperties() {
Properties props = new Properties();
try {
props.load(getClass().getResourceAsStream(glassfishJndiPropertiesPath));
} catch (IOException e) {
System.out.println("Failed to load " + glassfishJndiPropertiesPath);
}
System.out.println("Got glassfish-jndi.properties: " + props);
return props;
}

public void init() {
try {
InitialContext ic = new InitialContext(getGlassFishJndiProperties());
testBean = (TestIF) ic.lookup(testBeanJndi);
System.out.println("Looked up " + testBeanJndi + ", got " + testBean);
} catch (NamingException e) {
throw new RuntimeException(e);
}
}

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("testBean.hello(): " + testBean.hello());
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>test.TestServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
After packaging, test2.war (a sample app test.war is already included in jetty/webapps, so use a different name) contains the following entries:
WEB-INF/classes/glassfish-jndi.properties
WEB-INF/classes/test/TestIF.class
WEB-INF/classes/test/TestServlet.class
WEB-INF/web.xml
Other files, TestIF, TestBean, test-ejb.jar and glassfish-jndi.properties, are all the same as in Tomcat and Resin exercise.

5 comments:

Anonymous said...

the content of glassfish-jndi.properties ?

Anonymous said...

the content of glassfish-jndi.properties?

Anna said...

Great and Useful Article.

J2EE Training

Vũ Diệu Linh said...

I enjoyed on reading your blog post. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. Please visit my website, Friv 4000 Games is where all the free friv games.
Friv 4000

Vũ Diệu Linh said...

I enjoyed on reading your blog post. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. Please visit my website, Friv 4000 Games is where all the free friv games.
Friv 4000