title: Unexpected behavior with ViewScoped beans when using Spring-Session clustering with Hazelcast
body: <p>I am working on introducing clustering to a JSF based Spring-Boot web application and as soon as we enabled session-replication with Hazelcast, we started noticing that several of our JSF pages that utilize ViewScoped beans were no longer functioning correctly.  If we disable session replication and Hazelcast the odd behavior no longer occurs.</p>
  
  <p>I first noticed the problem in one of our pages that utilizes a PrimeFaces Wizard component.  Values entered on the first page of the wizard are lost when the second page is "submitted".  </p>
  
  <p>Then on a different page I noticed that a command button was no longer calling the actionListener method on the managed bean.  I set a breakpoint in the method and the breakpoint is never hit, but the page "blinks" and refreshes back to its initial state.  I did note that the PostConstruct method on the managed bean is NOT called again, so it is not generating a new instance of the ViewScoped bean.</p>
  
  <p>None of these problems occur when I disable session replication and Hazelcast though.  As far as I can tell, inspecting the session and its contents, it does look like sessions are being created and properly stored, as far as I can tell anyway.</p>
  
  <p>The app is a Spring-Boot web application using the joinfaces starters to bring in JSF 2.3.7 (Mojarra), PrimeFaces 6.2, and Omnifaces 1.14.1.  We initially developed the app without any session-replication in place and we had no problems with our ViewScoped beans.</p>
  
  <p>The ViewScoped beans are using org.springframework.stereotype.Component annotation, just like you see in the joinfaces examples, and javax.faces.view.ViewScoped as the scope annotation.  I have also tried bringing in Weld and using the @Named annotation as well as falling back on the old deprecated JSF @ManagedBean and @ViewScoped annotations, but the same behavior persists in all cases. </p>
  
  <p>I have gone through and ensured that our ManagedBeans are all fully serializable as well as any attributes on the beans themselves.</p>
  
  <p>To demonstrate what I am seeing I picked two very simple examples from a couple places on the web and created a simple Spring-Boot project that you can clone and run yourself.<br>
  <a href="https://github.com/illingtonFlex/ViewScopeDemo" rel="nofollow noreferrer">https://github.com/illingtonFlex/ViewScopeDemo</a></p>
  
  <p>This demo app contains two managed beans and two xhtml files.</p>
  
  <p>The first example is one that a copied from an example on BalusC's website:
  <a href="http://balusc.omnifaces.org/2010/06/benefits-and-pitfalls-of-viewscoped.html" rel="nofollow noreferrer">http://balusc.omnifaces.org/2010/06/benefits-and-pitfalls-of-viewscoped.html</a></p>
  
  <p>The xhtml file looks like this:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:h="http://java.sun.com/jsf/html"&gt;
  &lt;h:head&gt;
      &lt;title&gt;Really simple CRUD&lt;/title&gt;
  &lt;/h:head&gt;
  &lt;h:body&gt;
      &lt;h3&gt;List items&lt;/h3&gt;
      &lt;h:form rendered="#{not empty viewScopedController.list}"&gt;
          &lt;h:dataTable value="#{viewScopedController.list}" var="item"&gt;
              &lt;h:column&gt;&lt;f:facet name="header"&gt;ID&lt;/f:facet&gt;#{item.id}&lt;/h:column&gt;
              &lt;h:column&gt;&lt;f:facet name="header"&gt;Value&lt;/f:facet&gt;#{item.value}&lt;/h:column&gt;
              &lt;h:column&gt;&lt;h:commandButton value="edit" action="#{viewScopedController.doEdit(item)}" /&gt;&lt;/h:column&gt;
              &lt;h:column&gt;&lt;h:commandButton value="delete" action="#{viewScopedController.delete(item)}" /&gt;&lt;/h:column&gt;
          &lt;/h:dataTable&gt;
      &lt;/h:form&gt;
      &lt;h:panelGroup rendered="#{empty viewScopedController.list}"&gt;
          &lt;p&gt;Table is empty! Please add new items.&lt;/p&gt;
      &lt;/h:panelGroup&gt;
      &lt;h:panelGroup rendered="#{!viewScopedController.edit}"&gt;
          &lt;h3&gt;Add item&lt;/h3&gt;
          &lt;h:form&gt;
              &lt;p&gt;Value: &lt;h:inputText value="#{viewScopedController.item.value}" /&gt;&lt;/p&gt;
              &lt;p&gt;&lt;h:commandButton value="add" action="#{viewScopedController.add}" /&gt;&lt;/p&gt;
          &lt;/h:form&gt;
      &lt;/h:panelGroup&gt;
      &lt;h:panelGroup rendered="#{viewScopedController.edit}"&gt;
          &lt;h3&gt;Edit item #{viewScopedController.item.id}&lt;/h3&gt;
          &lt;h:form&gt;
              &lt;p&gt;Value: &lt;h:inputText value="#{viewScopedController.item.value}" /&gt;&lt;/p&gt;
              &lt;p&gt;&lt;h:commandButton value="save" action="#{viewScopedController.save}" /&gt;&lt;/p&gt;
          &lt;/h:form&gt;
      &lt;/h:panelGroup&gt;
  &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and the ViewScoped bean backing this page looks like this:</p>
  
  <pre><code>package help.me.understand.jsf.ViewScopeDemo.controller;
  
  import help.me.understand.jsf.ViewScopeDemo.model.Item;
  import lombok.Data;
  import lombok.EqualsAndHashCode;
  import lombok.ToString;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.stereotype.Component;
  
  import javax.annotation.PostConstruct;
  import javax.faces.view.ViewScoped;
  import java.io.Serializable;
  import java.util.ArrayList;
  import java.util.List;
  
  @Component
  @ViewScoped
  @Data
  @EqualsAndHashCode(callSuper=false)
  @ToString
  public class ViewScopedController implements Serializable {
      private static final Logger log = LoggerFactory.getLogger(ViewScopedController.class);
      private List&lt;Item&gt; list;
      private Item item = new Item();
      private boolean edit;
  
      @PostConstruct
      public void init() {
          // list = dao.list();
          // Actually, you should retrieve the list from DAO. This is just for demo.
          list = new ArrayList&lt;Item&gt;();
          list.add(new Item(1L, "item1"));
          list.add(new Item(2L, "item2"));
          list.add(new Item(3L, "item3"));
      }
  
      public void add() {
          // dao.create(item);
          // Actually, the DAO should already have set the ID from DB. This is just for demo.
          item.setId(list.isEmpty() ? 1 : list.get(list.size() - 1).getId() + 1);
          list.add(item);
          item = new Item(); // Reset placeholder.
      }
  
      public void doEdit(Item item) {
          this.item = item;
          edit = true;
      }
  
      public void save() {
          // dao.update(item);
          item = new Item(); // Reset placeholder.
          edit = false;
      }
  
      public void delete(Item item) {
          // dao.delete(item);
          list.remove(item);
      }
  
      public List&lt;Item&gt; getList() {
          return list;
      }
  
      public Item getItem() {
          return item;
      }
  
      public boolean isEdit() {
          return edit;
      }
  
      // Other getters/setters are actually unnecessary. Feel free to add them though.
  }
  </code></pre>
  
  <p>If you start the application and navigate to localhost:8080/index.xhtml, click edit on one of the entries.  Then enter a new name in the text field and click save.  The save method on the managed bean is never called, and the page "resets" to its initial state.  If you disable Hazelcast and session-replication by commenting out the @EnableHazelcastHttpSession annotation along with the hazelcastInstance @Bean defined in ViewScopeDemoApplication, the above example steps work.  The save method is called, and the name of the edited item is changed.</p>
  
  <p>To demonstrate another example of odd ViewScoped behavior I copied the Wizard example code verbatim from the PrimeFaces showcase:
  <a href="https://www.primefaces.org/showcase/ui/panel/wizard.xhtml" rel="nofollow noreferrer">https://www.primefaces.org/showcase/ui/panel/wizard.xhtml</a></p>
  
  <p>With the app started, you can access this example via 
  localhost:8080/wizard.xhtml</p>
  
  <p>With Hazelcast and session-replication enabled, you can set a break point in the onFlowProcess method, which fires when navigating from one page of the wizard to the next.  You can see that values entered on the first step of the wizard are lost (they become null) on subsequent wizard page changes.  Disable Hazelcast and the values persist across the entire span of Wizard tabs.  </p>
  
  <p>I am not seeing any errors or exceptions of any kind in the logs when the problems occur.  Nor do I see any problems in the browser debug console for that matter.  However, its clear from these two examples that the ViewScoped beans are behaving differently depending on whether or not Hazelcast session-replication is enabled.</p>
  
  <p>Thank you in advance for your help and consideration!</p>