EJB Tutorial

EJBs are Enterprise Java Beans .It is an important API in J2EE . They are server side components.A developer can write Enterprise Java Bean components and can deploy in any application server supporting J2EE. The client applications can access the EJBs remotely.This chapter gives a basic EJB Tutorial

Enterprise Java Beans – An overview

As we mentioned , EJBs are server side components. EJBs are managed by the EJB container of application server. So developer can concentrate on implementing business logic. Developer need not worry about the management of an EJB in server.

j2eeserverThe web container manages all the servlets and jsp pages.The EJB Container manages all the EJBs.

Enterprise Java Beans – Types

There are 3 types of Enterprise Beans. They are:

1)Session Beans

2)Message Driven Beans

3)Entity Beans

Session Beans – An overview

Assume we have a client application .The client can use the session bean methods to access the applications deployed in the server.So the client need not worry about the complexities in server side.There are two types of session beans. They are:

a)Stateless Session Beans

It is a type of session bean which does not retain a conversational state for the client.Suppose the bean has two methods. Suppose the bean has a state when the client calls the first method. But the bean cannot retain the state beyond that method. In other words , when the method ends , the state will be lost.Also for a particular method in bean , the state of all bean instances will be same.This is a good advantage , because the EJB container can assign any bean instance to any client.It can implement webservice end point.

b)Stateful Session Beans

This type of session beans retains a conversational state with a client.It retains its state throughout the entire session with a particular client. When the client disconnects the bean , then only the state of bean is removed.

d)Singleton Session Bean

It is similar to a Stateless Session Bean . But this bean has only one instance for the entire application.It can also implement webservice end points

Message Driven Beans – An Overview

We already discussed the various concepts of JMS in earlier discussions.There we discussed asynchronous as well as synchronous message consumptions with examples.A Message Driven Bean is simply an Asynchronous Message Consumer.It receives the messages(either JMS or other messages) and distributes those messages to various components.The Message Driven beans does not retain  any  conversational state .A Message Driven Bean can receive messages from multiple clients.The synchronous message consumption is not a fare option if  we need to receive messages in our application. Asynchronous mode does not need any waiting. Message Driven Beans are useful there .

Entity Beans-An Overview

In application server, data is persisting in database. An Entity Bean  represents an object in storage.Each entity bean will be having a table in the storage unit . And each instance is corresponding to a row in the table.

There are two types of persistence:

a)Bean Managed persistence

In a bean managed persistence , the code for entity bean contains database  calls also.

b)Container Manged Persistence

In a container managed persistence , the database calls will be managed by the EJB container.

EJB 3.x

In our coming chapters we will be discussing EJB examples. There we will be using EJB 3.0 . It does not need an y deployment descriptor.Instead ,it is using annotations.

Life Cycle of Enterprise Java Beans

The various stages through which an EJB is passing through its entire life time is called as EJB life cycle.It is different for each type of bean.

Life Cycle of Stateless Session Bean

There are two states for  a stateless session bean.They are:

1)Non existent state

2)Ready for invocation state.

The EJB container creates a pool of stateless beans .The container itself does the necessary dependency injection and the method annotated with  @PostConstruct will be called , if one such method is there.Now the bean is ready for usage.At the end of life cycle the conatiner calls the method which is annotated with @PreDestroy . Now the bean is ready for garbage collection.

Life cycle of Stateful Session bean

1)The client initiates the bean life cycle by invoking it

2)Dependency injection by EJB container

3)If there is a method annotated with @PostConstruct , the method will be invoked . Then the init() or ejbCreate()method will be invoked

4)Now the bean is in ready stage.

5)While the bean in ready stage , the container can deactivate or passivate the bean. Passivation means the bean is moving to secondary storage.Before passivation , if there is a method annotated with @PrePassivate will be invoked.

6)If a client invokes a business method while it is in passive state , the bean will be reactivated .And the method  annotated with @PostActivate will be called ,if any.The bean will  stay in active stage then .

7)If the client calls a method annotated with @Remove  , then the EJB container calls the method annotated with @PreDestroy.The bean can be garbage collected then.

Life Cycle of Message Driven Bean

Like a Stateless Session Bean , it has only two stages in its life cycle.Life cycle stages are similar to Stateless Session Beans.

1)Non existent

2)Ready for invocation state.

Life Cycle of Entity Bean

There are 3 states in entity bean life cycle. They are:

1)Does not exist

2)Pooled

3)Ready state.

In the coming chapters ,we will be discussing the various types of beans in detail with suitable code samples.

See Related Topics

JNDI

JNDI Tutorial

JMS

JMS Tutorial

EJB

EJB Stateless Session Bean – Example

EJB Stateful Session Bean Example

Stateful Session Bean Vs Stateless Session Bean

EJB Message Driven Bean Example

Refernces

1)EJB Tutorial by Oracle

2)EJB Tutorial from IBM