谢谢 Vikas
以下是EJB规范所述:
在开发企业Bean时,本地和远程编程模型之间的选择是Bean提供者所做的设计决策。
虽然可以为企业bean提供远程客户端视图和本地客户端视图,但通常只提供其中一种。
因此,在编写Bean时,请考虑客户端是谁,很少有本地客户端需要相同的方法或甚至相同的Bean。
我不同意在设计时将远程和本地视为可以轻易互换的。
首先,远程调用会产生额外开销,因此在设计远程接口时,需要仔细考虑参数大小和粒度是否正确。因此,在设计时提醒这将比较昂贵是有帮助的。
此外,由于远程接口参数按值传递,而本地接口参数按引用传递,因此两种情况之间存在基本的语义差异,因此您可能会选择以不同的方式设计这两个接口。
客户端通过bean的接口访问会话或实体bean。EJB容器生成接口实现来强制和管理此行为,充当客户端和bean之间通信的媒介。在EJB 2.0规范之前的版本中,所有bean都被定义和实现为分布式远程组件。因此,bean所需的两个接口被称为home接口(一般定义生命周期方法)和remote接口(一般定义功能业务方法)。
Internally, J2EE uses the Java Remote Method Invocation over Internet Inter-ORB Protocol (RMI-IIOP) to enable remote, distributed method calls and applications. While this approach provides many benefits, it also generates a large amount of overhead, with a corresponding performance hit as stubs are referenced, parameters go through the marshaling process, and objects are tossed around the network.
Considerations of performance, practicality, and typical usage in the field resulted in the introduction of local interfaces in the EJB 2.0 specification. As noted, prior terminology referred to the home interface and the remote interface; at this point, depending on which approach is used, local interface and local home interface or remote interface and remote home interface are better terms. Either of the local home or remote home interfaces is referred to as the home interface; either of the local or remote interfaces is referred to as the component interface. This tutorial refers to the interfaces in these terms and uses these conventions for names.
When using J2EE technologies, it is normal to focus on distributed, or remote, beans, but you should keep the local option in mind, when applicable. It may be surprising to learn that a bean can have local interfaces, remote interfaces, or both. However, the client must write to a specific (that is, local or remote) interface. There are some issues to keep in mind when using local interfaces:
这些bean必须在同一个虚拟机中运行,因为它们是本地的。参数通过引用发送而不是被复制,就像远程接口和对象一样。如果您忽略这个区别并且没有相应地编码,可能会导致意外的副作用。
通常,使用本地访问还是远程访问受以下因素的影响:
客户端的类型 - 除非您总是期望客户端是Web组件或另一个bean,请选择远程访问。
bean之间的耦合程度 - 如果bean彼此依赖并频繁交互,请考虑使用本地访问。
可扩展性 - 远程访问本质上是可扩展的,并且如果可扩展性是重要因素,则应该使用远程访问。随着EJB 2.0规范中本地接口的出现,大多数来源建议实体bean几乎总是基于本地访问。使用本地接口,关于非常细粒度数据访问的大多数性能问题都消失了。如果客户端是远程的,则标准设计模式要求客户端使用远程接口访问会话bean,然后会话bean充当与实体bean进行联络的联络员。会话bean通过本地接口与实体bean通信(从模式的角度来看,这种技术称为会话外观,并且实际上可以在远程或本地上下文中使用)。