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; } }
jsf jsf-2 cdi
Captain mingo
source share