EJB3: Session Beans Introduction :

What Is an Enterprise Bean :

A session bean is the enterprise bean that directly interact with the user and contains the business logic of the enterprise application.A Session Bean can be invoked by a client over local, remote, or web service client views.


Session beans contains the logic such as creating users, add items for an auction, bidding for an item, etc. Session beans are the only EJB component that are invoked directly by the client, the client could be a web application component (servlet, JSP, JSF, etc), a command line application or a Swing GUI application.


A session bean tends to be responsible for a group of related functionality. For example, an application for a Bank might have a session bean whose methods contain logic for handling Customer records. Another session bean might contain logic that maintains the lists of Branches and ATM available at that Bank.


What Is an Enterprise Bean :

A session bean is the enterprise bean that directly interact with the user and contains the business logic of the enterprise application.A Session Bean can be invoked by a client over local, remote, or web service client views.


Session beans contains the logic such as creating users, add items for an auction, bidding for an item, etc. Session beans are the only EJB component that are invoked directly by the client, the client could be a web application component (servlet, JSP, JSF, etc), a command line application or a Swing GUI application.


A session bean tends to be responsible for a group of related functionality. For example, an application for a Bank might have a session bean whose methods contain logic for handling Customer records. Another session bean might contain logic that maintains the lists of Branches and ATM available at that Bank.




It is a platform for building portable, reusable, and scalable business applications which allows to focus on building business logic without having to spend time on building infrastructure code for services such as transactions, security, automated persistence etc.

Types of Session bean :

Session beans are of two types, as given below:

  • Stateless: This type of bean does not maintain any conversational state of a client application.Stateles Session beans tasks must be completed in a single method call.

  • Stateful: This type of bean maintains conversational state, and a particular instance of the bean is associated with a specific client request.

When to use Session Beans :

Session beans contains the logic such as creating users, add items for an auction, bidding for an item, etc.Typical implementation of Session Bean is mentioned below.

  • A session bean in a Bank application that creates a new Customer records and assigns the Customer to a particular Branch of bank.

  • A session bean that manages the items of a shopping cart in an e-commerce application.

  • A session bean in a CARGO application that will generate Unique Air Way Bill(AWB) number.

EJB 3.0 Session Bean Rules :

Session beans follow a number of rules as mentioned bellow.

  1. All beans must have one business interface (local, remote, web-services, etc)
  2. The session bean class must be concrete, if cannot be either final or abstract.
  3. You must have to write no-argument constructor in your bean class, because the container uses it to construct the bean instance.
  4. A session bean can sub-class another session bean or any other POJO.
  5. The business methods and lifecycle callback methods may be defined either in the bean class or in a superclass
  6. Business method names must not start with "ejb" and must be defined as public but not final or static.
  7. If exposing a method in a remote business interface make the return and arguments types implement the Serializable interface

Session beans can be used in traditional 2-tier or 3-tier architectures with professional/ rich client applications, or in 3-tier web-based applications.



Architecture for a Web Application using EJB :

The architecture shown below, is a typical front-end web application running in the browser of a desktop or laptop machine. These days, other types of client devices, such as PDAs, cell phones, and telnet devices, are also being used to run these applications.

The web application running in a browser or mobile device renders the user interface (data entry screens, Submit buttons, etc.) using web technologies such as JavaServer Pages (JSP), JavaServer Faces (JSF), or Java Servlets.

Typical user actions, such as entering search criteria or adding certain items to the web application shopping cart, will invoke/call session beans running in an EJB container via one of web technologies like jsp,struts,jsf etc. Once the session bean gets invoked, it processes the request and sends a response back to the web application, which formats the response as required, and then sends the response on to the requesting client device (browser, PDA, telnet, etc.).

EJB3 Web Architecture

Stateless Session Bean :

Stateless session beans are the most common bean type used in an application. A stateless session bean doesn't maintain a clients conversational state. This means that session beans tasks must be completed in a single method call.

The Bean Class :

A stateless session bean class is any standard Java class that has a class-level annotation of @Stateless.The @Stateless annotation indicates that this bean is a stateless session bean. Just add this notation and you'll turn your POJO into an EJB.

To understand the use of stateless session beans, we are going to implement a real time scenario in which we will create a session bean named SearchItem that will provide various search facilities to client applications regarding available items. The workflow is as follows:

  1. Users of the application will type in or choose one or more search criteria, which will be submitted to the SearchItem session bean.
  2. The SearchItem bean will access databases to retrieve the requested information.
  3. The bean returns results to the client applications that satisfied the search criteria.

The code for SearchItem Bean Class is given below.

SearchItem.java

package com.jwt.ejb.bean;
 
import javax.ejb.Stateless;
 
@Stateless
public class SearchItem implements SearchItemRemote,SearchItemLocal {
   public SearchItem() {
  
   }
}

Business Interface :

An interface through which a client invokes the bean is called a business interface.This interface has a list of business method definitions that will be available for the client application.Every session bean must have a business interface that can be implemented by the bean class.

There are three types of business interfaces available in EJB 3 : Local, Remote and WebService.

  • The @Remote annotation can be used to denote the remote business interface.

  • The @Local annotation can be used to denote the local business interface.

If no annotation is specified in the interface, then it will treated as local interface.

Which interface I need to use in my Application ?


When to use Remote interface ?

If your architecture has a requirement whereby the client application (web application or standalone application) has to run on a different Java Virtual Machine (JVM) from the one where your session beans are deployed, then you need to use the remote interface. The separate JVMs can be on the same machine or on separate machines.

A client using Remote interface of session beans is shown below.

EJB3 Architecture

When to use Local Interface ?

If your application architecture is going to use the same JVM for both the client application and the session beans, then use the local interface.

A client using Local interface of session beans is shown below.

EJB3 Architecture

Depend upon scenario we can use both Remote and Local interface for Session Bean.

Note: We will discuss Statefull Session bean in separate article because it contains lot of concepts which is important to discuss




comments powered by Disqus