users@glassfish.java.net

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

From: Alexis Moussine-Pouchkine <alexis.mp_at_sun.com>
Date: Tue, 09 Mar 2010 11:15:32 +0100

You should not create the component instance yourself with a "new" but rather let the container do this (use an @EJB injection in your case).
Here's a sample (albeit with a managed bean, not an EJB): http://kenai.com/projects/beginningee6/sources/src/show/tutorial/trunk/Completed/Demo01-ManagedBean
(you can use NetBeans 6.8 to directly check out that code from kenai)
-Alexis

On 9 mars 2010, at 11:10, glassfish_at_javadesktop.org wrote:

> [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
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>