May 01, 2004

Enterprise JavaBeans 101 - Session Beans

Introduction
With Java development gaining traction at Excellus, many may be left with more questions than answers at times. The Java 2 Enterprise Edition (J2EE) computing platform is a necessarily complex one because of its intent to address software computing needs on a grand scale. However, the J2EE platform, like any good software application, has been modularized to aide those who endeavor to understand it. One key feature of J2EE that catches a lot of attention (and flack!) is Enterprise JavaBeans (EJB). In this first article in a series, I will outline the features of EJBs by first providing a brief overview of EJBs and then discussing one of the key types of EJBs, the Session Bean. In subsequent articles, I will discuss other types of EJBs and then wrap-up the series with a discussion how EJBs are being used within Excellus.

Overview and Definition
Java programs come in a number of varieties. Some execute by themselves within an operating system much like Notepad or Solitaire. Some run within the context of a web browser (e.g. earlier releases of Planview). EJBs are another type of Java program that execute within a managed environment much like a screen-less IMS or CICS program would execute.

EJBs, like most other Java-related technologies, start with a standard. This standard specifies an EJB�s services and benefits. To give you an idea of the complexity (and hence robustness) of EJBs, the specification alone weighs in at nearly 600 pages! Currently at Excellus, we are developing to the EJB 2.0 standard because that is what our application server, WebSphere Application Server 5.0, uses.

What do EJBs provide the developer? Contrary to what industry hype may lead us to believe, EJBs do not magically provide reuse. Rather, they provide a mechanism to register and manage reusable software components in a Java environment. When I say reusable software components, I�m referring to those services that are the result of careful analysis and design prior to their implementation. Such components could be used in a number of different applications within a corporation. For example, the Encode/Decode routines used within LRSP are examples of software reuse within a company. The inventory of Data Gatherers is another example of a reusable software component.

The management of these components is performed not by an application developer but by an administrator. Allowing for the management of the component makes it more usable and responsive to business needs. In the IMS or CICS world, one is required to register and setup a program in the respective environment. If one does not perform this task, the program cannot take advantage of the services available to the program by IMS or CICS. Manageability qualities such as security or transaction control are simply unavailable and left to the developer to re-implement over and over again. With the EJB standard, the developer is liberated from repeated implementation of transaction, security, and other non-business features to support a software system.

Session Beans
There are three types of EJBs in the 2.0 version of the standard. Those are Session Beans, Entity Beans, and Message-Driven Beans. In this article, I�ll discuss Session Beans.

The overall purpose of Session Beans is to provide a mechanism to implement a series of business rules implemented in Java. In languages like COBOL, one may think of a called subroutine with a bundle of repeated business logic (e.g. complex date calculations, driver routines for transaction execution). Session Beans may also contain routines (or methods) that perform complex coordination of I/O operations (e.g. finding a Member in multiple back-end systems). Session Beans do not contain logic that controls the user-interface (UI). There is another set of standards and mechanisms for handling the UI in a J2EE application.

Session Beans come in two varieties, stateless and stateful. When we say stateful, we mean that the software component remembers where you are in a transaction between screens. For example, when you go grocery shopping, you may need to purchase 5 items. You don�t visit the checkout after finding each item. Rather, you accumulate the items in a basket or a cart and then make one trip to the checkout register. In this example, the shopping basket or cart manages the �state� of your �transaction�. The Stateless Session Beans are the most efficient and widely used Session Bean mostly because it�s �cheaper� for the application server to administer them. Stateful Session Beans manage the state of a series of interactions within the application server. The Stateful Session Bean manages the virtual version of the shopping cart or basket. Because of this feature, Stateful Session Beans consume more computing resources and are often avoided in favor of other session-management techniques. However, future implementations of Stateful Session Beans may reverse this trend.

Summary
In this article, I provided a brief overview of EJB technology followed by an introduction to Session Beans. In future articles, I will describe Entity Beans that interact with databases and Message-Driven Beans that respond to MQ Series messages. If you have additional questions or are interested in more information on how your team can begin learning J2EE technology, please contact me at the email address listed below. I would be happy to visit teams to discuss any J2EE-related topic in further detail.

I�d like to thank Keith Crossley and John Klahn for reviewing this article.

References
1. Shirah, Joe Sam. Getting Started with Enterprise JavaBeans Technology. http://ibm.com/developerworks/java/edu/j-dw-java-gsejb-i.html.

2. Sun Microsystems. Enterprise JavaBeans 2.0 Specification. http://java.sun.com/j2ee/1.3/docs.

3. Roman, Ambler, Jewell. Mastering Enterprise JavaBeans 2/e. Available in the Excellus iSpace Teamroom.