users@glassfish.java.net

Help step-by-step for the use of Interceptors with a full code implem.

From: <glassfish_at_javadesktop.org>
Date: Tue, 09 Mar 2010 02:10:49 PST

[b]Someone could help me step-by-step for the use of Interceptors with a full code implementation?[/b]

I would like use the interceptors but i have several problem because i don't understand well how use it.

For example, after reading
[url=http://www.adam-bien.com/roller/abien/entry/interceptors_ejb_3_for_absolute]Interceptors (EJB 3) For Absolute Beginners - Or Pragmatic AOP in 2 Minutes (without XML :-))[/url]
I tried this example:

[code]
import javax.ejb.EJB;

public class AppClient {

    @EJB
    private static MyBean myB;

    public static void main(String args[]) {
        MyBean myB = new MyBean();
        try {
            myB.hello();
            System.out.println(myB.hello()); // Display the string.
        } catch (Exception ex) {
            System.out.println("Got some exception");
        }
    }
}
[/code]

[code]
import javax.interceptor.*;

public class MyInterceptor {
  @AroundInvoke
  protected Object log(InvocationContext cxt)
    throws Exception
  {
    System.out.println("INTERCEPTOR");
    System.out.println("Before: " + cxt.getMethod());

    Object value = cxt.proceed();

    System.out.println("After: " + cxt.getMethod());

    return value;
  }
}
[/code]

[code]
import javax.ejb.Stateless;
import javax.interceptor.*;

@Stateless
public class MyBean {
  @Interceptors(MyInterceptor.class)
  public String hello()
  {
    return "hello, world";
  }

}
[/code]

but everytime I run directly [u]Run file on AppClient[/u] as output i obtain:
[code]
init:
deps-jar:
compile-single:
run-main:
hello, world
BUILD SUCCESSFUL (total time: 0 seconds)
[/code]

I was thinking that on the output i should see even the message present on the interceptor, in this case something similar as :
INTERCEPTOR
Before:
etc..
hello, world

While now i see only the message:
hello, world

So i'm confused and i don't understand the problem and where i'm wrong.

I use Netbeans 6.8 and glassfish 3.
On the attachment there is the project for NetBeans.

Note:
I also see the example present on j2ee 5
interceptor-stateless-ear
[url=https://glassfish-samples.dev.java.net/files/documents/4742/50661/javaee5-samples-installer-1.0-b10.jar]samples[/url] (226 kb)
[Message sent by forum member 'alexjava2008' (alekirk2004_at_yahoo.it)]

http://forums.java.net/jive/thread.jspa?messageID=390828