Websphere 是否尊重守护进程线程?

发布于 2025-01-04 22:53:06 字数 276 浏览 5 评论 0原文

我有一个创建大量守护线程的应用程序,我希望每个线程在应用程序关闭时关闭。

我有点担心 Websphere 7 可能不会将它们全部关闭。

有谁知道Websphere 7是否以不同的方式对待守护进程线程? (我知道应该这样做)

注意: 我知道什么不应该手动创建线程,并且我可能应该使用 WebSphere WorkManager 或其他东西,但这个应用程序必须在 Tomcat 和 WebSphere 中运行。

我知道我应该将所有线程绑定到某个上下文/关闭机制,这正在进行中。

I've got an app that creates a load of Daemon threads, I'd like each one to shut down when the app is shut down.

I'm a little worried thought that Websphere 7 might not be shutting them all down.

Does anyone know if Websphere 7 treats Daemons threads differently? (I know it should do)

Note:
I know what shouldn't create threads manually, and that I should probably use WebSphere WorkManager or something, but this app has to run in Tomcat and WebSphere.

I know that I should tie in all threads to some context/shutdown mechanism, this is in progress.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

触ぅ动初心 2025-01-11 22:53:06

每个 WAS 服务器运行一个 JVM,守护线程与 JVM 的生命周期相关,而不是与应用程序的生命周期相关。因此,您不应期望在应用程序停止时关闭任何守护线程。

正如您已经指出的,您不应该手动创建线程; Java EE 规范禁止这样做,并且 Java EE 容器中的行为与您已经发现的独立 Java 应用程序不同。不幸的是,目前还没有 WorkManager 等效项的 Java EE 标准;但是,JSR-236(Java EE 并发实用程序)可能会作为包含在 Java EE 7 中的候选者。

同时,在 WAS 上,您可以使用 异步 Bean (WorkManager)。我们已经成功地使用此方法将线程与应用程序生命周期联系起来。

但是,由于您还需要在另一个容器(Tomcat)中运行,因此可能有一些其他选项可以考虑处理应用程序中的并发性:

其他一些处理并发的潜在选项包括以下内容,但这些需要 EJB,而 Tomcat 中可能不可用:

以下是一些关于 Java EE 并发性主题的相关主题:

Each WAS server runs a single JVM, and daemon threads are tied to the JVM's lifecycle, not the app's lifecycle. Therefore, you should not expect any daemon threads to be shut down when your app stops.

As you've already indicated, you should not create threads manually; the Java EE specs forbid this and the behavior in a Java EE container is different than a standalone Java application as you've already found. Unfortunately, there is currently no Java EE standard for a WorkManager equivalent; however, JSR-236 (Concurrency Utilities for Java EE) may be back as a candidate for inclusion in Java EE 7.

In the meantime, on WAS, you can use the asynchronous beans (WorkManager). We have successfully used this method to tie threads to the application lifecycle.

However, since you need to run in another container as well (Tomcat), there may be some other options to consider handling concurrency in your applications:

Some other potential options for handling concurrency include the following, but these require EJBs, which may not be available in Tomcat:

Here are a few related threads on the topic of concurrency in Java EE:

岁月染过的梦 2025-01-11 22:53:06

正如已经提到的,你不应该这样做,但没有一个好的方法可以做到这一点。这并没有给我带来任何问题。

这种方法需要集中创建线程,并使用侦听器在应用程序停止时终止线程。

您必须做一些事情:

  1. 将所有线程创建集中在一个类中(将其称为 ThreadService)。当此处创建线程时,将其放入列表中,以便稍后可以循环遍历列表以停止所有线程。
  2. 创建一个您的线程实现的接口,允许您通过同一接口停止每个线程。您拥有的每个线程都必须实现自己的机制来处理此问题。例如,如果您的线程使用循环和Thread.sleep(),则设置stopped=true并中断线程。循环应该检查这一点,并在 stop=true 时从循环中中断。
  3. 创建一个侦听器并实现 ServletContextListener。当 contextDestroyed() 被调用时,调用 ThreadService.stopThreads()。在 web.xml 中注册此侦听器。

As has been mentioned you're not supposed to do this, but there isn't a good way to do it. This hasn't caused any problems for me.

This approach requires centralized thread-creation and the use of a listener to terminate threads when the app is stopping.

You'll have to do a few things:

  1. Centralize all thread creation in a single class (call it ThreadService). When a thread is created here put it in a list so you can later loop through the list to stop them all.
  2. Make an interface that your threads implement that allows you to stop each thread via the same interface. Each thread you have has to implement it's own mechanism for handling this. For example if your Thread uses a loop and Thread.sleep() then set stopped=true and interrupt the thread. The loop should check this and break from the loop when stopped=true.
  3. Make a listener and implement ServletContextListener. When contextDestroyed() is called call ThreadService.stopThreads(). Register this listener in web.xml.
笑梦风尘 2025-01-11 22:53:06

Websphere 只是一个java 应用程序。它不能尊重或不尊重作为 JVM 或 java 运行时环境特征的守护线程。因此,如果您在 Java EE 应用程序中创建守护线程,它将在每个应用程序服务器中成为守护线程。

此外,据我所知,即使您创建常规线程,它也不会阻止应用程序服务器关闭:每个应用程序服务器的关闭机制都会尝试关闭其所有组件,并最终运行 System.exit() 来赢得手动打开线程的犯罪分子:)。

Websphere is just a java application. It cannot respect or do not respect deamon threads that are the feature of JVM or java runtime environment. So, if you create deamon thread inside Java EE application it will be deamon in every application server.

Moreover as far as I know even if you create regular thread it will not prevent application server from shutting down: the shutdown mechanism of every application server tries to close all its components and in the end runs System.exit() to win the criminals :) that open threads manually.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文