开发者

Do CDI Events observed across session scoped JSF backing beans

I'm wondering if it is possible to observe a CDI event with multiple JSF 2.0 session scoped backing beans. I thought that I could push event/data to multiple sessions by observing the event.

I've set up a small test that allows the user to fire the event using a button on the page (which is tied to a method in the session scoped backing bean that actually fires the event). I thought that if I opened two different browsers, two sessions would be created and the event would notify each of the session scoped backing beans.

However, when running my little test and clicking the button to fire the event on one of the browsers, I see that the event only notifies one of the session scoped beans. It only notifies the bean from which the event was fired (i.e. - if I click the button in browser 1 the session scoped bean backing the session in browser 1 is notified, and if I click the button in browser 2 the bean backing the session in brow开发者_JAVA百科ser 2 is notified).

I was under the impression that events would notify all bean instances. However, this doesn't seem to be the case. Should I be able to do this? Do I just have something setup wrong?

UPDATE to show what my code looks like:

A snippet of jsfpage.xhtml to fire an event and show the session-scoped data:

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

The Session-scoped bean that receives 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();
    }
}

An application scoped bean to fire the 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;
    }
}


First, you'd better specify the type of the event:

@Inject
private Event<EventObj> events;

Apart from that there is no indication in the spec that would limit the bean instances on which the observer method is invoked. I'd file an issue about this (in the bugtracker of the implementation you are using. Perhaps Weld?)


I've found that all registered observers are fired.

Most notably if I have an observer on a Conversation Scoped bean and that bean is not active in the current Conversation then, when the event is fired, a new instance is created specially to receive it!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜