简体   繁体   English

带有Jersey和Tomcat的Java Web Services。 ¿当Web服务停止时如何停止所有线程?

[英]Java Web Services with Jersey and Tomcat. ¿how to stop all the threads when the web service is stopped?

I need some kind of help to see the way to solve this problem. 我需要某种帮助,以了解解决此问题的方法。

I have a java webservice using jersey in a tomcat server. 我在tomcat服务器中使用jersey的Java Web服务。 My java code launches some threads for some tasks. 我的Java代码为某些任务启动了一些线程。 All works fine. 一切正常。 The problem is that when the webservice is stopped on the manager panel of the tomcat server, the threads continue his execution. 问题是,当Web服务在tomcat服务器的管理器面板上停止时,线程继续执行它。

Exists a way to stop all the threads of this kind of java jersey webservice when the user press the Stop button on the manager panel of the tomcat server? 当用户按下tomcat服务器管理器面板上的“停止”按钮时,是否存在一种停止此类java jersey webservice的所有线程的方法?

Thanks 谢谢

PD: i know that exists a interrupt() function for the threads in java, but i dont know how to launch that function when the user press the stop button in the manager panel of the server. PD:我知道在Java中存在针对线程的interrupt()函数,但是当用户按下服务器管理器面板中的停止按钮时,我不知道如何启动该函数。

You can try setting the threads to deamon mode. 您可以尝试将线程设置为守护进程模式。 You can use the method setDaemon() within Thread to do this. 您可以在Thread中使用setDaemon()方法来执行此操作。 The Java Virtual Machine exits when the only threads running are all daemon threads. 当仅运行的所有线程都是守护程序线程时,Java虚拟机将退出。 It could be that the threads you created are user threads and hence the JVM does not exit. 您创建的线程可能是用户线程,因此JVM不会退出。

http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setDaemon(boolean) http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setDaemon(boolean)

I do have software that does not explicitly do this yet I do not have this problem so it could be the case that your threads do not know that they should stop running. 我确实有没有明确执行此操作的软件,但是我没有这个问题,因此可能是您的线程不知道它们应该停止运行的情况。 You can fix this by having some sort of control flag that lets your threads know that they can stop now. 您可以通过具有某种控制标志来让线程知道它们现在可以停止来解决此问题。 Using an AtomicBoolean works well for this. 为此,使用AtomicBoolean效果很好。 If you are using Tomcat you can create a servlet to act as your main root to launch and control your thread. 如果使用的是Tomcat,则可以创建一个servlet作为启动和控制线程的主根。 Tomcat promises to run your servlet's destroy() method upon shut down, which can properly stop your thread from running. Tomcat承诺在关闭时运行servlet的destroy()方法,这可以适当地阻止线程运行。 Here is a rudimentary example of this. 这是一个简单的例子。

@WebServlet(urlPatterns = {"/checkStatus"}, loadOnStartup = 1)
public class ThreadController extends HttpServlet {

    private AtomicBoolean running = new AtomicBoolean(false);
    private Thread myThread = new Thread() {

        @Override
        public void run() {
            while (running.get()) {
                //do stuff
            }
        }

    };

@Override
public void destroy() {
    running.set(false);
    myThread.interrupt();
}

@Override
public void init() throws ServletException {
    running.set(true);
    myThread.start();
}

}

Though in my implementation I use Executors to launch and interrupt my threads, but you can look that up further on your own. 尽管在我的实现中,我使用Executors启动和中断线程,但是您可以自己进一步查找。

Another option is to use a ScheduledExecutorService. 另一种选择是使用ScheduledExecutorService。

 private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
private ScheduledFuture<?> cleanUpHandle;


@Override
public void init() {
    cleanUpHandle = scheduler.scheduleAtFixedRate(cleanUp, 60, 60, TimeUnit.SECONDS);
}

@Override
public void destroy() {
    cleanUpHandle.cancel(true);
}

In my example above cleanUp is a Thread implementation. 在上面的示例中,cleanUp是一个Thread实现。 The good thing about using the scheduler is that you do not need a control while loop in your thread's run method. 使用调度程序的好处是,您不需要在线程的run方法中进行while循环的控件。 The run method just does one iteration of its task and finishes. run方法仅执行其任务的一个迭代并完成。

These are just a few things you can try and the right solution will be based on what you are trying to accomplish. 这些只是您可以尝试的几件事,正确的解决方案将基于您要实现的目标。

With Web service context listener WSServletContextListener and configure it in your web.xml and write the logic to stop all your threads in contextDestroyed Method. 使用Web服务上下文侦听器WSServletContextListener并在web.xml对其进行配置,并编写逻辑以在contextDestroyed方法中停止所有线程。 Clicking the Stop in Tomcat Manager will send an ServletContextEvent event to the 在Tomcat Manager中单击“停止”会将ServletContextEvent事件发送到

public void contextDestroyed(ServletContextEvent event)

Here, you can hijack the event and kill all your threads and release all your resources.That's the best place to stop your threads. 在这里,您可以劫持事件并杀死所有线程并释放所有资源,这是停止线程的最佳位置。 Your web.xml should add the listener as follows. 您的web.xml应该如下添加侦听器。 Ex : 例如:

      <listener>
//Fully qualified listener class name implementing javax.servlet.ServletContextListener
       <listener-class>XXX.XXX.XXX.YourListenerClass</listener-class>
     </listener>

Hope this gives an idea! 希望这能给您一个想法!

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

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