Article

Home » Server-side Coding » Java and J2EE » Enterprise Java Beans, A Primer: Part 2, Implement Your First EJBs

About the Author

Jeff Hanson

author_jeff Jeff has more than 18 years of experience in the software development industry, including almost 7 years experience with Java and J2EE. He's authored numerous articles and books, including NET versus J2EE Web Services: A Comparison of Approaches and Connecting JMX Clients and Servers: Understanding the Java Management Extensions.

View all articles by Jeff Hanson...

Enterprise Java Beans, A Primer: Part 2, Implement Your First EJBs

By Jeff Hanson

November 14th, 2003

Reader Rating: 9

Page: 1 2 3 4 5 Next

One of the hardest steps for beginning J2EE developers to take is to build their first EJBs. This article will set aside those fears and help you venture into the world of EJB development.

If you're not yet sure what EJBs are or why you should consider them for a particular project, be sure to read Part 1 of this primer.

Installing the J2EE SDK

In order to build and run an EJB, you must install a J2EE-compliant EJB container. The reference implementation EJB container can be found in the J2EE development kit that's obtainable from Sun. You can download the latest version from http://java.sun.com/j2ee.

The J2EE development kit depends on the Java 2 SDK, Standard Edition (J2SE) development kit, so you will need to download it as well. You can find it at http://java.sun.com/j2se/index.html. You need the J2SE SDK to run the J2EE server and to build and run J2EE applications.

Once you have downloaded the development kits, install them and select the desired location for each kit. Define an environment variable named JAVA_HOME and point it to the install directory for the J2SE development kit. Next, define an environment variable named J2EE_HOME and point it to the install directory for the J2EE development kit.

Now, you're ready to begin writing your EJB.

Writing a Session Bean

Developing an EJB involves most of the same steps and concepts that you have become familiar with when developing plain old Java objects (POJOs), with a few minor differences. The following steps illustrate the typical process to develop and deploy an EJB:

  1. Write the classes and interfaces for your enterprise bean
  2. Write a deployment descriptor
  3. Package the enterprise bean and associated files inside of a jar file
  4. Deploy the bean

Before we get into the gory details of writing our bean, let's look again at the two different types of session beans.

A Recap of Stateful Session Beans

A stateful session bean maintains a conversational state with one client for the duration of a single session. This implies that the stateful session bean can maintain instance variables across multiple invocations from one client during a single session.

Once the client finishes interacting with the enterprise bean and the EJB container removes the enterprise bean, the session for the bean ends and all state data for the bean is discarded.

A Recap of Stateless Session Beans

A stateless session bean does not maintain a conversational state for each individual client. Each invocation of a stateless session bean should be considered as a request to a brand new object instance, since any instance-variable state will be lost between invocations.

Stateless session beans are not persisted to secondary storage by the EJB container; therefore a programmer must recognize that all data is transient between invocations for each client. The transient nature of stateless session beans allows an EJB container to reuse bean instances and, therefore, usually optimize the performance of the beans.

Session Bean Relationships

Figure 1 illustrates the relationships that exist between an EJB client, an EJB container, and a couple of session beans:

1250_architect
Figure 1

A session bean is composed of a minimum of three mandatory classes/interfaces. The first mandatory class/interface is the Component interface, and it must extend javax.ejb.EJBObject. We will implement a very simple stateless session bean that returns a short String message from a method named “retrieveMessage".

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links