0

我的简单 Web 服务接收 XML 文档的 Zip 文件,我尝试使用 XSD 模式文件验证 XML。下面是代码片段

        Schema schema = BlahSchemaFactory.newSchema("Blah.xsd");
        Validator validator = schema.newValidator();
        BlahErrorHandler eh = new BlahErrorHandler();
        validator.setErrorHandler(eh);

        ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(zipFileContentAsBytes));
        zis.getNextEntry(); 

        Source xmlSource = new StreamSource(zis);
        validator.validate(xmlSource);

BlahSchemaFactory是一个单例,每个传递的 XSD 文件只帮助创建一次 Schema 对象现在,当多个 HTTP 线程通过上面的代码传递时,就会出现问题。他们在 validate() 方法的代码中互相阻塞。

以下是从 HP 诊断工具获得的堆栈跟踪。

    "blah_thread_77" Id=472 in BLOCKED on lock=org.apache.xerces.impl.xpath.regex.RegularExpression@3c9d97f9
         owned by d1_thread_237 Id=21648
        at org.apache.xerces.impl.xpath.regex.RegularExpression.matches(Unknown Source)
        at org.apache.xerces.impl.xpath.regex.RegularExpression.matches(Unknown Source)
        at org.apache.xerces.impl.dv.xs.XSSimpleTypeDecl.getActualValue(Unknown Source)
        at org.apache.xerces.impl.dv.xs.XSSimpleTypeDecl.validate(Unknown Source)
        at org.apache.xerces.impl.xs.XMLSchemaValidator.processOneAttribute(Unknown Source)
        at org.apache.xerces.impl.xs.XMLSchemaValidator.processAttributes(Unknown Source)
        at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
        at org.apache.xerces.impl.xs.XMLSchemaValidator.emptyElement(Unknown Source)
        at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.jaxp.validation.StreamValidatorHelper.validate(Unknown Source)
        at org.apache.xerces.jaxp.validation.ValidatorImpl.validate(Unknown Source)
        at javax.xml.validation.Validator.validate(Validator.java:124)
        at BlahPackage.BlahClass.uploadSaveFileRecord(BlahRecord.java:77)
        at sun.reflect.GeneratedMethodAccessor210.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.jboss.ws.common.invocation.AbstractInvocationHandlerJSE.invoke(AbstractInvocationHandlerJSE.java:111)
        at org.jboss.wsf.stack.cxf.JBossWSInvoker._invokeInternal(JBossWSInvoker.java:181)
        at org.jboss.wsf.stack.cxf.JBossWSInvoker.invoke(JBossWSInvoker.java:127)
        at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)

问题:有什么办法可以避免线程阻塞?请建议!

4

1 回答 1

0

下面是在尝试使用 XSD 模式验证 XML 时解决线程阻塞问题的代码片段。你需要 :

  1. 确保为每个线程创建一个新架构
  2. 确保在 SchemaFactory 上调用 newSchema 时同步调用(如图所示)
  3. 而不是调用 BlahObject.class.getClassLoader().getResource(schemaFileOnClassPath) 使用某种类型的 Singleton 只获取 XSD 的 URL 一次,否则您将在类加载器尝试查找资源时遇到线程阻塞(已确定使用 HP 诊断工具)

        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        URL schemaURL = BlahSchemaSingleton.getSchemaURL("Blah.xsd");
        Schema schema = null; 
    
        synchronized(schemaFactory) {
            schema = schemaFactory.newSchema(schemaURL);
        }
    
        Validator validator = schema.newValidator();
        BlahErrorHandler eh = new BlahErrorHandler();
        validator.setErrorHandler(eh);
    
        ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(zipFileContentAsBytes));
        zis.getNextEntry(); 
    
        Source xmlSource = new StreamSource(zis);
        validator.validate(xmlSource);
    
于 2013-02-03T23:58:50.423 回答