Search code examples
javaamazon-ec2rmi

Strange RMI connection exception with Amazon EC2


I know this is something repetead but couldn't find the solution to my problem...

I am trying to implement a simulated distributed system and I struggling a lot with the connectivity through RMI.

I have three different components, grid schedulers, resources managers and clients. The three of them interact among themselves. The behaviour is the following: the user looks up resource manager reference on the registry and then invokes a remote method which will invoke another one in the grid scheduler and eventually one method in the user will be invoked delivering the results. If I execute the whole system in local everything works as expected.

Now the thing is, I am trying to move one of the resource mananger nodes to a EC2 Amazon instance with public address A.A.A.A, the rest of the system will run locally on my laptop with public address B.B.B.B. I allowed TCP traffic on the port 1099 TCP/UDP on the security group policy, also set the java.security.policy to grant all permision in both machines. To create the registry in every component I do:

LocateRegistry.createRegistry(1099);

Then the components will discover themselves by

Naming.lookup(url)

Now, user will look up for ip address A.A.A.A/component the public address of the Amazon instance. I can check that sometimes it gets the reference, sometimes it doesn't (NotBoundException). However, when it does gets the reference and tries to call the method I get:

java.rmi.ConnectException: Connection refused to host: INTERNAL IP OF AMAZON INSTANCE  nested exception is: java.net.ConnectException: Connection timed out: connec.

Am I doing something wrong? I tried to set the property

java.rmi.server.hostname

To the public ip A.A.A.A in the Amazon instance and I got same results. I also tried doing the same on my laptop to the public address B.B.B.B and I got "connection refused IP B.B.B.B is not a localhost address"

I also opened the port in the router of my houe just in case that's the problem but it doesn't seem to be...

Am I missing something?

Thanks in advance.


Solution

  • Okay apparently, the issue was that even if the server listens on port 1099 then the port you use to connect the stub is random so I had to open all TCP ports in the security group of AWS. In that way it's working, however even if I turned off my windows firewall and open all the ports in the router it wasn't working my laptop - AWS, just different instances in AWS. But well, I can live with that...

    Regarding the hostname you have to set it to the private address returned by

    hostname -i
    

    If you're going to deploy it inside AWS or to the public IP if you're connecting different instances such as your laptop and one EC2.

    I hope this helps people on the same situation (have seen couple of questions with the same issue but no response).

    Have a nice day!