progress bar while long running server job with Primefaces
03:20 03 Sep 2014

I'd like a bar showing progress of a long-running server job launched with commandButton in jsf / Primefaces.

The showcase for Primefaces shows how to create a pb which updates according to the state of some variable on server side, with Ajax: http://www.primefaces.org/showcase/ui/misc/progressBar.xhtml

Ajax ProgressBar



I try to add an action on the commandButton, that should have for effect to update the progress value:


The Computer bean:

@ManagedBean
@SessionScoped
public class Computer {

    long i;

    public Computer() {
    }

    public String compute() throws InterruptedException {
        i = 1;
        while (i < 10) {
            i++;
            Thread.sleep(1000);
        }
        return "welcomePrimefaces.xhtml";
    }
}

The ControllerBean:

ManagedBean
@ViewScoped
public class ControllerBean {

@Inject Computer computer;

    public ControllerBean() {
    }

    private Integer progress;


    public Integer getProgress() {
        if (computer.i == 1){
            progress = 30;
        }
        if (computer.i == 2){
            progress = 60;
        }
        if (computer.i == 3){
            progress = 90;
        }
        if (computer.i == 4){
            progress = 100;
        }
        return progress;
    }

    public void setProgress(Integer progress) {
        this.progress = progress;
    }

    public void onComplete() {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Progress Completed"));
    }

    public void cancel() {
        progress = null;
    }

}

But the compute method is never called, i is never updated. Surely this is this logic (injecting computer into controllerbean) which is incorrect. Any pointer to get it to work is appreciated!

jsf jsf-2 primefaces