GlasshFish.dev
How do I access a Remote EJB (3.0 or 2.x) component from a non-Java EE web container like Tomcat or Resin?
Accessing a Remote EJB component from a non-Java EE web container is similar to the stand-alone java client case. However, the complication is that most Java web servers set the default JNDI name provider for the JVM, which prevents our appserver naming provider from being instantiated when the application uses the no-arg InitialContext() constructor. The solution is to explicitly instantiate an InitialContext(Hashtable) with the properties for our naming provider, as contained in GlassFish’s jndi.properties file.
Step 1. Instantiate the InitialContext
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”);
// optional. Defaults to localhost. Only needed if web server is running
// on a different host than the appserver
props.setProperty(“org.omg.CORBA.ORBInitialHost”, “localhost”);
// optional. Defaults to 3700. Only needed if target orb port is not 3700.
props.setProperty(“org.omg.CORBA.ORBInitialPort”, “3700”);
InitialContext ic = new InitialContext(props);
Step 2. Use the global JNDI name of the target Remote EJB in the lookup.
EJB 3.x, assuming a global JNDI name of “com.acme.FooRemoteBusiness” :
FooRemoteBusiness foo = (FooRemoteBusiness) ic.lookup("com.acme.FooRemoteBusiness");
EJB 2.x, assuming a global JNDI name of “com.acme.FooHome” :
Object obj = ic.lookup(“com.acme.FooHome”);
FooHome fooHome = (FooHome) PortableRemoteObject.narrow(obj, FooHome.class);
Step 3. Add the necessary appserver code to the web server’s classpath.
See step 3 of stand-alone client access for the list of required .jars.
Step 4. For EJB 3.0 Remote access, use at least Glassfish V2 or Java EE 5 SDK(SJS AS 9) Update 1.
Builds from this point on will contain a required bug fix.
See https://glassfish.dev.java.net/issues/show_bug.cgi?id=920 for more details.