gpt4 book ai didi

java - Java EE 中 ManagedExecutorService 和 ManagedThreadFactory 之间的区别

转载 作者:行者123 更新时间:2023-12-02 09:48:41 24 4
gpt4 key购买 nike

我们当前的旧版 Web 应用程序在其中创建不受应用程序服务器容器管理的线程。我必须用JavaEE多线程标准来修改它。
我的网络应用程序在 Tomcat 上运行良好,但在 Websphere 上失败。
Websphere 上的错误:

... ... Caused by: javax.naming.ConfigurationException: A JNDI operation on a "java:" name cannot be completed because the server runtime is not able to associate the operation's thread with any J2EE application component.  This condition can occur when the JNDI client using the "java:" name is not executed on the thread of a server application request.  Make sure that a J2EE application does not execute JNDI operations on "java:" names within static code blocks or in threads created by that J2EE application.  Such code does not necessarily run on the thread of a server application request and therefore is not supported by JNDI operations on "java:" names.
at com.ibm.ws.naming.java.javaURLContextImpl.throwExceptionIfDefaultJavaNS(javaURLContextImpl.java:534) ~[com.ibm.ws.runtime.jar:?]
at com.ibm.ws.naming.java.javaURLContextImpl.throwConfigurationExceptionWithDefaultJavaNS(javaURLContextImpl.java:564) ~[com.ibm.ws.runtime.jar:?]
at com.ibm.ws.naming.java.javaURLContextImpl.lookupExt(javaURLContextImpl.java:485) ~[com.ibm.ws.runtime.jar:?]
at com.ibm.ws.naming.java.javaURLContextRoot.lookupExt(javaURLContextRoot.java:485) ~[com.ibm.ws.runtime.jar:?]

为了解决这个问题,我引用Concurrency Utilities in Java EE 。我发现 ManagedExecutorService 类似的描述和示例和 ManagedThreadFactory .

  • ManagedExecutorService: A managed executor service is used by applications to execute submitted tasks asynchronously. Tasks areexecuted on threads that are started and managed by the container. Thecontext of the container is propagated to the thread executing thetask.
  • ManagedThreadFactory: A managed thread factory is used by applications to create managed threads. The threads are started andmanaged by the container. The context of the container is propagatedto the thread executing the task. This object can also be used toprovide custom factories for specific use cases (with custom Threads)and, for example, set specific/proprietary properties to theseobjects.

在哪种情况下首选哪一个?为什么?

最佳答案

我已经通过使用 ManagedExecutorService 解决了问题。

ExecutorService 框架确实有更多处理线程的方法,而 ManagedThreadFactory 只能调用 newThread() 方法。

Websphere 问题可以通过使用 ManagedExecutorService 或 ManagedThreadFactory 来解决。两者都有效。但对于进一步的线程处理,ManagedExecutorService 效果要好得多。

现在,此解决方案会导致相同的 Web 应用程序在 Tomcat 上失败。 JNDI 命名异常。根据我的研发,TomEE 服务器支持基于容器的并发,而不是 Tomcat,因此我们必须使用路由机制根据底层应用程序服务器在代码之间切换。

关于java - Java EE 中 ManagedExecutorService 和 ManagedThreadFactory 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56478742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com