raffael
raffael

Reputation: 2456

Is there a way to know if there are any observers for CDI events?

I have a Button in my UI that should not always be added. The only thing it does is firing a CDI event when clicked.

Now I'd like to prevent adding the Button to the UI if there is no bean observing that event. Is there a way to see if there are any observers on the injected Event object or preventing injection of it so I can check if it is null?

Upvotes: 3

Views: 292

Answers (1)

palacsint
palacsint

Reputation: 28865

The following seems to work:

@Inject
private BeanManager beanManager;

...

Set<ObserverMethod<? super Document>> observers = 
    beanManager.resolveObserverMethods(
        new Document(), new AnnotationLiteral<Any>() {});
observers.isEmpty();

(Although it might not be the best architecture. Later you might have an observer which simply has an empty implementation or just ignores the event based on a runtime condition.)

Upvotes: 3

Related Questions