1 2 Previous Next 15 Replies Latest reply: Jul 30, 2010 1:36 AM by 843793 RSS

    Can't start rmiregistry from application

    843793
      Hi!

      This is my code, which works, when I start rmiregistry from command-line:
      public class RemoteDBStarter extends RMIStarter {
      
        private final static int PORT = 2359;
        private final static String NAME = "DB";
      
        public RemoteDBStarter() {
          super(RemoteDBInterface.class);
        }
      
        public void serverOrClientContinue() {
          try {
            RemoteDBInterface db = new RemoteDB();
            RemoteDBInterface stub = (RemoteDBInterface)UnicastRemoteObject
                .exportObject(db, PORT);
      
            Registry registry = LocateRegistry.getRegistry();
            registry.rebind(NAME, stub);
          }
          catch(Exception e) {
            //TODO
            e.printStackTrace();
          }
        }
      
        public static void main(String[] args) {
          new RemoteDBStarter();
        }
      }
      But when I try to add createRegistry
      LocateRegistry.createRegistry(PORT);
      RemoteDBInterface db = new RemoteDB();
      RemoteDBInterface stub = (RemoteDBInterface)UnicastRemoteObject
          .exportObject(db, PORT);
      I get exception
      java.rmi.ConnectException: Connection refused to host: 10.x.x.x; 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.rebind(Unknown Source)
           at server.RemoteDBStarter.serverOrClientContinue(RemoteDBStarter.java:32)
           at common.RMIStarter.<init>(RMIStarter.java:15)
           at server.RemoteDBStarter.<init>(RemoteDBStarter.java:16)
           at server.RemoteDBStarter.main(RemoteDBStarter.java:41)
      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)
           ... 8 more
      I tried to set hostname
      System.setProperty("java.rmi.server.hostname", "localhost");
      but it gives me same exception, as if it didn't even look for this property. Any suggestions?
        • 1. Re: Can't start rmiregistry from application
          EJP
          LocateRegistry.createRegistry() returns a value. You have to save it.
          • 2. Re: Can't start rmiregistry from application
            843793
            Like this?
            Registry reg = LocateRegistry.createRegistry(PORT);
            RemoteDBInterface db = new RemoteDB();
            RemoteDBInterface stub = (RemoteDBInterface)UnicastRemoteObject
                .exportObject(db, PORT);
            
            //Registry registry = LocateRegistry.getRegistry();
            reg.rebind(NAME, stub);
            In that case I can run Server, but when I try to run Client, exception is there again.
            • 3. Re: Can't start rmiregistry from application
              EJP
              Store it in a static variable. Otherwise it can be garbage- collected.
              • 4. Re: Can't start rmiregistry from application
                843793
                Problem solved with not using registry.rebind(), but Naming.rebind().
                • 5. Re: Can't start rmiregistry from application
                  EJP
                  Nope. The only difference when you use Naming.rebind() is that there is, temporarily, another reference to the Registry. This time it's a remote reference, so it is subject to DGC, which has a ten-minute timeout on it, prior to being eligible to local GC, so the Registry will last ten minutes longer, but the problem remains.

                  Store it in a static variable.

                  NB your title is misleading. You did start the Registry but it disappeared quickly due to GC.
                  • 6. Re: Can't start rmiregistry from application
                    843793
                    But that still doesn't solve client-side exception. I use there LocateRegistry.getRegistry();
                    • 7. Re: Can't start rmiregistry from application
                      EJP
                      What client-side exception?
                      • 8. Re: Can't start rmiregistry from application
                        843793
                        Server:
                            registry = LocateRegistry.createRegistry(PORT);
                            RemoteDBInterface db = new RemoteDB();
                            RemoteDBInterface stub = (RemoteDBInterface)UnicastRemoteObject
                                .exportObject(db, PORT);
                            registry.rebind(NAME, stub);
                        That works. Now Client:
                            registry = LocateRegistry.getRegistry();
                            RemoteDBInterface stub = (RemoteDBInterface)registry.lookup(NAME);
                            stub.retrieveDBTable();
                        When Client wants to call that method from server, it gives me same exception as I stated before. I tried LocateRegistry.getRegistry(PORT); too, but with same efect.
                        • 9. Re: Can't start rmiregistry from application
                          EJP
                          registry = LocateRegistry.createRegistry(PORT);
                          Creates a Registry listening at PORT in the localhost.
                          registry = LocateRegistry.getRegistry();
                          Creates a stub for a Registry listening at port 1099 in the localhost.

                          So if PORT != 1099 or the server host != 'localhost' the stub won't work.
                          RemoteDBInterface stub = (RemoteDBInterface)registry.lookup(NAME);
                          which would make this throw a ConnectException.

                          Nothing to do with Naming.rebind(), except that you probably avoided using PORT when you did so.
                          I tried LocateRegistry.getRegistry(PORT); too, but with same effect.
                          Did you try that at the same time you were using createRegistry(PORT) in the server? There are four possibilities:

                          - use PORT in the server and in the client
                          - use PORT in the server and default = 1099 in the client
                          - use default = 1099 in the server and PORT in the client
                          - use default = 1099 in both the server and the client.

                          I doubt you have tried all four, and only two of them will work.
                          • 10. Re: Can't start rmiregistry from application
                            843793
                            I used PORT on both Server and Client and even set it to 1099. I also tried
                            registry = LocateRegistry.getRegistry("localhost", PORT);
                            and it's still same.
                            • 11. Re: Can't start rmiregistry from application
                              EJP
                              Calling the method? Not doing the lookup? See item A.1 in the RMI FAQ.
                              • 12. Re: Can't start rmiregistry from application
                                843793
                                I'm getting lost here, so I'll put whole code once more:
                                Server
                                public class RemoteDBStarter {
                                
                                  private final static int PORT = 1099;
                                  private final static String NAME = "DB";
                                  private static Registry registry;
                                
                                  public static void main(String[] args) throws ClassNotFoundException,
                                                                                SQLException {
                                    
                                    System.setProperty("java.security.policy",
                                        "D:/work/data/allow_all.policy");
                                    if(System.getSecurityManager() == null) {
                                      System.setSecurityManager(new SecurityManager());
                                    }
                                    try {
                                      registry = LocateRegistry.createRegistry(PORT);
                                      RemoteDBInterface db = new RemoteDB();
                                      RemoteDBInterface stub = (RemoteDBInterface)UnicastRemoteObject
                                          .exportObject(db, PORT);
                                      registry.rebind(NAME, stub);
                                    }
                                    catch(Exception e) {
                                      e.printStackTrace();
                                    }
                                  }
                                }
                                Client
                                public class Client {
                                
                                  private final static int PORT = 1099;
                                  private final static String NAME = "DB";
                                  private static Registry registry;
                                
                                  public void callForData() {
                                    try {
                                      registry = LocateRegistry.getRegistry(PORT);
                                      System.out.println(registry);
                                      RemoteDBInterface stub = (RemoteDBInterface)registry.lookup(NAME);
                                      stub.retrieveDBTable();
                                    }
                                    catch (Exception e) {
                                      e.printStackTrace();
                                    }
                                  }
                                }
                                Console after calling callForData() method
                                RegistryImpl_Stub[UnicastRef [liveRef: [endpoint:[x.x.x.x:1099](remote),objID:[0:0:0, 0]]]]
                                java.rmi.ConnectException: Connection refused to host: x.x.x.x; 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.invoke(UnicastRef.java:94)
                                     at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:179)
                                     at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:132)
                                     at $Proxy0.retrieveDBTable(Unknown Source)
                                     at com.gk_software.es.pklasna.client.Client.callForData(Client.java:36)
                                     at com.gk_software.es.pklasna.gui.GUI.actionPerformed(GUI.java:85)
                                     at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
                                     at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
                                     at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
                                     at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
                                     at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
                                     at java.awt.Component.processMouseEvent(Component.java:5517)
                                     at javax.swing.JComponent.processMouseEvent(JComponent.java:3135)
                                     at java.awt.Component.processEvent(Component.java:5282)
                                     at java.awt.Container.processEvent(Container.java:1966)
                                     at java.awt.Component.dispatchEventImpl(Component.java:3984)
                                     at java.awt.Container.dispatchEventImpl(Container.java:2024)
                                     at java.awt.Component.dispatchEvent(Component.java:3819)
                                     at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
                                     at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
                                     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
                                     at java.awt.Container.dispatchEventImpl(Container.java:2010)
                                     at java.awt.Window.dispatchEventImpl(Window.java:1791)
                                     at java.awt.Component.dispatchEvent(Component.java:3819)
                                     at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
                                     at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
                                     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
                                     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
                                     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
                                     at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
                                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)
                                     ... 32 more
                                IPs form println and exception are same.
                                • 13. Re: Can't start rmiregistry from application
                                  EJP
                                  at $Proxy0.retrieveDBTable(Unknown Source)
                                  So this is happening when calling your remote method, not when doing the lookup(). So there is no problem with the Registry here.

                                  So are you quite sure the 'x's are the same in both lines? If so the entire server JVM must have exited between your lookup() and your retrieveDBTable() calls in the client. Doesn't seem very likely.
                                  • 14. Re: Can't start rmiregistry from application
                                    843793
                                    Yes they are same, I'm pretty sure about that:)
                                    1 2 Previous Next