简体   繁体   English

我怎样才能使阻塞的Java线程等待Tomcat可互操作

[英]How can I make a blocking Java thread waiting for Tomcat interuptable

I have embedded Tomcat inside a JavaFX application. 我已经在JavaFX应用程序中嵌入了Tomcat。 I need Tomcat to run for the duration of my application lifespan. 我需要Tomcat在我的应用程序生命周期内运行。

Thread.stop() is deprecated, so I'm trying to use another method, but this always gives me an exception Thread.stop()已弃用,因此我尝试使用另一种方法,但这总是给我一个异常

"Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException" on the line 74: tomcat.wait(500);.

 Caused by: java.lang.IllegalMonitorStateException
        at java.lang.Object.wait(Native Method)
        at webappgen.TomcatThread.run(WebServer.java:74)
        at webappgen.WebServer.Start(WebServer.java:22)
        at webappgen.Project.StartWebServer(Project.java:31)
        at webappgen.FXMLDocumentController.handleNewProjectAction(FXMLDocumentController.java:81)
        ... 56 more

Here is what I'm trying: 这是我正在尝试的:

package webappgen;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;


public class WebServer {

    TomcatThread thread;

    public WebServer(){

    }

    public void Start(String dir){

        thread = new TomcatThread(dir);
        thread.run();

    }

    public void Stop(){
        synchronized(TomcatThread.obj){
            thread.interrupt();
        }
    }
}


class TomcatThread extends Thread {
    public static final Object obj = new Object();
    private String dir;
    private volatile boolean stopServer = false;

    public void StopServer(boolean stop){
        this.stopServer = stop;
    }
    public TomcatThread(String dir){
        this.dir = dir;
    }
    @Override
    public void run() {
        String webappDirLocation = dir;
        Tomcat tomcat = new Tomcat();

        String webPort = "8083";
        tomcat.setPort(Integer.valueOf(webPort));

        try {
            tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());
        } catch (ServletException ex) {
            Logger.getLogger(WebServer.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("configuring app with basedir: " + new File(webappDirLocation).getAbsolutePath());

        try {
            tomcat.start();
        } catch (LifecycleException ex) {
            Logger.getLogger(WebServer.class.getName()).log(Level.SEVERE, null, ex);
        }

        synchronized(TomcatThread.obj){
            while(!stopServer){

                try {
                     tomcat.wait(500);

                } catch (InterruptedException ex) {
                       Logger.getLogger(TomcatThread.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        }
        try {
            tomcat.getServer().stop();
            tomcat.getServer().destroy();
        } catch (LifecycleException ex) {
            Logger.getLogger(TomcatThread.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
};

在线程启动后,使用Thread.join()等待TomcatThread死掉。

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

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