Do CDI events occur as part of beans session support JSF

I am wondering if it is possible to observe a CDI event with several JSF 2.0 support components in a session. I thought that I could transfer the event / data to several sessions by observing the event.

I installed a small test that allows the user to fire an event using the button on the page (which is associated with the method in the scope support component that actually fires the event). I thought that if I open two different browsers, two sessions will be created and the event will notify each of the bean components with a scope.

However, when I run my little test and click the button to fire the event in one of the browsers, I see that the event notifies only one of the scope beans. It only notifies the bin from which the event was generated (i.e., if I press a button in browser 1, a bin with a scope that supports the session in browser 1 receives a notification, and if I press a button in browser 2, the bin supporting session in browser 2 notified).

My impression is that events will notify all bean instances. However, this is not so. Should I be able to do this? Is there just something wrong with my setup?

UPDATE to show what my code looks like:

Snippet jsfpage.xhtml to trigger an event and display data in the session area:

<h:form> <h:outputText value="#{BackingBean.property}" /> <p:commandButton value="Fire Event" action="#{EventFirer.fireEvent}" /> </h:form> 

The session component receiving the event:

 @Named @SessionScoped public class BackingBean implements Serializable { private String property; public String getProperty() { return property } public void listenForChange(@Observes EventObj event) { logger.log(Level.INFO, "Event received"); property = event.toString(); } } 

Application scope bean to trigger an event:

 @Named @ApplicationScoped public class EventFirer implements Serializable { @Inject private Event<EventObj> events; public String fireEvent() { logger.log(Level.INFO, "Event fired"); events.fire(new EventObj()); return null; } } 
+10
jsf jsf-2 cdi
source share
2 answers

First you must specify the type of event:

 @Inject private Event<EventObj> events; 

In addition, there are no pointers in the specification that restrict the bean instances to which the observer method is invoked. I would ask a question about this (in the bugtracker implementation that you are using. Perhaps Weld?)

+2
source share

I found that all registered observers were fired.

First of all, if I have an observer on the Scope bean and that the bean is not active in the current conversation, then when the event is fired, a new instance is created specifically to receive it!

0
source share

Source: https://habr.com/ru/post/650282/


All Articles