title: CDI: Which instance's @Observes-method is called ?
body: <p>I observe a strange behavoir for cdi events: </p>
  
  <p>Given the following scenario: An action on the admin page shall update the view  of web page news.xhtml (via omnifaces) by adding the value for "Event-Id": </p>
  
  <p><a href="https://i.stack.imgur.com/t0LMM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/t0LMM.png" alt="enter image description here"></a> </p>
  
  <p>Both beans are session scoped backing beans for their jsf based web pages. </p>
  
  <blockquote>
    <p><strong>What happens:</strong> There are two instances of NewsBean due to two browser windows accessing  news.xhtml. When an event is fired, both
    browser windows are updated. However, <strong>both  updates come from the
    @Observes-method of the first instance of NewsBean</strong>.</p>
  </blockquote>
  
  <p>More precisely: NewsBean has a property "id" that is initialized @PostConstruct with a random 
  number. This id is displayed on news.xhtml as "Property-Id" (cf. image). The id is also pushed 
  by the @Observes-method of the bean and displayed on news.xhtml as "Event-Id":  </p>
  
  <pre><code>private void onBreakingNews(@Observes Info info) {       
    channel.send(id);  // displayed as "Event-Id" on news.xhtml      
  } 
  </code></pre>
  
  <p>Let the id's for the two instances simply be 1 and 2. Before the update, browser windows for news.xhtml display: </p>
  
  <pre><code>Browser 1:                  Browser 2: 
  
  Property-Id: 1              Property-Id: 2
  Event-Id:                   Event-Id:  
  </code></pre>
  
  <p>After the update (Event-Id value added): </p>
  
  <pre><code>Browser 1:                  Browser 2: 
  
  Property-Id: 1              Property-Id: 2
  Event-Id: 1                 Event-Id: 1 
  </code></pre>
  
  <p>That means: </p>
  
  <ul>
  <li>both instances of NewsBean observed the event and updated their associated browser window </li>
  <li>but the data send to the two browsers comes from the @Observes-method of the first instance</li>
  </ul>
  
  <p>I had expected that each instance's own @Observes-method would be called. </p>
  
  <blockquote>
    <p><strong>Q1:</strong> Is this behavoir intended ? </p>
    
    <p><strong>Q2:</strong> Is the @Observes-method allowed to use internal state of the bean instance ? (in this case it would be a bug)</p>
  </blockquote>
  
  <p>As cdi container I used Weld from WildFly 11. Code for NewsBean is: </p>
  
  <pre><code>@Named @SessionScoped
  public class NewsBean {
  
    private int id; 
    @Inject @Push(channel="pushChannel") private PushContext channel;  
  
    private void onBreakingNews(@Observes Info info) { 
      // channel.send(info.getMsg()); 
      channel.send(id); 
    } 
  
    @PostConstruct 
    private void init() { 
      id = new Random().nextInt(100); 
    } 
  
    ... getter and setter for id ... 
  }
  </code></pre>
  
  <p>Code of news.xhtml: </p>
  
  <pre><code>&lt;h:head&gt;
    &lt;f:verbatim&gt;
      &lt;script type="text/javascript"&gt;
        function socketListener(message, channel, event) {
          document.getElementById("formId:info").value = message;
        }; 
      &lt;/script&gt;
    &lt;/f:verbatim&gt;
  &lt;/h:head&gt;
  &lt;h:body&gt;
    &lt;h:outputText value="Property-Id: "/&gt;
    &lt;h:outputText value="#{newsBean.id}"/&gt;
    &lt;p&gt;&lt;/p&gt;
    &lt;h:form id="formId"&gt;
      &lt;h:outputText value="Event-Id: "/&gt;       
      &lt;h:inputText value="" id="info" readonly="true"/&gt;
      &lt;o:socket channel="pushChannel" onmessage="socketListener"/&gt;       
    &lt;/h:form&gt;
  &lt;/h:body&gt; 
  </code></pre>