Display console output in webapp
I want to come up with a web application that does a maven build on a server and displays the build console output as it happening. I am looking for something similar to what is available in Hudson.
I have read the solution given here : Need to execute shell script from webapp and display consol开发者_Go百科e output in page
Well, I can get run the script and get the entire output, but I want the UI to be updated as the build is happening. How can I accomplish this?
Is this possible to do this using JSF and Jboss Richfaces components?
Yes , you can accomplish this using Richfaces 's <a4j:poll>
, which will periodically poll a request to the server . For example ,
View:
<a4j:region>
<h:form>
<a4j:poll id="poll" enabled="#{testBean.enablePolling}" reRender="consoleOutput,poll" />
</h:form>
</a4j:region>
<h:form>
<a4j:jsFunction name="startBuild" action="#{testBean.startBuild}" />
<a4j:commandButton value="Start Build" action="#{testBean.startPolling}" oncomplete="startBuild()" reRender="poll"/>
<hr/>
<h:outputText id="consoleOutput" value="#{testBean.consoleOuput}" escape="false"/>
</h:form>
The MBean:
public class TestBean {
private boolean enablePolling;
private StringBuffer buildOutputSb = new StringBuffer();
public TestBean() {
}
public boolean isEnablePolling() {
return enablePolling;
}
public void setEnablePolling(boolean enablePolling) {
this.enablePolling = enablePolling;
}
public void startPolling(){
this.enablePolling = true;
}
public void startBuild(){
this.buildOutputSb= new StringBuffer();
//Stimulating the build process , which will output the log message to the buildOutputSb
for (int i=0 ; i <10 ; i++){
buildOutputSb.append("Output").append(i).append("<br/>");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.enablePolling = false;
}
}
The log messages outputted by the build script are appended to the string buffer (buildOutputSb) and we use a <h:outputText id="consoleOutput">
to display the value from this string buffer to the UI.
Once the "Start Build" button is pressed , the enablePolling
properties will set to true , which enables the <a4j:poll id="poll">
and causes it to start polling the server periodically . At the meantime , startBuild()
(ie. building process) will run .The <a4j:poll id="poll" >
will reRender
(ie. update) the <h:outputText id="consoleOutput">
after each round of polling, so it just like refreshing the <h:outputText id="consoleOutput">
and hence refreshing the log messages of the build script.
As the <a4j:poll>
will send an request for each round of pooling , I register the MBean in the session scope
to prevent it is instantiated many times.
You can refer to the official demo for more information about <a4j:poll>
and the best practices of using it . For example , it suggests that to have a separate form for <a4j:poll>
and surrounding <a4j:poll>
with <a4j:region>
精彩评论