Adr*_*fie 7 java jaxb forkjoinpool
我使用 jaxb 进行解组,但是当我使用 ForkJoinPoolexecute() 方法时,我得到一个“java.log.ClassNotFoundException:com.sun.xml.internal.bind.v2.ContextFactory”,但我确信存在在我的运行时的类路径中,因为当我不使用 ForkJoinPool 时它可以正常工作......你知道这个问题或解决方法吗?
我使用Java 11
我的代码:
ForkJoinPool commonPool = ForkJoinPool.commonPool();
commonPool.execute(() -> {
try {
String messageFileContent = Files.readString(Paths.get(
this.getClass().getClassLoader().getResource("xml-to-process.xml").getPath()));
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
// avoiding schema validation for more performance
jaxbUnmarshaller.setSchema(null);
UpdateWorkOrder updateWorkOrder = (UpdateWorkOrder) jaxbUnmarshaller.unmarshal(new StringReader(messageFileContent));
} catch (Exception e) {
e.printStackTrace();
}
});
Run Code Online (Sandbox Code Playgroud)
这很奇怪不是...?在ForkJoinPool的execute()之外,解析是正确执行的。
错误是:
javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory]
java.base/java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(Unknown Source)
at java.base/java.util.concurrent.ForkJoinTask.doExec(Unknown Source)
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(Unknown Source)
at java.base/java.util.concurrent.ForkJoinPool.scan(Unknown Source)
at java.base/java.util.concurrent.ForkJoinPool.runWorker(Unknown Source)
at java.base/java.util.concurrent.ForkJoinWorkerThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
at javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:92)
at javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:125)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:230)
Run Code Online (Sandbox Code Playgroud)
虽然 NightShade 的答案完全描述了问题,但让我添加一些细节。
Servlet API 世界和并发世界非常不兼容:前者假设所有计算都在同一个线程上完成(ServletRequest#startAsync是一个相对较新的添加)。因此,使用 Servlet API 的应用程序和库通常将对象附加到当前Thread,例如:
RequestContextHolder到当前线程(如ThreadLocal),CurrentInstance,这意味着每当您想在另一个线程上执行某些操作时,您需要将所有这些对象复制到目标线程上。Runnable一个简单的方法是为您的(和)编写一个包装器Callable<T>,它在开始执行之前设置环境:
public class WrappedRunnable implements Runnable {
private final ClassLoader ccl;
... // other objects
private final Runnable runnable;
public static Runnable wrap(final Runnable runnable) {
if (runnable instanceof WrappedRunnable) {
return runnable;
}
return new WrappedRunnable(runnable);
}
public WrappedRunnable(final Runnable runnable) {
this.ccl = Thread.currentThread().getContextClassLoader();
... // other objects
this.runnable = runnable;
}
@Override
public void run() {
final ClassLoader oldCcl = Thread.currentThread().getContextClassLoader();
... // save the value of other objects
try {
Thread.currentThread().setContextClassLoader(ccl);
... // set the value of other objects
runnable.run();
} finally {
Thread.currentThread().setContextClassLoader(oldCcl);
// restore the value of other objects
}
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以编写自己的ExecutorService(您实际上正在使用ForkJoinPoolas ExecutorService)或ForkJoinPool,它将Runnable自动包装 s 。
如果 ForkJoinPool 使用与您的应用程序不同的类加载器,特别是如果您使用 Tomcat 等 Web 容器,则可能会发生这种情况。
您可以尝试为 ForkJoinPool 设置线程工厂,并在该工厂中,将创建的线程的上下文类加载器设置为正确的应用程序类加载器。