Java RMI - server client creation

If I want to enable the two-way relationship in my RMI application (that is, allow the server to call methods on the client, as well as the client to call methods on the server), is this the easiest way to make the client into the Remote class as well?

Also, if I intend to pass instances of my client to the server as a parameter of the method, do I correctly think that there is no need to add a "client class" to rmiregistry?

And one last question, do all my classes still need to be compiled in one place? I. Can I compile the server and client on two completely independent machines and expect them to communicate correctly?

* EDIT **

Another question, my question makes a link to my client interface (IClient): it has an arraylist (so I have an ArrayList<IClient> ) to store new client instances so that the server can track registered clients. When I try to compile the server on another computer, it complains that it cannot find IClient - obviously, since IClient is on the client machine. How do I get around this?

+4
source share
3 answers

You are right in all your assumptions.

You do not need to add your remote called client classes to the rmi registry, but you still have to export them.

The only caveat with compilation is that they must run with the same version of java with the same compiler settings (at least those that affect the generation of the RMI kernel).

+3
source

If you have any requirements that the client or server will be behind the firewall in future deployments, stay away from RMI. You ask for trouble. Especially if you want the server to call the client.

Here are two alternative solutions that worked for us:

  • the client calls the server with the server blocking the call until data is available to the client. This uses a thread for each client (unless you use NIO), but it is easy to implement. Return null once in a while to prevent a long call from the client that needs to be closed by the firewall (depending on the configuration of the firewall, of course)

  • if you need some pretty Java interfaces, consider Hessian , which is very lightweight, runs on top of HTTP and doesn't worry about the RMI registry or similar stuff

  • if you intend to open the server side to other clients, Hessian is still a good choice, but if scaling is a problem, look at the RESTful architectural style.

+3
source

Repeat your last question, remote interface classes and all the classes they depend on, etc. recursively, until a close should be present on both hosts.

0
source

Source: https://habr.com/ru/post/1337188/


All Articles