是否可以从使用Transfer-Encoding的XPage发送响应:chunked

时间:2015-07-29 14:16:02

标签: xpages

我的目标是向浏览器传输大量响应。我需要尽可能减少XPage的内存使用量,以便多次点击不会导致服务器崩溃。 设置XPage" XAgent"样式为rendering = false。

有人有这个工作吗?

尝试1

根据各种HttpServletResponse帖子,它应该像在你继续时刷新作者一样简单,但事实并非如此。下面的代码包含一个名为Experiment1的评论部分,我过去常常故意破坏响应。我的逻辑是,resetBuffer调用不应该影响我已经"刷新" - 但确实如此。破坏的响应只包含错误消息而没有包含任何块。当然,我的假设很可能是错误的。代码在afterRenderResponse事件中运行。

importPackage(java.util);
importPackage(java.io);
importPackage(java.lang);

var ec=facesContext.getExternalContext();
var response=ec.getResponse();
var request=ec.getRequest();
var writer=facesContext.getResponseWriter();
try{
    response.setHeader("Content-Type","text/plain; charset=utf-8");

    for(var i=0;i<100;i++){
        var s="CHUNKCHUNKCHUNK"+i;
        writer.write(s);
        writer.flush();
        response.flushBuffer();
        //EXPERIMENT1
        //if(i==50){
            //throw new Error("FAIL ON PURPOSE")
        //}
    }

}catch(e){
    //EXPERIMENT1
    //response.resetBuffer()

    var errorObj={};
    response.setStatus(500);

    var errorMessage = e.message;

    if( typeof e.printStackTrace !== 'undefined' ){
        var stringWriter = new java.io.StringWriter();
        e.printStackTrace( new java.io.PrintWriter( stringWriter ) );
        errorMessage = stringWriter.toString();
    }

    if( typeof e === 'com.ibm.jscript.InterpretException' ){
        errorMessage = e.getNode().getTraceString() + '\n\n' + errorMessage;
    }

    errorObj.status="error";
    errorObj.message=errorMessage;
    writer.write(toJson(errorObj));
}finally{
    if(creport!=null){
        if(debug){
            creport.close(true);
        }else{
            creport.closeOnlyIfIssue(true);
        }
    }
    facesContext.responseComplete();
}

尝试2

我的第二次尝试纯属绝望。它使用beforeRenderResponse事件并根据HTML1.1规范写入块。下面的代码显示了&#34; Transfer-Encoding&#34;标头注释掉,因为它导致零字节有效负载。标题可以实现..

importPackage(java.util);
importPackage(java.io);
importPackage(java.lang)

var ec=facesContext.getExternalContext();
var response=ec.getResponse();
var request=ec.getRequest();
var writer=response.getOutputStream();
try{
    response.setHeader("Content-Type","text/plain; charset=utf-8");
    //response.setHeader("Transfer-Encoding","chunked")
    response.setBufferSize(1024);
    for(var i=0;i<100;i++){
        var s="CHUNKCHUNKCHUNK"+i;
        writer.write((Integer.toHexString(s.length())+"\r\n").getBytes("utf-8"));
        writer.write((s+"\r\n").getBytes("utf-8"));
        writer.flush();
        response.flushBuffer();
    }
    writer.write("0\r\n\r\n".getBytes("utf-8"))

}catch(e){
    var errorObj={};
    //response.resetBuffer();
    response.setStatus(500);

    var errorMessage = e.message;

    if( typeof e.printStackTrace !== 'undefined' ){
        var stringWriter = new java.io.StringWriter();
        e.printStackTrace( new java.io.PrintWriter( stringWriter ) );
        errorMessage = stringWriter.toString();
    }

    if( typeof e === 'com.ibm.jscript.InterpretException' ){
        errorMessage = e.getNode().getTraceString() + '\n\n' + errorMessage;
    }

    errorObj.status="error";
    errorObj.message=errorMessage;
    writer.write(toJson(errorObj).getBytes("utf-8"));
}finally{
    facesContext.responseComplete();
}

2 个答案:

答案 0 :(得分:2)

使用

访问回复时
var response=ec.getResponse().getDelegate();

您将获得 com.ibm.xsp.webapp.XspHttpServletResponse 的实例。它在内部使用缓冲区/块机制以获得更好的性能,这就是为什么您的第一个实验无法按预期工作的原因。

但是如果您直接访问底层的 LCDAdapterHttpServletResponse ,则可以获得输出流的完全控制权:

get '/some_request' do
  slim :file_name
end

答案 1 :(得分:0)

importPackage(java.util);
importPackage(java.io);
importPackage(java.lang);
importPackage(imtk);

var ec=facesContext.getExternalContext();
var response=ec.getResponse().getDelegate();
var request=ec.getRequest();
var writer=response.getWriter();

try{
    response.setHeader("Content-Type","application/json; charset=utf-8");
    response.setHeader("Transfer-Encoding","chunked");
    response.flushBuffer();

    for(var i=0;i<100;i++){
        var s="CHUNK"+i;
        writer.println(Integer.toHexString(s.length()));
        writer.println(s);
        writer.flush(); 
    }

    writer.print("0\r\n");
    writer.print("\r\n");
    writer.flush();

}catch(e){
    var errorObj={};
    response.setStatus(500);

    var errorMessage = e.message;

    if( typeof e.printStackTrace !== 'undefined' ){
        var stringWriter = new java.io.StringWriter();
        e.printStackTrace( new java.io.PrintWriter( stringWriter ) );
        errorMessage = stringWriter.toString();
    }

    if( typeof e === 'com.ibm.jscript.InterpretException' ){
        errorMessage = e.getNode().getTraceString() + '\n\n' + errorMessage;
    }

    errorObj.status="error";
    errorObj.message=errorMessage;
    var json=toJson(errorObj);
    writer.println(Integer.toHexString(json.length()))
    writer.println(json);
    writer.flush();
    writer.print("0\r\n");
    writer.print("\r\n");
    writer.flush();
}finally{
    if(creport!=null){
        if(debug){
            creport.close(true);
        }else{
            creport.closeOnlyIfIssue(true);
        }    
    }

    facesContext.responseComplete();
}