You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by David Sells <ds...@gmail.com> on 2009/07/22 18:46:02 UTC

JNDI EJB Lookup and OpenEJB Loader Application

I am trying some basic tests with *Stateless*  *EJB*'s using the Calculator
example.  I am running into difficulties with:

 *JNDI* with respect to setting the *InitialContext* correctly for
*remote*lookups.


*Scenario*:

I have deployed the Calculator example with a Remote interface into a *
Tomcat* environment (with the OpenEjb Servlet installed).  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.INITIAL_CONTEXT_FACTORY,"org.apache.openejb.client.RemoteInitialContextFactory");
properties.setProperty(Context.PROVIDER_URL, "
http://127.0.0.1:8080/openejb/ejb/");
InitialContext initialContext = new InitialContext(properties);
CalculatorRemote calculator = (CalculatorRemote)
initialContext.lookup("java:cal-1.0/CalculatorRemote");
result = cow.multiply(2, 333);

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.

*Question*:

 What is the correct context to set for the given scenario?

Thanks,

David

(sorry if this is appearing twice.  It didn't seem to appear the first
time... was part way through registration.)

Re: JNDI EJB Lookup and OpenEJB Loader Application

Posted by David Sells <ds...@gmail.com>.
Wow!

What an answer ( to my question and the ones I was just about to ask) and it
works.

Thanks, David

On Wed, Jul 22, 2009 at 2:27 PM, David Blevins <da...@visi.com>wrote:

> 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.INITIAL_CONTEXT_FACTORY,"org.apache.openejb.client.RemoteInitialContextFactory");
>> properties.setProperty(Context.PROVIDER_URL, "
>> http://127.0.0.1:8080/openejb/ejb/");
>> 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:
>
> NON-WEBAPP CLIENT:
>
>    Properties properties = new Properties();
>    properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
> "org.apache.openejb.client.LocalInitialContextFactory");
>    InitialContext initialContext = new InitialContext(properties);
>
>    CalculatorRemote calculator = (CalculatorRemote)
> initialContext.lookup("cal-1.0/CalculatorRemote");
>
>    result = calculator.multiply(2, 333);
>
> WEBAPP CLIENT:
>
> Or if your client is in a webapp, you can add an ejb ref to
> CalculatorRemote in your web.xml
>
>    <ejb-ref>
>      <ejb-ref-name>MyCalculator</ejb-ref-name>
>      <remote>org.foo.CalculatorRemote</remote>
>    </ejb-ref>
>
> Then you can look up the bean anywhere in the webapp like so:
>
>    InitialContext initialContext = new InitialContext();
>    CalculatorRemote calculator = (CalculatorRemote)
> initialContext.lookup("java:comp/env/MyCalculator");
>
> SERVLET, FILTER, LISTENER, MANAGED BEAN:
>
> 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.
>
>    @EJB
>    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!
>
>
> -David
>
>

Re: JNDI EJB Lookup and OpenEJB Loader Application

Posted by David Blevins <da...@visi.com>.
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 
> .INITIAL_CONTEXT_FACTORY 
> ,"org.apache.openejb.client.RemoteInitialContextFactory");
> properties.setProperty(Context.PROVIDER_URL, "
> http://127.0.0.1:8080/openejb/ejb/");
> 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:

NON-WEBAPP CLIENT:

     Properties properties = new Properties();
     properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,  
"org.apache.openejb.client.LocalInitialContextFactory");
     InitialContext initialContext = new InitialContext(properties);

     CalculatorRemote calculator = (CalculatorRemote)  
initialContext.lookup("cal-1.0/CalculatorRemote");

     result = calculator.multiply(2, 333);

WEBAPP CLIENT:

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

     <ejb-ref>
       <ejb-ref-name>MyCalculator</ejb-ref-name>
       <remote>org.foo.CalculatorRemote</remote>
     </ejb-ref>

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

     InitialContext initialContext = new InitialContext();
     CalculatorRemote calculator = (CalculatorRemote)  
initialContext.lookup("java:comp/env/MyCalculator");

SERVLET, FILTER, LISTENER, MANAGED BEAN:

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.

     @EJB
     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!


-David