- 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
3.8 The EJB container and its proxies
In Java RMI, 10 the clients of a remote object did not call methods on the remote object directly. Instead, they call methods on a stub, which passes the method call information over the network to the ‘skeleton,’ which calls the method on the remote object. The EJB model extends this notion by introducing the idea of the EJB container as an additional element in the interaction. The container has two main functions:
It intercepts all method calls made by the client and carries out various actions (concerned with security and transaction management, among other things) before delegating them to an instance of the implementation class itself. That is, the container provides proxies that isolate the EJB’s implementation class from its clients.
It provides general services to the EJB, such as database connection pooling.
3.8.1 Container proxies
As we have seen, a remote client of a session or entity EJB makes calls on stubs that implement the home interface and remote interface. These stubs communicate with the home object and remote object, which can be considered part of the EJB container. These proxies then delegate the method calls to the EJB where necessary. Because the EJB server does not know in advance what methods will be provided in your EJBs, it can’t provide general-purpose proxies. Such proxies must be generated for each new EJB that is deployed. The server vendor will provide tools to do this, usually at deployment time. The relationship between the proxies and the stubs is show diagrammatically in Figure 3.4.
Where do the home interface and remote interface fit into all of this? The only Java class that must implement the home interface is the stub that the client uses to communicate with the home object. Similarly, the stub for the EJB object must implement the remote interface. These stubs must implement the interfaces, because the client will call methods on the stubs via the interfaces. At compilation time, the client will likely not have access to the stubs themselves, but at runtime we can be sure that they will implement methods that the client will call.
However, there is no need for the (remote) home object or (remote) EJB object to implement any of the EJB developer’s interfaces. These proxy objects have methods that are called by the skeletons—in a vendor-specific manner, not directly by the client. They therefore don’t need to implement any of the EJB’s interfaces. They may implement them, if the server vendor believes that this is helpful to the implementation of the server, but it is irrelevant to the EJB developer.
If it is irrelevant, why have I mention it at all? The reason is that many introductory textbooks and courses describe the home object as an implementation of the home interface and the EJB object as an implementation of the remote interface. This supports the view that the client ‘calls methods on the home object’ or on the EJB object. This oversimplification obscures the roles of the stubs and skeletons, and the fact that client-EJB interactions using the remote client view are always network operations, with the overheads and complexities that this entails.
We will now consider the functions of the home object and EJB object in more detail. Unless otherwise stated, the comments in this section apply equally to the proxies supporting the local and distributed client views.
The home object
As we have already seen, the home object for each EJB is instantiated as soon as the EJB is deployed, or whenever the server is restarted. The home object must be available at all times, as the client will use it to get access to the EJB.
The home object is responsible for responding to ‘create’ and ‘find’ calls from the client. When the client calls a ‘create’ method, the home object brings about the creation of an instance of the implementation, if necessary (or returns one from the pool). It also causes an EJB object to be instantiated, if necessary. When the client makes a ‘find’ call, the home object either locates the EJB itself or delegates the find call to the implementation. These issues will be discussed in more detail in later chapters.
With distributed interaction, the class on the client that encapsulates the communication between the client and the home object is called the home stub.
The EJB object
In this section, the term ‘EJB object’ refers to both remote and local EJB objects.
The EJB object acts as a proxy for the EJB’s business methods. When the client makes a call on a business method, it is actually making the call on to the EJB object, which carries out two important actions before calling the method itself. First, it checks whether the client is authorized to call the requested method. If not, it throws an exception. If the call is allowed, the EJB object checks whether a new transaction needs to be initiated (based on the transaction attributes supplied by the developer).
After calling the method on the EJB, the EJB object attempts to commit any transaction it started before the call.
It is important to understand that there is not necessarily a one-to-one relationship between EJB object instances and implementation class instances, nor is there a one-to-one relationship between clients and EJB objects. Failure to understand this principle is a major cause of errors in EJB development. The relationship between the instantiation of EJB objects and implementation class instances will be discussed in more detail in the chapters on session EJBs and entity EJBs.
An important feature of the EJB object is to provide the EJB with the illusion that it is running in a single-threaded environment. This relieves the developer of the need to include thread management and synchronization code. The techniques it uses to do this are discussed in Chapter 6.
With a remote client, the class deployed on the client that encapsulates the communication between the client and the EJB object is called the remote stub. Local clients don’t use stubs, as the interactions are in the same JVM.
3.8.2 ‘EJB skeletons’
In Java RMI, the client and server endpoints of the remote method protocol were supported by a stub and a skeleton. It was the skeleton that made the method calls on the remote object. It did this directly at the behest of clients, without interacting with any supporting infrastructure. For example, the skeleton would never take it upon itself to determine whether the caller was allowed to make the method call. In a sense, the stub and skeleton were nothing more than a mechanism to conceal the networking operations.
With EJBs, the situation is more complicated. Between the network and EJB implementation class, two types of supporting functionality are required. First, we have to encapsulate the network operations, as for Java RMI. Second, we need to fulfil the contract between the proxy objects and the implementation class. This contract says, for example, that the proxies can initiate database transactions, carry out security checks, and so on.
All this means that the proxy objects—the home object and EJB object—are not merely skeletons in the Java RMI sense. However, there is no logical reason why skeleton (networking) functionality cannot be embedded into these objects along with their other responsibilities. That is, the stubs on the client could communicate directly with the proxies, which would fulfil this ‘dual role.’
Of course, this is only possible if there is good collaboration between the authors of the software that generates the stubs and those of the software that generates the proxies. This is likely to be the case only when both of these entities are generated by software from the same vendor.
To allow for increased flexibility, and to allow different RMI protocols to be supported with the same proxies, most vendors have chosen to implement separate skeletons for the EJB proxies, rather than allowing the proxies to be skeletons as well. In a scheme of this sort, when the client calls an EJB method, it calls the method on the stub, which communicates with the skeleton, which then calls a method on the proxy, which calls the EJB method. This slight increase in overhead is compensated by an increase in flexibility.
3.8.3 Container services
As well as its role in isolating EJBs from their clients, the container provides various services to the EJB. One of the most important of these is database connection pooling. When an EJB wants to make a connection to a database, it gets this connection from the container, not from a JDBC driver manager. This allows the container to pool database connections, and share them between different clients. With this strategy, an EJB application can support many clients while using only a few database connections. Database connection pooling is described in more detail in Chapter 8.
An EJB relies on its container for access to all external resources, not just database connections. This principle allows the container to manage resources efficiently while maintaining the scope of transactions and propagating security contexts, as will be discussed later. The problem with this strategy is that it can lead to problems, when EJBs need access to resources of a type that the container did not anticipate. This is by no means an uncommon situation: EJB applications frequently need access to legacy systems, networked services, or hardware devices.
Most EJB server products provide an extension API, which developers can use to extend the functionality of the container. The problem with this approach is that it is vendor-specific: Applications that rely on extension APIs will not be portable. The J2EE Connector Architecture—recently finalized—provides a vendor-independent mechanism for extending an EJB container to deal with nonstandard external resources. The connector API is described in detail in Chapter 18.
3.8.4 EJB instance pooling
The EJB architecture is designed, as far as possible, to allow the EJB container to pool instances of the implementation class and share them amongst clients. By doing this, we avoid the overhead of creating and garbage-collecting the large number of instances that would otherwise be required to support large client volumes.
Instance pooling is usually managed by EJB objects in collaboration with other parts of the EJB container (Figure 3.8). In the simple scheme shown, a single, shared EJB object is managing two implementation instances on behalf of a number of clients. It is important to remember that the EJB object must prevent an instance of the EJB’s implementation class from being entered by more than one thread at a time, so the object must have a notion of which instances are serving clients at any given time. If the EJB object is itself pooled or shared, and can accept multithreaded calls from skeletons, then it must itself must be thread-safe. When a client calls a method, the EJB object will make a call on one of the instances of the implementation class, if one is available. Otherwise, it will simply wait until an instance is free. This does impose a delay on the response to the client, so clearly there is an advantage to having enough instances that clients will not usually have to wait long. On the other hand, the server hardware cannot support an arbitrarily large number of active threads, so there may not be any advantage to having a very large pool. 10–30 instances seems to work well for most practical applications.
Figure 3.8 . Instance pooling with a shared EJB object. See text (Section 3.8.4) for details.
The developer cannot assume that the EJB container will direct a client request to any particular instance in the pool. 11 The client may well find that if it calls the same method twice, the second call involves a different instance than the first. Clearly this will have implications for designers. These implications for different types of EJB will be described in detail in later chapters.
Not all EJB types can be pooled. In particular, it is difficult to pool stateful session EJBs, as we shall see.