Quarkus 类加载器冲突
Quarkus classLoader conflicts
我在尝试构建我的 quarkus 应用程序时遇到错误。
这似乎与以下事实有关,即有两个 class 加载器具有不同的 class 对象 javax/xml/namespace/QName,因为在堆栈跟踪的这一部分中注意到:Caused by: java.lang.LinkageError: loader constraint violation: when resolving field "DATETIME" the class loader (instance of io/quarkus/bootstrap/classloading/QuarkusClassLoader) 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
。
但是我不明白到底是怎么回事,也不知道怎么解决。
有人可以帮忙吗?
这是完整的堆栈跟踪。
2020-07-21 11:25:18,432 ERROR [io.qua.run.boo.StartupActionImpl] (Quarkus Main Thread) Error running Quarkus: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at io.quarkus.runner.bootstrap.StartupActionImpl.run(StartupActionImpl.java:144)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ExceptionInInitializerError
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at io.quarkus.runtime.Quarkus.run(Quarkus.java:60)
at io.quarkus.runtime.Quarkus.run(Quarkus.java:38)
at io.quarkus.runtime.Quarkus.run(Quarkus.java:106)
at io.quarkus.runner.GeneratedMain.main(GeneratedMain.zig:29)
... 6 more
Caused by: java.lang.RuntimeException: Failed to start quarkus
at io.quarkus.runner.ApplicationImpl.<clinit>(ApplicationImpl.zig:190)
... 15 more
Caused by: java.lang.RuntimeException: Failed to initialize Arc
at io.quarkus.arc.Arc.initialize(Arc.java:26)
at io.quarkus.arc.runtime.ArcRecorder.getContainer(ArcRecorder.java:35)
at io.quarkus.deployment.steps.ArcProcessor$generateResources-1120260799.deploy_0(ArcProcessor$generateResources-1120260799.zig:72)
at io.quarkus.deployment.steps.ArcProcessor$generateResources-1120260799.deploy(ArcProcessor$generateResources-1120260799.zig:36)
at io.quarkus.runner.ApplicationImpl.<clinit>(ApplicationImpl.zig:149)
... 15 more
Caused by: java.lang.LinkageError: loader constraint violation: when resolving field "DATETIME" the class loader (instance of io/quarkus/bootstrap/classloading/QuarkusClassLoader) 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
at com.sun.xml.bind.v2.model.impl.RuntimeBuiltinLeafInfoImpl.<clinit>(RuntimeBuiltinLeafInfoImpl.java:254)
at com.sun.xml.bind.v2.model.impl.RuntimeTypeInfoSetImpl.<init>(RuntimeTypeInfoSetImpl.java:33)
at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.createTypeInfoSet(RuntimeModelBuilder.java:98)
at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.createTypeInfoSet(RuntimeModelBuilder.java:54)
at com.sun.xml.bind.v2.model.impl.ModelBuilder.<init>(ModelBuilder.java:132)
at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.<init>(RuntimeModelBuilder.java:62)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:425)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:273)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:109)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1126)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:135)
at com.sun.xml.bind.v2.JAXBContextFactory.createContext(JAXBContextFactory.java:35)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:393)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:691)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:632)
at com.examlpe.middleware.DistributedContextWrapper.<clinit>(DistributedContextWrapper.java:32)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at com.exemple.quarkus.producers.RegisterProducers_ProducerMethod_distributedContextWrapperProducer_43b1afaa6d80f8d1886d6736d01bae86f45d5b94_Bean.<init>(RegisterProducers_ProducerMethod_distributedContextWrapperProducer_43b1afaa6d80f8d1886d6736d01bae86f45d5b94_Bean.zig:44)
at io.quarkus.arc.setup.Default_ComponentsProvider.addBeans2(Default_ComponentsProvider.zig:1115)
at io.quarkus.arc.setup.Default_ComponentsProvider.getComponents(Default_ComponentsProvider.zig:43)
at io.quarkus.arc.impl.ArcContainerImpl.<init>(ArcContainerImpl.java:106)
at io.quarkus.arc.Arc.initialize(Arc.java:20)
... 19 more
2020-07-21 11:25:18,433 ERROR [io.qua.dep.dev.DevModeMain] (main) Failed to start Quarkus: java.lang.RuntimeException: java.lang.RuntimeException: Failed to initialize Arc
at io.quarkus.dev.appstate.ApplicationStateNotification.waitForApplicationStart(ApplicationStateNotification.java:51)
at io.quarkus.runner.bootstrap.StartupActionImpl.runMainClass(StartupActionImpl.java:155)
at io.quarkus.deployment.dev.IsolatedDevModeMain.firstStart(IsolatedDevModeMain.java:95)
at io.quarkus.deployment.dev.IsolatedDevModeMain.accept(IsolatedDevModeMain.java:301)
at io.quarkus.deployment.dev.IsolatedDevModeMain.accept(IsolatedDevModeMain.java:42)
at io.quarkus.bootstrap.app.CuratedApplication.runInCl(CuratedApplication.java:131)
at io.quarkus.bootstrap.app.CuratedApplication.runInAugmentClassLoader(CuratedApplication.java:84)
at io.quarkus.deployment.dev.DevModeMain.start(DevModeMain.java:126)
at io.quarkus.deployment.dev.DevModeMain.main(DevModeMain.java:56)
Caused by: java.lang.RuntimeException: Failed to initialize Arc
at io.quarkus.arc.Arc.initialize(Arc.java:26)
at io.quarkus.arc.runtime.ArcRecorder.getContainer(ArcRecorder.java:35)
at io.quarkus.deployment.steps.ArcProcessor$generateResources-1120260799.deploy_0(ArcProcessor$generateResources-1120260799.zig:72)
at io.quarkus.deployment.steps.ArcProcessor$generateResources-1120260799.deploy(ArcProcessor$generateResources-1120260799.zig:36)
在使用 maven 依赖树工具进一步研究我的依赖关系后,我发现问题是由于 stax-api 依赖关系引起的。这种依赖性被传递地包含在我的项目中。从那以后很难解决这个问题,因为它没有直接包含在我的 poms 中,因此不可见。
我所做的只是从我的 pom 中排除 stax-api 依赖关系,这已经解决了问题。
最终依赖将如下所示:
<dependency>
<groupId>com.example.lib</groupId>
<artifactId>my-lib</artifactId>
<version>${example-lib-version}</version>
<exclusions>
<exclusion>
<groupId>stax</groupId>
<artifactId>stax-api</artifactId>
</exclusion>
</exclusions>
</dependency>
通过 com.example.lib:my-lib 包含 stax api 的依赖项。
这个问题实际上是因为 javax.xml.namespace.QName
是从 QuarkusClassLoader
/RunnerClassLoader
加载的(因为 Quarkus 默认会尝试从 类 加载 类 QuarkusClassLoader
/RunnerClassLoader
首先),而在特定情况下应该从系统 ClassLoader
加载它。
要解决此问题,如果您可以排除此依赖项的所有 类,则确实可以排除导致 LinkageError
的依赖项,否则您可以依赖 属性 quarkus.class-loading.parent-first
指示 Quarkus 它的 类 需要先从父 ClassLoader
加载,然后再尝试从 QuarkusClassLoader
/RunnerClassLoader
.
加载它
quarkus.class-loading.parent-first
在 application.properties
中的用法示例:
quarkus.class-loading.parent-first-artifacts=stax:stax-api
我遇到了与 javax.transaction.xa.XAResource 类似的问题。问题是 JVM 中包含的 类 和外部依赖项(拆分包)总是由不同的类加载器加载(对于开发模式或 quarkus 测试),请参阅 https://quarkus.io/guides/class-loading-reference。通过删除那些拆分包(通过排除如上所述),问题就消失了。据我了解,由于那里的“平面类路径”,这个问题不会出现在生产中。
我在尝试构建我的 quarkus 应用程序时遇到错误。
这似乎与以下事实有关,即有两个 class 加载器具有不同的 class 对象 javax/xml/namespace/QName,因为在堆栈跟踪的这一部分中注意到:Caused by: java.lang.LinkageError: loader constraint violation: when resolving field "DATETIME" the class loader (instance of io/quarkus/bootstrap/classloading/QuarkusClassLoader) 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
。
但是我不明白到底是怎么回事,也不知道怎么解决。
有人可以帮忙吗?
这是完整的堆栈跟踪。
2020-07-21 11:25:18,432 ERROR [io.qua.run.boo.StartupActionImpl] (Quarkus Main Thread) Error running Quarkus: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at io.quarkus.runner.bootstrap.StartupActionImpl.run(StartupActionImpl.java:144)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ExceptionInInitializerError
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at io.quarkus.runtime.Quarkus.run(Quarkus.java:60)
at io.quarkus.runtime.Quarkus.run(Quarkus.java:38)
at io.quarkus.runtime.Quarkus.run(Quarkus.java:106)
at io.quarkus.runner.GeneratedMain.main(GeneratedMain.zig:29)
... 6 more
Caused by: java.lang.RuntimeException: Failed to start quarkus
at io.quarkus.runner.ApplicationImpl.<clinit>(ApplicationImpl.zig:190)
... 15 more
Caused by: java.lang.RuntimeException: Failed to initialize Arc
at io.quarkus.arc.Arc.initialize(Arc.java:26)
at io.quarkus.arc.runtime.ArcRecorder.getContainer(ArcRecorder.java:35)
at io.quarkus.deployment.steps.ArcProcessor$generateResources-1120260799.deploy_0(ArcProcessor$generateResources-1120260799.zig:72)
at io.quarkus.deployment.steps.ArcProcessor$generateResources-1120260799.deploy(ArcProcessor$generateResources-1120260799.zig:36)
at io.quarkus.runner.ApplicationImpl.<clinit>(ApplicationImpl.zig:149)
... 15 more
Caused by: java.lang.LinkageError: loader constraint violation: when resolving field "DATETIME" the class loader (instance of io/quarkus/bootstrap/classloading/QuarkusClassLoader) 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
at com.sun.xml.bind.v2.model.impl.RuntimeBuiltinLeafInfoImpl.<clinit>(RuntimeBuiltinLeafInfoImpl.java:254)
at com.sun.xml.bind.v2.model.impl.RuntimeTypeInfoSetImpl.<init>(RuntimeTypeInfoSetImpl.java:33)
at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.createTypeInfoSet(RuntimeModelBuilder.java:98)
at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.createTypeInfoSet(RuntimeModelBuilder.java:54)
at com.sun.xml.bind.v2.model.impl.ModelBuilder.<init>(ModelBuilder.java:132)
at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.<init>(RuntimeModelBuilder.java:62)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:425)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:273)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:109)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1126)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:135)
at com.sun.xml.bind.v2.JAXBContextFactory.createContext(JAXBContextFactory.java:35)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:393)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:691)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:632)
at com.examlpe.middleware.DistributedContextWrapper.<clinit>(DistributedContextWrapper.java:32)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at com.exemple.quarkus.producers.RegisterProducers_ProducerMethod_distributedContextWrapperProducer_43b1afaa6d80f8d1886d6736d01bae86f45d5b94_Bean.<init>(RegisterProducers_ProducerMethod_distributedContextWrapperProducer_43b1afaa6d80f8d1886d6736d01bae86f45d5b94_Bean.zig:44)
at io.quarkus.arc.setup.Default_ComponentsProvider.addBeans2(Default_ComponentsProvider.zig:1115)
at io.quarkus.arc.setup.Default_ComponentsProvider.getComponents(Default_ComponentsProvider.zig:43)
at io.quarkus.arc.impl.ArcContainerImpl.<init>(ArcContainerImpl.java:106)
at io.quarkus.arc.Arc.initialize(Arc.java:20)
... 19 more
2020-07-21 11:25:18,433 ERROR [io.qua.dep.dev.DevModeMain] (main) Failed to start Quarkus: java.lang.RuntimeException: java.lang.RuntimeException: Failed to initialize Arc
at io.quarkus.dev.appstate.ApplicationStateNotification.waitForApplicationStart(ApplicationStateNotification.java:51)
at io.quarkus.runner.bootstrap.StartupActionImpl.runMainClass(StartupActionImpl.java:155)
at io.quarkus.deployment.dev.IsolatedDevModeMain.firstStart(IsolatedDevModeMain.java:95)
at io.quarkus.deployment.dev.IsolatedDevModeMain.accept(IsolatedDevModeMain.java:301)
at io.quarkus.deployment.dev.IsolatedDevModeMain.accept(IsolatedDevModeMain.java:42)
at io.quarkus.bootstrap.app.CuratedApplication.runInCl(CuratedApplication.java:131)
at io.quarkus.bootstrap.app.CuratedApplication.runInAugmentClassLoader(CuratedApplication.java:84)
at io.quarkus.deployment.dev.DevModeMain.start(DevModeMain.java:126)
at io.quarkus.deployment.dev.DevModeMain.main(DevModeMain.java:56)
Caused by: java.lang.RuntimeException: Failed to initialize Arc
at io.quarkus.arc.Arc.initialize(Arc.java:26)
at io.quarkus.arc.runtime.ArcRecorder.getContainer(ArcRecorder.java:35)
at io.quarkus.deployment.steps.ArcProcessor$generateResources-1120260799.deploy_0(ArcProcessor$generateResources-1120260799.zig:72)
at io.quarkus.deployment.steps.ArcProcessor$generateResources-1120260799.deploy(ArcProcessor$generateResources-1120260799.zig:36)
在使用 maven 依赖树工具进一步研究我的依赖关系后,我发现问题是由于 stax-api 依赖关系引起的。这种依赖性被传递地包含在我的项目中。从那以后很难解决这个问题,因为它没有直接包含在我的 poms 中,因此不可见。 我所做的只是从我的 pom 中排除 stax-api 依赖关系,这已经解决了问题。
最终依赖将如下所示:
<dependency>
<groupId>com.example.lib</groupId>
<artifactId>my-lib</artifactId>
<version>${example-lib-version}</version>
<exclusions>
<exclusion>
<groupId>stax</groupId>
<artifactId>stax-api</artifactId>
</exclusion>
</exclusions>
</dependency>
通过 com.example.lib:my-lib 包含 stax api 的依赖项。
这个问题实际上是因为 javax.xml.namespace.QName
是从 QuarkusClassLoader
/RunnerClassLoader
加载的(因为 Quarkus 默认会尝试从 类 加载 类 QuarkusClassLoader
/RunnerClassLoader
首先),而在特定情况下应该从系统 ClassLoader
加载它。
要解决此问题,如果您可以排除此依赖项的所有 类,则确实可以排除导致 LinkageError
的依赖项,否则您可以依赖 属性 quarkus.class-loading.parent-first
指示 Quarkus 它的 类 需要先从父 ClassLoader
加载,然后再尝试从 QuarkusClassLoader
/RunnerClassLoader
.
quarkus.class-loading.parent-first
在 application.properties
中的用法示例:
quarkus.class-loading.parent-first-artifacts=stax:stax-api
我遇到了与 javax.transaction.xa.XAResource 类似的问题。问题是 JVM 中包含的 类 和外部依赖项(拆分包)总是由不同的类加载器加载(对于开发模式或 quarkus 测试),请参阅 https://quarkus.io/guides/class-loading-reference。通过删除那些拆分包(通过排除如上所述),问题就消失了。据我了解,由于那里的“平面类路径”,这个问题不会出现在生产中。