Introduction to Enterprise JavaBeans 3.0
by Lynn Munsinger
The EJB 3.0 specification makes it easier than ever to develop Enterprise JavaBeans, perhaps encouraging you to consider developing EJBs for the first time. If that is the case, congratulations, you have successfully avoided the many pitfalls of EJB developers before you, and can enjoy the ease of EJB 3.0 development. But before you start development, you might want to know what Enterprise JavaBeans are and what purpose they serve. This article explains the basics of EJBs and how you can utilize them in a J2EE application.What is an EJB?
An Enterprise JavaBean (EJB) is a reusable, portable J2EE component. EJBs consist of methods that encapsulate business logic. For example, an EJB may have business logic that contains a method to update customer data in a database. A variety of remote and local clients can invoke this method. Additionally, EJBs run inside a container, allowing the developer to focus on the business logic contained in the bean without having to worry about complicated and error-prone issues such as transaction support, security, and remote object access. EJBs are developed as POJOs, or Plain Old Java Objects, and developers can use metadata annotations to specify to the container how these beans are to be managed.Types of EJBs
EJBs consist of three main types: Session, Entity, and Message-Driven. A Session bean performs a distinct, de-coupled task, such as checking credit history for a customer. An Entity bean is a complex business entity which represents a business object that exists in the database. A Message-Driven bean is used to receive asynchronous JMS messages. Let's examine these types further:Session Beans
Session beans generally represent actions in the business process such as "process order." Session beans are classified based on the maintenance of the conversation state, either stateful or stateless.
A stateful session bean maintains a conversational state across method invocations; for example, an online shopping cart of a customer. When the customer starts online shopping, the customer's details are retrieved from the database. The same details are available for the other methods that are invoked when the customer adds or removes items from the cart, places the order, and so on. Yet, stateful session beans are transient because the state does not survive session termination, system crash, or network failure. When a client requests a stateful session bean instance, that client is assigned one stateful instance, and the state of the bean is maintained for that client. To specify to the container that a stateful session bean instance should be removed upon the completion of a certain method, add the annotation
Session Bean Example
An entity bean is an object that manages persistent data, potentially uses several dependent Java objects, and can be uniquely identified by a primary key. Specify that a class is an entity bean by including the
Entity Bean Example
Message-driven beans (MDBs) provide an easier method to implement asynchronous communication than by using straight Java Message Services (JMS). MDBs were created to receive asynchronous JMS messages. The container handles most of the setup processes that are required for JMS queues and topics. It sends all the messages to the interested MDB. An MDB allows J2EE applications to send asynchronous messages that can then be processed by the application. To specify that a bean is an MDB, implement the
Message Driven Bean Example
An EJB client is the application that accesses the bean. It does not necessarily reside on the client tier, but can be a stand-alone application, JSP, servlet, or another EJB. The client accesses the methods of an EJB through the remote or local interfaces of the bean, depending on whether the client resides in the same or a different JVM than the bean. These interfaces define the methods of the bean, while the bean class actually implements the methods. When a client accesses a method of the bean class, the container generates a proxy for the bean, called the remote or local object. The remote or local object receives the request, delegates it to the corresponding bean instance, and returns the results to the client. To invoke a bean's methods, a client finds the bean by using its name defined in the EJB deployment descriptor. In the following example, the client finds the bean named "Statelessejb" using the Context object.
EJB Client Example
Developing Enterprise JavaBeans is significantly easier with the EJB 3.0 specification. The specification uses metadata annotations to define the type of bean and the methods exposed to the client. Thus, whether you are creating a session bean for executing a specific task or mapping a table to an entity bean for updating data, you can do so using plain Java objects and interfaces, and expose methods for clients by using annotations within the business methods. Now that you have an understanding of the basics of EJBs, refer to OTN's EJB 3.0 Resources Page for more information.