XML模式验证时超时和挂起线程

时间:2012-09-16 13:11:26

标签: java xml xsd timeout

我在通过XSD架构验证XML时遇到TimeoutException,并且与checker关联的线程被挂起。

为什么会这样?怎么避免这个?

堆栈追踪:

TimeoutManage I   WTRN0124I: When the timeout occurred the thread with which the transaction is, or was most recently, associated was Thread[WebContainer : 3,5,main].  The stack trace of this thread when the timeout occurred was:
        java.net.SocketInputStream.socketRead0(Native Method)
        java.net.SocketInputStream.read(SocketInputStream.java:140)
        java.io.BufferedInputStream.fill(BufferedInputStream.java:229)
        java.io.BufferedInputStream.read1(BufferedInputStream.java:269)
        java.io.BufferedInputStream.read(BufferedInputStream.java:328)
        sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:700)
        sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:645)
        sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1205)
        org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
        org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
        org.apache.xerces.impl.xs.opti.SchemaParsingConfig.parse(Unknown Source)
        org.apache.xerces.impl.xs.opti.SchemaParsingConfig.parse(Unknown Source)
        org.apache.xerces.impl.xs.opti.SchemaDOMParser.parse(Unknown Source)
        org.apache.xerces.impl.xs.traversers.XSDHandler.getSchemaDocument(Unknown Source)
        org.apache.xerces.impl.xs.traversers.XSDHandler.resolveSchema(Unknown Source)
        org.apache.xerces.impl.xs.traversers.XSDHandler.constructTrees(Unknown Source)
        org.apache.xerces.impl.xs.traversers.XSDHandler.parseSchema(Unknown Source)
        org.apache.xerces.impl.xs.XMLSchemaLoader.loadSchema(Unknown Source)
        org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
        org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
        org.apache.xerces.jaxp.validation.XMLSchemaFactory.newSchema(Unknown Source)
        javax.xml.validation.SchemaFactory.newSchema(Unknown Source)
        my.utils.XmlUtils.validate(XmlUtils.java:38)

用于验证的代码段:

SchemaFactory schemaFactory = 
    SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
    Schema schema = schemaFactory.newSchema(new StreamSource(new StringReader(xsd)));
    Validator validator = schema.newValidator();
...

异常发生在以下行:

Schema schema = schemaFactory.newSchema(new StreamSource(new StringReader(xsd)));

2 个答案:

答案 0 :(得分:3)

很难确定没有看到实际的XSD,但很可能您的架构不是自包含的,但包含一个或多个引用外部URL的 include 语句。 schemaFactory可能会尝试获取它们以构建完整的XSD。

除此之外,在您的传递的xsd上执行getSchemaDocument()的过程中,在堆栈跟踪中调用parseSchema()并尝试通过HTTP获取模式文档英寸

答案 1 :(得分:1)

如果您的架构引用W3C网站上的文档(例如XML命名空间的架构),则会出现特定问题。 W3C故意延迟回应此类请求以阻止过度使用。我们的想法是您应该重定向此类引用以使用文件的本地副本。