Question

I'm building an application that permits users to manage a task. The task monitoring screen can be accessed by two users at the same time. Each user can see if the task is currently running and can either stop it or start it. Whenever one user starts/stops the task, the other user must see the task state changement on her own screen.

However, the code I built doesn't work. When a user starts the task, the other user's screen tries to stop the task immediately. Then some kind of "war" starts: first user's screen re-activates the task and the second user's one re-stops the task and so on.

How can I avoid this war ?

Here is my code:

taskMonitoring.xhtml

<p:poll
      interval="5"
      process="sbbActivateTask" />

<h:outputLabel 
      value="Activate task:" />

<p:selectBooleanButton
      id="sbbActivateTask"
      value="#{taskBean.state}"
      onLabel="YES"
      offLabel="NO"
      valueChangeListener="#{taskBean.switchTask}"/>

TaskBean.java

@ManagedBean(name = "taskBean")
@ViewScoped
public class TaskBean {

   public void setState(boolean b) {
      // I deliberately put nothing here ...
   }

   public boolean getState() {
       return TaskManager.getState();
   } 

   public void switchTask() {
       TaskManager.switchTask();
   } 
}
Was it helpful?

Solution

I would guess that when the poll triggers and updates the <selectBooleanButton>, it sees the change and fires the valueChangeListener, switching it back, thus the race condition. I would prefer not to mix both state and command in the same control.

My suggestion: the same code as the showcase of the component. I.e.:

<p:selectBooleanButton
   id="sbbActivateTask"
   value="#{taskBean.state}"
   onLabel="YES"
   offLabel="NO"
>
    <p:ajax listener="#{taskBean.switchTask}"/>
</p:selectBooleanButton>

This way the action will only get triggered when the user actually presses the button.

In order for the button to update visually, you will need to add the update attribute to poll:

<p:poll
  interval="5"
  process="sbbActivateTask"
  update="selectBooleanButton" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top