Enterprise JavaBeans
From Free net encyclopedia
Template:Expert The Enterprise JavaBeans (EJB) 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 by Sun Microsystems (EJB 1.0 and 1.1) and later under the Java Community Process as JSR 19 (EJB 2.0), JSR 153 (EJB 2.1) and JSR 220 (EJB 3.0).
The EJB specification details how an application server provides:
- persistence
- transaction processing
- concurrency control
- events using Java Message Service
- naming and directory services (JNDI)
- security ( JCE and JAAS )
- deployment of software components in an application server
- remote procedure calls using RMI-IIOP or CORBA
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.
Contents |
Enterprise JavaBean basics
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 Template:Javadoc:EE package. (The Template:Javadoc:EE package is a service provider interface used only by EJB container implementations.)
Each EJB must provide a Java implementation class and two Java interfaces. The EJB container will create instances of the Java implementation class to provide the EJB implementation. The Java interfaces are used by client code of the EJB.
The two interfaces, referred to as the Home and the Component interface, specify the signatures of the EJB's remote methods. The methods are split into two groups:
Note: Additionally Home or the Component Interfaces can be Local or Remote
- methods that are 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 are declared by the Home interface.
- methods that are tied to a specific instance. These are placed in the Remote 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.
The proxies use RMI-IIOP to communicate with the EJB server.
The server will invoke a corresponding method on an instance of the Java implementation class to handle the remote method invocation.
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 extends either the Template:Javadoc:EE or Template:Javadoc:EE interface. 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.
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 extends the interface Template:Javadoc:EE.
The remote interface defines the remote object used by the client code to call methods on the EJB. A remote interface extends the interface Template:Javadoc:EE. Unlike a local interface, each method in a remote interface declares that it "throws
Template:Javadoc:EE".
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.
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.
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.
EJB types
An EJB container can hold four major categories of beans:
- 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.
Stateful session beans are distributed objects having state. The state could be persisted, but access to the bean 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 Beans are distributed objects that respond to JMS messages. These beans were added in the EJB 2.0 specification to allow event-driven beans.
Remote communication
The EJB specification requires that EJB containers support accessing the EJBs using RMI-IIOP. EJBs may be accessed from any CORBA application.
Transactions
EJB containers must support both container managed transactions and bean managed transactions. Container-managed transactions use a declarative syntax for specifying transactions in the deployment descriptor.
Events
JMS is used to send messages from the beans to client objects, to let clients receive asynchronous messages from these beans.
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.
Security
The EJB container is responsible for ensuring the client code has sufficient access rights to an EJB.
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.
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.
External links
- Sun's EJB Product main page
- Template:Javadoc:EE
- EJB 2.1 API Javadocs
- The EJB 3.0 Specification
- Sun's EJB Tutorial
- EJB (3.0) Glossary
- JSR 220 (EJB 3.0)
- JSR 153 (EJB 2.1)
- JSR 19 (EJB 2.0)de:Enterprise Java Beans
es:Enterprise JavaBeans fr:Enterprise JavaBeans it:Enterprise JavaBeans nl:Enterprise JavaBeans ja:Enterprise JavaBeans pl:Enterprise JavaBeans pt:EJB zh:EJB