RSS

Session Bean Interceptor

10 Mar

HelloChinaLocal.java

package com.xavier.ejb3.sessionbean;

public interface HelloChinaLocal {

public String sayChina();

}

 

HelloChinaLocalBean.java

package com.xavier.ejb3.sessionbean;

import javax.ejb.Local;

import javax.ejb.Stateless;

import javax.interceptor.Interceptors;

import com.xavier.ejb3.sessioninterceptor.HelloChinaInterceptor;

 

@Stateless

@Local(HelloChinaLocal.class)

@Interceptors(HelloChinaInterceptor.class)

public class HelloChinaLocalBean implements HelloChinaLocal {

 

    public String sayChina() {

return “Hello, China!”;

    }

 

}

HelloChinaInterceptor.java

package com.xavier.ejb3.sessioninterceptor;

import javax.interceptor.AroundInvoke;

import javax.interceptor.InvocationContext;

 

public class HelloChinaInterceptor {

@AroundInvoke

public Object log(InvocationContext ctx) throws Exception{

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

try{

if(ctx.getMethod().getName().equals(“sayChina”)){

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

}

return ctx.proceed();

}catch(Exception e){

throw e;

}

}

}

HelloChinaLocalBeanInterceptor.jsp.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>Hello China Local Bean Interceptor</title>

</head>

<body>

<%

try {

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);

HelloChinaLocal hcl = (HelloChinaLocal) ctx

.lookup(“ejb:ejb3/ejb3//HelloChinaLocalBean!com.xavier.ejb3.sessionbean.HelloChinaLocal”);

out.println(hcl.sayChina());

} catch (Exception e) {

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

}

%>

</body>

</html>

Note:
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.


 

Advertisements
 
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:

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: