users@glassfish.java.net

Re: Local Session Bean Lookup

From: Mahesh.Kannan <Mahesh.Kannan_at_Sun.COM>
Date: Mon, 12 Jun 2006 18:24:39 -0700

Hi Jason,
 If this is a stateless session bean, then you must be able to inject it
directly into your servlet code (as @EJB private ITestSession testSession)
OR
You can use the @EJB annotation to the declare the dependency and then
use the initialContext.lookup() in your servlet as follows:

import javax.ejb.EJB;
import javax.naming.*;

@EJB(name="ejb/ITestSession", beanInterface=ITestSession.class)
public class .... extends Servlet {
   ....
   ....
   InitialContext ctx = new InitialContext();
   ITestSession ssn = (ITestSession)
ctx.lookup("java:comp/env/ejb/ITestSession") should work.
}

Hope this helps.

--Mahesh

Jason Lee wrote:

>I'm having trouble grabbing a reference to a local session bean
>interface from a web app deployed to the app server as the session bean
>(thus the local reference). The session bean has this interface:
>
>@Local
>public interface ITestSession {
> void start() throws Exception;
>}
>
>If I mark that as @Remote, I can get the remote reference from an out of
>container client just fine using the class name as the JNDI lookup. I
>mark it as @Local, however, and try to look it up like this from my web
>app:
>
> Context ctx = new InitialContext();
> Object bean = ctx.lookup("com.iecokc.test.ITestSession"); //
>No cast need
>
>Which gets me "javax.naming.NameNotFoundException:
>com.iecokc.test.ITestSession not found". (For what it's worth,
>ITestSession is not in the web apps classpath, and I plan to be working
>against "bean" via reflection, so I won't explicitly reference the
>interface's class name.) If I try "java:comp/env/ejb/ITestSession" as
>the JNDI name as one (possibly old) reference I found, I get
>"javax.naming.NameNotFoundException: No object bound to name
>java:comp/env/ejb/ITestSession". What's the proper way to lookup the
>reference? Or is Glassfish smart enough to treat it as a local call
>even if I were to mark it as @Remote? I have the EJB3 spec PDFs in
>front of me, but I'm not seeing anything on the subject. Any hints,
>pointers, etc?
>
>I appreciate it!
>
>--
>Jason Lee
>Programmer/Analyst
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: ejb-unsubscribe_at_glassfish.dev.java.net
>For additional commands, e-mail: ejb-help_at_glassfish.dev.java.net
>
>
>