简体   繁体   English

拥有用于 XSD 验证的线程安全缓存

[英]Having a thread-safe cache for XSD Validation

I am working on an application for XSD validation and I want my schemas to be cached.我正在开发一个用于 XSD 验证的应用程序,我希望缓存我的架构。
On the other hand the application uses multiple threads so I am wonder what is the thread-safe approach to load the XSD files.另一方面,应用程序使用多个线程,所以我想知道加载 XSD 文件的线程安全方法是什么。
At the moment I have a new net.sf.saxon.s9api.Processor gets created for every XSD.目前,我为每个 XSD 创建了一个新的net.sf.saxon.s9api.Processor A SchemaManager is using to validate lots of xmls after that之后, SchemaManager用于验证大量 xml

Processor processor = new Processor(true);

SchemaManager sm = processor.getSchemaManager();
sm.load(new StreamSource(new File(xsdFilename)));

Is it really necessary?真的有必要吗? Can I instantiate a single Processor and use it for all the XSDs?我可以实例化单个处理器并将其用于所有 XSD 吗? So would it be safe to get the SMs in multi-thread context in this case?那么在这种情况下在多线程上下文中获取 SM 是否安全?

Additionally, is it correct to store SchemaManager instances in a Map by which the application cache is represented?此外,将SchemaManager实例存储在表示应用程序缓存的 Map 中是否正确? Or SchemaValidator objects should be use for it?还是应该使用SchemaValidator对象?

The Saxon Processor and SchemaManager can be used to store multiple schemas (or rather, one schema that is the union of all the schema components from multiple schema documents), and it's thread safe, so it should work fine so long as all the schemas are compatible. Saxon Processor 和 SchemaManager 可用于存储多个模式(或者更确切地说,一个模式是来自多个模式文档的所有模式组件的联合),并且它是线程安全的,因此只要所有模式都正确,它应该可以正常工作兼容的。 By that I mean you can't have two different schema components with the same name, eg as a result of loading different no-namespace schemas, or as a result of using xs:redefines.我的意思是你不能有两个具有相同名称的不同模式组件,例如由于加载不同的无命名空间模式,或者由于使用 xs:redefines。

If you want to keep your schemas separate, however, you will need a different Processor and SchemaManager for each one.但是,如果您希望将架构分开,则需要为每个架构使用不同的ProcessorSchemaManager

The SchemaValidator object isn't thread-safe: you should create a new SchemaValidator for each validation task. SchemaValidator对象不是线程安全的:您应该为每个验证任务创建一个新的SchemaValidator Creating this object is cheap.创建这个对象很便宜。

It's also worth noting that there are corner cases where validating against a "composite" schema may change the validation outcome even if the several parts of the schema are disjoint: for example, when an element wildcard has processContents="strict" or processContents="lax" .还值得注意的是,在某些极端情况下,即使架构的几个部分不相交,针对“复合”架构进行验证也可能会改变验证结果:例如,当元素通配符具有processContents="strict"processContents="lax"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM