An EJB is a server component model for Java, based on multi-tier (3-tier) distributed object-oriented programming. This is a framework for creating server components with a high degree of abstraction simply by coding the business logic processing. Low-level interfaces, such as management of the lifecycle of components required for server applications, transaction management, and so on, are hidden.
EJB implements the various management processes, such as the lifecycle management required for server applications, as server component receptacles known as containers. Containers shoulder the burden of complicated processes. Under EJB, server components that operate in containers are called enterprise beans.
Installing an enterprise bean in a container and making it executable is called deployment. Under EJB, the installation method known as deployment enables the creation of server components that have portability and that are not dependent on a specific container.
The following functions are regulated by EJB:
Enterprise bean instance lifecycle management
Transaction management
Security management
Session management
Resource management
Enterprise beans can be broadly divided into the following types:
Session beans
An enterprise bean that performs interactive processing with clients. It codes mainly processes (business logic) required for business processing.
Message-driven beans
An enterprise bean for performing asynchronous processing.
These classifications and their uses are described below.
Note
Specifications up to EJB 2.1 also have entity beans for data handling in database systems or similar. However, from EJB 3.0, the Java Persistence API is used instead of entity beans, so an entity bean description is not included in this manual.
Session beans deploy application business logic to servers and provide services to multiple clients via a network.
Session beans execute business logic by invoking other enterprise beans, controlling the flow of transactions and processes, and by implementing independent processes.
There are two types of session beans:
Stateful
A stateful session bean has a one-to-one relationship with a client. It can maintain the transaction state and the variable values defined in an enterprise bean across multiple methods invoked by the client. Accordingly, it is used if multiple procedures (methods) are required to provide a certain function.
Stateless
A stateless session bean cannot maintain the transaction state and the variable values defined in an enterprise bean across multiple methods. Accordingly, it is used to provide functions that are completed by one method. In addition, since multiple clients can share the same session bean instance, the server load can be reduced.
For example, a stateless session bean should be used if a session bean implements processing of a simple arithmetic operation. Since there is no information that needs to be held, use of the stateless session bean reduces the load on the server. However, if a session bean implements the shopping cart of an online shopping site, a stateful session bean should be used. This is because the goods that the user has put into the shopping cart must be maintained across multiple pages.
Message-driven beans provide the foundation for performing asynchronous processing in relation to a message sent from a client.
A Message-driven bean can invoke a session bean or similar to execute that function asynchronously.
Message-driven beans receive and process JMS messages and resource adapter messages. For JMS messages, message processing methods can be classified as the following two types:
Point-To-Point model
A specific recipient performs processing in relation to a single message sent from a sender. That is, this model is used if the message is allocated to a single recipient.
With this model, the sent messages are held in a queue on the JMS server. The queued messages are allocated to a specific recipient and processed.
Publish/Subscribe model
Multiple recipients perform processing in relation to a single message sent from a sender. That is, this model is used if the same message needs to be allocated to multiple recipients.
With this model, the sent messages are queued in a topic on the JMS server. The queued messages are allocated to all recipients and processed by each recipient.
The EJB specification included in Java EE is EJB 3.0. In EJB 3.0, the specifications have been improved to enable easier development in response to complaints concerning the difficulty of EJB up to J2EE 1.4.
Simplification of Coding
One of the major features of EJB 3.0 is the simplification of EJB coding.
Under conventional EJB specifications, the various EJB definitions needed to be described in the deployment descriptor known as ejb-jar.xml. Under EJB 3.0, Java annotations can now be used to describe the various EJB definitions in the enterprise bean class. Thus, in most cases, EJB deployment descriptors do not need to be created.
Another improvement is that the various EJB definitions need only be defined when required. This reduces the number of definitions that the developer must describe. For example, when a single Java interface is implemented in a session bean class, it is automatically handled as an EJB local interface. In this case, the developer does not need to code information that indicates which interface is the Local interface.
In addition, the minimum number of files required for implementing an enterprise bean has now been reduced. The following table shows the elements required for developing an enterprise bean under conventional EJB and EJB 3.0 respectively.
Enterprise Bean type | Earlier EJB (up to EJB 2.1) | EJB 3.0 |
---|---|---|
Session bean |
|
|
Message-driven bean |
|
|
The Introduction of Java Persistence API (JPA)
Another major feature of EJB 3.0 is the introduction of JPA. JPA is a persistence API used instead of entity beans. As with other enterprise beans, Java annotation can be used for JPA coding. In addition, under JPA, a query language to which EJB QL is enhanced is used for access to databases.
A major feature of JPA is that it is an ordinary persistence API. JPA is not only used from EJB but can also be used from Web applications and Java applications.
For details on JPA and development using JPA, refer to "4.1.2 Developing Applications that use JPA".
The main EJB 3.0 annotations are introduced below with reference to simple examples.
javax.ejb.Stateless/javax.ejb.Stateful Annotation
This annotation defines a class as a session bean. The following properties can be specified.
Property name | Explanation |
---|---|
name | Defines the EJB name. If this property is not defined, the session bean class name is used as the EJB name. |
mappedName | Use this property if the application server product requires a unique bean mapping name. |
description | Defines the explanation of this session bean. |
Example of Session Bean Creation using Stateless Annotation
package sample; import javax.ejb.Stateless; @Stateless public class CalcBean implements Calc { public int add(int param1,int param2) { return param1 + param2; } } |
javax.ejb.MessageDriven Annotation
This annotation defines a class as a message-driven bean. The following properties can be specified.
Property name | Explanation |
---|---|
name | Defines the EJB name. If this property is not defined, the message-driven bean class name is used as the EJB name. |
messageListenerInterface | Defines the message listener interface of the bean. This property needs to be defined only if the bean class does not implement a message listener interface or if multiple interfaces are implemented. |
activationConfig | Defines the properties for the message-driven bean. |
mappedName | Use this property if the application server product requires a unique bean mapping name. |
description | Defines the explanation of this message-driven bean. |
Example of Message Driven Bean Creation using Message Driven Annotation
package sample; import javax.ejb.ActivationConfigProperty; import javax.ejb.MessageDriven; import javax.jms.MessageListener; import javax.jms.Message; @MessageDriven(mappedName = "jms/CalcBean" activationConfig = { @ActivationConfigProperty( propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"), @ActivationConfigProperty( propertyName = "destinationType", propertyValue = "javax.jms.Queue") }) public class CalcBean implements MessageListener { public void onMessage(Message msg) { ... } } |
javax.ejb.EJB Annotation
Annotation for performing Dependency Injection when invoking an enterprise bean. The following properties can be specified.
Property name | Explanation |
---|---|
name | JNDI name of bean to be referenced. |
beanInterface | Business interface of bean to be referenced. |
beanName | Bean name of bean to be referenced. |
mappedName | Use this property if the application server product requires a unique bean mapping name. |
description | Defines the explanation of this invocation. |
Example of EJB Client Creation using EJB Annotation
@EJB private static Calc calc; public int callCalc(int param1,int param2) { return calc.add(param1,param2); } |