一个实体BEAN是被设计用来为多个客户端服务的,一个客户端创建了实体BEAN,其他客户端也应该可以使用它。
remote or local interface描述了客户端程序可以调用的商业方法。当在remote or local interface中定义了这些方法后,你要在BEAN class中实现它,客户端不会直接访问BEAN,他们是通过remote or local interface来访问这些方法的。
remote or local interface中定义的方法必须遵守以下原则:
它必须是public的;
如果是定义在remote interface中的方法,它必须抛出java.rmi.RemoteException;如果不是,它不能抛出java.rmi.RemoteException。
所有客户端能访问的方法必须在remote or local interface中定义。
BEAN的interface的几种选择:
选择Remote意味着EJB Designer生成一个Bean class,Remote home和Remote interface。
选择Local意味着EJB Designer生成一个Bean class,Local home和Local interface。
如果选择Remote/Local,EJB Designer将生成Bean class,Remote home,Remote interface,Local home和Local interface。在JBUILDER中,会话BEAN默认是remote的,实体BEAN默认是local的。
如果你为一个会话BEAN (Component)选择了Remote/Local,将生成的文件如下:
ComponentHome - the remote home interface;
ComponentBean - the bean class;
Component - the remote interface ;
ComponentLocalHome - the local home interface ;
ComponentLocal - the local interface。
如果你为一个实体BEAN (Component)选择了Remote/Local,将生成的文件如下:
ComponentHome - the local home interface ;
ComponentBean - the bean class ;
Component - the local interface ;
ComponentRemoteHome - the remote home interface ;
ComponentRemote - the remote interface。
Remote and local access
An EJB 2.0 component can be accessed remotely or locally. Clients that access a remote bean use the bean's remote and remote home interfaces. A remote home is often referred to as the home interface. A client with remote access to a bean can run on a different machine and use a different Java Virtual Machine (JVM) than the bean itself. In method calls to a remote bean, parameters are passed by value, which helps maintain loose coupling between the client and the bean.
A client with local access to a bean must run in the same JVM as the bean it accesses. A local client won't be an external client application, but rather another enterprise bean or web component. In method calls to a local bean, parameters are passed by reference, resulting in a tighter coupling between the calling bean or web component and the called bean.
Like the remote interface, the local interface provides access to the bean's business methods, while its local home interface provides access to the methods that control the life cycle of the bean as well as its finder methods. Often entity beans that have a container-managed relationship with other entity beans have local access to them.
Because beans with local interfaces must run in the same JVM, there is no need for remote calls. Therefore, the overhead of serializing and transporting objects is reduced. Usually this means greater performance.