Remote Versus Local Interfaces

One of the most significant improvements in the EJB 2.0 specification was the inclusion of local interfaces, as well as remote interfaces.

All the beans that you have seen so far have provided only a remote interface. That is, both their home and remote interfaces have extended from javax.ejb.EJBHome and javax.ejbEJBObject, both of which, in turn, extend the java.rmi.Remote interface. For Session beans this ability to invoke methods on a bean without regard for its location is crucial, but is less useful for Entity beans. Very often, a client must deal with many Entity beans to transact some piece of work, and if each of those Entity beans is remote, this will incur substantial network traffic.

A local interface can be specified instead of, or in addition to, the remote interfaces. A local interface does not incur the network overhead of remote interfaces.

To add weight to the need for a local interface for Entity beans, the client of the Entity bean may well be a Session bean (indeed, it is generally considered bad practice to use anything other than a Session bean to interact with Entity beans) and more often than not, this Session bean will be running in the same J2EE container as the Entity. Using remote interfaces, all Session-to-Entity bean calls must be made across the network. Significant performance gains can be made if local, rather than remote, interfaces are used.

As with remote interfaces, local interfaces use the home and proxy concept, with the home interface being extended from javax.ejb.EJBLocalHome, and the proxy for the bean extending from javax.ejb.EJBLocalObject. Otherwise though, these are regular Java interfaces, and the normal “pass by reference” semantics for objects passed across these interfaces apply.

CAUTION

Local EJB interfaces pass objects by reference, whereas remote interfaces pass objects by value. When writing EJBs that use both interfaces, you should be careful that the bean semantics (behavior) is the same for both interfaces.

Whereas local interfaces do not require the objects passed to be serializable this would create semantic differences between local and remote interfaces – this is not a good design practice.


Local interfaces are not specific to Entity beans; Session beans can also provide local interfaces. Session beans (especially stateless Session beans) can provide a local interface for use by servlets or JSPs running in the same container as well as a remote interface for other clients. In general, it would be expected for the two interfaces to offer similar capabilities, although there is nothing in the EJB specification that enforces this.

Figure 6.3 shows the two sets of interfaces that a bean can provide.

Figure 6.3. EJBs can have local and remote interfaces.


For both interfaces, the EJB home/local-home and proxy objects take responsibility for security (Day 15, “Security”) and transactions (Day 8, “Transactions and Persistence”), while home/remote interfaces make the physical location of the bean transparent to the remote client.

To keep things simple, in both the case study and the examples for today and tomorrow the Entity beans have only a local interface.

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

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