![](/img/trans.png)
[英]Is it bad to keep a server client response connection open and alive in node.js?
[英]How to keep client request alive for delayed response from server in jsf
我的JSF頁面中有ap:commandButton。 當用戶單擊時,從數據庫中檢索大量數據並將其寫入excel文件並作為響應返回給用戶。 這可能需要幾個小時。 但是在使用IE近一個小時后,它顯示“無法加載頁面”錯誤消息,即使進程仍在服務器端運行。 我知道這是因為客戶端配置服務器的預期響應時間。 它與瀏覽器不同。
即使很難,我在計算過程中不斷地從服務器向客戶端發送ajax更新,以向用戶顯示已處理數據的百分比,但它仍然有效,但我的客戶端仍然死機。 此外,我的JSF頁面上有ap:progressBar,它還顯示了每個更新間隔的百分比。
如何通過代碼保持客戶端的活力? 因此,在發送請求后,它可以等待很長時間來響應。
我們有不同的用戶使用不同的瀏覽器 我需要一個適用於所有主流瀏覽器的解決方案。 這是我的代碼。
<p:dialog id="alert-exp-info-dialog"
widgetVar="alertExpInfoDialog">
<p:ajax event="close"
listener="#{bean.onExportDialogClose}"
process="@this"
update=":alert-exp-info-form"
immediate="true" />
<h:form id="alert-exp-info-form">
<h:outputFormat id="alert-exp-detail"
value="So info for user">
<f:param value="#{bean.exportedAlertsCount}" />
<f:param value="#{bean.totalExportableAlerts}" />
</h:outputFormat>
<p:progressBar id="alert-exp-progress"
widgetVar="alertExpProgress"
value="#{bean.exportProgress}"
labelTemplate="{value}%"
interval="3000"
ajax="true">
<p:ajax event="complete"
listener="# {bean.onExportComplete}"/>
<f:event type="preValidate"
update="alert-exp-abort-btn"
listener="#{bean.updateComponents}" />
</p:progressBar>
<h:panelGroup id="alert-exp-button-panel">
<p:commandButton id="alert-exp-proceed-btn"
value="Process"
action="#{bean.doCalculation}"
onclick="alertExpProgress.start();"
ajax="false">
<p:ajax update="alert-exp-button-panel" />
</p:commandButton>
</h:panelGroup>
</h:form>
</p:dialog>
public void doCalculation()
{
FacesContext context = FacesContext.getCurrentInstance();
//Too long calculation...
for ( loop )
{
...
...
}
//Finally writing excel file on responce
context.setResponseContentType( "application/vnd.openxmlformats- officedocument.spreadsheetml.sheet" );
context.setResponseHeader( "Expires", "0" );
context.setResponseHeader( "Cache-Control", "must-revalidate, post- check=0, pre-check=0" );
context.setResponseHeader( "Pragma", "public" );
context.setResponseHeader( "Content-disposition", "attachment;filename=" + ( fileName.endsWith( ".xlsx" ) ? fileName : fileName + ".xlsx" ) );
context.addResponseCookie( Constants.DOWNLOAD_COOKIE, "true", new HashMap<String, Object>() );
OutputStream out = context.getResponseOutputStream();
generatedExcel.write( out );
out.close();
context.responseFlushBuffer();
context.responseComplete();
}
public void updateComponents()
{
RequestContext.getCurrentInstance().update( "alert-exp-info-form:alert- exp-detail" );
}
public void onExportComplete()
{
RequestContext context = RequestContext.getCurrentInstance();
context.execute("alertExpInfoDialog.hide();");
}
我通過ajax觸發長處理來解決問題,最后通過單獨的按鈕單擊下載生成的excel文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.