Skip to main content

EJB lookup fails ONLY when defined with *both* remote and local interface?!

1 reply [Last post]
sairndain
Offline
Joined: 2004-06-08

Hi All

In NetBeans 6.0, I created a simple enterprise project where I perform a local lookup of a stateless session bean(EJB 2.1).

(NOTE: I am using the default "GlassFish v2" application server)

THE PROBLEM: When the session bean -- that I am performing the lookup on -- is defined with *both* local and remote interface, the local lookup fails!
(There is no issue when performing a local lookup on a session bean defined with only a local interface)

QUESTION: Is there something *more* I have to do when the Session bean is defined with *both* local and remote interfaces?

MORE INFORMATION:

In this simple "enterprise" project, a POJO (jsf "backing bean" in the WEB module), is calling another POJO ("helper" class in the EJB module).
The POJO "helper" class looks up the local reference for the session bean and calls one of its methods...

This is the local ejb lookup code (NOTE: generated by NetBeans 6.0):

private TestEJBLocal lookupTestEJBBean() {
try {
Context c = new InitialContext();
TestEJBLocalHome rv = (TestEJBLocalHome) c.lookup("java:comp/env/TestEJBBean");
return rv.create();
} catch (NamingException ne) {
java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ne);
throw new RuntimeException(ne);
} catch (CreateException ce) {
java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ce);
throw new RuntimeException(ce);
}
}

This is the error message received after attempting to use the above code:
-
-
-
Caused by: java.lang.RuntimeException: javax.naming.NameNotFoundException: No object bound to name java:comp/env/TestEJBBean
at test.ejb.TestHelper.lookupTestEJBBean(TestHelper.java:34)
at test.ejb.TestHelper.getField1(TestHelper.java:23)
at test.web.WelcomeJSFBean.getField1(WelcomeJSFBean.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:261)
... 49 more
-
-
-

This is the java package structure in this enterprise project...

(EJB module)
|
|__test.ejb
| |__TestEJBBean.java
| |__TestEJBLocal.java
| |__TestEJBLocalHome.java
| |__TestHelper.java
|

(WEB module)
|
|__test.web
| |__WelcomeJSFBean.java
|

EVEN MORE INFORMATION. (code, etc)

**ejb-jar.xml**
<?xml version="1.0" encoding="UTF-8"?>

TestEJBSB
TestEJBBean
test.ejb.TestEJBRemoteHome
test.ejb.TestEJBRemote
test.ejb.TestEJBLocalHome
test.ejb.TestEJBLocal
test.ejb.TestEJBBean
Stateless
Container

TestEJBBean
*

Required

*** test.ejb.TestHelper ***
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package test.ejb;

import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
*
* @author sairndain
*/
public class TestHelper {
private String field1 = "...the value of field1 in TestHelper.java...";
public String getField1()
{
TestEJBLocal localref = (TestEJBLocal) this.lookupTestEJBBean();
return localref.getField1() + this.field1;
}

private TestEJBLocal lookupTestEJBBean() {
try {
Context c = new InitialContext();
TestEJBLocalHome rv = (TestEJBLocalHome) c.lookup("java:comp/env/TestEJBBean");
return rv.create();
} catch (NamingException ne) {
java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ne);
throw new RuntimeException(ne);
} catch (CreateException ce) {
java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ce);
throw new RuntimeException(ce);
}
}
}

*** test.ejb.TestEJBBean ***
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package test.ejb;

import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

/**
*
* @author sairndain
*/
public class TestEJBBean implements SessionBean {

private SessionContext context;

// ;

// TODO Add code to acquire and use other enterprise resources (DataSource, JMS, enterprise bean, Web services)
// TODO Add business methods or web service operations

/**
* @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
*/
public void setSessionContext(SessionContext aContext) {
context = aContext;
}

/**
* @see javax.ejb.SessionBean#ejbActivate()
*/
public void ejbActivate() {

}

/**
* @see javax.ejb.SessionBean#ejbPassivate()
*/
public void ejbPassivate() {

}

/**
* @see javax.ejb.SessionBean#ejbRemove()
*/
public void ejbRemove() {

}

// ;

/**
* See section 7.10.3 of the EJB 2.0 specification
* See section 7.11.3 of the EJB 2.1 specification
*/
public void ejbCreate() {
// TODO implement ejbCreate if necessary, acquire resources
// This method has access to the JNDI context so resource aquisition
// spanning all methods can be performed here such as home interfaces
// and data sources.
}

private String field1 = "...the value of field1 in TestEJBBean.java...";
public String getField1() {
return this.field1;
}

// Add business logic below. (Right-click in editor and choose
// "EJB Methods > Add Business Method" or "Web Service > Add Operation")

}

*** test.web.WelcomeJSFBean ***
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package test.web;

import test.ejb.TestHelper;

/**
*
* @author sairndain
*/

public class WelcomeJSFBean {

/** Creates a new instance of WelcomeJSFBean */
public WelcomeJSFBean() {
}

private String field1 = "...value of field1 in WelcomeJSFBean.java...";
public String getField1()
{
TestHelper helper = new TestHelper();
return helper.getField1() + this.field1;
}
}

*** welcomeJSF.jsp ***
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>

<%--
This file is an entry point for JavaServer Faces application.
--%>

JSP Page

the value of field1:

*** web.xml ***
<?xml version="1.0" encoding="UTF-8"?>

com.sun.faces.verifyObjects
false

com.sun.faces.validateXml
true

javax.faces.STATE_SAVING_METHOD
client

Faces Servlet
javax.faces.webapp.FacesServlet
1

Faces Servlet
*.faces

30

index.jsp

*** faces-config.xml ***
<?xml version='1.0' encoding='UTF-8'?>

WelcomeJSFBean
test.web.WelcomeJSFBean
request

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
bughunter123
Offline
Joined: 2008-01-15

try to add "/remote" or "/local" to the EJB name you're looking up!