Remote and Local Interfaces

During design, you need to decide on the kind of interfaces you will provide to your enterprise bean. The interfaces you provide can be local or remote. Remote interfaces are RMI interfaces that are provided to allow the clients of a bean to be location-independent. EJB 2.0 introduced local interfaces to improve performance of client access to enterprise beans.

Remote Interfaces

A remote client accesses a session bean or an entity bean through the bean's remote interface and remote home interface. The remote and remote home interfaces of the bean provide the remote client view of the EJB.

  • The remote interface extends the javax.ejb.EJBObject interface. Container tools generate the corresponding EJB object implementing this interface.

  • The remote home interface extends the javax.ejb.EJBHome interface. Container tools generate the corresponding EJB home object implementing this interface.

Figure 2.3 shows a remote client can run in the same or a different JVM as that of the Enterprise JavaBean. The remote client can be a Web component (such as a JSP or servlet) or another Enterprise JavaBean, or an application client.

Figure 2.3. Remote clients.


Local Interfaces

Local interfaces were first introduced in the EJB 2.0 specification. Local interfaces improve the performance of client access to Enterprise JavaBeans that are located in the same JVM. This optimization is achieved by making a direct local process call instead of using remote invocation. Any RMI call is expensive compared to a local call. Local calls don't incur the communication overhead, such as connection initiation, and stubs and skeletons marshalling and unmarshalling the call parameters, that is associated with remote calls. Local calls are magnitude times faster compared to remote calls.

A local client accesses a session or entity bean through the bean's local interface and local home interface. A local client is located in the same JVM as the Enterprise JavaBean.

  • The local interface extends the javax.ejb.EJBLocalObject interface. Container tools generate the corresponding EJB local object implementing this interface.

  • The local home interface extends the javax.ejb.EJBLocalHome interface. Container tools generate the corresponding EJB local home object implementing this interface.

Figure 2.4 shows a local client must run in the same JVM as that of the Enterprise JavaBean. Unlike a remote client, a local client is not location-transparent. A local client can be a Web component (such as a JSP or servlet) or another Enterprise JavaBean.

Figure 2.4. Local clients.


Note

If you provide remote interfaces, you get maximum flexibility through location transparency. Your clients can be located anywhere. If you provide local interfaces to your Enterprise JavaBean, you get maximum performance, but at the price of location transparency: Your clients must be located in the same JVM as the Enterprise JavaBean instance. On the other hand, with remote interfaces, you can improve the performance by distributing the components among different servers.


Caution

The calling semantics of local interfaces are different from those of remote interfaces. Remote interfaces pass parameters using call-by-value semantics, whereas local interfaces use call-by-reference. For example, an Enterprise JavaBean could pass a large document to the client. With remote interfaces, the system would return a copy of the document to the client. On the other hand, with local interfaces, the client would get a reference to the bean's document. So, the client could potentially change the Enterprise JavaBean's state without the bean's knowledge. If this isn't acceptable, your Enterprise JavaBean must explicitly copy the data before returning to the client.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.75.221