I have a command dialog on a web page that uses command buttons to confirm whether or not the user would like to run a back-end script. If the user confirms they'd like to continue, there should be an immediate growl message letting them know that the script has begun. The script then runs, blocking all other processes until its done. Upon completion, there should be another growl message letting the user know that the script has finished.
The xhtml code is set up as follows:
<p:commandButton value="Yes Sure" actionListener="#{listBean.invokeBackend}"
onclick="PF('backendRun').hide()" update="invokeBackendGrowl"/>
These attributes do not execute simultaneously The order of execution for these attributes is onclick, then actionlistener, then update. Due to the fact that my actionlistener item refers to a function that blocks other processes, I need the update attribute to be executed before the actionlistener item is complete.
onclick closes the command dialog
update works to display the most recent version of the growls
actionlistener executes the bean method, which uses process builder to ensure the script runs without interruption. It also contains the Java code for both growls (notification for beginning of script and notification for end of script). Since the script is running alone, these growls both display at the same time, after script completion, rather than the beginning growl being displayed right when the script begins.
The actionlistener item is as follows:
public void invokeBackend() throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder(<command for script to run>);
pb.redirectErrorStream(true);
File outputFile = new File(<location>);
pb.redirectOutput(outputFile);
Process p = pb.start();
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Successful", "Script Called!"));
p.waitFor();
context.addMessage(null, new FacesMessage("Completed", "Script Complete!"));
}
Update
There is one thread in this code, and this question was looking for a way to re-organize when the thread began in relation to other actions.