Composition of a Session Bean
A Session Bean is composed of three parts: the home interface, the component interface, and the Bean class. The home interfaces list the life cycle methods of the bean, whereas the component interfaces list its business methods. The Bean class, or Bean implementation, is where you code the business logic of the bean's business methods and also define its life cycle methods. All three are required constituents of a Session Bean. If a bean needs to be accessed over the network, you must provide a remote home interface and a remote interface along with the bean implementation. With EJB 1.1, the only way to access an EJB was remotely, and hence a set of remote interfaces was mandatory.
EJB 2.0 introduced a new concept in which Session Beans may be accessed locally by other objects that live within the same EJB container without the overhead of networking calls. This is done by using a set of local home and local interfaces in place of the remote home and remote interfaces. The advantage of using local interfaces is their performance: The bean instance is invoked just like any other object within the JVM, without using RMI. No network overhead is involved.
In this section, we look at these components and the role each of them plays in the usage of the bean. Let's look at the remote interfaces first, followed by the local interfaces.
The remote home interface—
The remote home interface provides applications with access to the bean's life cycle methods over the network. Life cycle methods are used to create new beans, remove existing ones, and find beans based on a given criteria. The remote home interface is represented by the interface javax.ejb.EJBHome. The bean's remote home interface should extend from this interface and provide access to appropriate methods. The EJBHome interface in turn extends from the java.io.Remote interface, thus making it accessible over the network. This interface works in conjunction with the remote interface.
The remote interface—
The remote interface provides access to the bean's business methods. This interface extends from the javax.ejb.EJBObject interface, which in turn extends from java.io.Remote interface, thus ensuring remote accessibility. This interface works in conjunction with the remote home interface of a bean.
The local home interface—
If the home interface of the bean extends from the interface javax.ejb.EJBLocalHome instead of the javax.ejb.EJBHome interface, the bean can be efficiently used by other objects residing within the same container. This interface offers bean life cycle methods similar to those of the remote home interface. The difference between the two is that when beans are created using this interface, no network connectivity is involved because both the requested and the requestor exist within the same application, which significantly improves performance. This interface works in conjunction with the local interface. Remember that the specification talks about using local interfaces for objects that lie within the same JVM, but the WebLogic container restricts it further to the same application (that is, JAR, EAR, and so on) This is a direct consequence of the class loader scheme adopted by WebLogic Server, which basically uses objects within the same application only.
The local interface—
The local interface of a bean is similar to the remote interface in that both provide access to the business methods provided by the bean class. This interface works with the local home interface. All method calls made using this interface avoid network protocols and thus improve performance. The local interface of a bean extends javax.ejb.EJBLocalObject.
The bean implementation—
The Bean class is the server-side class in which code is written to perform all the business operations and life cycle operations, as listed in its remote home, local home, and remote and local component interfaces. That said, the Bean class does not implement any of these interfaces directly; it merely defines methods that resemble the ones in these interfaces. This is so because the objects that actually implement the home and component interfaces are generated by the EJB compiler. The Bean class should implement the javax.ejb.SessionBean interface to identify the bean as a Session Bean. Both Stateless and Stateful Session Beans implement the same interface. This interface in turn extends from javax.ejb.EnterpriseBean.
Session Beans should provide a set of local (home and component) interfaces to enable local access to the bean. They also should provide a set of remote (home and component) interfaces to enable remote access. At least one set of interfaces is mandatory, although both can be provided for enabling both remote and local access to a bean.
In this chapter, the remote and local interfaces are sometimes referred to as component interfaces.