views:

171

answers:

3
+1  Q: 

EJB client error

I created an EJB 2. I deployed it on JBoss-IDE 1.6 and then, I created a JUnit test to access it. here's the code I used for the client :

   public class DossierBeanTest extends TestCase {
         protected DossierHome dossierHome;
         protected Dossier dossier;

         public DossierBeanTest(String argo){
                super(arg0);
         }

         protected void setUp() throws Exception {
                super.setUp();
                Hashtable env=new Hashtable();


               env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
                env.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
                env.put(Context.PROVIDER_URL,"jnp://localhost:1199");
                Context context=new InitialContext(env);

                Object finderRef=context.lookup(dossierHome.JNDI_NAME);
                dossierHome=(DossierHome)PortableRemoteObject.narrow(finderRef,DossierHome.class);
                dossier=dossierHome.create();

          }

          public void testGetString() throws RemoteException {
                 assertEquals("Test",dossier.getTest());
          }

but the problem is that this client fails with the error :

      javax.naming.CommunicationException: Could not obtain connection to any of these  
      urls: localhost:1199 and discovery failed with error :  
      javax.naming.CommunicationException: Receive timed out [Root Exception is 
      java.net.SocketTimeoutException: Receive timed out][Root exception is  
      javax.naming.CommunicationException: Failed to connect to server localhost:1199 
      ....


      at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1416) 

....

A: 

I'd guess you are using the wrong port number. You can check this first by looking at the output of the command netstat -a. If no process is listening on port 1199 you have to inspect your JBoss configuration.

tangens
which config file should I change ?
Attilah
Look at the file "jboss-service.xml" and there at <mbean name="jboss:service=naming"/>.
tangens
A: 

The default port is 1099, did you change it? if not try switching the port from 1199.

Also if you are on Linux you may want to try 127.0.0.1 instead of localhost.

David Holbrook
i tried switching to 127.0.0.1 . and it's the same error.
Attilah
David Holbrook
A: 

As other already pointed out, JBoss naming server listens by default on port 1099. So, unless you changed this value in conf/jboss-service.xml (service name jboss:service=Naming), you should write:

env.put(Context.PROVIDER_URL,"jnp://localhost:1099); 

That being said, I usually prefer to provide the JNDI environment settings in a jndi.properties file (that you put on the classpath). In your case, with the following content:

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099

And use the empty parameter constructor InitiatContext():

Context context = new InitialContext();

This makes the code portable (might not be an issue in a testing context, well, I prefer to not hard code such things).

Pascal Thivent