Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to lookup a Local EJB from a POJO in the same application

htran_888Jan 28 2008 — edited Apr 11 2008
Hi All,

I am awared that looking up a Local EJB3 Interface is not recommended compared to either referencing an equivalent Remote Interface, or via Dependency Injection on managed local beans. Nevertheless, I am determined to find out the limitation of using a Local Interface since there has been a number of tutorials around which I have tried with little success. Let's look at the following example I have tried to follow as closely to the suggestion in the glassfish EJB FAQ.

Question - How do I access a Local EJB from a POJO? (bottom of page 9)

Answer: Local EJB access is only portable from within the same application, so the POJO class must be called from a component running within the same application as the target local EJB. Injection is not supported for POJO classes, so the POJO needs to look up the EJB reference within the private namespace (java:comp/env) of the component within whose scope it is executing.

Step 1. Define the local ejb dependency for the component within whose scope the POJO will execute.

I have created the project EJBModule30-GlassFish-Local in Netbeans which consists of an EJB SessionBean in the ejb package. At the sametime, a SessionLocalClient(POJO) has also been created under the clients directory/package in the same project. The programs are as follows:
package ejb;

import javax.ejb.Local;

@Local
public interface SessionLocal {

    String getResult();

}
package ejb;

import javax.ejb.Stateless;

@Stateless
public class SessionBean implements SessionLocal {

    public String getResult() {
        return "This is a Local EJB 3.0 Bean";
    }
}
package clients;

import javax.naming.InitialContext;
import ejb.SessionLocal;

public class SessionLocalClient {

    public static void main(String[] args) {

        try {
            InitialContext ctx = new InitialContext();
//          SessionLocal localSessionBean = (SessionLocal) ctx.lookup("java:comp/env/SessionLocal");
//          SessionLocal localSessionBean = (SessionLocal) ctx.lookup("java:comp/env/client.SessionBean");
            SessionLocal localSessionBean = (SessionLocal) ctx.lookup("java:comp/env/SessionBean/local");
            System.err.println("result=" + localSessionBean.getResult());
        } catch (javax.naming.NamingException ne) {
            ne.printStackTrace();
        }
    }
}
The SessionLocalClient.java is ran separately after the SessionBean is deployed to GlassFish v2 in Netbeans 6.0 on Windows XP platform even though it is part of the SessionBean project.

The outputs from GlassFish v2 is:

deployed with moduleid = EJBModule30-GlassFish-Local
*LDR5010: All ejb(s) of [EJBModule30-GlassFish-Local] loaded successfully!*

----------------------------------------------------------------------------

Likewise, the above combination of Jndi lookups generated these exceptions below:

*javax.naming.NameNotFoundException: No object bound for java:comp/env/SessionBean [Root exception is java.lang.NullPointerException]*

*javax.naming.NameNotFoundException: No object bound for java:comp/env/SessionLocal [Root exception is java.lang.NullPointerException]*

*javax.naming.NameNotFoundException: No object bound for java:comp/env/client.SessionBean [Root exception is java.lang.NullPointerException]*

*javax.naming.NameNotFoundException: No object bound for java:comp/env/SessionBean/local [Root exception is java.lang.NullPointerException]*----------------------------------------------------------------------------

Please correct the above example to illustrate what Step 1 means, if it is not correct.

I am also uncleared on where you have got the name from the first line of page 10, *@EJB(name="fooejbref", beanInterface=FooLocal.class).* Is fooejbref is just any arbitrary name you have come up with to reference the FooLocal interface?

A copy of both appserv-rt.jar & javaee.jar have been included as part of the project's classpath.

Any assistance would be much appreciated.

Thanks,

Henry
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 9 2008
Added on Jan 28 2008
2 comments
511 views