WIldfly / Undertow UT010029 流已关闭

问题描述

在 jdk 9 上运行 Wildfly 16 并重复调用 java StringReader 时,我得到 UT010029 Stream is closed.

public void export(OutputStream out) throws Exception   {
...
while() { 
  ...
  csvstream = new StringReader(csvcontent.toString());
  try { 
    while ((iRead = csvstream.read()) != -1) out.write(iRead);
    out.flush();
  } catch(IOException e)    { 
    validconnection=false;
    log.error(e);
    e.printstacktrace();
    break;      
  } finally {if (csvstream != null) csvstream.close();}
}

错误发生在 csvstream.read()

java.io.IOException: UT010029: Stream is closed
at io.undertow.servlet.spec.ServletoutputStreamImpl.write(ServletoutputStreamImpl.java:137)
at io.undertow.servlet.spec.ServletoutputStreamImpl.write(ServletoutputStreamImpl.java:122)

我已验证 csvstream 不为空,但已关闭且 csvcontent 不为空且长度为正。 有时它运行所有迭代没有问题,有时它会出错。

看起来“out”可能是关闭的流。 ServletoutputStreamImpl.java 中的违规行是:

if (anyAreSet(state,FLAG_CLOSED) || servletRequestContext.getoriginalResponse().isTreatAsCommitted()) {
      throw UndertowServletMessages.MESSAGES.streamIsClosed();

就我而言,out 作为参数传入。调用方是一个像这样的 servlet:

OutputStream out = response.getoutputStream(  );
if (ve!=null)   {
  try {
        ve.export(out);
    } catch (Exception e)   {
        e.printstacktrace();
    }
} //if ve!=null

ve 是一个简单的 POJO。

对此的任何帮助将不胜感激! 谢谢。

解决方法

以防其他人遇到此问题 - 这是通过standalone.xml中的wildfly配置解决的: http-listener 元素具有属性 read-timeout 和 write-timeout。 最新版本的 wildfly 限制了默认值,并且这些属性未在默认的 standalone.xml 中指定。 见:

[Wildfly 模型参考][1]