Home > Articles

Overview of EJB

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.

NOTE

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.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020