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

Pete Muir pmuir at redhat.com
Mon Jul 27 07:47:10 EDT 2009


On 27 Jul 2009, at 10:47, David Allen wrote:

> 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.

Well, that may be how it is implemented, but it's not what the spec  
requires :-) See 10.4.2 and 10.1

https://jira.jboss.org/jira/browse/WBRI-313

>
> 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.

As I said on the issue, this will work correctly for all container  
defined events, but I don't think it will for custom events...

>
>>
>> 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
>>
>
> _______________________________________________
> 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