tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Blevins <>
Subject Re: JNDI EJB Lookup and OpenEJB Loader Application
Date Wed, 22 Jul 2009 18:27:44 GMT
Hi David.  Like the name :)

On Jul 22, 2009, at 9:46 AM, David Sells wrote:

> I have been able
> to lookup and invoke the calculator using a *standalone* program  
> using the
> following code:
> Properties properties = new Properties();
> properties 
> .setProperty 
> (Context 
> ,"org.apache.openejb.client.RemoteInitialContextFactory");
> properties.setProperty(Context.PROVIDER_URL, "
> InitialContext initialContext = new InitialContext(properties);
> CalculatorRemote calculator = (CalculatorRemote)
> initialContext.lookup("java:cal-1.0/CalculatorRemote");
> result = cow.multiply(2, 333);

That's good code but I would remove the "java:" from the lookup url.   
The "java:" lookups on an InitialContex actually bypass the  
Context.INITIAL_CONTEXT_FACTORY and go instead to whomever owns the  
"java" url prefix as configured via Context.URL_PKG_PREFIXES which is  
set internally by containers.  Long story short, when running in  
Tomcat, Tomcat owns "java" and your lookup is going there instead of  
to the RemoteInitialContextFactory.

> Now I tried this same code inside a web application to see if it  
> would work
> and it failed.   This web application was running in the same  
> instance of
> tomcat that the EJB is deployed in.

So for the scenario where your client is running in the same vm where  
Tomcat/OpenEJB are running, then you can do this:


     Properties properties = new Properties();
     InitialContext initialContext = new InitialContext(properties);

     CalculatorRemote calculator = (CalculatorRemote)  

     result = calculator.multiply(2, 333);


Or if your client is in a webapp, you can add an ejb ref to  
CalculatorRemote in your web.xml


Then you can look up the bean anywhere in the webapp like so:

     InitialContext initialContext = new InitialContext();
     CalculatorRemote calculator = (CalculatorRemote)  


Or if your client is a Servlet, Filter, Listener, ManagedBean or other  
Java EE injectable object, then you can really get terse declaring  
just an annotated field like so.

     private CalculatorRemote calculator;

Note, in the "WEBAPP CLIENT" scenario you can actually skip the xml  
declaration if you add this as a class annotation on any servlet in  
your webapp.

     @EJB(beanInterface = CalculatorRemote.class, name = "MyCalculator")
     public class MyServlet ... {

That has the same effect as declaring it in xml.

Hope this helps!


View raw message