On May 3, 2009, at 5:31 PM, Christian Bourque wrote:

Hi David,

I already know this method but I need to inject (lookup) ejbs
dynamically so it is useless for me...

I guess my only choice left is to use the 'new InitialContext()' alternative?

Doing 'new InitialContext()' is going to get you the same JNDI namespace that sessionContext.lookup() gives you, so that won't work for what you need.

You can use the LocalInitialContextFactory if you want to lookup any bean deployed in the container system.

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

    initialContext = new InitialContext(properties);

That will bypass the bean's private jndi namespace and hook you up with the generic "client" namespace.

-David




Thanks

Christian

On Sun, May 3, 2009 at 3:42 PM, David Blevins <[email protected]> wrote:
Hi Christian,

Try this:

   @Stateless
   public class OrangeBean implements OrangeLocal
   {

       public String echo(String s)
       {
           return s;
       }
   }

   @EJB(name="orange", beanInterface = OrangeLocal.class)
   @Stateless
   public class TestBean implements TestLocal
   {
      @Resource
      private SessionContext sessionContext;

      public void test()
      {
          OrangeLocal orangeLocal = (OrangeLocal)
sessionContext.lookup("orange");
      }
   }

The @EJB annotation declares a reference to the Orange bean.  The
sessionContext.lookup is a convenience method around 'new
InitialContext().lookup("java:comp/env/"+name)' and by default the
'java:comp/env' namespace is empty unless references are declared via
annotation or xml.

-David


On May 1, 2009, at 2:28 PM, Christian Bourque wrote:

Hi,

What JNDI path should I use to lookup an EJB using a SessionContext
which has been injected:

@Stateless
public class TestBean implements TestLocal
{
  @Resource
  private SessionContext sessionContext;

  public void test()
  {
      sessionContext.lookup("???");
  }
}

I've tried different combinations without any success...

Thanks

Christian





Reply via email to