Home > Articles > Programming > Java

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

3.5 Anatomy of an EJB

When authoring a session or entity EJB, the developer must provide at least three Java .class files: two interfaces that expose the methods of the EJB that are accessible to clients, and an implementation class (often called the ‘bean class’ or ‘EJB class’). We need two interfaces because one exposes the factory methods of the EJB (create(), find()), while the other exposes the business methods that can be called on the specific EJB. In EJB 1.1, all EJB method calls were RMI calls, and only one pair of interfaces was required, the home interface and the remote interface. Of these, the former exposed the factory methods, and the latter the business methods.

As we have discussed, in EJB 2.0, to support the notion of intra-JVM EJB interaction, the developer can choose whether to specify a ‘local’ view, a ‘remote’ view, or both. When the local view is used, the factory methods are exposed by an interface called the local home interface, and business methods by the local interface. These interfaces and their relationships are summarized in Table 3.1. Although the names of the remote view interfaces have changed between EJB 1.1 and EJB 2.0, their functions have not, and neither has the Java code required to define them. All the interfaces are described in more detail later.

On a practical note, the factory interfaces tell the container how to implement the factory proxy, while the business method interfaces fulfill the same function for the business method proxy. These objects are described later in this chapter. Where we are providing a remote view, the interfaces also tell the server how to generate the stubs and skeletons needed to support RMI.

For a message-driven EJB only the implementation class is necessary.

All classes and interfaces must be supplied in a JAR file with a particular structure (see below).

As we shall see, the implementation class must provide implementations not only of the methods exposed by the interfaces, but of the life cycle management methods, which are called by the EJB container, not the client.

The correspondence between the interfaces and container-generated proxies is shown in Figures 3.3 to 3.5. These diagrams are for reference, rather than study, so please don’t try to memorize them (yet!).

Figure 3.3Figure 3.3 . The relationship between the factory and business method interfaces in general. Bold boxes indicate Java classes or interfaces supplied by the EJB developer. The proxies will be generated automatically by the server vendor’s tools.


Figure 3.4Figure 3.4 . The relationship between the factory and business method interfaces in the remote client view. With EJB 1.1, this is the only view available.


Figure 3.5Figure 3.5 . The relationship between the factory and business method interfaces in the local client view. Note how much simpler this model is than Figure 3.4, as a result of the absence of RMI support classes.


Table 3.1. Interfaces supplied by the EJB developer in EJB 1.1 and EJB 2.0. It is unusual0 for both distributed and local interfaces to be provided for the same EJB.

EJB usage

EJB 1.1 interface

EJB 2.0 interface

Exposes

Distributed interaction

Home interface

Remote home interface

Factory methods

Remote interface

Remote interface

Business methods

Local interaction

n/a

Local home interface

Factory methods

n/a

Local interface

Business methods

Along with these three .class files, the EJB developer must provide at least one other file: the deployment descriptor. This is an XML file that contains information about the structure of the EJB.

3.5.1 Business method interfaces

Session and entity EJBs need a business method interface, 8 which will expose to clients the business methods they may call. Message-driven EJBs don’t have clients in the strict sense, and therefore don’t need a business method interface.

When the EJB provides a local client view, the business method interface is provided by the local interface. For the remote client view, it is provided by the remote interface. Despite the different names, these interfaces have exactly the same function: to expose business methods on the EJB to clients. Each method that the client will call on the EJB must be specified in the remote or local interface. Local interfaces are new in Version 2.0 of the EJB Specification. It is unlikely that both a remote and a local interface will be required for the same EJB, but where both are provided, they will probably expose the same methods.

For example, if the EJB is to have a method withdrawFunds(double amount), which can throw an exception InsufficientBalanceException, then it would be defined in the remote interface like this:

public void withdrawFunds(double amount)
  throws RemoteException,
    InsufficientBalanceException; 

As in Java RMI, methods in the remote interface must be defined to throw RemoteException, because the class that actually implements the remote interface (the remote stub; see below) will need to be able to throw this exception. In addition, the remote interface must declare any other exceptions that can be thrown from the EJB’s methods. In the local interface, the definition would read:

public void withdrawFunds(double amount)
  throws InsufficientBalanceException; 

That is, there is no provision to throw RemoteException. This is because the local interface is used only for intra-JVM calls; no stubs are required and there is therefore nothing to throw this exception.

All EJB remote interfaces must be subinterfaces—direct or indirect—of javax. ejb.EJBObject. Local interfaces must be subclasses of javax.ejb.EJBLocal Object.

When the EJB is deployed, the deployment tool or the EJB server will use the remote interface to generate a proxy for the EJB called the EJB object. 9 It will use the local interface to generate a proxy called the local object.

3.5.2 Factory interface

The factory interface of a session or entity EJB exposes the methods that the client can call that relate to life cycle management of the EJB. These methods do not necessarily correspond to method calls in the implementation class: They may be handled entirely by the EJB container. With distributed client view, the factory interface is formally called the remote home interface, while with local client view, it is the local home interface. Because EJB 1.1 only supported distributed clients, there was no concept of a ‘local home interface.’ Thus the terms ‘factory interface’ and ‘home interface’ were synonyms. For simplicity, this book follows that same pattern, and the term ‘home interface’ refers to either factory method. I will use the terms ‘remote home interface’ and ‘local home interface’ only when it is necessary to distinguish between them. Such circumstances will be rare, as the remote and local home interfaces fulfill exactly the same purpose.

The home interface (local or remote) can specify two types of method: ‘create’ methods and ‘find’ methods. These relate to the two ways in which the client can get a reference to an EJB: It can create a new one or find an existing one. For example, all stateless session EJBs must specify the following method in the remote home interface:

public [remote] create()
  throws RemoteException,
    javax.ejb.CreateException; 

where [remote] is the type of the remote interface. This method creates an EJB, and returns to the client a reference to the EJB object that acts as the EJB’s proxy.

For local home interfaces, the equivalent is:

public [local] create()
  throws javax.ejb.CreateException; 

where [local] is the type of the local interface. In this case, the EJB server returns a reference to the the local object that acts as the EJB’s proxy.

To accompany the method definitions in the interface, the implementation class must provide a method, ejbCreate(), that matches each create() in the home interface.

All remote home interfaces must be subinterfaces—direct or indirect—of javax. ejb.EJBHome, while local home interfaces must be subinterfaces of javax.ejb.EJB LocalHome.

When the EJB is deployed, the deployment tool or the EJB server will use the home interface to generate a proxy called the home object.

Message-driven EJBs don’t need a home interface.

3.5.3 Implementation class: session and entity EJBs

In a session or entity EJB, the implementation class provides implementations of the methods specified in the business method interface, and perhaps of those in the factory interface. The quotes around the word ‘implementations’ are there because, however odd it sounds at first, the implementation class does not implement either of these interfaces in the Java sense. That is, if we are providing a remote client view (as we usually will be) where the home interface is MyHome and the remote interface MyRemote, we will not write code like this:

public class MyImplementation implements MyHome, MyRemote 

Instead, it is the container-generated stubs and proxies that will implement the home and remote interfaces. Of course, the implementation class must provide code that supports the methods specified in the interfaces. We will see how this is done later.

In addition to the business and factory methods provided for the benefit of clients, this class must implement the methods that will be called by the EJB container during life cycle management. For example, there will be method calls that indicate when the implementation class instance is created and destroyed.

Methods that are called by the EJB container are usually referred to as callback methods or life cycle methods.

The implementation class must implement javax.ejb.SessionBean, javax. ejb.EntityBean, or javax.ejb.MessageDrivenBean, depending on whether it is a session, entity, or message-driven EJB. Thus implementations must be provided for all the methods in these interfaces, although in many cases the method bodies may be empty.

3.5.4 Implementation class: message-driven EJBs

A message-driven EJB’s implementation has no business methods. The container calls the onMessage() method when a message arrives. These EJBs have no home or remote interface, and no home object or EJB object is generated. However, like session and entity EJBs, the message-driven EJB’s implementation class must handle the callback methods that are concerned with life cycle management.

3.5.5 Deployment descriptor

The deployment descriptor provides configuration information to the EJB server. Information in the descriptor includes the following:

  • the display name: the name of the EJB displayed by deployment tools;

  • names of the classes that the EJB requires: implementation, interfaces, and primary key class, if any;

  • the type (session, entity, message-driven) and subtypes (stateful, stateless, etc.);

  • security attributes (see Chapter 16);

  • transaction attributes (see Chapter 9);

  • configuration (‘environment’) variables (see Chapter 7);

  • EJB cross-reference information;

  • resource reference information (e.g., names of databases used).

The deployment descriptor is an XML file, and the EJB Specification [EJB2.0 21] states exactly what its syntax should be. In addition, it specifies that the deployment descriptor must be a file called ejb-jar.xml in a directory META-INF in the JAR file.

The format of the deployment descriptor is not particularly complex, but it can be quite lengthy. While it is straightforward to construct the file manually for simple applications, most serious development work will require the use of software tools. Most developers use graphical tools supplied by the vendor of the EJB server, which are often part of an integrated EJB assembly and deployment tool. For that reason, this book will not have much to say about the deployment descriptor in the text, but a brief overview is presented in Appendix B. More interested readers should refer to [EJB2.0 23] for full details.

3.5.6 EJB class naming conventions

Because the deployment descriptor defines the roles of the .class files that make up the EJB, the names used for the implementation class, home interface, and remote interface are arbitrary. However, it is sensible for clarity to adopt a naming convention that indicates immediately what the different classes and interfaces do. A convention that is widely used, and that I have followed in this book, is to base the class and interface names on the EJB name. For example, if the name of the EJB is to be Booking, then the names would be:

Home interface

BookingHome

Remote interface

Booking

Bean class

BookingBean

In particular, the remote interface has the same name as the EJB, as this is the name that will be manipulated most frequently in client code. Of course, none of this applies to message-driven EJBs, as they don’t have home or remote interfaces. There is, as yet, no widely established naming convention for the local home interface or local interface.

  • + Share This
  • 🔖 Save To Your Account

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