简体   繁体   English

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

[英]Does Websphere respect Daemon threads?

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. 我有点担心想到Websphere 7可能没有关闭它们。

Does anyone know if Websphere 7 treats Daemons threads differently? 有谁知道Websphere 7是否以不同方式处理Daemons线程? (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. 注意:我知道什么不应该手动创建线程,我应该使用WebSphere WorkManager或其他东西,但这个应用程序必须在Tomcat和WebSphere中运行。

I know that I should tie in all threads to some context/shutdown mechanism, this is in progress. 我知道我应该将所有线程绑定到某个上下文/关闭机制,这正在进行中。

Each WAS server runs a single JVM, and daemon threads are tied to the JVM's lifecycle, not the app's lifecycle. 每个WAS服务器都运行一个JVM,守护程序线程与JVM的生命周期相关联,而不是应用程序的生命周期。 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. Java EE规范禁止这样做,并且Java EE容器中的行为与您已经找到的独立Java应用程序不同。 Unfortunately, there is currently no Java EE standard for a WorkManager equivalent; 不幸的是,目前没有WorkManager等效的Java EE标准; however, JSR-236 (Concurrency Utilities for Java EE) may be back as a candidate for inclusion in Java EE 7 . 但是, JSR-236 (Java EE的并发实用程序)可能会成为包含在Java EE 7中的候选者。

In the meantime, on WAS, you can use the asynchronous beans ( WorkManager ). 与此同时,在WAS上,您可以使用异步beanWorkManager )。 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: 但是,由于您还需要在另一个容器(Tomcat)中运行,因此可能还有其他一些选项需要考虑在应用程序中处理并发:

Some other potential options for handling concurrency include the following, but these require EJBs, which may not be available in Tomcat: 处理并发性的其他一些可能选项包括以下内容,但这些选项需要EJB,这些可能在Tomcat中不可用:

Here are a few related threads on the topic of concurrency in Java EE: 以下是Java EE中并发主题的一些相关主题:

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). 将所有线程创建集中在一个类中(称之为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. 例如,如果你的Thread使用循环和Thread.sleep()然后设置stopped = true并中断线程。 The loop should check this and break from the loop when stopped=true. 循环应该检查这个并在stop = true时从循环中断。
  3. Make a listener and implement ServletContextListener. 创建一个监听器并实现ServletContextListener。 When contextDestroyed() is called call ThreadService.stopThreads(). 当调用contextDestroyed()时,调用ThreadService.stopThreads()。 Register this listener in web.xml. 在web.xml中注册此侦听器。

Websphere is just a java application. Websphere只是一个java应用程序。 It cannot respect or do not respect deamon threads that are the feature of JVM or java runtime environment. 它不能尊重或不尊重作为JVM或Java运行时环境特征的deamon线程。 So, if you create deamon thread inside Java EE application it will be deamon in every application server. 因此,如果您在Java EE应用程序中创建deamon线程,它将在每个应用程序服务器中都是deamon。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM