Search code examples
javarmi

What is the proper way of using RMI remote objects on the client side?


In a dialog (window) let suppose we have some buttons and when a button is pressed a remote method invocation take place.

How it is better:

  • create the remote object (registry.lookup()) one time and use this object every time we need a remote method invocation?

  • each time we need a remote method invocation we execute registry.lookup() and after this we call the remote method?


Solution

  • Just do your lookup once, and cache the reference to the remote object somewhere. The rmiregistry is just used for bootstrapping. Doing a lookup every time will cause lots of unnecessary network calls.

    The ServiceLocator pattern talks about it:

    The Service Locator pattern centralizes distributed service object lookups, provides a centralized point of control, and may act as a cache that eliminates redundant lookups.