[webbeans-dev] @Any required on @Observer argument?

David Allen drallendc at gmail.com
Mon Jul 27 05:47:44 EDT 2009


On Sat, 2009-07-25 at 12:49 +0100, Pete Muir wrote: 
> On 25 Jul 2009, at 12:24, David Allen wrote:
> 
> > On Fri, 2009-07-24 at 19:05 -0400, Dan Allen wrote:
> >> Is it necessary to specify the @Any annotation on the @Observer
> >> argument (the event object) of an observer method if no other event
> >> binding type is specified?
> >>
> >> public void studentRegistered(@Observers @Any RegistrationEvent  
> >> event)
> >> { ... }
> >>                                                                  ^
> >>                                                                  is
> >> this annotation required?
> >
> > No, this annotation is not required.  It is always added automatically
> > to every observer method.
> 
> To be precise, it's not added to every observer method automatically,  
> it's added to Event that is fired.

Each occurrence of @Observes defines an observer method, which does
automatically get the @Any annotation every time (it is hard-coded in
the constructor).  Only the @Current annotation binding is optional and
depends on whether one was specified or not.

Apparently this does work fine since there already is a test using just
@Observes without the @Any or any other binding annotation.  Have a look
at the StockWatcher class under the event test package.

> 
> My tests show that this all works as expected (see https://svn.jboss.org/repos/webbeans/ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/implementation/event/SimpleEventTest.java 
>   )
> 
>     public static class Receiver
>     {
>        public void receive1(@Observes @Updated String s)
>        {
>           RECEIVE_1_OBSERVED = true;
>        }
> 
>        public void receive2(@Any @Observes String s)
>        {
>           RECEIVE_2_OBSERVED = true;
>        }
> 
>        public void receive3(@Observes String s)
>        {
>           RECEIVE_3_OBSERVED = true;
>        }
>     }
> 
>        @Updated
>        Event<String> event2;
> 
>        @Any
>        Event<String> event3;
> 
>        @Current
>        Event<String> event4;
> 
> Given the above observer, and events, you should expect to *only* see  
> receive3 called when event4 is called - it's the only time the  
> @Current binding is used to fire an Event.
> 
> Same goes for firing via the BeanManager - only  
> manager.fireEvent("Fired using Manager Interface."); or  
> manager.fireEvent("Fired using Manager Interface.", new  
> AnnotationLiteral<Current>() {}); should call receive3()
> 
> So, I wonder what event you are firing that causes the observer method  
> not to work?
> 
> 
> >  So this is a bug...file a WBRI for this.
> >
> >>
> >> Currently, if the @Any annotation is not present, the observer is not
> >> notified. This breaks event tests in a major way if it is not  
> >> supposed
> >> to be required.
> >>
> >> -Dan
> >>
> >> -- 
> >> Dan Allen
> >> Senior Software Engineer, Red Hat | Author of Seam in Action
> >> Registered Linux User #231597
> >>
> >> http://mojavelinux.com
> >> http://mojavelinux.com/seaminaction
> >> http://in.relation.to/Bloggers/Dan
> >> _______________________________________________
> >> webbeans-dev mailing list
> >> webbeans-dev at lists.jboss.org
> >> https://lists.jboss.org/mailman/listinfo/webbeans-dev
> >
> > _______________________________________________
> > webbeans-dev mailing list
> > webbeans-dev at lists.jboss.org
> > https://lists.jboss.org/mailman/listinfo/webbeans-dev
> 




More information about the weld-dev mailing list