Session Bean Interceptor

10 Mar

package com.xavier.ejb3.sessionbean;

public interface HelloChinaLocal {

public String sayChina();


package com.xavier.ejb3.sessionbean;

import javax.ejb.Local;

import javax.ejb.Stateless;

import javax.interceptor.Interceptors;

import com.xavier.ejb3.sessioninterceptor.HelloChinaInterceptor;





public class HelloChinaLocalBean implements HelloChinaLocal {


    public String sayChina() {

return “Hello, China!”;




package com.xavier.ejb3.sessioninterceptor;

import javax.interceptor.AroundInvoke;

import javax.interceptor.InvocationContext;


public class HelloChinaInterceptor {


public Object log(InvocationContext ctx) throws Exception{

System.out.println(“*** Interceptor begins***”);



System.out.println(“********sayChina Method is invoked!********”);


return ctx.proceed();

}catch(Exception e){

throw e;





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


<%@ page


<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>



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

<title>Hello China Local Bean Interceptor</title>




try {

final Properties jndiProperties = new Properties();

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



Context ctx = new InitialContext(jndiProperties);

HelloChinaLocal hcl = (HelloChinaLocal) ctx



} catch (Exception e) {

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





1) Multiple interceptors should written as thus: @Interceptors({A.class, B.class})

2) @AroundInvoke annotation defines interceptor methods. Business methods will be executed after all interceptor methods execute. Method formats should be:

public Object XXX(javax.interceptor.InvocationContext ctx) throws Exception


InvocationContext interface includes the following methods:

public interface InvocationContext{

    public abstract Object getTarget();

    public abstract Method getMethod();

    public abstract Object[] getParameters();

    public abstract void setParameters(Object aobj[]);

    public abstract Map getContextData();

    public abstract Object getTimer();

    public abstract Object proceed() throws Exception;


getContextData() Returns the context data associated with this invocation or lifecycle callback.
getMethod() Returns the method of the target class for which the interceptor was invoked.
getParameters() Returns the parameter values that will be passed to the method of the target class.
getTarget() Returns the target instance.
getTimer() Returns the timer object associated with a timeout method invocation on the target class, or a null value for method and lifecycle callback interceptor methods.
proceed() Proceed to the next interceptor in the interceptor chain.
setParameters(java.lang.Object[] params) Sets the parameter values that will be passed to the method of the target class.


Leave a comment

Posted by on 03/10/2012 in EJB


Leave a Reply

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

You are commenting using your 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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: