RSS

Session Bean Life Cycle Annotations

09 Mar

LifeCycleLocal.java

package com.xavier.ejb3.sessionbean;

public interface LifeCycleLocal {

public String operate();

public void stopbean();

}

 

LifeCycleLocalBean.java

package com.xavier.ejb3.sessionbean;

import javax.annotation.PostConstruct;

import javax.annotation.PreDestroy;

import javax.ejb.Init;

import javax.ejb.Local;

import javax.ejb.Remove;

import javax.ejb.Stateful;

 

@Stateful

@Local(LifeCycleLocal.class)

public class LifeCycleLocalBean implements LifeCycleLocal {

 

@Init

public void initialize(){

System.out.println(“The method which is marked with @Init Annotation is invoked.”);

}

@PostConstruct

public void construct(){

System.out.println(“The method which is marked with @PostConstruct Annotation is invoked.”);

}

@PreDestroy

public void exit(){

System.out.println(“The method which is marked with @PreDestroy Annotation is invoked.”);

}

@Remove    //This method which is marked with @Remove Annotation will inform Bean Container to remove the session bean. Method body can be empty.

    public void stopbean() {

System.out.println(“The method which is marked with @Remove Annotation is invoked.”);

    }

 

    public String operate() {

        System.out.println(“Business method is invoked.”);

return “Invoke Business Method”;

    }

 

}
 

LifeCycleLocalBean.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding=“UTF-8”%>

<%@ page

import=“javax.naming.*,com.xavier.ejb3.sessionbean.*,com.xavier.ejb3.entitybean.*,java.util.*”%>

<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” http://www.w3.org/TR/html4/loose.dtd>

<html>

<head>

<meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>

<title>Life Cycle Local Bean</title>

</head>

<body>

<%

try {

LifeCycleLocal lcl = (LifeCycleLocal) session

.getAttribute(“lifecycle”);

 

if (lcl == null) {

final Properties jndiProperties = new Properties();

jndiProperties.put(Context.PROVIDER_URL, “localhost:1099”);

jndiProperties.put(Context.URL_PKG_PREFIXES,

“org.jboss.ejb.client.naming”);

Context ctx = new InitialContext(jndiProperties);

lcl = (LifeCycleLocal) ctx

.lookup(“ejb:ejb3/ejb3//LifeCycleLocalBean!com.xavier.ejb3.sessionbean.LifeCycleLocal?stateful”);

session.setAttribute(“lifecycle”, lcl);

}

 

System.out.println(lcl.operate());

lcl.stopbean();

 

} catch (Exception e) {

out.println(“<br>Invoke failed.”);

}

%>

</body>

</html>

Note: Annotations used in Stateless/Stateful Session Bean includes:

@PostConstruct (Stateless + Stateful)

@PreDestroy  (Stateless + Stateful)

@PrePassivate (Stateful)

@PostActivate (Stateful)

@Init (Stateful, execute before @PostConstruct)

@Remove (Stateful, execute before @PreDestroy)

Advertisements
 
Leave a comment

Posted by on 03/09/2012 in EJB

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: