我的简单 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)
问题:有什么办法可以避免线程阻塞?请建议!