java 使用java rmi的优缺点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2339853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Benefits and disadvantages of using java rmi
提问by samuel
What are the advantages and disadvantages of RMI?
RMI的优缺点是什么?
回答by Rob Lachlan
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.
优点和缺点类似于任何类似 RPC(远程过程调用)系统的优点和缺点。有一种表面上的简单性,因为实际上远程的对象可以被视为本地对象。
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.
这对于简化编程来说似乎是一个很大的好处,但存在隐藏的成本。分布式系统存在延迟和潜在的部分故障问题,程序员必须意识到这一点。远程方法的调用容易受到安全性、延迟问题、网络故障等潜在故障的影响。掩盖这些类型的问题可能是可靠性的灾难。
Waldo et al.have a good discussion of the issues.
沃尔多等人。对问题进行很好的讨论。
回答by rafalmag
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)
- 容易上手
- 动态类加载非常强大
- 如果您实现如下所示的内容,您将无法长时间更改服务器端并开发客户端(rmi 服务器上的一个例外必须在类路径中获取这些类 - 因此要么通过网络为它们提供服务,要么包含它们并重建服务器)
You can implement two interfaces like that:
你可以实现两个这样的接口:
Common task interface:
常用任务界面:
public interface Task<T extends Serializable> extends Serializable {
T execute();
}
Rmi interface:
rmi接口:
public interface RmiTask extends Remote {
<T extends Serializable> T executeTask(Task<T> task) throws RemoteException;
}
RmiTaskimplementation on server side:
RmiTask服务器端的实现:
public class RmiTaskExecutor implements RmiTask {
public <T extends Serializable> T executeTask(Task<T> task) {
return task.execute();
}
}
Example client Taskimplementation:
示例客户端Task实现:
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
Callables and run them with defined timeouts).
- 可能是不安全的,使用时动态类加载(客户服务实现通过类型) -例如,你知道RMI服务器调用
method()上PassedObject,但奇妙的客户端可以覆盖此方法并执行任何他想做的有... - 难以实现可通过 Internet 工作的回调(它需要建立从服务器到客户端的新连接 - 通过 NAT/路由器/防火墙传递它可能具有挑战性)
- 当您在执行远程方法期间突然断开连接时,该方法将不会返回(我建议将 rmi 调用包装到
Callables 中并在定义的超时时间内运行它们)。

