G |
J2EE/EJB Overview |
|
Get |
Get |
The Component Interface |
Note: The following discussion of bean interfaces applies only to session and entity beans. Message-driven beans effectively run within a batch environment on the server and are represented only by the bean class — no interfaces are used.
Enterprise JavaBean functionality is accessed by means of the bean's component interface, which defines the business methods visible to, and callable by, the client. Again, the developer writes the component interface, and the container creates the implementation for client interaction.
The client uses a home interface's create() method to obtain a reference to a bean's component interface. In the entity bean section, we will see that a component interface may also be returned by findByPrimaryKey() and other finder methods. The sample code below locates the home interface for a remote bean with a JNDI name of ejb/MyEJB, then obtains the component interface by invoking <HomeInterface>.create(). This code, and the following code for local home lookup, is virtually identical in every client's EJB access routine:
// get the bean's Home interface
InitialContext ic = new InitialContext();
Object oRef =
ic.lookup( "java:comp/env/ejb/MyEJBBean" );
MyEJBRemoteHome MyEJBHome =
(MyEJBRemoteHome)PortableRemoteObject.
narrow( oRef, MyEJBRemoteHome.class );
// get the component interface
MyEJBRemote MyEJB = MyEJBHome.create();
Again, note that PortableRemoteObject.narrow() must be used on the object returned from the JNDI lookup for remote types rather than simple Java language casts.
For a bean that provides a local home and component interface, the code looks like this:
// get the EJB's Home interface
InitialContext ic = new InitialContext();
MyEJBLocalHome MyEJBHome =
(MyEJBLocalHome)ic.lookup( "java:comp/env/ejb/MyEJBBean" );
// get the component interface
MyEJBLocal MyEJB = MyEJBHome.create();
javax.ejb.EJBObject and javax.ejb.EJBLocalObject interfaces
|
When writing a remote interface, your interface extends javax.ejb.EJBObject. When writing a local interface, your interface extends javax.ejb.EJBLocalObject. Remember that the component interface is the client's view of your bean's functionality. Therefore, this is the place where you define all of the methods that should be available to the client.
The component interface's remove() method disengages the current bean from the client for session beans but deletes data from the datastore for entity beans. The life cycle and associated methods are discussed more completely for each bean type in the relevant sections ahead.
|
|