Top
Interstage Studio User's Guide
FUJITSU Software

3.1.1 What is an EJB?

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 Types

Enterprise beans can be broadly divided into the following types:

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

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:

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

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:

3.1.1.1 Modifications since J2EE1.4

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.

Table 3.1 Mandatory Elements for Implementing Enterprise Beans

Enterprise Bean type

Earlier EJB (up to EJB 2.1)

EJB 3.0

Session bean

  • Enterprise bean class

  • Home interface

  • Component interface

  • deployment descriptor

  • Enterprise bean class

  • Business interface

Message-driven bean

  • Enterprise bean class

  • deployment descriptor

  • Enterprise bean class

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".

EJB 3.0 Main Annotations

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);
}