What are the advantages and disadvantages of RMI?
The advantages and disadvantages are similar to those of any RPC-like (Remote Procedure Call) System. There is a superficial appearance of simplicity, because it objects which are in fact remote can be treated as though they were local.
This would seem like a great benefit to simplicity of programming, but there are hidden costs. Distributed systems have issues of latency and potential for partial failure which the programmer has to be aware of. An invocation of a remote method is subject to potential failure from security, latency problems, network failure, etc. Papering over these sorts of problems can be a disaster for reliability.
From my experience:
Pros:
- Easy to start
- Dynamic class loading is very powerful
- If you implement something like below you can not change server side for a long time and develop client (one exception on rmi server has to get these classes in classpath - so either server them over net or include them and rebuild server)
You can implement two interfaces like that:
Common task interface:
public interface Task<T extends Serializable> extends Serializable {
T execute();
}
Rmi interface:
public interface RmiTask extends Remote {
<T extends Serializable> T executeTask(Task<T> task) throws RemoteException;
}
RmiTask
implementation on server side:
public class RmiTaskExecutor implements RmiTask {
public <T extends Serializable> T executeTask(Task<T> task) {
return task.execute();
}
}
Example client Task
implementation:
public class IsFileTask implements Task<Boolean> {
final String path;
public IsFileTask(String path) {
this.path = path;
}
public Boolean execute() {
return new File(path).isFile();
}
}
Cons:
- Could be insecure, when using Dynamic class loading (client serves implementation of passed types) - for example you know that rmi server calls
method()
onPassedObject
, but marvellous client could override this method and execute whatever he wants there... - hard to implement callback which would work over Internet (it needs to establish new connection from server to client - it can be challenging to pass it through NAT/routers/ firewalls)
- when you suddenly broke the connection during execution of remote method it happens that this method would not return (I recommend wrapping rmi calls into
Callable
s and run them with defined timeouts).
© 2022 - 2024 — McMap. All rights reserved.