Jboss 5、类加载器和多个类实例。

时间:2022-08-08 17:02:34

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 :)

谢谢你的解释:)

3 个解决方案

#1


8  

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类加载器->系统类加载器-> JBoss系统类装入器-> Ear类加载器-> War类加载器。

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类加载器有一个装入的jar,并且ear也被部署在jar a中,那么Bootstrap类Lodaer和ear类装入器将在单独的类装入器中创建两次相同的类。

I would assume (not 100% sure of this) that JBoss 4 didnt come bundled with 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). 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. However you are giving it the QName class from the Bootstrap classloader because of the classloader preferences (parent first and so forth).

我假定(不是100%肯定)JBoss 4没有与javax/xml/名称空间/QName绑定。如果这是真正的JBoss 5,那么它很可能是一个不同的、升级的、版本的Java(4 -> 5或5 -> 6)。但是,由于类加载器首选项(父类等等),您将从Bootstrap类加载器中给它提供QName类。

Since the class types are equal but the class instances are not equal you get a LinkageError

由于类类型是相等的,但是类实例不相等,所以您会得到一个链接错误。

Edit:

编辑:

Just two address the two comments -

只有两个地址,这两个评论。

The class loading behavior as jtahlborn noted is deffinitely different. In normal applications, the system classes like QName will be consistently looked for in the bootstrap classloader. In your error it looks as if javax/xml/datatype/DatatypeConstants is being loaded in org/jboss/classloader/spi/base/BaseClassLoader. Lets assume thats the EAR classloader (or WAR). A quick google shows that is part of the xml-apis' family and possibly jaxp-api.

jtahlborn所指出的类装入行为是完全不同的。在正常的应用程序中,像QName这样的系统类会在引导类加载器中不断地寻找。在您的错误中,看起来好像javax/xml/datatype/ datatype/ datatypecon是在org/jboss/classloader/spi/base/BaseClassLoader中加载的。让我们假设这是EAR classloader(或WAR)。一个快速的谷歌显示,它是xml-api家族的一部分,可能是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. The creation of the QName object though loaded the class from the bootstrap classloader (instead of the EAR). This would happen if the QName class has already been initialized by the system, which it probably has.

因此,代码中的某个地方(或位于EAR的类装入器中的另一个库代码)需要datatypecon——它强制在EAR的类加载器中查找类。虽然从引导类加载器(而不是EAR)加载了类,但是QName对象的创建。如果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. So as jtahlborn noted you would want the children's classloader here to be ignored.

你可以想象,这是不可能发生的。它看起来就像你的父母。因为当在JBoss类加载机制上加载一个类时,如果启用了parent-first,初始的datatypecon将会返回父类(bootstrap)数据类型,而不是childs。因此,正如jtahlborn所指出的,您希望在这里的children的类加载器被忽略。

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. If thats not the case, you can take a look at the class-loading java2ClassLoadingCompliance element that the jboss configuration has.

就该解决方案而言,除非您需要特定原因的依赖项(比如稍微更新的版本较好),否则我将委托给jboss的实现。如果不是这样,您可以查看jboss配置所具有的类加载java2ClassLoadingCompliance元素。

#2


1  

-verbose:class in VM args will give how the Class is getting loaded. If there is duplicate you can remove the conflicting jar/jars.

VM args中的类将给出如何加载类的方法。如果有副本,您可以删除冲突的jar/jar。

#3


0  

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. But even though I am running an older version of JBoss, I still got this error. Anyway, I did extensive research and run maven dependency tree a few times to see where the duplicate definitions are coming from. I was finally able to resolve this error by adding two dependencies - with scope set to provided - to my main pom:
(sun.jaxb-impl 2.1 , javax.jaxb-api 2.2 ) I hope this information helps someone.

我了解到这个问题与JBoss bug有关,它是在5.0.0版本中修复的。但是,即使我运行的是老版本的JBoss,我仍然有这个错误。无论如何,我做了大量的研究并运行maven依赖树,以查看重复定义的来源。我最终能够通过添加两个依赖项来解决这个错误,这两个依赖项将提供给我的主pom (sun)。2.1 jaxb-impl,javax。我希望这些信息能帮助别人。

#1


8  

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类加载器->系统类加载器-> JBoss系统类装入器-> Ear类加载器-> War类加载器。

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类加载器有一个装入的jar,并且ear也被部署在jar a中,那么Bootstrap类Lodaer和ear类装入器将在单独的类装入器中创建两次相同的类。

I would assume (not 100% sure of this) that JBoss 4 didnt come bundled with 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). 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. However you are giving it the QName class from the Bootstrap classloader because of the classloader preferences (parent first and so forth).

我假定(不是100%肯定)JBoss 4没有与javax/xml/名称空间/QName绑定。如果这是真正的JBoss 5,那么它很可能是一个不同的、升级的、版本的Java(4 -> 5或5 -> 6)。但是,由于类加载器首选项(父类等等),您将从Bootstrap类加载器中给它提供QName类。

Since the class types are equal but the class instances are not equal you get a LinkageError

由于类类型是相等的,但是类实例不相等,所以您会得到一个链接错误。

Edit:

编辑:

Just two address the two comments -

只有两个地址,这两个评论。

The class loading behavior as jtahlborn noted is deffinitely different. In normal applications, the system classes like QName will be consistently looked for in the bootstrap classloader. In your error it looks as if javax/xml/datatype/DatatypeConstants is being loaded in org/jboss/classloader/spi/base/BaseClassLoader. Lets assume thats the EAR classloader (or WAR). A quick google shows that is part of the xml-apis' family and possibly jaxp-api.

jtahlborn所指出的类装入行为是完全不同的。在正常的应用程序中,像QName这样的系统类会在引导类加载器中不断地寻找。在您的错误中,看起来好像javax/xml/datatype/ datatype/ datatypecon是在org/jboss/classloader/spi/base/BaseClassLoader中加载的。让我们假设这是EAR classloader(或WAR)。一个快速的谷歌显示,它是xml-api家族的一部分,可能是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. The creation of the QName object though loaded the class from the bootstrap classloader (instead of the EAR). This would happen if the QName class has already been initialized by the system, which it probably has.

因此,代码中的某个地方(或位于EAR的类装入器中的另一个库代码)需要datatypecon——它强制在EAR的类加载器中查找类。虽然从引导类加载器(而不是EAR)加载了类,但是QName对象的创建。如果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. So as jtahlborn noted you would want the children's classloader here to be ignored.

你可以想象,这是不可能发生的。它看起来就像你的父母。因为当在JBoss类加载机制上加载一个类时,如果启用了parent-first,初始的datatypecon将会返回父类(bootstrap)数据类型,而不是childs。因此,正如jtahlborn所指出的,您希望在这里的children的类加载器被忽略。

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. If thats not the case, you can take a look at the class-loading java2ClassLoadingCompliance element that the jboss configuration has.

就该解决方案而言,除非您需要特定原因的依赖项(比如稍微更新的版本较好),否则我将委托给jboss的实现。如果不是这样,您可以查看jboss配置所具有的类加载java2ClassLoadingCompliance元素。

#2


1  

-verbose:class in VM args will give how the Class is getting loaded. If there is duplicate you can remove the conflicting jar/jars.

VM args中的类将给出如何加载类的方法。如果有副本,您可以删除冲突的jar/jar。

#3


0  

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. But even though I am running an older version of JBoss, I still got this error. Anyway, I did extensive research and run maven dependency tree a few times to see where the duplicate definitions are coming from. I was finally able to resolve this error by adding two dependencies - with scope set to provided - to my main pom:
(sun.jaxb-impl 2.1 , javax.jaxb-api 2.2 ) I hope this information helps someone.

我了解到这个问题与JBoss bug有关,它是在5.0.0版本中修复的。但是,即使我运行的是老版本的JBoss,我仍然有这个错误。无论如何,我做了大量的研究并运行maven依赖树,以查看重复定义的来源。我最终能够通过添加两个依赖项来解决这个错误,这两个依赖项将提供给我的主pom (sun)。2.1 jaxb-impl,javax。我希望这些信息能帮助别人。