代码之家  ›  专栏  ›  技术社区  ›  volvox

将内容流式传输到JSF UI

  •  1
  • volvox  · 技术社区  · 14 年前

    我非常满意我的JSF应用程序,它读取接收到的MQ消息的内容,并将它们提供给UI,如下所示:

    <rich:panel>
    <snip>
      <rich:panelMenuItem label="mylabel"  action="#{MyBacking.updateCurrent}">
        <f:param name="current" value="mylog.log" />    
      </rich:panelMenuItem>
    </snip>
    </rich:panel>
    
    <rich:panel>
      <a4j:outputPanel ajaxRendered="true">
        <rich:insert content="#{MyBacking.log}" highlight="groovy" />
      </a4j:outputPanel>
    </rich:panel>
    

    private String logFile = null;
    ...
    
        public String updateCurrent() {
            FacesContext context=FacesContext.getCurrentInstance();
            setCurrent((String)context.getExternalContext().getRequestParameterMap().get("current"));
            setLog(getCurrent());
            return null;
        }
    
        public void setLog(String log) {
            sendMsg(log);
            msgBody = receiveMsg(moreargs);
            logFile = msgBody;
        }
    
        public String getLog() {
            return logFile;
        }
    

    直到其中一条消息的内容太大,tomcat掉了下来。显然,我认为,我需要改变它的工作方式,以便返回某种形式的流,这样就不会有一个对象变大到容器死亡的程度,并且连续消息返回的内容在传入时流式传输到UI。

    我认为我可以取代我现在正在做的工作,对吗 String 带有 BufferedOutputStream

    private BufferedOutputStream logFile = null;
    
        public void setLog(String log) {
            sendMsg(args);
            logFile = (BufferedOutputStream) receiveMsg(moreargs); 
        }
    
        public String getLog() {
            return logFile;
        }
    
    1 回复  |  直到 14 年前
        1
  •  1
  •   BalusC    14 年前

    如果Tomcat超过了这个值,那么它的大小必须超过128MB或者可能是两倍(这是某些Tomcat版本的最小默认内存大小)。我不认为用户会重视访问这么大的网页。在localhost上同时充当服务器和客户端时,它可能感觉很快,但在internet上服务时,它的速度会慢100倍。

    谷歌也不会一次在一个网页上显示出所有千千万万个可用结果,他们的服务器肯定也会“倒下”的:)

    根据注释:bean是否被放在会话范围内?这样它很快就会在记忆中积累起来。只有当您有一个 InputStream 在一边和另一边 OutputStream 在另一边。没有办法将字符串转换为流,这样就不会再存储在Java内存中。源必须留在另一端,并且必须通过行逐字节检索。唯一可行的方法是使用 <iframe> 谁的 src HttpServlet 它将数据从源直接流到响应。

    您的最佳选择可能是将整个内容存储在数据库中,或者(如果它不包含特定于用户的数据)存储在应用程序范围中,并在所有会话/请求中共享。