简体   繁体   English

Jboss 5,类加载器和多个类实例

[英]Jboss 5, classloader and multiple class instances

I had a problem with my application. 我的申请有问题。 To resume the problem, I had to do migrate an application from jboss 4 to jboss 5. 要恢复这个问题,我必须将应用程序从jboss 4迁移到jboss 5。

During the war deployement, I had this error: 在战争部署期间,我遇到了这个错误:

java.lang.LinkageError: loader constraint violation: when resolving field "DATETIME"
the class loader (instance of org/jboss/classloader/spi/base/BaseClassLoader) of the referring class,
javax/xml/datatype/DatatypeConstants, and the class loader (instance of <bootloader>)
for the field's resolved type, javax/xml/namespace/QName,
have different Class objects for that type

After many searchs, I found this error was here because I had several time the same class in differents packages. 经过多次搜索,我发现这个错误在这里是因为我在不同的包中有过几次相同的类。 Once in a dependency package (from my pom.xml) and once provided by jboss. 一旦进入依赖包(来自我的pom.xml)并且一次由jboss提供。

So, to resolve this problem, I have give a scope "provided" for my dependency. 因此,为了解决这个问题,我为我的依赖项提供了“提供”范围。

But I don't understand why this solution works. 但我不明白为什么这个解决方案有效。 I thought it works to have several time the same class in an application. 我认为在应用程序中有几次同一个类是有效的。 I know it's not a good thing, but with jboss 4, it's work. 我知道这不是一件好事,但是对于jboss 4来说,这是有用的。

Someone can explain me why it works with jboss 4 and not with jboss 5. 有人可以解释为什么它适用于jboss 4而不适用于jboss 5。

Thanks for your explanation :) 谢谢你的解释:)

What you are seeing is the effect of an application server loading JBoss' libraries and EAR libraries in separate classloaders 您所看到的是应用程序服务器在单独的类加载器中加载JBoss库和EAR库的效果

You can think of an EAR's class loader hierarchy similar (but not necessarily) to : 您可以将EAR的类加载器层次结构视为类似(但不一定):

Bootstrap ClassLoader -> System Class Loader -> JBoss System Class Loader -> Ear Class Loader -> War Class Loader. Bootstrap ClassLoader - > System Class Loader - > JBoss System Class Loader - > Ear Class Loader - > War Class Loader。

Where war class loader's parent is the ear class loader and so forth. war类加载器的父级是ear类加载器等等。

Now if Bootstrap ClasssLoader has a jar A loaded and the ear is also being deployed with jar A, Bootstrap Class Lodaer and Ear Class Loader will have the same class created twice in separate class loaders. 现在,如果Bootstrap ClasssLoader加载了jar A并且还使用jar A部署了ear,则Bootstrap Class Lodaer和Ear Class Loader将在单独的类加载器中创建两次相同的类。

I would assume (not 100% sure of this) that JBoss 4 didnt come bundled with javax/xml/namespace/QName. 我认为(不是100%确定这一点)JBoss 4没有与javax / xml / namespace / QName捆绑在一起。 If that is true JBoss 5 is more then likely a different, upgraded, version of Java (4 -> 5 or 5 -> 6). 如果这是真的,JBoss 5可能更像是一个不同的,升级的Java版本(4 - > 5或5 - > 6)。 As a result (with the new JBoss 5), when you try to pass javax/xml/namespace/QName into one of your classes, it is expecting the class from the ear. 因此(使用新的JBoss 5),当您尝试将javax / xml / namespace / QName传递到其中一个类时,它会期待该类。 However you are giving it the QName class from the Bootstrap classloader because of the classloader preferences (parent first and so forth). 但是,由于类加载器首选项(父级首先等等),您将从Bootstrap类加载器为其提供QName类。

Since the class types are equal but the class instances are not equal you get a LinkageError 由于类类型相等但类实例不相等,因此会出现LinkageError

Edit: 编辑:

Just two address the two comments - 只有两个评论两个评论 -

The class loading behavior as jtahlborn noted is deffinitely different. jtahlborn指出的类加载行为是绝对不同的。 In normal applications, the system classes like QName will be consistently looked for in the bootstrap classloader. 在普通应用程序中,将在引导程序类加载器中一致地查找像QName这样的系统类。 In your error it looks as if javax/xml/datatype/DatatypeConstants is being loaded in org/jboss/classloader/spi/base/BaseClassLoader. 在您的错误中,看起来好像在org / jboss / classloader / spi / base / BaseClassLoader中加载了javax / xml / datatype / DatatypeConstants。 Lets assume thats the EAR classloader (or WAR). 让我们假设这是EAR类加载器(或WAR)。 A quick google shows that is part of the xml-apis' family and possibly jaxp-api. 快速谷歌节目是xml-apis'家族的一部分,可能是jaxp-api。

So somewhere in your code (or another libraries code located in the EAR's class loader) needed DatatypeConstants - which forced the lookup of the class in the EAR's classloader. 因此,代码中的某个位置(或位于EAR类加载器中的其他库代码)需要DatatypeConstants - 这会强制在EAR的类加载器中查找类。 The creation of the QName object though loaded the class from the bootstrap classloader (instead of the EAR). 虽然从引导类加载器(而不是EAR)加载了类,但是创建了QName对象。 This would happen if the QName class has already been initialized by the system, which it probably has. 如果QName类已经被系统初始化了,那么就会发生这种情况。

This as you can imagine isn't suppose to happen. 你可以想象这不会发生。 It actually looks like you have parent-last. 它实际上看起来像你有父母的最后一个。 Because when loading a class off the JBoss class loading mechanism, had parent-first been enabled, the initial DatatypeConstants would have returned the parent's (bootstrap) DatatypeConstants and not the childs. 因为在从JBoss类加载机制加载类时,如果已启用parent-first,则初始DatatypeConstants将返回父级(引导程序)DatatypeConstants而不是子级。 So as jtahlborn noted you would want the children's classloader here to be ignored. 因此,jtahlborn指出,你会希望忽略孩子们在这里的类加载器。

As far as the resolution goes, unless you need the dependencies for a specific reason (like a slightly newer version is better then the current) I would delegate to the jboss's implementation. 就解决方案而言,除非您因特定原因需要依赖项(比较新版本比当前更好),我将委托给jboss的实现。 If thats not the case, you can take a look at the class-loading java2ClassLoadingCompliance element that the jboss configuration has. 如果不是这种情况,您可以查看jboss配置所具有的class-loading java2ClassLoadingCompliance元素。

-verbose:class in VM args will give how the Class is getting loaded. -verbose:class VM args中的类将给出如何加载Class。 If there is duplicate you can remove the conflicting jar/jars. 如果有重复,您可以删除冲突的jar / jar。

Thank you for the explanation - it was very helpful. 谢谢你的解释 - 这非常有帮助。

I learned that this issue is related to a JBoss bug which was fixed in a 5.0.0 release. 我了解到这个问题与在5.0.0版本中修复的JBoss错误有关。 But even though I am running an older version of JBoss, I still got this error. 但即使我运行的是旧版本的JBoss,我仍然遇到了这个错误。 Anyway, I did extensive research and run maven dependency tree a few times to see where the duplicate definitions are coming from. 无论如何,我做了大量研究并运行maven依赖树几次,以查看重复定义的来源。 I was finally able to resolve this error by adding two dependencies - with scope set to provided - to my main pom: 我终于能够通过向我的主pom添加两个依赖项(将范围设置为提供)来解决此错误:
(sun.jaxb-impl 2.1 , javax.jaxb-api 2.2 ) I hope this information helps someone. (sun.jaxb-impl 2.1,javax.jaxb-api 2.2)我希望这些信息有助于某人。

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

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