3 Replies Latest reply on Feb 12, 2004 5:53 PM by erik777

    EJB Lookup, Connection Refused.

    troyrudolph

      I could use a little help, please. When I try to connect to a JBoss running on a remote system, I always get "connection refused" when connecting to JBoss. It works fine when I run the program on the same machine as JBoss. I'm sure I'm missing something obvious. Thanks in advance for any help.

      I have JBoss 2.4.4 with J2SDK 1.4.0 running on RedHat 6.2. I have a simple client program (see below) running on a Win2K with J2SDK 1.4.0.

      Here's the exception information. Note the reference to 127.0.0.1 even though the client specified 130.200.138.11, the remote machine.

      F:\dev\EJBClient>java EJBClient
      locating Calculator
      javax.naming.CommunicationException. Root exception is java.rmi.ConnectExceptio
      n: Connection refused to host: 127.0.0.1; nested exception is:
      java.net.ConnectException: Connection refused: connect
      at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:567)
      at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:185
      )
      at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:171)
      at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:101)
      at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:349)
      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:333)
      at javax.naming.InitialContext.lookup(InitialContext.java:347)
      at EJBClient.main(EJBClient.java:22)
      Caused by: java.net.ConnectException: Connection refused: connect
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:295)
      at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:161)
      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:148)
      at java.net.Socket.connect(Socket.java:425)
      at java.net.Socket.connect(Socket.java:375)
      at java.net.Socket.(Socket.java:290)
      at java.net.Socket.(Socket.java:118)
      at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirect
      SocketFactory.java:22)
      at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMaster
      SocketFactory.java:122)
      at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:562)
      ... 8 more

      Here's the client program - no rocket science.

      public class EJBClient
      {
      public static void main(String [] args)
      {
      try
      {
      Context jndiContext = getInitialContext();
      System.out.println("locating Calculator" );
      Object obj = jndiContext.lookup("Calculator");

      System.out.println("getting the home interface" );
      CalculatorHome home =
      (CalculatorHome) javax.rmi.PortableRemoteObject.narrow(obj, CalculatorHome.class);

      Calculator calc = home.create();
      System.out.println("created it!");

      int i = calc.add( 2, 2 );
      System.out.println( "2 + 2 = " + i );
      }
      catch (Exception ex){ex.printStackTrace();}
      }

      public static Context getInitialContext()
      throws javax.naming.NamingException
      {
      Properties p = new Properties();
      // JBoss
      p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      p.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
      p.put(Context.PROVIDER_URL, "jnp://130.200.138.11:1099");
      return new InitialContext(p);
      }
      }

      Please let me know what I have done wrong. Thanks.

        • 1. Re: EJB Lookup, Connection Refused.
          • 2. Re: EJB Lookup, Connection Refused.
            troyrudolph

            I added "-Djava.rmi.server.hostname=" to the run.sh file on the server. It solved the problem.

            Many, many thanks!

            • 3. Re: EJB Lookup, Connection Refused.
              erik777

              This blows me away because when I set JBoss' host to be 192.168.1.8 via --host= I get connection refused from 127.0.0.1. But, when i set JBoss' host to 127.0.0.1, I get connection refused from 192.168.1.8!

              Here is the code I'm using to try to connect:

               Context context = getContext();
               log.debug(" Context.DNS_URL: " + context.DNS_URL);
               log.debug("Context.INITIAL_CONTEXT_FACTORY: " +
              context.INITIAL_CONTEXT_FACTORY);
               log.debug(" Context.PROVIDER_URL: " +
              context.PROVIDER_URL);
               Hashtable table = context.getEnvironment();
               String url = (String) table.get(context.PROVIDER_URL);
               log.debug(" URL: " + url);
               ClientConfig config = new ClientConfig();
               config.setRemoteJNDIhost("192.168.1.8");
               url = config.getRemoteJNDIurl();
               log.debug(" new URL: " + url);
               try {
               table.put(Context.PROVIDER_URL, url);
               context = new InitialContext(table);
               Object remoteObject =
               context.lookup(url + "/" + SECURITY_CONTEXT_NAME +
              "/SecurityController");
               log.debug("Obtained remote home interface. Narrowing...");
               securityControllerHome =
              
              (SecurityControllerHome)PortableRemoteObject.narrow(remoteObject,
               SecurityControllerHome.class);
               return securityControllerHome;
               } catch (Exception ex) {
               log.debug("Caughting exception trying to get remote interface");
               log.debug("Message: " + ex.getMessage());
               ex.printStackTrace();
               }
              


              I'm using JBoss 3.2.3.

              If this is not the correct way to connect to an EJB in another container, then can someone please clarify.

              The actual host IP will be dynamically configured. It cannot be in the ear. So I am intentionally using the JNDI code to get it to look at the host I specify.

              The code above is just test code to get it to work. Later I'll clean it up and make it truly dynamic.