An Overview of EJB Technology
- The client’s view
- Fundamentals of the EJB architecture
- Types of EJB
- Distributed and local EJB semantics
- Anatomy of an EJB
- Principle of operation: session and entity EJBs
- Principle of operation: message-driven EJBs
- The EJB container and its proxies
- Overview of the EJB API
- EJB rules, standards and limitations
- Assembly and deployment
- Configuration
- Summary
Overview
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.
Hint
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)); interest.remove();
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.
Gotcha!
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:
{ejb.overview.client.view.session} 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.