Lifecycle Callback methods of Stateless Session Bean

Below example demostractes Lifecycle Callback methods of Stateless Session Bean

The lifecycle callback methods can be defined in the bean class using annotations in EJB 3.0. It is called by EJB Container based on events.

The following annotations are used to define lifecycle callbacks in stateless session beans :
  • @PostConstruct :@PostConstruct annotation method is called. It is used to initilise or allocate any resources.
  • @PreDestroy : This method is called by the Container before destroying the Stateless Session Bean. It is used to release or close any resources used in the bean.
Some facts of @PostConstruct and @PreDestroy annotations :
  • The method must return void and does not take any arguments.
  • The method should not throw any Checked Exception.
  • The method can have any access specifiers public, private, package-private and protected.
  • The method can be final.

Below steps are to create Stateless session bean :

  • Create Enterprise Application Project StatelessCallbackMethods.ear.
  • Create Stateless Session Bean with Local interface in EJB project.
  • Create index.jsp in Web project.
  • Create Servlet in Web project.
  • Run the Application.

Step 1: Create Enterprise Application Project StatelessCallbackMethods.ear with EJB (StatelessCallbackMethodsEJB)and Web (StatelessCallbackMethodsWeb) Module :

[ Click here if you are not familiear how to create Enterprise Application Project ]

Stateless Callback Methods
Specify the Enterprise Application Project name StatelessCallbackMethods and click Next.

Below screen is used to create EJB and Web module in Enterprise Application Project. Click New Module
Stateless Callback Methods

Step 2: Create Stateless Session Bean class StatelessCallbackSession and local interface StatelessCallbackSessionLocal :
Stateless Callback Methods
This step will create two java file. One will be local interface and other will be bean class

Edit both class java file as below :
package techmyguru.statlesslifecyclemethods;
import javax.ejb.Local;
public interface StatelessCallbackSessionLocal
  public String welcome(String msg);
package techmyguru.statlesslifecyclemethods;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateless;

@Stateless(mappedName = "statelessjndi1")
public class StatelessCallbackSession implements StatelessCallbackSessionLocal 
public StatelessCallbackSession() 
   System.out.println("It is constructor in Stateless Session Bean");

public void initialize()
   System.out.println("It is PostConstruct in Stateless Session Bean");

public void release()
   System.out.println("It is PreDestroy in Stateless Session Bean");

public String welcome(String msg
   return "Welcome to Session Bean" + msg;

Step 3: Add EJB Project to Web project
Right Click on Web Project - > Properties - > Java Build Path -> Click Projects -> Add -> Select StatelessCallbackMethodsEJB.

Step 4: Create index.jsp in Web project
  <h2>Stateless Session Bean Callback Methods Demo</h2>
  <form action="Servlet1">
    Enter message <input type="text" name="msg">
    <input type="submit" value="Call EJB">

Step 5: Create Servlet in Web project
package techmyguru.web;
// all import statements

import techmyguru.statlesslifecyclemethods.StatelessCallbackSessionLocal;

public class Servlet1 extends HttpServlet 
private static final long serialVersionUID = 1L;

StatelessCallbackSessionLocal local;

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException 
  PrintWriter pw=response.getWriter();
  catch (Exception e
  pw.println("<br><a href='index.jsp'>Try Again</a>");

Step 6: Verify the directory structure as below -
Stateless Callback Methods

Step 7: Run the Application
Right click on Web Project -> Run As -> Run on Server
Stateless Callback Methods
Input any message and click Call EJB

You will get below output -
Stateless Callback Methods
After running the application, undeploy project from server then container will call @PreDestroy method.

Previous Download Source Code Next

Share your valuable comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code :