EJB Intercepts

EJB3 offers the ability to intercept EJB business methods by using AroundInvoke interceptors.

From this you can access the method name, the parameters passed, and the EJB context.

This is useful for

1. Performing security checks
2. Performing audits
3. Changing the values of method parameters in your Java EE applications using EJB interceptors

Here is a simple EJB interceptor:

public class InterceptorHandler {

@Resource
 private EJBContext context;

@AroundInvoke
protected Object audit(InvocationContext ctx) throws Exception {
    Principal principal = context.getCallerPrincipal();
    if (userIsValid(principal)) {
  // Complete code for logging
      }else{
  // Complete code for logging and exception handling
    }
    return ctx.proceed();
  }
}
To use this interceptor put an annotation on the designated EJB, for example

@Interceptors(InterceptorHandler.class)
@Stateless
public class TestBean {
  // Complete code here
}

Comments

  1. I heve read your blog it's very interesting and informative. Keep sharing.
    erp providers in chennai | erp software solutions in chennai

    ReplyDelete
  2. Nice blog. Thank you for sharing. The information you shared is very effective for learners I have got some important suggestions from it. erp in chennai.

    ReplyDelete
  3. I found a lot of interesting information here. A really good post
    office 2011 mac download deutsch

    ReplyDelete

Post a Comment

Popular posts from this blog

Basic Send Message to MQ with Java and IBM MQ JMS

Basic Receive Message to MQ with Java and IBM MQ JMS

Creating a simple Alert / Success Message with ASP.NET/VB using Bootstrap