예. Richfaces의 <a4j:poll>
을 사용하면 주기적으로 서버에 대한 요청을 폴링 할 수 있습니다. 예를 들어,
보기 :
<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>
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;
}
}
로그 메시지 문자열 버퍼 (buildOutputSb)에 추가하고, 우리가 사용하는 이 문자열 버퍼의 값을 UI에 표시하려면 <h:outputText id="consoleOutput">
을 입력하십시오.
"Build Build"버튼을 누르면 속성이 true로 설정되어 <a4j:poll id="poll">
을 활성화하고 주기적으로 서버 폴링을 시작합니다. 그동안 startBuild()
(즉, 빌드 프로세스)이 실행됩니다. <a4j:poll id="poll" >
은 <h:outputText id="consoleOutput">
을 새로 고침하고 빌드 스크립트의 로그 메시지를 새로 고치는 것과 같이 각 폴링 라운드 후에 <h:outputText id="consoleOutput">
번 (즉 업데이트)이됩니다.
<a4j:poll>
은 풀링 라운드를 요청할 것이므로 session scope
에 MBean을 등록하여 여러 번 인스턴스화되는 것을 방지합니다.
<a4j:poll>
에 대한 자세한 정보 및 사용에 대한 우수 사례는 official demo을 참조하십시오. 예를 들어, <a4j:poll>
및 그 주변에 대해 <a4j:poll>
과 <a4j:region>
의 형식을 별도로 사용하는 것이 좋습니다. 왜 허드슨을 사용할 수 없습니까? – Thilo