Home > Articles > Programming > Java

An Overview of EJB Technology

Get an outline of Enterprise JavaBeans technology, and learn about the interaction between the EJB, the client, and the EJB container. You will also learn the EJB objects and home objects, and why the use of these constructs simplifies the development of substantial software projects.
This chapter is from the book


This chapter describes EJB technology in outline and provides an overview of the interaction between the EJB, the client, and the EJB container. It also describes the EJB objects and home objects, and explains why the use of these constructs—although perhaps somewhat confusing at first— ultimately simplifies the development of substantial software projects. We begin by examing how an EJB is seen by its clients, which may be synchronous, using RMI, or asynchronous, using messaging. We then discuss the Java entities that make up an EJB, the home interfaces, local interface, remote interface, and implementation class, and how they are packaged with a deployment descriptor to make a complete EJB.

The chapter concludes with a discussion of the techniques that commercial EJB products may use to optimize performance and increase reliability. As it is an overview, this chapter does not provide any complete examples of EJBs, and could lead readers into thinking that the subject is more complicated than it really is. Moreover, to avoid fragmenting the text, I have included material in this chapter that, although part of an outline of the technology, is rather more complex than you may wish to see on a first reading. If you are impatient to see an EJB working and look at some real code, you may prefer to read Chapter 4 first. You may find that this helps to put some of the more complicated parts of this chapter into perspective.

3.1 The client’s view

In this book, we will encouter some rather complex, and perhaps even intimidating, technology. To put it into context, I would like to begin by discussing how an EJB is seen by its clients. We will see that this is, in fact, quite straightforward. This is important, because EJBs are usefully viewed as providers of services to their clients: A service can be as complex as the needs of the application demand, but the provision of the service must be simple. For example, when I wish to make a telephone call, I know that I have to interact with the telecommunications system in a simple, well-defined way: I dial a number on the handset. This is my interface to the provision of the service. What happens inside the telephone exchange is doubtless very complex, but as a user of the service, this is not important to me.

As we have discussed, and will describe in more detail later, EJBs have two sorts of clients: synchronous clients and messaging clients. Synchronous clients invoke services on EJBs by calling methods on them, perhaps over a network connection. Such clients may be standalone Java programs (or perhaps programs written in other languages), servlets, JSP pages, or other EJBs. The EJBs that support synchronous access are the ‘session’ and ‘entity’ EJBs, which are described in much more detail below. Messaging clients obtain services by posting messages into a message service monitored by the EJB server. Messaging clients are serviced by message-driven EJBs, which will be the subject of Chapter 10.

We will see that both of these kinds of clients obtain a straightforward interface to the services of the EJB.

3.1.1 Synchronous clients

A fundamental principle of the EJB architecture is that synchronous EJBs (that is, session EJBs and entity EJBs) are used much like ordinary Java classes. We will see that entity EJBs represent persistent objects and have to be synchronized to a data storage medium (e.g., a relational database); even here, these details are invisible to the client. Such an EJB looks like an ordinary Java object, albeit with persistent state. By way of illustration, this chapter will make use of a simple EJB that calculates interest repayments.


The EJB used as an example for this section is described in full in Chapter 4, along with step-by-step instructions on how to compile, assemble, and deploy it, and to test it using a test client.

In general, an EJB’s synchronous clients will interact with the EJB in a straightforward Java sense.1 The listing below is an extract from the test client for the ‘interest calculator’ EJB, which is listed in full on page 96.

Interest interest = getInterest ();
double principal = 10000.0;
double rate = 10.0;
int terms = 10;
System.out.println (”Interest = $”+
  interest.getInterestOnPrincipal (principal, rate, terms));
System.out.println (”Total = $”+
  interest.getTotalRepayment (principal, rate, terms));

The client calls a method getInterest() (described below) to obtain a reference to the EJB. It then calls methods on it, just like any ordinary Java class. Finally, it calls remove() on the EJB instance to signal that it has finished using it.

Now, in reality the client and the EJB are likely to be located on different physical hosts. Note that this is not apparent in the code: The client is not concerned whether the EJB is on the same host, a different host in the same room, or on the Moon; the code is the same in all cases. Of course, the variable interest in the listing above cannot reference the real remote object. The Java language has no built-in support for remote references. Clearly, it has to be a proxy of some kind. The identifier Interest does not, in fact, refer to a class, but to an interface. The interface will be implemented by a proxy that has the capability of communicating with the remote object. In this way, the low-level details of the RMI procedure are completely invisble to the client. As we shall see, Interest is the remote interface for the EJB.

So, from a Java language perspective, an EJB is manipulated through a set of interfaces. These interfaces specify the behaviour of the EJB and form a contract of service between the EJB and its clients. The interfaces are produced by the EJB developer and distributed to clients that require access to the EJB. Clearly, a Java class cannot call methods on an interface unless there is, somewhere, a class that implements that interface. However, this class will be generated automatically by the server’s tools. As far as the client is concerned it calls methods on the interface. 2

The EJB Specification defines two kinds of interfaces: the ‘local view’ and the ‘remote view.’ The local view interfaces are used by clients that will be located in the same JVM as the EJBs they are calling, while the remote view interfaces will be used by all other clients. An EJB can be provided with both sets of interfaces, and they can provide access to the same functionality, or different functionality, as the developer sees fit. We will have much more to say about the distinction between the local view and the client view, both later in this chapter (page 57) and throughout the book.

Whether we are using local or remote access, two different interfaces are required for method of access. The home interface (or ‘factory interface’) exposes functions that allow clients to obtain references to EJBs, create new EJBs, and remove redundant EJBs, while the remote interface and local interface provide access to the methods of the EJB itself. In this book, I refer to the remote interface and the local interface collectively as ‘business method interfaces.’ You may care to consider whether these two sets of methods could, in fact, be expressed usefully on the one interface. This matter is discussed along with the technicalities of RMI in Chapter 2.


The terminology used in describing interactions between EJBs and their clients is a potent source of confusion. This is because the client’s view of ‘instantiation’ of an EJB is different to what happens in the JVM on the EJB server. In this book, I have followed two conventions in an attempt to reduce the problem. First, I never described the client as ‘instantiating’ an EJB. Instantiation is something that happens to a specific Java class. The JVM hosting the EJB may, or may not, instantiate one or more Java classes in response to the client’s request. So I always talk about the client ‘creating,’ ‘finding,’ or ‘removing’ an EJB. Second, where something is instantiated, I have tried—to the extent compatible with reasonable brevity—to say exactly what class is instantiated. So, when something instantiates an instance of the EJB’s implementation class, this is what the text will say, unless it is plainly obvious. In this book, I avoid terms like ‘instantiates an EJB’ because an EJB is not simply a class.

To begin an interaction with an EJB, its client must first of all obtain a reference to something that implements the home interface (we will discuss what this ‘something’ is later). We can then call a method on this interface to create or locate the required EJB. This is probably the only part of a client’s interaction with an EJB that is substantially different from the interaction between Java objects in the same JVM. According to the EJB Specification [EJB2.0 6.2.1], the client uses JNDI to get a reference to the home object. In the Interest example, this logic is encapsulated in the getInterest() method, which looks like this:

InitialContext initialContext = new InitialContext();
Object o = initialContext.lookup (”Interest”);
InterestHome home = (InterestHome)
  PortableRemoteObject.narrow (o, InterestHome.class);
return home.create(); 

The interface between JNDI and EJBs is described in much more detail in Chapter 7. For the moment, notice that the key operation is the call to the lookup() method on the InitialContext(). The argument to this method provides the name of the EJB, as it is known to the EJB server, and the call returns something that implements the EJB’s home interface, InterestHome. 3 The client can then call the create() method to get a reference to the EJB itself. More accurately, it gets a proxy that implements the remote or local interface. We will have more to say about the home object, and other server-side proxies, later in this chapter.

With a local client view, the client code is even simpler because the ‘narrowing’ operation is not required:

Context context = new InitialContext();
Object ref  = context.lookup("MyEJB");
MyEJBHome home = (MyEJBHome) ref; 

The object returned by the lookup() operation will always be in the same JVM as the caller and does not need to be narrowed.

3.1.2 Asynchronous (messaging) clients

Message-driven EJBs have fewer Java elements than the other types. Because they don’t take part in RMI, or indeed in any synchronous access by clients, they don’t have interfaces of any type. Instead, the developer registers the EJB with a particular message queue or message topic hosted on a message broker. A client interacts with the message-driven EJB by posting a message to it. This is important, because a messaging client is asynchronous with respect to the EJB application: When the client has posted its message, it can continue without waiting for a response. The EJB can then pick up the message and handle it in its own time. Asynchronous clients are useful in business-to-business operations, where the the applications may have to be loosely coupled. 4

The client code needed to interact with message-driven EJBs is somewhat more involved than that for a session or entity EJB (but not much more), so we defer an example until later.

3.1.3 Client view: summary

We have seen that an EJB presents a straightforward interface to its clients. Session and entity EJBs appear to the client as ordinary Java objects on which method calls can be made, while message-driven EJBs appear simply as message queues or topics. To obtain this simplicity, the EJB infrastructure is actually rather complex, and it is to this that we must turn our attention next.

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.


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.


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.


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.


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


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


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.


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.


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