The Artima Developer Community
Sponsored Link

Java Buzz Forum
Annotation-driven interceptors with EJB3

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Norman Richards

Posts: 396
Nickname: orb
Registered: Jun, 2003

Norman Richards is co-author of XDoclet in Action
Annotation-driven interceptors with EJB3 Posted: Aug 19, 2005 9:08 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Norman Richards.
Original Post: Annotation-driven interceptors with EJB3
Feed Title: Orb [norman richards]
Feed URL: http://members.capmac.org/~orb/blog.cgi/tech/java?flav=rss
Feed Description: Monkey number 312,978,199
Latest Java Buzz Posts
Latest Java Buzz Posts by Norman Richards
Latest Posts From Orb [norman richards]

Advertisement

EJB3 provides a standard interceptor model. Most of the examples I've seen use simple reflection on the name of the method to determine whether or not to apply an interceptor.

Having seen the power of annotation-drive AOP in JBoss AOP, I knew there was a better way. Annotations are an absolutely brilliant way of specifying a pointcut. It's much better than the wacky pointcut expressions you see, and it certainly beats hardcoding method names in your interceptor.

I wasn't sure how much work it would be, so I decided to put together a simple example to try it out. It turns out to be even easier than I thought. The most difficult part is defining the annotation. Here's one I put together as a test:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface TimedMethod
{
}

Note RetentionPolicy.RUNTIME, which makes sure the annotation stays on the class and can be queried at runtime. ElementType.METHOD means the annotation can be applied to any method, like this:

@TimedMethod
public List<Product> getRecentHistory(Customer customer, int howmany) {
// ...
}

The idea is that I can add or remove the @TimedMethod annotation to change whether or not the method is timed. The interceptor that uses the annotation is easy to write:

@AroundInvoke
public Object timer(InvocationContext ctx)
throws Exception
{
if (ctx.getMethod().getAnnotation(TimedMethod.class) == null) {
return ctx.proceed();
}

Object result = null;

long t1 = System.currentTimeMillis();
result = ctx.proceed();
long t2 = System.currentTimeMillis();

System.out.println("Time[" + ctx.getMethod().getName() + "]:" + (t2-t1) + " ms");
return result;
}

One line of Java code is all that is needed to test for the presence of the annotation. It all works. In this example, I've assumed the interceptor method will be on the bean itself, but more than likely it would be in it's own class and linked with an @Interceptor(TimedMethodInterceptor.class) annotation on the class.

You might be wondering about the utility of this. If I have to recompile and redeploy to bring in the annotation, what is the point? The redeploy isn't actually that bad. Building EJB3 projects is very fast if you don't complicate your build, and JBoss is very fast at redeploying apps. You don't have to lose your HTTP session on redeploy, so for all practical purposes you should be able to update the annotations between clicks in a web app without noticing anything. I used this while testing out an application recently. "Oh, that's a bit slow." I added the annotation, built and deployed. 10 seconds later I reloaded the web page and there was my timing information. It's not instantaneous, but it's pretty good response time for a change.

Getting back to the original topic of annotation-drive interceptors, give it a shot if you've written off EJB3 interceptors as uninteresting. I think you'll be pleasantly surprised.

Read: Annotation-driven interceptors with EJB3

Topic: Print-Friendly Version Not So Friendly Previous Topic   Next Topic Topic: Back

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use