简体   繁体   中英

How to keep client request alive for delayed response from server in jsf

I have ap:commandButton in my JSF page. When user clicks there is a huge amount of data being retrieved from the database and written in excel file and returned to user as a response. This can take up to hours. But after almost one hour using IE it shows "cannot load the page" error message even the process remains running at server side. I know it happens due to client's configuration for expected response time from server. it differs from browser to browser.

Even tough, i am continuously sending ajax updates from server to client during the calculation to show the percentage of the processed data to user and it works but still my client gets dead. Moreover, there is ap:progressBar on my JSF page, which is also showing the percentage in it's every update intervals.

How can I keep my client alive through code? So that after sending a request, it can wait for a long time for the response.

We have different users using different browsers. I need a solution which works for all major browsers. This is my code.

<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文件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM