Does Websphere respect Daemon threads?

廉价感情. 提交于 2019-12-23 13:22:43

问题


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.


回答1:


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:

  • CommonJ WorkManager
  • Servlet 3.0 Asynchronous Servlets
  • ServletContextListener to hook into the web app lifecycle

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

  • EJB 3.0 Timer Service
  • EJB 3.1 Asynchronous Beans

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

  • Replacing Websphere's WorkManager in JBoss?
  • Getting thread from Container?



回答2:


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.



回答3:


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.



来源:https://stackoverflow.com/questions/9274222/does-websphere-respect-daemon-threads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!