Enterprise JavaBeans (EJB) technology is the server side component of the Java Enterprise Edition specification where the business logic of an application resides. Versions prior to 3.x were considered too cumbersome because they required developers to create multiple interfaces and XML files just to define a single bean. The 3.x version of the specification has improved the framework by using annotations and making XML configuration files optional in most cases. The introduction of the embedded enterprise bean container allows EJB components to be run outside of the application server without losing the benefits the application server provides. The EJB container provides services such as security, transactions, resource management, dependency injection, interceptors, concurrency and lookup so that the developer is free to focus on business logic.

There are four types of beans: Stateless session, Stateful session, Singleton and Message-Driven. The Entity bean is now a part of the Java Persistence API standard and will not be covered here. Below is a break down of each type of bean and an example.

Stateless session bean

The Stateless session bean (SLSB) is the most common bean used when programming EJBs. The bean holds state only during the invocation and is returned to the pool once the invocation is complete. A client may receive a different bean instance from the pool with each invocation as determined by the EJB container. The SLSB is ideal for light weight server calls.

import javax.ejb.Stateless; @Stateless public class MyStatelessBean { public String echo(String name) { return "Hello " + name; } }

The @Stateless annotation is all that is needed to mark this Java class as a SLSB. Additional annotations are provided to separate methods available to local (@Local) clients and remote (@Remote) clients. These annotations may be added at the class level specifying the interface class or at the interface level. Adding no additional annotations will allow the bean to be referenced locally, but the @Remote annotation is required if the bean is to be referenced remotely.

Stateful session bean

The Stateful session bean (SFSB) is different than the SLSB because it may hold state information while interacting with a client. The SFSB can have only one client and once the conversation ends or the container ends the session, the state is removed.

import javax.ejb.Stateful; @Stateful public class MyStatefulBean { public String echo(String name) { return "Hello " + name; } }

The @Stateful annotation is used to mark this Java class as a SFSB. As with the SLSB, additional annotations are provided to separate methods available to local (@Local) clients and remote (@Remote) clients. These annotations may be added at the class level specifying the interface class or at the interface level. Adding no additional annotations will allow the bean to be referenced locally, but the @Remote annotation is required if the bean is to be referenced remotely.

Singleton session bean

The Singleton session bean (SSB) is different than the SLSB and SFSB in that there is only ever one instance at a time. This type of bean may be useful for fetching properties that can be used by other beans.

import javax.ejb.Singleton; @Singleton public class MySingletonBean { public String echo(String name) { return "Hello " + name; } }

The @Singleton annotation is used to mark this Java class as a SSB. The @Startup annotation may be used to create the instance during application startup.

Message-Driven bean

The Message-Driven bean (MDB) is different that the three types of session bean in that it is used for asynchronous communication. Once a client (usually Java Message Service) sends a request, it must check back for a response.

import javax.ejb.MessageDriven; @MessageDriven public class MyMessageDrivenBean { public String echo(String name) { return "Hello " + name; } }

The @MessageDriven annotation is used to mark a class as a MDB.

Dependency Injection

Dependency Injection has become a widely used pattern since the Spring framework burst onto the scene and the EJB specification has made it very easy to use. Adding a simple @EJB annotation tells the container to inject a bean into the requesting object. The example below shows how the Stateful bean example could be modified to use the Stateless bean to perform the simple echo method call.

import javax.ejb.Stateful; @Stateful public class MyStatefulBean { @EJB private MyStatelessBean echoBean; public String echo(String name) { return echoBean.echo(name); } }

Additional Resources

Building Java Enterprise Edition artifacts with Maven

Enterprise JavaBeans 3.1