I am testing a web application using Java Remote Method Invocation (RMI). When I am connected to internet through dongle, broadband; it takes very long time for the RMI to respond to the request. It takes around 10 seconds in completing the method call. I can also see the data transfer rate rising in the internet connection as soon as I make a method call. So definitely it seems to be making an RMI connection between two internal processes through the external network.
While disconnected from internet it responds vary fast, like in few milliseconds. My /etc/hosts file have entry for 127.0.0.1 localhost. I always use 127.0.0.1 in the method invocation url for naming.lookup. But can't figure out how to make machine respond quickly to RMI calls while I am connected to the internet. Any suggestions?
You problem is not the the address that you use for the lookup call to the RMI Registry, but rather it's the address in the URL that is the result of the call to lookup. This defaults to the IP address of the first interface on the system so my guess is that when you're connected to the Internet it's your external connection.
What I think is happening is that you make a call to the RMI Registry on 127.0.0.1 and this returns your Internet address the location of the service you want, and so your RMI call gets routed out over the dongle interface.
You need to set the java.rmi.server.hostname
property to tell the RMI Registry which hostname or IP Adress to return in its RMI URLs.
So you'll need something like:
String ipAddress = "10.1.2.3"; //Local IP address
System.setProperty("java.rmi.server.hostname",ipAddress);
or
String hostname = "myserver"; //hostname that resolves for a local address
System.setProperty("java.rmi.server.hostname",hostname);
来源:https://stackoverflow.com/questions/11343132/rmi-responding-very-slow