2

If you want to start jstatd on a specific port for its RMI registry, you can pass -p parameter for this.

But the problem is that it opens a second random port (called "anonymous port" in java) which causes problem for writing firewall rules or to use JVisualVM to connect to a remote jstatd running in a Docker container.

If you look at jstatd source, you'll see that it is calling UnicastRemoteObject.exportObject(remoteHost, 0) which will open a new "anonymous port" which seems to be random.

Is there a way to force this last port to a fixed one, or a way to predict which one will be chosen?

Anthony O.
  • 22,041
  • 18
  • 107
  • 163
  • Possible duplicate of [How do I ensure that RMI uses only a specific set of ports?](http://stackoverflow.com/questions/56687/how-do-i-ensure-that-rmi-uses-only-a-specific-set-of-ports) – jchampemont Oct 27 '16 at 15:45
  • Actually it will cause the newly exported object to reuse a previously used port, such as the Registry port, unless there are socket factory conflicts. Do you have evidence? or is this question just based on this misreading? – user207421 Oct 27 '16 at 23:44
  • @EJP: I don't really understand your comment... My question is based on the fact that when launching `jstatd` another port is opened (as well as the RMI registry port) and if you don't also forward this port in a firewall rule, then JVisualVM (for example) will not be able to access this remote server... – Anthony O. Oct 28 '16 at 08:24
  • @EJP I also can confirm that it is an "anonymous port" which is opened in that case by looking at the source & documentation of `UnicastRemoteObject.exportObject(Remote obj)` which will use a `new UnicastServerRef(true)` which calls `this(0)` in the constructor (so its setting local port to `0` which is said "anonymous port" in multiple location in the sources & docs). So it is nearly equivalent to calling `UnicastRemoteObject.exportObject(Remote obj, 0)` (the only difference is the `forceStubUse` which will be set to `false` in `UnicastServerRef`). – Anthony O. Oct 28 '16 at 08:45
  • @AnthonyO. If you dig a little deeper you will find the port-sharing logic. It's there. – user207421 Oct 28 '16 at 20:36
  • @EJP: I dug and found nothing about how to predict the port that will be used by specifying an "anonymous port" – Anthony O. Nov 02 '16 at 15:57

1 Answers1

2

I found no easy way to predict which concrete port will be opened by using an anonymous port.

But I found a rewrite of jstatd called "jakestatd" which will force the 3 ports (because at last, I discovered that jstatd actually opens 3 ports and not 2 as I first thought) that jstatd uses.

As it was not enough for me because I needed to control those ports, I wrote ejstatd that answer this exact question (as well as others), so now I can control thos ports using (inside ejstatd's folder):

mvn exec:java -Dexec.args="-pr 2222 -ph 2223 -pv 2224"

Here the 3 ports that will be opened will be 2222, 2223 and 2224, and the RMI registry will be available at port 2222.

Anthony O.
  • 22,041
  • 18
  • 107
  • 163