JAXBContext 初始化加速? [英] JAXBContext initialization speedup?

查看:51
本文介绍了JAXBContext 初始化加速?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以加快 javax.xml.bind.JAXBContexts 的初始化速度,并且有大量 (>1000) 个类?在我们的 XML 繁重的应用程序中,启动时间大约为 10 分钟,主要包括 JAXBContexts 的初始化时间.:-(

Is there any way to speed up the initialization of javax.xml.bind.JAXBContexts with a large (>1000) number of classes? In our XML heavy application the startup time is some 10 minutes and consists mainly of the initialization time of the JAXBContexts. :-(

我们在 JDK 1.5 中使用 Sun 的 JAXB 实现,并使用 org.jvnet.jaxb2.maven2.maven-jaxb2-plugin 从 XSD 生成代码.

We are using Sun's JAXB implementation in the JDK 1.5 and the org.jvnet.jaxb2.maven2.maven-jaxb2-plugin for the code generation from XSDs.

澄清:问题不在于我们有许多具有相同上下文路径的 JAXBContext 实例,而是问题在于单个 JAXBContext 的初始化需要数十秒,因为它必须加载和处理数千个类.(我们的 XSD 相当大且复杂.)所有 JAXBContext 实例都有不同的上下文路径 - 我们无法进一步减少数量.

Clarification: The problem is not that we have many instances of a JAXBContext with the same contextpaths, but the problem is that the initialization of one single JAXBContext takes tens of seconds since it has to load and process thousands of classes. (Our XSDs are fairly large and complicated.) All JAXBContext instances have different contextpaths - we cannot reduce the number further.

推荐答案

正是由于这个原因,JAXB 参考实现具有某种未记录的系统属性:

The JAXB reference implementation has a sort-of-undocumented system property for exactly this reason:

-Dcom.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.fastBoot=true

或者对于包重构之前的旧版本:

or for old versions prior to the package refactoring:

-Dcom.sun.xml.bind.v2.runtime.JAXBContextImpl.fastBoot=true

这指示 JAXB 跳过预先缓存它完成工作所需的各种反射肌肉的昂贵过程.相反,它会在使用上下文时进行所有反射.这会导致运行时变慢,但初始化速度明显加快,尤其是对于大量类.

This instructs JAXB to skip the expensive process of pre-caching the various reflection muscles it needs to do the job. Instead, it will do all the reflection when the context gets used. This makes for a slower runtime, but considerably faster initialization, especially for large numbers of classes.

然而,速度问题的一部分是不可避免的,这就是 JAXB 必须加载您的每一个类的事实,并且类加载很慢.如果您使用相同的配置在第一个上下文之后立即创建第二个上下文,这一点很明显 - 您会发现它已经加载了类,速度要快得多.

However, one part of the speed problem is unavoidable, and that's the fact that JAXB has to load every single one of your classes, and classloading is slow. This is apparent if you create a 2nd context immediately after the first, with the same configuration - you'll see it's much, much faster, having already loaded the classes.

另外,你说你有多个 JAXBContext 实例是因为你有多个上下文路径.您是否意识到可以将多个上下文路径放入单个上下文中?您只需要在初始化上下文时将它们全部作为分号分隔的字符串传递,例如

Also, you say that you have multiple JAXBContext instances because you have multiple contextpaths. Did you realise that you can put multiple context paths into a single context? You just need to pass them all as a semicolon-delimited string when you initialize the context, e.g.

JaxbContext.newInstance("a.b.c:x.y.z");

将加载上下文 a.b.cx.y.z.不过,这可能不会对性能产生任何影响.

will load the contexts a.b.c and x.y.z. It likely won't make any difference to performance, though.

这篇关于JAXBContext 初始化加速?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆