1

I'm trying new technology: RMI. I tried this sample and it works on localhost. Then I tried it on VPS: copied RemoteHelloServiceImpl.class and RemoteHelloService.class to the VSP, started server - it works. Then I changed this string in HelloServiceClient

Registry registry = LocateRegistry.getRegistry("localhost", 2099);

to this

Registry registry = LocateRegistry.getRegistry("178.62.30.124", 2099);

compiled it and run.

Catched this exception:

Exception in thread "main" java.rmi.ConnectException: Connection refused to host: 127.0.1.1; nested exception is:...

Why 127.0.1.1? Do I need to change anything else to work with remote server?

And the last one: that sample works when I only run 2 java classes, but this page says I need to run rmiregistry; so, why I need it(rmiregistry)?

UPDATE:

RemoteHelloService.java

import java.rmi.*;

public interface RemoteHelloService extends Remote {

    Object sayHello(String name) throws RemoteException;

}

RemoteHelloServiceImpl.java

import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;

public class RemoteHelloServiceImpl implements RemoteHelloService {

    public static final String BINDING_NAME = "sample/HelloService";

    public Object sayHello(String name) {
        String string = "Hello, " + name + "! It is " + System.currentTimeMillis() + " ms now";
        try {
            System.out.println(name + " from " + UnicastRemoteObject.getClientHost());
        } catch (ServerNotActiveException e) {
            System.out.println(e.getMessage());
        }
        if ("Killer".equals(name)) {
            System.out.println("Shutting down...");
            System.exit(1);
        }
        return string;
    }

    public static void main(String... args) throws Exception {
        System.out.print("Starting registry...");
        final Registry registry = LocateRegistry.createRegistry(2099);
        System.out.println(" OK");

        final RemoteHelloService service = new RemoteHelloServiceImpl();
        Remote stub = UnicastRemoteObject.exportObject(service, 0);

        System.out.print("Binding service...");
        registry.bind(BINDING_NAME, stub);
        System.out.println(" OK");

        while (true) {
            Thread.sleep(Integer.MAX_VALUE);
        }
    }
}

HelloServiceClient.java

import java.rmi.registry.*;

public class HelloServiceClient {

    public static void main(String... args) throws Exception {
        Registry registry = LocateRegistry.getRegistry("178.62.30.124", 2099);
        RemoteHelloService service = (RemoteHelloService) registry.lookup("sample/HelloService");
        String[] names = { "John", "Jan", "Иван", "Johan", "Hans", "Bill", "Kill" };
        for (String name : names) {
            System.out.println(service.sayHello(name));
        }
    }

}
sinedsem
  • 5,413
  • 7
  • 29
  • 46
  • 1
    Isn't LocateRegistry.getRegistry(...) code from client side? – Stefan Aug 06 '14 at 13:37
  • 1
    OK, I DONE IT!!! Here is the solution: http://stackoverflow.com/questions/17703719/cannot-establish-rmi-connection-to-remote-machine – sinedsem Aug 06 '14 at 17:53

0 Answers0