Stateful Session Beans Example, EJB Tutorial

Stateful Session Beans Example, EJB Tutorial

Stateful Session Beans Example, EJB Tutorial

Stateful Session Bean Example

     

In this part of Enterprise Session Beans, you will learn how to develop, deploy, and run a simple Java EE application named account using stateful session bean. The purpose of account is to performs two transaction operations (deposit and withdraw) for the customer.  
The account application consists of an enterprise bean, which performs the
transactions, and two types of clients: an application client and a web client.

here are following steps that you have to follow to develop a account JEE application.

  1. Create the enterprise bean: AccountBean

  2. Create the application client: AccountCustomer

  3. Deploy account onto the server.

  4. Run the application client.

I. Creating the enterprise bean:

The enterprise bean in our example is a statelful session bean called AccountBean.

The account session bean represents an account information in an online customer account. The bean?s customer can deposit to or withdraw the amount from his account. To manage account, you need the following code:

  • Remote business interface (Account)

  • Session bean class (AccountBean)

The Business Interface:

The Account business interface is a plain Java interface that defines all the business methods implemented in the bean class. If the bean class implements a single interface, that interface is assumed to the business interface. The business interface is a local interface unless it is annotated with the javax.ejb.Remote annotation.

The bean class may also implement more than one interface. If the bean class implements more than one interface, the business interfaces must be specified by decorating the bean class with @Local or @Remote

The source code for the Account business interface is given below.

package ejbExample.stateful;

import javax.ejb.Remote;
@Remote
public interface Account {

  public float deposit(float amount);
  public float withdraw(float amount);
 
 @Remove 
public void remove();
}

 Coding the Session Bean Class:

The session bean class for this example is called AccountBean. This class implements the two business methods (deposit and withdraw). The AccountBean class must meet these requirements:

  • The class is annotated @Stateful.

  • The class implements the business methods defined in the business interface.

The source code for the AccountBean class is given below.

package ejbExample.stateful;

import javax.ejb.Stateful;
import javax.ejb.Remote;  
import javax.ejb.Remove; 
import javax.ejb.*;

@Stateful(name="AccountBean")
@Remote(AccountRemote.class)  
public class AccountBean implements AccountRemote {
 float balance = 0;

  public float deposit(float amount){
  balance += amount;
  return balance;
  }
  public float withdraw(float amount){
  balance -= amount;
  return balance;
  }
@Remove  
public void remove() {
  balance = 0;
  }
}

The Remove Method

Business methods annotated with javax.ejb.Remove in the stateful session bean class can be invoked by enterprise bean client to remove the bean instance. The container will remove the enterprise bean after a @Remove method completes, either normally or abnormally. Thus, we can retain the bean's state invoked by the client until we call the annotated @Remove method. 

In AccountBean , the remove method is a @Remove  method shown as:

@Remove
public void remove() {
	balance = 0;
}

Stateful session beans also may:

  • Implement any optional life cycle callback methods, annotated @PostConstruct, @PreDestroy, @PostActivate, and @PrePassivate.

  • Implement optional business method annotated @Remote.

Life-Cycle Callback Methods:

Methods in the bean class may be declared as a life-cycle callback method by annotating the method with the following annotations:

  • javax.annotation.PostConstruct
  • javax.annotation.PreDestroy
  • javax.ejb.PostActivate
  • javax.ejb.PrePassivate

Life-cycle callback methods must return void and have no parameters. Lets  understands these callback annotated methods shown in the table given below:

 Method  Description
 @PostConstruct Invoked by the container on newly constructed bean instances before the first business method is invoked on the enterprise bean and after all dependency injection has completed.
 @PreDestroy Invoked, when the bean is about to be destoryed by EJB container before removing the enterprise bean instance and after any method annotated @Remove has completed.
 @PostActivate Invoked by the container after the container moves the bean from secondary storage to active status.
 @PrePassivate Invoked by the container before the container passivates the enterprise bean, i.e. the container temporarily removes the bean from the environment and saves it to secondary storage.

II. Coding the account Web Client

The application client source code is in the WebClient.jsp file That illustrates the basic tasks performed by the client of an enterprise bean: 0

  • Creating an enterprise bean instance

  • Invoking a business method

  
The full source code for the WebClient.jsp program is given below. 1


<%@page language="java" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="com.javajazzup.examples.ejb3.stateful.*, javax.naming.*"%>

 <%!
  public AccountRemote account = null;
  float bal=0;
  public void jspInit() {
  try {
 InitialContext ic = new InitialContext();
 account = (AccountRemote) ic.lookup("example/AccountBean/remote");
   System.out.println("Loaded Account Bean");

  } catch (Exception ex) {
  System.out.println("Error:"+
  ex.getMessage());
  }
  }
  public void jspDestroy() {
  account = null;
  }
%>
<%  try {
  String s1 = request.getParameter("amt");
  	String s2 = request.getParameter("group1");

  if ( s1 != null) {
  Float amt  = new Float(s1);
  
  if(s2.equals("dep"))
  bal=account.deposit(amt.floatValue());
  else if(s2.equals("with"))
  bal=account.withdraw(amt.floatValue());
  else  
 %>
 <p>Please select your choice</p>
  
  <%
  }
  else
  %>

  <br>Please enter the amount<br>
  <p>
  The Transaction is complete<br>
  <b>Your Current Balance is:</b> <%= bal%>
  <p>

  <%
  }// end of try
  catch (Exception e) {
  e.printStackTrace ();
 }
%>

The source code for the ?form.jsp? is given below.

<html>
<head>
<title>Bank Account</title>
</head>
<body>
<h1>
	<p align="center">
		<font size="6" color="#800000">Bank Transaction Request Form</h1>
<hr><br>
<table bgcolor="#FFFFCC" align="center"> 
	<form action="WebClient.jsp" method="POST">
		<tr><td></tr></td>
		<tr><td>Enter the amount in rupees:
		<input type="text" name="amt" size="10"></tr></td><br> 
		<tr><td><b>Select your choice:</b></tr></td>
		<tr><td><input type="radio" name="group1" value ="dep">Deposit</tr></td>
		<tr><td><input type="radio" name="group1" value ="with">Withdraw<br>
		</tr></td><tr><td>
		<input type="submit" value="Transmit">
		<input type="reset" value="Reset"></tr></td>
		<tr><td></tr></td>	
	 </form>
  </table>
</body>
</html> 

The source code for the ?index.jsp? is given below that will actual call the client-design form.

<%@page language="java" %>
<html>

<head>
<title>Ejb3 Stateful Tutorial</title>
</head>

<body bgcolor="#FFFFCC">

<p align="center"><font size="6" color="#800000"><b>Welcome to <br>
Ejb3-Jboss 4.2.0 Tutorial</b></font>
Click <a href="ejb3/form.jsp">Bank Transaction Example</a> to execute Bank Bean<br></p>
</body>
</html>

III. Deploy account onto the server

IV. Running the account Application Client  2