1 Reply Latest reply on May 4, 2015 4:09 AM by csa

    Advanced CDI Event Support

    aanderson1776

      Hi,

       

      I have a composite that needs to support quite a few CDI event generators and observer methods. I would like to minimize the number of injected Event fields and dynamically fire events using the Event.select() method. While the simple event case works:

       

      @Qualifier
        @Retention(RetentionPolicy.RUNTIME)
        @Target({ ElementType.FIELD, ElementType.PARAMETER })
        public static @interface MyQualifier {
        }
      ...
      @Inject
      @MyQualifier
        Event<MyEvent> myEvent;
      ... 
      myEvent.fire(new MyEvent())
      ...
      public void eventListener(@Observes @MyQualifier MyEvent event) {
      

       

      I am having problems with more advanced event triggering. For example, if I try to use an extension of AnnotationLiteral to dynamically fire an event I receive this compilation error:

       

      public static class MyQualifierAnnotationLiteral extends AnnotationLiteral<Topic>{
      
        }
      


      [ERROR] Line 129: No source code is available for type javax.enterprise.util.AnnotationLiteral<T>; did you forget to inherit a required module?

       

      Also if I try to use an enumeration as an annotation parameter

       

      public enum MyEnum{
           SomeValue;
      }
      @Qualifier
        @Retention(RetentionPolicy.RUNTIME)
        @Target({ ElementType.FIELD, ElementType.PARAMETER })
        public static @interface MyQualifier {
           MyEnum value();
        }
      
      MyQualifierImpl implements MyQualifier{
           MyEnum value(){
                return MyEnum.SomeValue;
           }
           
      @Override
        public Class<? extends Annotation> annotationType() {
           return MyQualifier.class
        }
      }
      ...
      @Inject
        Event<MyEvent> myEvent;
      ...
      myEvent.select(new MyQualifierImpl() ).fire(new MyEvent());
      ...
      public void eventListener(@Observes @MyQualifier(SomeValue) @MyQualifier MyEvent event) {
      

       

      The qualifier is ignored and the observer method is invoked for every MyEvent fired.