Francais | English | Espanõl

Enterprise JavaBean

From Wikipedia, the free encyclopedia

(Redirected from EJB container)
Jump to: navigation, search

Image:Ejb.jpg

Enterprise Java Bean (EJB) is a managed, server-sided component for modular construction of enterprise applications.

The Enterprise JavaBeans specification is one of the several Java APIs in the Java Platform, Enterprise Edition. EJB is a server-side component that encapsulates the business logic of an application. The EJB specification was originally developed in 1997 by IBM and later adopted by Sun Microsystems (EJB 1.0 and 1.1) and enhanced under the Java Community Process as JSR 19 (EJB 2.0), JSR 153 (EJB 2.1) and JSR 220 (EJB 3.0).

Contents

[edit] Rationale

The EJB specification intends to provide a standard way to implement the back-end 'business' code typically found in enterprise applications (as opposed to 'front-end' user-interface code). Such code was frequently found to reproduce the same types of problems, and it was found that solutions to these problems are often re-implemented again and again by programmers. Enterprise Java Beans were intended to handle these common concerns such as persistence, transactional integrity, and security in a standard way, leaving programmers free to concentrate on the particular program at hand.

Accordingly, the EJB specification details how an application server provides:

Additionally, the Enterprise JavaBean specification defines the roles played by the EJB container and the EJBs as well as how to deploy the EJBs in a container.

[edit] History

[edit] Rapid adoption followed by criticism

This vision was persuasively presented by EJB advocates such as IBM and Sun Microsystems, and Enterprise JavaBeans were quickly adopted by large companies. Problems were quick to appear, however, and the reputation of EJBs began to suffer as a result. For starters, the APIs of the standard were far more complex than what typical developers are used to. An abundance of checked exceptions, required interfaces, and the implementation of the bean class as an abstract class were all unusual and counter-intuitive for many programmers. Granted, the problems that the EJB standard was attempting to address, such as object-relational mapping and transactional integrity, are complex. However many programmers found the APIs to be just as difficult if not more so, leading to a widespread perception that EJBs introduced complexity without delivering real benefits.

In addition, businesses found that using EJBs to encapsulate business logic brought a performance penalty. This is because the original specification only allowed for remote method invocation through CORBA (and optionally other protocols), even though the large majority of business applications actually do not require this distributed computing functionality. The EJB 2.1 standard addressed this concern by adding the concept of Local interfaces which could be called directly without performance penalties by applications that were not distributed over multiple servers.

The complexity issue, however, continued to hinder EJB's acceptance. Although high-quality developer tools made it easy to create and use EJBs by automating most of the repetitive tasks, these tools did not make it any easier to learn how to use the technology. Moreover, a counter-movement had grown up on the grass-roots level among programmers. The main products of this movement were the so-called 'lightweight' (i.e. in comparison to EJB) technologies of Hibernate (for persistence and object-relational mapping) and Spring Framework (which provided an alternate and far less verbose way to encode business logic). Despite their lacking the backing of big businesses that Enterprise JavaBeans had, these technologies grew in popularity and were adopted more and more by businesses who had become disillusioned with Enterprise JavaBeans.

[edit] Reinventing EJBs

Gradually an industry consensus emerged that the original EJB specification's primary virtue--enabling transactional integrity over distributed applications--was not of use to the majority of enterprise applications. The functionality delivered by Spring and Hibernate was what enterprise applications needed. The EJB 3.0 specification was a radical departure from its predecessors, and illustrates this new understanding. It shows a clear influence from Spring in its use of POJOs, and its support for dependency injection to simplify configuration and integration of heterogeneous systems. Hibernate's influence is even more clear as Gavin King, the creator of Hibernate, participated in the EJB 3.0 process and is an outspoken advocate of the technology. Many features originally in Hibernate were incorporated in the Java Persistence API, the replacement for entity beans in EJB 3.0. The EJB 3.0 specification relies heavily on the use of annotations, a feature added to the Java language with its 5.0 release, to enable a much less verbose coding style.

Accordingly, although all versions of the EJB spec seek to deliver a similar functionality, in practical terms EJB 3.0 is very nearly a completely new API, bearing little resemblance to the previous EJB specifications.

[edit] EJB types

An EJB container can hold four major categories of beans:

  • Session Beans
    • Stateless Session Beans
    • Stateful Session Beans
  • Entity Beans
  • Message Driven Beans (MDBs or Message Beans)

Stateless Session Beans are distributed objects that do not have state associated with them thus allowing concurrent access to the bean. The contents of instance variables are not guaranteed to be preserved across method calls. The lack of overhead to maintain a conversation with the calling program makes them less resource-intensive than stateful beans.

Stateful Session Beans are distributed objects having state, that is, they keep track of which calling program they are dealing with throughout a session. For example, checking out in a web store might be handled by a stateful session bean, which would use its state to keep track of where the customer is in the checkout process. On the other hand, sending an e-mail to customer support might be handled by a stateless bean, since this is a one-off operation and not part of a multi-step process. Stateful session bean's state may be persisted, but access to the bean instance is limited to only one client.

Entity Beans are distributed objects having persistent state. The persistent state may or may not be managed by the bean itself. Beans in which their container manages the persistent state are said to be using Container-Managed Persistence (CMP), whereas beans that manage their own state are said to be using Bean-Managed Persistence (BMP).

Message Driven Beans are distributed objects that behave asynchronously. That is, they handle operations that do not require an immediate response. For example, a user of a website clicking on a "keep me informed of future updates" box may trigger a call to a Message Driven Bean to add the user to a list in the companies database. (This call is asynchronous because the user does not need to wait to be informed of its success or failure.) These beans subscribe to JMS (Java Message Service) message queues or message topics. They were added in the EJB 2.0 specification to allow event-driven processing inside EJB Container. Unlike other types of beans, MDB does not have a client view (Remote/Home interfaces), i.e. clients can not look-up an MDB instance. It just listens for any incoming message on a JMS queue (or topic) and processes them automatically.

Other types of Enterprise Beans have been proposed. For instance, Enterprise Media Beans (JSR 86) address the integration of multimedia objects in Java EE applications.

[edit] Execution Of EJB

EJBs are deployed in an EJB container within the application server. The specification describes how an EJB interacts with its container and how client code interacts with the container/EJB combination. The EJB classes used by applications are included in the javax.ejb package. (The javax.ejb.spi package is a service provider interface used only by EJB container implementations.)

With EJB 2.1 and earlier, each Enterprise JavaBean had to provide a Java implementation class and two Java interfaces. The EJB container created instances of the Java implementation class to provide the EJB implementation. The Java interfaces were used by client code of the EJB.

The two interfaces, referred to as the Home and the Component interface, specified the signatures of the EJB's remote methods. The methods were split into two groups:

  • methods that were not tied to a specific instance, such as those used to create an EJB instance or to find an existing entity EJB (see EJB Types, below). These were declared by the Home interface.
  • methods that were tied to a specific instance. These are placed in the Component interface.

Because these are merely Java interfaces and not concrete classes, the EJB container must generate classes for these interfaces that will act as a proxy in the client. Client code invokes a method on the generated proxies, which in turn places the method arguments into a message and sends the message to the EJB server.

[edit] The make-up of an Enterprise JavaBean (version 2.1 and earlier)

[edit] Home interface

The Home interface lets client code manipulate certain class methods (that is, methods that are not associated with any particular instance) of the EJB. An EJB Home interface implements either of the javax.ejb.EJBHome or javax.ejb.EJBLocalHome interfaces. The EJB container provides the actual implementation of a bean's Home interface(s).

The EJB 1.1 specification fixes the kind of class methods that can be defined to either be methods to create an EJB or to find an existing EJB if it is an entity bean.

The EJB 2.0 specification lets application developers define new class methods beyond create, delete, and find.

The Home interface defines the lifecycle methods of an EJB class, which includes creating, instantiating, and destroying the instance of EJB from the container.

[edit] Remote and local interfaces

The remote and local interfaces define the instance-specific methods of the bean class that may be called by the bean client's code. A bean provides either a remote or local interface, or both. The local interface can only be used by clients running in the same EJB container and same Java virtual machine; the remote interface can be used by both distributed and local clients.

A local interface defines the object used by local client code to call methods on the EJB. A local interface implements the interface javax.ejb.EJBLocalObject.

The remote interface defines the remote object used by the client code to call methods on the EJB. A remote interface implements the interface javax.ejb.EJBObject. Unlike a local interface, each method in a remote interface declares that it "throws RemoteException".

Remote and local interfaces have different method call semantics. A local interface uses call by reference semantics for object parameters to EJB methods, the same as ordinary Java method invocations. A remote interface uses call by value semantics. The difference between call-by-reference and call-by-value becomes important if the EJB changes the state of the object parameters—changing the state of a call-by-value parameter only changes the state of the copy of the object in the method, whereas changing the state of a call-by-reference parameter changes the state of the original object.

[edit] EJB implementation class

EJB implementation classes are supplied by the application developers. They contain business logic or hold the business data for the object interface. They implement all the methods specified by the Remote interface, and potentially some of those specified by the Home interface.

[edit] Correspondence between interface methods and implementation methods

Method invocations on the Home interface are dispatched to the corresponding methods on the bean implementation class with 1) the prefix 'ejb'; 2) the first letter of the Home interface method capitalized; and 3) having exactly the same argument types.

Method invocations on the Remote interface are dispatched to the corresponding implementation method having the same name and arguments.


[edit] Remote communication

The EJB specification requires that EJB containers support accessing the EJBs using RMI-IIOP. EJBs may be accessed from any CORBA application or provide Web Services.

[edit] Transactions

EJB containers must support both container managed ACID transactions and bean managed transactions. Container-managed transactions use a declarative syntax for specifying transactions in the deployment descriptor.

[edit] Events

JMS is used to send messages from the beans to client objects, to let clients receive asynchronous messages from these beans. MDB can be used to receive messages from client applications asynchronously using either a JMS Queue or a Topic.

[edit] Naming and directory services

Clients of the enterprise Java bean locate the Home Interface implementation object using JNDI. The Home interface may also be found using the CORBA name service. From the home interface, client code can find entity beans, as well as create and delete existing EJBs.

[edit] Security

The EJB container is responsible for ensuring the client code has sufficient access rights to an EJB.

[edit] Deploying EJBs

The EJB Specification also defines a mechanism that lets enterprise Java beans be deployed in a similar manner regardless of the specific EJB platform that is chosen. Information about how the bean should be deployed (such as the name of the Home or Remote interfaces, whether and how to store the bean in a database, etc.) are specified in the deployment descriptor.

The deployment descriptor is an XML document having an entry for each EJB to be deployed. This XML document specifies the following information for each EJB:

  • Name of the Home interface
  • Java class for the Bean
  • Java interface for the Home interface
  • Java interface for the object
  • Persistent store
  • Security roles and permissions

EJB containers from many vendors require more deployment information than that in the EJB specification. They will require the additional information as separate XML files, or some other configuration file format. An EJB platform vendor generally provides their own tools that will read this deployment descriptor, and possibly generate a set of classes that will implement the Home and Remote interfaces.

Since EJB3.0 (JSR 220), the XML descriptor is replaced by Java annotations setted in the Enterprise Bean implementation (at source level), although it is still possible to use an XML descriptor instead.

[edit] Recommended programming model

EJBs should be used in large, distributed, transaction-intensive enterprise scenarios where scalability is a key factor.

In the recommended programming model from Sun, stateful and stateless session beans should encapsulate business logic whereas entity beans should be used to represent business data. Message Driven Beans should represent asynchronous business logic.

NOTE: For large-scale enterprise applications processing large volumes of data, the decision to use entity beans should be based on a very careful analysis. Entity beans provide a way of mapping a given bean to a given table (or tables) for the purpose of working with data on a particular table (or tables). Conceptually, this is a very organized approach. However, in practice, this can result in significant performance drags on the application server when servicing large transaction volumes requiring data from many different relational tables. Under these conditions, other alternative J2EE ways of accessing the required data exist and should be seriously considered over the use of entity beans. If entity beans are used, a coarse-grained approach is generally recommended in which a given entity bean is mapped to multiple database tables, meaning that entity bean can contain data retrieved from multiple tables to service the transaction. By using a coarse-grained entity-bean approach, the total number of entity beans required by the application generally goes down, thereby significantly reducing performance overhead on the server

[edit] External links

es:Enterprise JavaBeans fr:Enterprise JavaBeans it:Enterprise JavaBeans nl:Enterprise JavaBeans ja:Enterprise JavaBeans pl:Enterprise JavaBeans pt:EJB ru:Enterprise JavaBeans sv:EJB zh:EJB

Personal tools