Search code examples
javaeventsdependency-injectioncdi

Suppress observing a CDI event with a specific qualifier


I have a CDI event (say eventA that doesn't have any qualifiers) that gets fired, eventA has several consumers. In a special case, I need to fire eventA with a specific qualifier (say qualifierX). Now, all the consumers of eventA will essentially receive eventA (with and without qualifierX).

I would like to maintain the behavior my consumers to only observe eventA without qualifierX.

P.S. I am not allowed to modify every consumer.

I thought about extending eventA with a child eventB and having this eventB fired, but this will also trigger eventA observers.

Is there a direct way to do this in CDI?


Solution

  • If you would like your consumers to only observe EventA without qualifier, than you should have defined them as @Observes @Default EventA. From what you say, they are now bound to @Any event - hence they will receive the event as long as the payload type fits.

    About extending - that wont work, EventB will still have type of EventA, therefore it will notify the original observers. However, if you could change the EventA class, you might create a superclass (MyEvent) and have EventA extends MyEvent. Then firing an event with MyEvent will not notify original observers.