Home > Articles

Overview of EJB

  • Print
  • + Share This
This chapter is from the book

Types of EJB

The EJB 2.0 specification has several major and important additions and changes. One of the biggest additions has been a new type of bean—a message-driven bean. These beans provide the J2EE applications with the capability to be triggered by a messaging engine, and more specifically by the arrival of a specific message. Three types of beans are defined in the EJB specifications: entity beans, session beans, and message-driven beans. Each bean enables a specific set of interactions between the client and the J2EE application.

Entity Beans

An entity bean represents the different data objects of a J2EE application and the relationships between them. Entity beans are a representation of a J2EE application's data, and are independent of the actual persistent storage mechanism. Each entity bean has an abstract schema that describes the structure (fields and relationships as well as the access mechanisms). The fields and their relationships are actually managed by the entity bean container in an implementation-specific way. The deployment descriptor of the entity bean describes the abstract schema that is associated with the physical classes used by the container to store the entity bean fields and relationships.

Container-Managed Persistence

In container-managed persistence, the container is responsible for making the right calls to the persistence storage mechanism (RDBMS, XML database, ASCII files, and so on), and ensuring the synchronization between the entity bean and the physical storage. An entity bean is essentially a framework for transforming data stored in external persistence mechanisms into Java objects. The code of a container-managed entity bean does not have any calls to the physical persistence environment. So if the data is actually stored in a relational database, then the entity bean has no JDBC calls to update the database. That is deferred to the entity bean container. The deployer maps the entity bean schema (and its fields) to the RDBMS tables and columns. The deployment tools provided by the container provider generate the necessary classes (drivers), which interact with the RDBMS and transform data to entity beans.


Deployment tools play a critical role in the installation, configuration, and maintenance of J2EE applications. J2EE users have the choice of using deployment tools from third parties that specialize in deployment management technologies. For example, tools such as TopLink and CocoBase are commonly used for deploying J2EE applications, especially when some of the application servers do not have deployment tools.

This separation of the entity bean from the actual storage mechanism makes the container-managed persistence scheme very flexible. If the entity bean needs to change its underlying persistence mechanism, the deployer can do it without having to change the entity bean code. The individual fields of the entity bean will be remapped to the new persistence mechanism.

Because there are no direct JDBC calls in the entity bean, there has to be a different interface for querying persistence environment for data. EJB QL is the query language used by the entity bean, and is very similar in syntax to SQL. So although the entity beans are independent of the persistence environment, the query language is very much like SQL-based RDBMS queries. It is the deployer's job to map EJB QL to the physical interface, which can be JDBC-based SQL or another persistence-management mechanism depending on the implementation classes generated by the deployment tool.

The benefit of container-managed persistence is the resulting flexibility, but the overhead of working with logical schemas is not always justified—especially if the schema is small or if the persistent storage is expected to remain constant for a long time.

Bean-Managed Persistence

In this method of accessing persistent data, the entity bean provides an object view of the data. Just as the container-managed persistence mechanism transformed external data representation into a Java object, a bean-managed persistence mechanism transforms the physical data structure to a Java object. The difference is that the entity bean has code that accesses the persistence environment directly. There is no logical schema involved, and therefore there is no mapping of the entity bean's structure (fields and relationships) to the physical data model.

Bean-managed persistence can be used to reduce the overhead of container-managed persistence, but the price to pay is loss of flexibility.

Session Beans

A session bean is an EJB that manages sessions (or conversations) on behalf of the client (application components or JCA resource adapters). The lifecycle of a session bean is controlled by the client. In some situations, such as server (host of the session bean) errors, the EJB container can terminate a session bean. Therefore, clients of a session bean should be prepared to create a new instance of the session bean if the original instance is terminated by the container.

A typical session is transient, and its state is usually not persistent. An example of a session could be tracking your courier package using a Web-based status query application. If for some reason the Web server dies or the session times out (does not get the response back within a predetermined time interval), then the session terminates and the user is required to start a new session. Most online transactions are session-oriented, with the user initiating a session, performing a set of actions, and then terminating the session. Hence, a session bean generally stores its state in transient variables.

Not all sessions are conversational, and some sessions are unidirectional—with the client invoking some methods or actions as part of the session without expecting any reply. These sessions are called stateless sessions; the state management mode of the session bean is described in the deployment descriptor. Conversational sessions are managed by stateful session beans. In practice, especially in the context of distributed Web applications, the stateless session beans are used more frequently than stateful session beans.

There can be many session bean instances being managed by the bean container, and it is possible that not all session beans are active at any given moment. Session beans can be active as a result of direct invocation by client components or as part of container-managed transactions. In either case, it is possible that the session bean container may want to swap out inactive session beans to secondary storage temporarily, and swap them back when required. This swapping-out process is known as passivation, and the swapping-in process is known as activation of session beans.

All session beans must implement the SessionBean interface. The bean container invokes the setSessionContext method to associate a session bean instance with a context that is maintained by the container. The context instance is usually held by the session bean instance as part of its state. Because the context is valid while the session bean is in existence, it should be held in a transient variable. This is to ensure that when the session bean is swapped out by the container, the context instance is not lost when the session bean is swapped back in.

Some of the events generated by the session bean container, which are associated with specific actions in a session bean, include ejbRemove, ejbPassivate, and ejbActivate. When a session bean receives these notifications, it should release the resource (in the case of ejbRemove and ejbPassivate) and require the resource (in the case of ejbActivate).

Unlike stateful session beans, stateless session beans do not store any reference or state information that associates the bean to a particular client. This makes stateless session beans equivalent in terms of servicing clients. The session bean container can therefore pass along the client's request for a specific stateless bean method to any instance of the stateless bean, as long as it belongs to the right class.

This means if the resource adapter interacts with an asynchronous messaging engine, then a stateless session bean may be better suited as the client. If on the other hand the message engine is a synchronous message platform, or if there is a synchronous service that the resource adapter is interacting with, then a stateful session bean is better suited as a client. Overall session beans (stateful or stateless) will be some of the primary clients of resource adapters.

Message-Driven Beans

A message-driven bean is different from the session bean or the entity bean. Its client is the container that invokes the message-driven bean upon receiving a JMS message. So a client that wants to access the business logic encapsulated in the message-driven bean must send a JMS message to the appropriate JMS destination (queue or a specific topic). The message-driven bean listens for messages on a queue or for a topic. In some ways, a message-driven bean is like a stateless session bean, with the capability of waiting for a JMS message on an asynchronous messaging platform.

Because JMS is a messaging standard not restricted to the J2EE environment, other messaging platforms that are also JMS-compatible, such as IBM MQSeries, can be used to integrate legacy applications. Herein lies a conflict in terms of what is the better mechanism for integrating message-based legacy systems. Would a message-driven bean be the appropriate mechanism for receiving messages from the legacy application, or should that job be left for JCA resource adapters? Note that a JCA resource adapter does not have a message-driven interface in its client component interface (CCI) interface. Thus, in some instances when the legacy application is sending messages to the J2EE application, a message-driven bean could then be a trigger for processing inbound messages. Outbound messages could be generated by a resource adapter.

Figure 3.2 shows a possible design pattern in which message-driven beans and JCA resource adapters work together to support a distributed asynchronous integration scenario involving a message platform-based legacy application and a J2EE application. In the scenario, a session bean that is part of the J2EE application accesses the resource adapter for the legacy system using its CCI interface. The resource adapter creates a JMS message encapsulating the service request, and puts the message into the legacy application's inbound message queue. At this time, the resource adapter has completed its job, and either returns a successful status if the message is written properly to the queue, or throws an exception. The legacy system processes the inbound message when it has time, and encapsulates the response in a message before writing the message to the outbound queue. A message-driven bean is monitoring the outbound queue, and the bean container invokes the message-driven bean as soon as it detects the presence of the response message.

Figure 3.2 Distributed J2EE integration scenario.

Suppose that the scenario changes a little, and instead of processing the response in an asynchronous way, the J2EE application wants to wait for the response. Then, the JCA resource adapter must monitor the outbound queue and wait for the response message before returning control to the client session bean. A number of similar design patterns involve session beans, message-driven beans, and entity beans, in conjunction with resource adapters, to solve application integration problems and scenarios.

  • + Share This
  • 🔖 Save To Your Account