Link to home
Create AccountLog in
Avatar of jwright9
jwright9

asked on

Getting javax.naming.NameNotFoundException on JNDI Context lookup method call

When I execute this code:
 try {
    Hashtable <String, String> env = new Hashtable<String, String>();    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); env.put("java:comp/env/jms/myQueueConnectionFactory", "javax.jms.QueueConnectionFactory");
env.put("Fish", "AndChips");
Context jndiContext = new InitialContext(env); testval = (String) jndiContext.lookup("Fish");

 } catch (NamingException ne)
{ ne.printStackTrace(); }

I get a javax.naming.NameNotFoundException
The stack trace is as follows:

javax.naming.NameNotFoundException: Fish
      at com.sun.jndi.fscontext.RefFSContext.getObjectFromBindings(RefFSContext.java:400)
      at com.sun.jndi.fscontext.RefFSContext.lookupObject(RefFSContext.java:327)
      at com.sun.jndi.fscontext.RefFSContext.lookup(RefFSContext.java:146)
      at com.sun.jndi.fscontext.FSContext.lookup(FSContext.java:127)
      at javax.naming.InitialContext.lookup(Unknown Source)
      at gov.hhs.cms.rnc.SampleRNCTest2.testRNC(SampleRNCTest2.java:63)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

It is unable to find the Fish name in the context.  Is there a way to overcome this?
Avatar of Am P
Am P
Flag of India image

From where you got this piece of code ?

You can try the following:


try {
    Hashtable <String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
    env.put("java:comp/env/jms/myQueueConnectionFactory", "javax.jms.QueueConnectionFactory");
Context jndiContext = new InitialContext(env);

jndiContext.bind("Fish", "AndChips");


String testval = (String) jndiContext.lookup("Fish");

 } catch (NamingException ne)
{ ne.printStackTrace(); }
Avatar of jwright9
jwright9

ASKER

I ran my code as you suggested:

package com.teststuff.mycode;

import static org.easymock.EasyMock.createMock;

import org.easymock.EasyMock;

import java.io.IOException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import java.util.Hashtable;
import org.junit.Test;


import javax.jms.QueueConnectionFactory;



public class SampleRNCTest2 {
      
      QueueConnectionFactory queueConnectionFactory = null;
      String testval = null;
      
      
      
      @Test
      public void testRNC() throws IOException {
      
            try {
                Hashtable <String, String> env = new Hashtable<String, String>();
                env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
                env.put("java:comp/env/jms/myQueueConnectionFactory", "javax.jms.QueueConnectionFactory");
            Context jndiContext = new InitialContext(env);
            String tmpVal = "AndChips";
            jndiContext.bind("Fish", tmpVal);


            String testval = (String) jndiContext.lookup("Fish");

             } catch (NamingException ne)
            { ne.printStackTrace(); }
            
                     
      }

}

And got this error:

javax.naming.OperationNotSupportedException: Can only bind References or Referenceable objects
      at com.sun.jndi.fscontext.RefFSContext.addObjectToBindings(RefFSContext.java:479)
      at com.sun.jndi.fscontext.RefFSContext.bindObject(RefFSContext.java:337)
      at com.sun.jndi.fscontext.RefFSContext.bind(RefFSContext.java:169)
      at com.sun.jndi.fscontext.FSContext.bind(FSContext.java:167)
      at javax.naming.InitialContext.bind(Unknown Source)
      at gov.hhs.cms.rnc.SampleRNCTest2.testRNC(SampleRNCTest2.java:36)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
I could go to the effort of making an example to do what you are trying to do here (ie. store a string in the directory) but I am guessing that is NOT your final goal, so I don't want to waste my time UNLESS that really is what you want.

Can you please say what you are actually trying to achieve? Then we can help with that!
I'm trying to create an EasyMock test so that I can create a mock JMS queue so that I can send messages to the mock queue and get a canned response back.  Part of this problem was to figure out how to bind to a context via JNDI.  I have a rather large application running agains an h2 database and it uses message queues.  The end to end Junit test is working.  The part that is failing is the mock queue that I have set up.  I get an EasyMock answer of no behavior specified.  So the mock queue is being found however I don't know where to put the EasyMock expect statement to get a the mocked (canned) response back.  This is a complex problem and I have been trying for quite a while to get this to work.  Thanks for any help you can give me.
ASKER CERTIFIED SOLUTION
Avatar of Am P
Am P
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Amit,  Thanks.  Very good articles.  For now I am stubbing out the class that sends an email message.  But I will definitely keep these articles in mind and bookmarked.  I am pretty sure I will be needing to mock out message sooner than later.