In this article, we will discuss the Enterprise Edition JavaBean to help you eliminate your fear of creating your first EJB and help you step into the world of EJB development.
Install the EJB running and development environment
EJB must be executed in the EJB container. Therefore, we must install an EJB container that complies with the J2EE standard. To help us deploy our own ejbs, we also need a deployment tool. The reference implementation for J2EE from Sun includes an EJB container and an EJB deployment tool. This J2EE reference implementation can be found in the J2EE development kit released by Sun.
The J2EE development kit depends on Java 2 SDK and Standard Edition (J2SE), so you also need to download it. You need J2SE SDK to run the J2EE server with reference implementation, and create and run the EJB application.
Once you download the Development Kit, you need to install them and select the desired location for each development kit. You must first define an environment variable named JAVA_HOME and point it to the installation directory of the J2SE development kit. Define an environment variable J2EE_HOME and point it to the installation directory of the J2EE development kit.
Now you are ready to write your EJB.
Compile a simple session EJB
The majority of the steps and concepts involved in developing an EJB are the same as those used for developing a traditional Java object (POJO), with only a few differences. The following is a basic step for developing and deploying ejbs:
1. Write classes and interfaces for your EJB.
2. Compile the deployment descriptor for your EJB.
3. Package the EJB and associated files into a jar file.
4. Deploy EJB.
There are two different types of session beans. Before writing code, let's take a look at the similarities and differences between them.
Status session EJB Overview
State session EJB maintains a state of conversation with the client during a single session. This means that state session EJB can maintain instance variables that span multiple calls from one client during a single session.
Once the client completes the interaction with EJB and the EJB is easy to delete, The EJB session ends and all status data for the EJB will be deleted.
Overview of stateless session ejbs
Stateless session ejbs do not maintain a conversation state for each individual client. Every call to the stateless session EJB should be treated as a request to a brand new object instance. Because any instance-variable status will be lost between calls.
Stateless session ejbs are not stored in the second storage space by the EJB container, so the programmer must know that all data between each call of each client is instantaneous. The transient feature of stateless session EJB allows the EJB container to reuse the EJB instance, so the EJB performance is often optimized.
Relationship between session beans
Figure A illustrates the relationship between the EJB client, the EJB container, and several session ejbs.