1.种类
Enterprise JavaBeans server-side components come in three fundamentally different types: entity, session, and message-driven beans. Both session and entity beans are RMI-based server-side components that are accessed using distributed object protocols. Message-driven beans process messages from non-RMI systems like Java Message Service, legacy systems, and web services. All EJB servers must at least support a JMS-based message driven bean, but they may also support other types of message-driven bean.
2.类与接口
Remote interface
The remote interface defines the bean's business methods which can be accessed from applications outside the EJB container: the business methods a bean presents to the outside world to do its work. The remote interface extends javax.ejb.EJBObject, which in turn extends java.rmi.Remote. It is used by session and entity beans in conjunction with the remote home interface.
The home interface defines the bean's life-cycle methods which can be accessed from applications outside the EJB container: the life-cycle methods for creating new beans, removing beans, and finding beans. The home interface extends javax.ejb.EJBHome, which in turn extends java.rmi.Remote. It is used by session and entity beans in conjunction with the remote interface.
The local interface for an enterprise bean defines business methods that can be used by other beans in the same EJB container: the business methods a bean presents to other beans running in the same JVM. It allows beans to interact without the overhead of a distributed object protocol, which improves their performance. The local interface extends javax.ejb.EJBLocalObject. It is used by session and entity beans in conjunction with the local home interface.
The local home interface defines life-cycle methods that can be used by other beans in the same EJB container; that is, the life-cycle methods a bean presents to other beans running in the same JVM. It allows beans to interact without the overhead of a distributed object protocol, which improves their performance. The local home interface extends javax.ejb.EJBLocalHome. It is used by session and entity beans in conjunction with the local interface.
The endpoint interface defines business methods that can be accessed from applications outside the EJB container via SOAP. The endpoint interface is based on JAX-RPC (Java API for XML-RPC) and is designed to adhere to the SOAP and WSDL standards. The endpoint interface extends java.rmi.Remote. It can be used only by stateless session beans. There is no home interface associated with the endpoint interface.
Message-driven beans implement the message interface, which defines the methods by which messaging systems, such as Java Message Service, can deliver messages to the bean.
The session and entity bean classes implement the bean's business and life-cycle methods. Note that the bean class usually does not implement the remote or local component interfaces, but it may implement the endpoint interface. However, the bean class must have methods matching the signatures of the methods defined in the remote, local, and endpoint interfaces, and must have methods corresponding to some of the methods in both the remote and local home interfaces. If this sounds perfectly confusing, it is. In addition, an entity bean must implement javax.ejb.EntityBean; a session bean must implement javax.ejb.SessionBean. The EntityBean and SessionBean extend javax.ejb.EnterpriseBean.
A message-driven bean implements one or more message delivery methods (e.g., onMessage( )) defined in a message interface. The container calls these methods when a new messages arrives. The message-driven bean class must also implement javax.ejb.MessageDrivenBean. EJB 2.1 and 2.0 containers must support JMS-based message-driven beans, which implement the javax.jms.MessageListener interface. EJB 2.1 also supports message-driven beans that process messages from other types of messaging systems with their own message interfaces. The MessageDrivenBean, like the EntityBean and the SessionBean, extends the javax.ejb.EnterpriseBean interface.
The primary key is a class that provides a pointer into the database. Only entity beans need a primary key. The principal requirement for this class is that it implements java.io.Serializable.
3.EJB Object
On the server side, an EJB object is an object that implements the remote and/or local interfaces of the enterprise bean. The EJB object is generated by your EJB container and wraps the enterprise bean instance—that is, an instance of the enterprise bean class you've created (in our example, the CabinBean) on the server—and expands its functionality to include javax.ejb.EJBObject and/or javax.ejb.EJBLocalObject behavior. This object works with the container to apply transactions, security, and other system-level operations to the bean at runtime.
4.EJB Home
The EJB home is a lot like the EJB object. It's another class that's generated automatically when you install an enterprise bean in a container. It implements all the methods defined by the home interfaces (local and/or remote) and is responsible for helping the container manage the bean's life cycle. The EJB home is responsible for locating, creating, and removing enterprise beans. These tasks may involve working with the EJB server's resource managers, instance pooling, and persistence mechanisms, the details of which are hidden from the developer.
For example, when a create method is invoked on a home interface, the EJB home creates an instance of the EJB object that references a bean instance of the appropriate type. Once the bean instance is associated with the EJB object, the instance's matching ejbCreate( ) method is called. In the case of an entity bean, a new record is inserted into the database. With session beans, the instance is simply initialized. Once the ejbCreate( ) method has completed, the EJB home returns a remote or local reference (i.e., a stub) for the EJB object to the client. The client can then work with the EJB object by invoking business methods. The stub relays the methods to the EJB object; in turn, the EJB object delegates those method calls to the bean instance.