Saturday, October 06, 2007

Why do i get NameNotFoundException while doing a JNDI lookup?

Many a times when you are doing a lookup in the JNDI tree, you see javax.naming.NameNotFoundException. A simple code that does the lookup will look something like:


Context ctx = new InitialContext();
Object obj = ctx.lookup("somepath/somename");


This code just looks up the JNDI tree to get an object bound by the name "somepath/somename". Looks simple. However, chances are that you might even see this exception:


javax.naming.NameNotFoundException: somepath not bound
at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)
at org.jnp.server.NamingServer.getObject(NamingServer.java:543)
at org.jnp.server.NamingServer.lookup(NamingServer.java:267)
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 sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:294)
at sun.rmi.transport.Transport$1.run(Transport.java:153)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:149)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)
at java.lang.Thread.run(Thread.java:595)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:247)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:223)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:126)
at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:625)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
at javax.naming.InitialContext.lookup(InitialContext.java:351)


Look closely at the stacktrace. It shows that while looking up the JNDI tree it could not find the jndi name "somepath" (this name may vary). The reason is simple, the JNDI tree does not have any object bound by this name.

To quote the javadocs of this exception "This exception is thrown when a component of the name cannot be resolved because it is not bound."

So how do i know, what's the name to which my object is bound? Each application server, usually provides a JNDI view which can be used to see the contents of the JNDI tree. If you know what object you are looking for (ex: the name of the bean), then you can traverse this JNDI tree to see what name it is bound to. The JNDI view is specific to every application server.

To give an example, JBoss provides its JDNI tree view, through the JMX console. Here are the steps, one has to follow to check the JNDI tree contents on JBoss:

- Go to http://< server>:< port>/jmx-console (Ex: http://localhost:8080/jmx-console)
- Search for service=JNDIView on the jmx-console page
- Click on that link
- On the page that comes up click on the Invoke button beside the list() method
- The page that comes up will show the contents of the JNDI tree.

Here's an sample of how the output looks like(just a small part of the entire output):


java: Namespace

+- XAConnectionFactory (class: org.jboss.mq.SpyXAConnectionFactory)
+- DefaultDS (class: org.jboss.resource.adapter.jdbc.WrapperDataSource)
+- SecurityProxyFactory (class: org.jboss.security.SubjectSecurityProxyFactory)
+- DefaultJMSProvider (class: org.jboss.jms.jndi.JNDIProviderAdapter)
+- comp (class: javax.naming.Context)
+- JmsXA (class: org.jboss.resource.adapter.jms.JmsConnectionFactoryImpl)
+- ConnectionFactory (class: org.jboss.mq.SpyConnectionFactory)
+- jaas (class: javax.naming.Context)
| +- dukesbank (class: org.jboss.security.plugins.SecurityDomainContext)
| +- HsqlDbRealm (class: org.jboss.security.plugins.SecurityDomainContext)
| +- jbossmq (class: org.jboss.security.plugins.SecurityDomainContext)
| +- JmsXARealm (class: org.jboss.security.plugins.SecurityDomainContext)


Global JNDI Namespace

+- ebankTxController (proxy: $Proxy79 implements interface com.sun.ebank.ejb.tx.TxControllerHome,interface javax.ejb.Handle)
+- ebankAccountController (proxy: $Proxy75 implements interface com.sun.ebank.ejb.account.AccountControllerHome,interface javax.ejb.Handle)
+- TopicConnectionFactory (class: org.jboss.naming.LinkRefPair)
+- jmx (class: org.jnp.interfaces.NamingContext)
| +- invoker (class: org.jnp.interfaces.NamingContext)
| | +- RMIAdaptor (proxy: $Proxy48 implements interface org.jboss.jmx.adaptor.rmi.RMIAdaptor,interface org.jboss.jmx.adaptor.rmi.RMIAdaptorExt)
| +- rmi (class: org.jnp.interfaces.NamingContext)
| | +- RMIAdaptor[link -> jmx/invoker/RMIAdaptor] (class: javax.naming.LinkRef)
+- HTTPXAConnectionFactory (class: org.jboss.mq.SpyXAConnectionFactory)
+- ConnectionFactory (class: org.jboss.mq.SpyConnectionFactory)
+- ebankCustomer (proxy: $Proxy67 implements interface com.sun.ebank.ejb.customer.LocalCustomerHome)
+- UserTransactionSessionFactory (proxy: $Proxy14 implements interface org.jboss.tm.usertx.interfaces.UserTransactionSessionFactory)
+- ebankCustomerController (proxy: $Proxy77 implements interface com.sun.ebank.ejb.customer.CustomerControllerHome,interface javax.ejb.Handle)
+- HTTPConnectionFactory (class: org.jboss.mq.SpyConnectionFactory)
+- XAConnectionFactory (class: org.jboss.mq.SpyXAConnectionFactory)
+- TransactionSynchronizationRegistry (class: com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple)
+- ebankAccount (proxy: $Proxy68 implements interface com.sun.ebank.ejb.account.LocalAccountHome)
+- UserTransaction (class: org.jboss.tm.usertx.client.ClientUserTransaction)
+- UILXAConnectionFactory[link -> XAConnectionFactory] (class: javax.naming.LinkRef)
+- UIL2XAConnectionFactory[link -> XAConnectionFactory] (class: javax.naming.LinkRef)
+- queue (class: org.jnp.interfaces.NamingContext)
| +- A (class: org.jboss.mq.SpyQueue)
| +- testQueue (class: org.jboss.mq.SpyQueue)
| +- ex (class: org.jboss.mq.SpyQueue)
| +- DLQ (class: org.jboss.mq.SpyQueue)
| +- D (class: org.jboss.mq.SpyQueue)
| +- C (class: org.jboss.mq.SpyQueue)
| +- B (class: org.jboss.mq.SpyQueue)


Let's see what this tells us. Let's consider the Global JNDI Namespace first. It contains (among other things) the following:

+- ebankTxController (proxy: $Proxy79 implements interface com.sun.ebank.ejb.tx.TxControllerHome,interface javax.ejb.Handle)


This tells me that an object which implements com.sun.ebank.ejb.tx.TxControllerHome and javax.ejb.Handle interfaces is bound to the JNDI tree by the jndi-name "ebankTxController". So if at all i have to lookup this object, my lookup code would be something like:


Context ctx = new InitialContext();
ctx.lookup("ebankTxController");


Similarly in the same Global JDNI Namespace, we see :


+- queue (class: org.jnp.interfaces.NamingContext)
| +- A (class: org.jboss.mq.SpyQueue)



Make note of the nesting of the names here. This tells me that an object of type org.jboss.mq.SpyQueue is bound by the name "A under the path queue". So your lookup for this object should look like:


Context ctx = new InitialContext();
ctx.lookup("queue/A");


Now let's move on to the java: namespace in the JNDI tree view above. The difference between a Global JNDI namespace and the java: namespace is that, the object bound in the java: namespace can be looked-up ONLY by clients within the SAME JVM. Whereas, in case of Global JNDI namespace, the objects bound in this namespace can be looked-up by clients, even if they are not in the same JVM as the server. One would ask, how does this matter? Consider a standalone java program(client) which tries to lookup some object on the server (running in its own JVM). Whenever a standalone client is started (using the java command), a new JVM is instantiated. As a result, the server (which is started in its own JVM) and the client are running on different JVMs. Effectively, the client will NOT be able to lookup objects bound in the java: namespace of the server. However, the client can lookup the objects present in the Global JNDI namespace of the server.

So, why are we discussing these details, in a topic which was meant to explain the NameNotFoundException? Let's consider the java: namespace output above. There's a


+- DefaultDS (class: org.jboss.resource.adapter.jdbc.WrapperDataSource)


This tells me that there's an object bound to the name DefaultDS in the java: namespace. So my lookup code would be:


Context ctx = new InitialContext();
ctx.lookup("java:/DefaultDS");


As explained above, this code is going to return you the object, if this piece of code runs in the same JVM as the server. However, if this piece of code is run from a client in different JVM (maybe a standalone client), then it's going to run into NameNotFoundException. The reason i explained the java: and the Global JNDI namespace is that, sometimes people are surprised that even though the JNDI view shows that the object is bound in the java: namespace(with the same name as the one they pass to the lookup method), they still run into NameNotFoundException. The probable reason might be, the client is in a different JVM.

37 comments:

nag9s2007 said...

myself Nag from B'lore . Nice to see you the article .
I want to know some information . Even
+- SimpleSessionBean (class: org.jnp.interfaces.NamingContext)
| +- remote (proxy: $Proxy74 implements interface beans.SimpleSession,interface org.jboss.ejb3.JBossProxy,interface javax.ejb.EJBObject)

is available in Global Name Space but i am not to access from the client by using constructs "SimpleSessionBean/remote" ... please tell me how can i access it.
Thanks in advance

Jaikiran said...

Nag,

Post the exception stacktrace that you see and also the code which you use for the JNDI lookup. Is your client a standalone Java class?

nag9s2007 said...

yeah here is the client code


//SimpleSessionClient.java
package client;

import beans.SimpleSession;
import javax.naming.InitialContext;

public class SimpleSessionClient {
public static void main(String[] args) throws Exception
{
InitialContext ctx = new InitialContext();
SimpleSession simpleSession
= (SimpleSession) ctx.lookup(SimpleSession.class.getName());
for (int i = 0; i < args.length; i++) {
String returnedString = simpleSession.getEchoString(args[i]);
System.out.println("sent string: " + args[i] +", received string: " + returnedString);
}
}
}

and the trace is
Exception in thread "main" javax.naming.NameNotFoundException: beans.SimpleSession not bound

nag9s2007 said...

and also plz let me know the links abt good tutorials , books about EJB

Jaikiran said...

ctx.lookup(SimpleSession.class.getName());

The lookup string is incorrect. If your JNDI view shows that the bean is bound to:

+- SimpleSessionBean (class: org.jnp.interfaces.NamingContext)
| +- remote (proxy: $Proxy74 implements interface beans.SimpleSession,interface org.jboss.ejb3.JBossProxy,interface javax.ejb.EJBObject)

then you should lookup with that exact same string. Change your lookup code to:

ctx.lookup("SimpleSessionBean/remote");

nag9s2007 said...

yesss ,,,, i know it is working with when I changed it to:

SimpleSession simpleSession = (SimpleSession) ctx.lookup("SimpleSessionBean/remote");


But ,

What is the correct naming convention to use for this? Why was the tutorial referring to the interface, while for it to work I had to change it to the actual implementation?

nag9s2007 said...

this is another issue....

client code is

package com.webage.client;

import java.util.Properties;

import javax.naming.*;
import com.webage.ejbs.SimpleBean;

public class TestClient {

public void runTest() throws Exception {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
props.setProperty("java.naming.provider.url", "localhost");

InitialContext ctx = new InitialContext(props);
SimpleBean bean = (SimpleBean) ctx.lookup("firstebj3/SimpleBeanImpl/remote");
String result = bean.sayHello("Billy Bob");
System.out.println(result);
}

public static void main(String[] args) {
try {
TestClient cli = new TestClient();
cli.runTest();

} catch (Exception e) {
e.printStackTrace();
}
}
}

JNDI View is

+- firstejb3 (class: org.jnp.interfaces.NamingContext)
| +- SimpleBeanImpl (class: org.jnp.interfaces.NamingContext)
| | +- remote (proxy: $Proxy79 implements interface com.webage.ejbs.SimpleBean,interface org.jboss.ejb3.JBossProxy,interface javax.ejb.EJBObject)


Stack trace is

javax.naming.NameNotFoundException: firstebj3 not bound
at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)
at org.jnp.server.NamingServer.getObject(NamingServer.java:543)
at org.jnp.server.NamingServer.lookup(NamingServer.java:267)
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:597)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:305)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:627)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:589)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.webage.client.TestClient.runTest(TestClient.java:17)
at com.webage.client.TestClient.main(TestClient.java:25)

Jaikiran said...

bestalltimes said:

ctx.lookup("firstebj3/SimpleBeanImpl/remote");


You have a typo in that lookup string. firstebj3 should be changed to firstejb3

bestalltimes said:

What is the correct naming convention to use for this? Why was the tutorial referring to the interface, while for it to work I had to change it to the actual implementation?



*By default*, that's what the JNDI name is. You can specify any JNDI name you want to use either using annotations or through jboss.xml.

nag9s2007 said...

Thanks ... especially for Typo mistake :-)

nag9s2007 said...

If u dont mind , please send me the links that describe the EJB to the point
Thanks in advance

Jaikiran said...

bestalltimes said:

If u dont mind , please send me the links that describe the EJB to the point


When i started off with EJB (2.x) i used "Mastering EJB". At present the new version of EJB3 has a lot of improvements for which you might want to refer "Mastering EJB3". I personally havent read that book yet.

I always hang out at www.javaranch.com site. This site has a section for various book reviews. Here's the url for the EJB book review section: http://www.javaranch.com/bunkhouse/JavaBeans.jsp
This might help you in deciding which one to go for.

Dimitris Andreadis said...
This comment has been removed by a blog administrator.
Anonymous said...

Hi Jai,
what you have written over here is pretty simple and I just got the hang of things in a couple of minutes.

I had this helloworld app and deployed this in glassfish and jBoss. I did not know that we need to use a different lookup format for locating beans for different App Servers.

But now I am able to say " hello " in any app server.

Thanks!

Anonymous said...

Hello,

What's the difference between 'not bound' and 'JNDI implementation returned null'

Thanks

Jaikiran said...

Anonymous said

What's the difference between 'not bound' and 'JNDI implementation returned null'

Sorry, i can't recollect seeing such an error, before.

Unknown said...

Great!

Got mine working on jboss!

Your post did it!

Youre heaven sent Jaikiran!

-Jesus Angeles/ fellow javarancher

Jaikiran said...

Jesus Angeles wrote

Great!

Got mine working on jboss!

Your post did it!


Glad to know that, this post helped :)

Arham said...

Hi,I was facing this same exception while doing a JNDI lookup when working on Jboss 4.2.0 eap and JBoss-messaging 1.4.0 GA.
I'm confused about what do I feed as a parameter to the following:
QueueConnectionFactory = (QueueConnectionFactory) ictx.lookup("...");

I went as per your this post of yours and got the following while invoking the list:

java: Namespace

+- DefaultDS (class: org.jboss.resource.adapter.jdbc.WrapperDataSource)
+- SecurityProxyFactory (class: org.jboss.security.SubjectSecurityProxyFactory)
+- DefaultJMSProvider (class: org.jboss.jms.jndi.JNDIProviderAdapter)
+- atgpub_ds (class: org.jboss.resource.adapter.jdbc.WrapperDataSource)
+- comp (class: javax.naming.Context)
+- JmsXA (class: org.jboss.resource.adapter.jms.JmsConnectionFactoryImpl)
+- jaas (class: javax.naming.Context)
| +- messaging (class: org.jboss.security.plugins.SecurityDomainContext)
| +- jmx-console (class: org.jboss.security.plugins.SecurityDomainContext)
| +- JmsXARealm (class: org.jboss.security.plugins.SecurityDomainContext)
+- comp.original (class: javax.namingMain.Context)
+- timedCacheFactory (class: javax.naming.Context)
Failed to lookup: timedCacheFactory, errmsg=org.jboss.util.TimedCachePolicy
+- TransactionPropagationContextExporter (class: com.arjuna.ats.internal.jbossatx.jta.PropagationContextManager)
+- StdJMSPool (class: org.jboss.jms.asf.StdServerSessionPoolFactory)
+- Mail (class: javax.mail.Session)
+- comp.ejb3 (class: javax.naming.Context)
| NonContext: null
+- TransactionPropagationContextImporter (class: com.arjuna.ats.internal.jbossatx.jta.PropagationContextManager)
+- atgcore_ds (class: org.jboss.resource.adapter.jdbc.WrapperDataSource)
+- TransactionManager (class: com.arjuna.ats.jbossatx.jta.TransactionManagerDelegate)


Global JNDI Namespace

+- TransactionSynchronizationRegistry (class: com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple)
+- UserTransactionSessionFactory (proxy: $Proxy14 implements interface org.jboss.tm.usertx.interfaces.UserTransactionSessionFactory)
+- UUIDKeyGeneratorFactory (class: org.jboss.ejb.plugins.keygenerator.uuid.UUIDKeyGeneratorFactory)
+- console (class: org.jnp.interfaces.NamingContext)
| +- PluginManager (proxy: $Proxy50 implements interface org.jboss.console.manager.PluginManagerMBean)
+- topic (class: org.jnp.interfaces.NamingContext)
+- queue (class: org.jnp.interfaces.NamingContext)
+- UserTransaction (class: org.jboss.tm.usertx.client.ClientUserTransaction)
+- local (class: org.jnp.interfaces.NamingContext)
| +- ClasspathSession@3425314 (proxy: $Proxy58 implements interface atg.dafdeploy.ClasspathSessionLocalHome)
+- jmx (class: org.jnp.interfaces.NamingContext)
| +- invoker (class: org.jnp.interfaces.NamingContext)
| | +- RMIAdaptor (proxy: $Proxy49 implements interface org.jboss.jmx.adaptor.rmi.RMIAdaptor,interface org.jboss.jmx.adaptor.rmi.RMIAdaptorExt)
| +- rmi (class: org.jnp.interfaces.NamingContext)
| | +- RMIAdaptor[link -> jmx/invoker/RMIAdaptor] (class: javax.naming.LinkRef)
+- HiLoKeyGeneratorFactory (class: org.jboss.ejb.plugins.keygenerator.hilo.HiLoKeyGeneratorFactory)

Thanks in advance.

Jaikiran said...

Arham,

Are you sure, you have installed and deployed the JBoss Messaging related artifacts correctly? The jndi contents that you posted does not contain anything related to the JMS. I would have expected a jndi-name "ConnectionFactory" to be present in the global JNDI namespace. You could then have used this in the lookup string.

QueueConnectionFactory qf = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");

Arham said...

Hi,I did try for using "ConnectionFactory" as a parameter in the lookup method tht U've suggested..but still teh same exception...Could you tell me as to how can I register my ConnectionFactory under Global namespace. I guess it's something to do with jms-ds.xml...but still cant figure out how to...:'(
Thanx in advance

Arham said...

Hi,I've verified that my installations are proper according to the document of jboss-messaging,could you redirect me to some links or pointers that could help me figure out what am I missing. :(

Unknown said...

I am also facing the same issue as Arham. I have deployed Connection Factory and Topic which are visible in jboss.messaging.connectionfactory and jboss.messaging.destination, however the JNDI name are not visible in JNDI view.

I am using JBoss EAP 4.3 and JBoss Messaging 1.4.0_SP3

As such I am experiencing NameNotFoundException.

Thanks for the help..

Jaikiran said...

gauravkbansal wrote

I am also facing the same issue as Arham...


Gaurav, could you please start a topic in the JBoss JNDI forum with more details about your setup and the exception you are seeing

Jay said...

I am also having similar issue. Any help will be highly appreciated

Here is my scenario:
I have EJB deployed on the localhost and port 1099 on JBoss (3.2.x) server on one application

Here is JNDI looks like from 1099 server…

Global JNDI Namespace
+- com (class: org.jnp.interfaces.NamingContext)
| +- retek (class: org.jnp.interfaces.NamingContext)
| | +- rsm (class: org.jnp.interfaces.NamingContext)
| | | +- external (class: org.jnp.interfaces.NamingContext)
| | | | +- service (class: org.jnp.interfaces.NamingContext)
| | | | | +- SecurityDisplayableReferenceService (proxy: $Proxy194 implements interface com.retek.rsm.external.service.SecurityDisplayableReferenceServiceRemoteHome,interface javax.ejb.Handle)
| | | | | +- SecurityDisplayableReferenceServiceLocal (proxy: $Proxy108 implements interface com.retek.rsm.external.service.SecurityDisplayableReferenceServiceLocalHome)


On the same box I have another application which calls above EJB using the JNDI, This application runs on separate JBoss server on port 1089.

Here is the code..

System.out.println("Locating jndi properties...");

try {
String rpmJndiName = "com/retek/rsm/external/service/SecurityDisplayableReferenceService";
ResourceBundle bundle = ResourceBundle.getBundle("jndi");

provider = “localhost:1099”;
factory = “org.jnp.interfaces.NamingContextFactory”;

if (null == provider || null == factory) { throw new RuntimeException(
"Unable to locate the provider url or factory in the jndi.properties file."); }
} catch (Exception e) {
throw new RuntimeException("Unable to load the jndi.properties file", e);
}

System.out.println("Validating jndi set up on server " + provider + " \n\twith " + factory
+ " factory...");

try {
InitialContext rpmAdminContext = getInitialContext(rpmJndiName);
rpmDisplayableReferenceServiceRaw = rpmAdminContext.lookup(rpmJndiName);
rpmAdminContext.close();

Getting following error:

java.lang.RuntimeException: javax.naming.NameNotFoundException: external not bound
Unable to locate jndiName on the jndi server. Check the jndi registry to ensure that the security service is appropriately set.
at com.retek.rsm.TestRemoteConnectionRPM.main(TestRemoteConnectionRPM.java:65)
Caused by: javax.naming.NameNotFoundException: external not bound
at org.jnp.server.NamingServer.getBinding(NamingServer.java:490)
at org.jnp.server.NamingServer.getBinding(NamingServer.java:498)
at org.jnp.server.NamingServer.getObject(NamingServer.java:504)
….
….

Please help!!

Anonymous said...

Hi Jai,

Your blogs are really good.
I have 1 question.As of my perception says..When we have client and app. server in the same maching it implies that we are running under the same JVM.When I read your this blog it says

Whereas, in case of Global JNDI namespace, the objects bound in this namespace can be looked-up by clients, even if they are not in the same JVM as the server. One would ask, how does this matter? Consider a standalone java program(client) which tries to lookup some object on the server (running in its own JVM). Whenever a standalone client is started (using the java command), a new JVM is instantiated. As a result, the server (which is started in its own JVM) and the client are running on different JVMs. Effectively, the client will NOT be able to lookup objects bound in the java: namespace of the server. However, the client can lookup the objects present in the Global JNDI namespace of the server.

It made be in confusion.Can you explain me bit more in this.

Thanks once again.

Rahul B.

Anonymous said...

Just want to say Thanks so much for your posting - it explained a lot. The Global Namespace stuff is not entirely clear for newbies. It would be helpful to have an example on the JBoss page to that effect. I have spent hours on this!

Jaikiran said...

Good to know it helped you :)

Corindiano said...

Hello Jai, excellent post by the way. I've got the following situation. I've injected (via guice) an ejb3 ref in a struts2 action. This is what I get in my global jndi view:
+- ep (class: org.jnp.interfaces.NamingContext)
| +- com.xyz.services.UserServiceBean (class: org.jnp.interfaces.NamingContext)
| | +- remote (proxy: $Proxy105 implements interface com.xyz.services.UserService,interface org.jboss.ejb3.JBossProxy)
| +- UserServiceBean (class: org.jnp.interfaces.NamingContext)

According to your post, I should reference the object via "ep/com.xyz.services.UserServiceBean/remote", right? Well, I keep getting this when I try to access this object and cast it to its interface (UserServiceBean implements UserService):

java.lang.ClassCastException
java.lang.Class.cast(Class.java:2990)
com.google.inject.jndi.JndiIntegration$JndiProvider.get(JndiIntegration.java:59)
com.google.inject.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:41)
com.google.inject.InjectorImpl$SingleFieldInjector.inject(InjectorImpl.java:473)


Any thoughts?

Thanks a lot!

Jaikiran said...

Corindiano,


You are using the correct lookup string. The ClassCastException either means an incorrect code or some classloading issue. Could you please create a forum thread here so that we can discuss this in detail? Please provide the entire exception stacktrace and other details there while posting. Thanks.

Praveen Peddi said...

Hi Jai Kiran,
This was a nice article. Thanks for posting it.
Here is my issue. I am configuring and using jndi names in spring. My issue is as follows.
We have different datasources already configured in jboss and being used for years. Now I need to configure same datasources in a different application (spring based) so the same code that looks up jndi names in jboss should also work in spring app. I do not have the flexibility of changing datasource jndi names.
Current jndi lookup code is as follows:
Context jndiContext = new InitialContext();
datasource = (DataSource) jndiContext.lookup("java:/aDSName");

This above code works when called with in jboss but not in spring app.
I used xbean-spring-common api to configure datasource jndi with jndiname = "java:/aDSName" as described here: http://servicemix.apache.org/jndi-configuration.html

But the above code does NOT work when called from spring app. Gives following exception. Since your article doesn't cover spring based jndi lookup, I thought of asking you, in case you can help me.
javax.naming.NameNotFoundException: aDSName
at org.apache.xbean.spring.jndi.DefaultContext.lookup(DefaultContext.java:147)
at javax.naming.InitialContext.lookup(InitialContext.java:351)

Anonymous said...

I m Sandeep

Neatly explained. Thanks for such a good explanation.

deepa said...

hi,

I am new to jboss & i am pretty stucked with the naming exception here is my stack trace
-----------------------------------

sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.jboss.ha.framework.interfaces.HARMIClient.invoke(HARMIClient.java
:187)
at $Proxy161.lookup(Unknown Source)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:610)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:572)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at org.jboss.resource.adapter.jms.JmsManagedConnection.setup(JmsManagedC
onnection.java:682)
... 52 more
11:04:37,788 INFO [STDOUT] javax.jms.JMSException: Could not create a session:
javax.resource.spi.CommException: javax.naming.NameNotFoundException: XAConnecti
onFactory
11:04:37,788 INFO [STDOUT] at org.jboss.resource.adapter.jms.JmsSessionFact
oryImpl.allocateConnection(JmsSessionFactoryImpl.java:392)
11:04:37,788 INFO [STDOUT] at org.jboss.resource.adapter.jms.JmsSessionFact
oryImpl.createQueueSession(JmsSessionFactoryImpl.java:133)
11:04:37,788 INFO [STDOUT] at Bean.JMSRABean.sendTxnToInputTrxnQueue(JMSRAB
ean.java:114)
11:04:37,788 INFO [STDOUT] at sun.reflect.GeneratedMethodAccessor85.invoke(
Unknown Source)
11:04:37,788 INFO [STDOUT] at sun.reflect.DelegatingMethodAccessorImpl.invo
ke(DelegatingMethodAccessorImpl.java:25)
11:04:37,788 INFO [STDOUT] at java.lang.reflect.Method.invoke(Method.java:5
97)
11:04:37,788 INFO [STDOUT] at org.jboss.invocation.Invocation.performCall(I
nvocation.java:345)
11:04:37,788 INFO [STDOUT] at org.jboss.ejb.StatelessSessionContainer$Conta
inerInterceptor.invoke(StatelessSessionContainer.java:214)
11:04:37,788 INFO [STDOUT] at org.jboss.resource.connectionmanager.CachedCo
nnectionInterceptor.invoke(CachedConnectionInterceptor.java:185)
11:04:37,788 INFO [STDOUT] at org.jboss.webservice.server.ServiceEndpointIn
terceptor.invoke(ServiceEndpointInterceptor.java:51)
11:04:37,788 INFO [STDOUT] at org.jboss.ejb.plugins.CallValidationIntercept
or.invoke(CallValidationInterceptor.java:48)
11:04:37,788 INFO [STDOUT] at org.jboss.ejb.plugins.AbstractTxInterceptor.i
nvokeNext(AbstractTxInterceptor.java:105)
11:04:37,788 INFO [STDOUT] at org.jboss.ejb.plugins.AbstractTxInterceptorBM
T.invokeNext(AbstractTxInterceptorBMT.java:153)
11:04:37,788 INFO [STDOUT] at org.jboss.ejb.plugins.TxInterceptorBMT.invoke
(TxInterceptorBMT.java:62)
11:04:37,788 INFO [STDOUT] at org.jboss.ejb.plugins.StatelessSessionInstanc
eInterceptor.invoke(StatelessSessionInstanceInterceptor.java:130)
11:04:37,788 INFO [STDOUT] at org.jboss.ejb.plugins.SecurityInterceptor.inv
oke(SecurityInterceptor.java:139)
11:04:37,788 INFO [STDOUT] at org.jboss.ejb.plugins.LogInterceptor.invoke(L
ogInterceptor.java:192)
11:04:37,788 INFO [STDOUT] at org.jboss.ejb.plugins.ProxyFactoryFinderInter
ceptor.invoke(ProxyFactoryFinderInterceptor.java:122)

-----------------
I am not able to find the queue name in the global jndi namespace & jboss version is 4.0.2

Anonymous said...

hi m new to jboss. m getting the error cannot lookup datasource with JNDI Name : jdbc-ds-SEC .I am not able to view the jndi name in the jndi tree.what i should do.

Anonymous said...

thanks JaiKiran by the help of this post i have successfully created my ejb hello word application.

Maxx said...

"Now let's move on to the java: namespace in the JNDI tree view above. The difference between a Global JNDI namespace and the java: namespace is that, the object bound in the java: namespace can be looked-up ONLY by clients within the SAME JVM."

Bless you, my friend. I have wasted 1 day with this!

Anonymous said...

Hi Jai,

Im trying to lookup an application/Bean name.While taking the jmx-console im not able to see the application in the jndi tree.My application is say x.ear and im trying to access a bean in x.ear from another module packed in y.war.Both of them are running on the same jboss.Pls help

Jaikiran said...

Please create a forum thread here http://www.coderanch.com/forums/f-63/JBoss and provide all the relevant details like the JBoss/WildFly version you are using and the code snippets. Also post there, how you are deploying the application