3 Replies Latest reply: Jun 28, 2010 6:32 PM by 843798 RSS

    Cannot connect a Client app to Server App.

    843798
      Hi
      I am having problems getting a Client application to connect to a Server application without MA-SSL, though JConsole working fine.

      Following various blogs and bits and pieces, I have now been successful getting the MA-SSL connections working, though the Server application needed to be aware of and setup the SSL stuff.

      BUT when I comment this block out and turn off the security options, I cannot get a non JConsole client to attach.

      Server.java
      package com.example.mbeans;

      import java.lang.management.*;
      import javax.management.*;

      public class Main {
      public static void main(String[] args) throws Exception {
      MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
      ObjectName name = new ObjectName("com.example.mbeans:type=Hello");

      Hello mbean = new Hello();
      mbs.registerMBean(mbean, name);
      System.out.println("Waiting forever...");
      Thread.sleep(Long.MAX_VALUE);
      }
      }

      Started up under linux with
      java \
      -classpath . \
      -Dcom.sun.management.jmxremote \
      -Dcom.sun.management.jmxremote.ssl \
      -Dcom.sun.management.jmxremote.registry.ssl=false \
      -Dcom.sun.management.jmxremote.ssl.need.client.auth=false \
      -Dcom.sun.management.jmxremote.authenticate=false \
      -Dcom.sun.management.jmxremote.port=5556 \
      com.example.mbeans.Main

      On Windows,
      jconsole Config427VM0:5556
      Connects wonderfully.

      BUT, I cannot find any sort of combination of sample fragments that will connect to this remote process.
      For example I have

      import javax.management.MBeanServerConnection;
      import javax.management.Notification;
      import javax.management.NotificationListener;
      import javax.management.remote.JMXConnector;
      import javax.management.remote.JMXConnectorFactory;
      import javax.management.remote.JMXServiceURL;

      import org.junit.Test;

      public class Testing {
      @SuppressWarnings("static-access")
      @Test
      public void testMe() throws Exception {
      Client test = new Client();
      test.main(null);
      }

      public static class Client {
      public static void main(String[] args) {
      String host = "Config427VM0";
      String port = "5556";
      if (args != null && args.length == 0) {
      System.out.println("Attempting to connect to port " + port + " on localhost");
      }
      if (args != null && args.length > 0) {
      host = args[0];
      if (args.length > 1) {
      port = args[1];
      }
      System.out.println("Attempting to connect to port " + port + " on " + host);
      }
      try {
      System.out.println("\nCreate an RMI connector client and " + "connect it to the RMI connector server");
      JMXServiceURL url = new JMXServiceURL("jmxmp", host, Integer.parseInt(port));
      JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
      ClientListener listener = new ClientListener();
      MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
      String domains[] = mbsc.getDomains();
      for (int i = 0; i < domains.length; i++) {
      System.out.println("\tDomain[" + i + "] = " + domains);
      }
      } catch (Exception e) {
      System.err.println("Error: " + e.getMessage());
      }
      }
      }

      public static class ClientListener implements NotificationListener {
      public void handleNotification(Notification notification, Object handback) {
      System.out.println("\nReceived notification: " + notification);
      }
      }
      }
      which hangs on connection out of Eclipe, but throws a
      java.net.ConnectException: Connection refused: connect
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
      at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
      at java.net.Socket.connect(Socket.java:520)
      at java.net.Socket.connect(Socket.java:470)
      at java.net.Socket.<init>(Socket.java:367)
      at java.net.Socket.<init>(Socket.java:180)
      at com.sun.jmx.remote.socket.SocketConnection.connect(SocketConnection.java:94)
      at com.sun.jmx.remote.generic.ClientSynchroMessageConnectionImpl.connect(ClientSynch
      :70)
      at javax.management.remote.generic.GenericConnector.connect(GenericConnector.java:17
      at javax.management.remote.jmxmp.JMXMPConnector.connect(JMXMPConnector.java:119)
      at javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:248)
      at Client.main(Client.java:57)

      When the example executes from command line using
      java -classpath .;client2;jmxremote_optional.jar -Djavax.net.debug=all Client Config427VM0 3001

      We are wanting the Non Secure for our dev work so that we don't have to muck around with setting up SSL on everyones dev machines when we really only need it in our Test/Prod environments.

      Any suggestions on where I am going wrong?

      Thanks...Andrew
        • 1. Re: Cannot connect a Client app to Server App.
          843798
          Hi Andrew,

          As far as I can tell here is the mistake:
          System.out.println("\nCreate an RMI connector client and "  "connect it to the RMI connector server");
          JMXServiceURL url = new JMXServiceURL("jmxmp", host, Integer.parseInt(port));
          You are creating an URL for using the optional JMXMP protocol - but your server is using the standard
          RMI connector.

          Try something like:
          new JMXServiceURL("service:jmx:rmi:///jndi/rmi://"+host+":"+port+"/jmxrmi");
          See also here:
          [http://blogs.sun.com/jmxetc/entry/how_to_retrieve_remote_jvm|http://blogs.sun.com/jmxetc/entry/how_to_retrieve_remote_jvm]

          -- daniel
          [http://blogs.sun.com/jmxetc|http://blogs.sun.com/jmxetc]
          • 2. Re: Cannot connect a Client app to Server App.
            843798
            Thanks for your quick reply! Quick change as suggested brings back the following exception, which seems to imply a need for some sort of rmi process, but starting a rmiregistry with the same port failed earlier today. Anyway I'll have a look at your suggested post tomorrow when I get in (its 10:30 pm here now) as I'm too blearly eyed right now :-)

            java.io.IOException: Failed to retrieve RMIServer stub: javax.naming.ServiceUnavailableException [Root exception is java
            .rmi.ConnectException: Connection refused to host: Config427VM0; nested exception is:
                    java.net.ConnectException: Connection refused: connect]
            at javax.management.remote.rmi.RMIConnector.connect(RMIConnector.java:317)
            at javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:248)
            at Client.main(Client.java:54)
            Caused by: javax.naming.ServiceUnavailableException [Root exception is java.rmi.ConnectException: Connection refused to
            host: Config427VM0; nested exception is:
                    java.net.ConnectException: Connection refused: connect]
            at com.sun.jndi.rmi.registry.RegistryContext.lookup(RegistryContext.java:97)
            at com.sun.jndi.toolkit.url.GenericURLContext.lookup(GenericURLContext.java:185)
            at javax.naming.InitialContext.lookup(InitialContext.java:351)
            at javax.management.remote.rmi.RMIConnector.findRMIServerJNDI(RMIConnector.java:1817)
            at javax.management.remote.rmi.RMIConnector.findRMIServer(RMIConnector.java:1787)
            at javax.management.remote.rmi.RMIConnector.connect(RMIConnector.java:259)
            ... 2 more
            Caused by: java.rmi.ConnectException: Connection refused to host: Config427VM0; nested exception is:
            java.net.ConnectException: Connection refused: connect
            at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:574)
            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.newCall(UnicastRef.java:306)
            at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
            at com.sun.jndi.rmi.registry.RegistryContext.lookup(RegistryContext.java:93)
            ... 7 more
            Caused by: java.net.ConnectException: Connection refused: connect
            at java.net.PlainSocketImpl.socketConnect(Native Method)
            at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
            at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
            at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
            at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
            at java.net.Socket.connect(Socket.java:520)
            at java.net.Socket.connect(Socket.java:470)
            at java.net.Socket.<init>(Socket.java:367)
            at java.net.Socket.<init>(Socket.java:180)
            at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)
            at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)
            at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:569)
            ... 12 more
            • 3. Re: Cannot connect a Client app to Server App.
              843798
              Interesting.
              Once I changed
              <code>
              JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi");
              JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
              </code>
              to
              <code>
              JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi");
              JMXConnector jmxc = JMXConnectorFactory.connect(url);
              </code>
              that is removed the null environment Map, suddenly started working.

              So in the end, did not like a JMXServiceURL of service:jmx:jmxmp://Config427VM0:5556 when connecting to the un-secured server process enabled using -Dcom.sun.management.jmxremote.port=5556.

              Thanks again for your help.