0

I'm using Glassfish clustering with 4 machines.

I'm also using remote EJB in order to call a method to this cluster. When I'm calling the method, one of these four machines processes it. I would like to know which one. So I create a remote EJB to get the identifier of the machine. With this, I can know which machine is processing my request. Problem is how can I assign an ID for each machine?

2 Answers 2

1

How about using the host name or IP of the machine itself?

InetAddress.getLocalHost().getHostName();
InetAddress.getLocalHost().getAddress();

http://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html

Note: If the machines in question have multiple network interfaces, I don't think there is any guarantee which interface is returned via these methods.

1
  • Thank you, I think it's the simplest idea. Otherwise, I also thought about generating a random number at startup using a singleton. I will try your solution, thank you!
    – ZouBi
    Apr 25, 2014 at 4:05
1

This is a better solution that displays the pid of the process with the hostname of the machine

ManagementFactory.getRuntimeMXBean().getName();

By example: 958@my-machine

Edit: As jgitter, this is not a better solution. Just another solution. Sorry.

3
  • This of course assumes that you have JMX configured to run on your server. "Better solution" is a stretch, it's just different. -1 for voting for yourself. ;-)
    – jgitter
    Apr 28, 2014 at 12:41
  • I didn't know that. When I found this, I just tried and worked fine. I put your answer as solution.
    – ZouBi
    Apr 29, 2014 at 8:31
  • No worries. JMX is standard Java SE so virtually all containers support it, but occasionally it is disabled by default. Cheers!
    – jgitter
    Apr 29, 2014 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.