Home > Articles > Programming > Java

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

3.2 Fundamentals of the EJB architecture

An EJB is essentially a software component on which method calls can be made, and which can itself make calls on other components. These calls may be made over a network. In this sense, EJB technology is similar to Java RMI and CORBA. However, there are some defining features of EJBs that make EJB development rather different from (and easier than) other techniques for developing distributed applications. In Version 2.0 of the EJB Specification, EJBs are allowed to interact using local calling semantics, as well as distributed, RMI-like techniques.

Here are some other important features of the EJB architecture.

The client’s view of an EJB is defined strictly by interfaces As we have already seen, synchronous clients can call only those methods exposed by the EJB’s interfaces. In the EJB Specification, the interfaces are collectively referred to as the client view. Each EJB publishes ‘factory’ interfaces and ‘business method’ interfaces (usually, but not necessarily, one of each). The factory interfaces expose methods that clients can use to create, locate, and remove EJBs of that type. The business method interfaces define all the methods that clients can call on a specific EJB after it has been located or created through the factory interface. The term ‘business method’ is a generic one used to describe any method that a client can call on a specific EJB, and does not have to be related to ‘business’ in the dictionary sense.

Figure 3.1 shows in outline the interaction between EJBs, with the interfaces forming the points of contact in all cases.

Figure 3.1Figure 3.1 . The interactions between EJBs and their clients are defined in terms of interfaces. Each EJB has a factory interface (‘F’) and a business method interface (‘B’), as discussed in the text. When EJBs make method calls on other EJBs, even in the same JVM, then the calling EJB is a client of the target EJB and can call only those methods exposed by the interfaces.


It is important for the developer to understand that anything that makes method calls on an EJB is a client of that EJB, and interacts with it via its factory and business method interfaces. This applies even in the case where multiple EJBs interact within the same JVM. Enforcing this model allows the EJB infrastructure to provide important services transparently. If we are sure that the EJBs must be in the same JVM, then we can use local home and local interfaces, rather than remote home and remote interfaces.

In Java, as in most programming languages, an interface is simply a specification of method signatures; it does not indicate how the methods are to be implemented. Obviously something has to implement the interfaces. You may be surprised to learn that in EJB technology the interfaces are never implemented by a class authored by the developer. They are implemented proxy objects generated dynamically by the server vendor’s tools.

EJBs are isolated and supported by an EJB container Although EJB clients make method calls as if they were directly on the EJB, in fact the method calls are on proxies which delegate to the EJB’s implementation class. These proxies, and their supporting classes, form the EJB container. 5 The client never calls EJB methods on the implementation directly, even if the client and the EJB are actually on the same server, or even in the same JVM. This strategy allows powerful features like distributed transaction management to be provided transparently, and provides for pooling of implementation instances to increase efficiency.

Message-driven EJBs are not called directly by clients at all, and don’t have container proxies in the same sense. Instead they are called directly by the container when it receives a message for a queue or topic in which the EJB has registered an interest.

The container encapsulates the EJB and acts as its security manager. It also provides general services to the EJB, as we shall see. The notion of the container and its proxies is illustrated in Figure 3.2. 6

Figure 3.2Figure 3.2 . The notion of the EJB container as a proxy for the EJB: the client calls methods on the home object and EJB object, which delegate to the implementation itself. The process is transparent to the client. There are different home objects and EJB objects for local and remote access, but the purpose of these objects is essentially the same

Because the methods on the EJB proxies will delegate to methods on the EJB itself, the proxies must be generated to match the EJB—that is, the proxies will be specific to the EJB they serve. The vendor of the EJB server will provide tools to support this generation, which will typically take place when the EJB is deployed to the server.

Gotcha!

The terms ‘EJB server’ and ‘EJB container’ are not well-defined in the EJB Specification, and are used by developers somewhat interchangeably. In this book, a ‘server’ will be taken to mean a process of some sort, providing a set of low-level services that may be shared by any number of EJBs. A ‘container’ refers specifically to the supporting class or classes that encapsulate and manage an EJB.

The EJB container provides an illusion of a single-threaded environment The developer of an EJB should not have to be concerned about concurrency and thread management; in fact, it contravenes the EJB Specification [EJB2.0 24.1.2] for the developer to include code to handle these issues. The container ensures that the EJB is never called re-entrantly, 7 so the developer can code the EJB as if it were used in a single-threaded environment. This issue is a subtle one, and is discussed in more detail on page 476.

The EJB container manages database transactions It does this even when the transaction spans multiple servers. There is a mechanism for specifying the transactional behaviour of EJBs outside of program code, although limited programmatic control of transactions is permissible as well.

The EJB container manages access and security Security attributes specify which methods on which EJBs are accessible to which groups of users. No coding is required, although limited programmatic intervention is allowed. The EJB server must provide a mechanism for end users to identify themselves to the application (e.g., a ‘login’ dialog box) or be able to access authentication from a Web browser in a Web application. The EJB developer should never have to code authentication procedures.

Creation and location of EJBs is standardized There is a well-defined way for the client to create new EJBs or to find existing ones. It does this on the factory interface, which it finds by doing a JNDI lookup on the EJB name. Having found the interface, the client can call its create() or find() methods to create or locate new EJBs. When it does this, it gets a reference to a proxy, not to the the EJB itself. The client manipulates the proxy exactly as if it were the real EJB, but the proxy carries out other functions (transaction management, for example) as well as delegating method calls to the EJB.

Instances can be pooled for efficiency When a client creates an EJB, it is really creating a ‘conceptual EJB,’ not an instance of a Java class. The EJB container may pool instances of the implementation class and give the client a reference to an existing instance when the client asks to create an EJB. Allowing the container to pool instances can lead to enormous gains in efficiency. Again, this is transparent to the client, but the developer must be aware of the circumstances in which pooling is permitted. Proxy instances may also be pooled in some circumstances.

The container manages resources EJBs get access to external resources—such as databases and messaging systems—through the container. The container can pool, share, and manage these resources on behalf of the EJBs, in a way that is transparent to the EJB developer. In this way, we get all the benefits or a resource sharing scheme, with few of the disadvantages.

There is a standard deployment process There is a specified, standard way for the EJB to be packaged and transferred to the server. This process is called deployment. Any EJB server that complies with the EJB Specification [EJB2.0 23] must be able to accept an EJB packaged in the correct way, regardless of the platform on which it was developed and tested.

  • + Share This
  • 🔖 Save To Your Account