如何在关闭jetty Web浏览器时终止javaw.exe

时间:2014-05-14 21:45:20

标签: java embedded-jetty

我有一个名为VcctlWrapper.jar的javaw应用程序,它启动一个嵌入式jetty会话,打开浏览器窗口并允许用户将数据输入到derby数据库等。该应用程序当前正在使用此命令启动:

start javaw -classpath "lib/derby.jar:lib/derbytools.jar" -jar "VcctlWrapper.jar"

目前,当用户完成应用程序时,终止javaw.exe应用程序的唯一方法似乎是打开任务管理器并手动结束关联的进程。我想找到一种方法让javaw.exe在用户关闭Web浏览器时自动终止。这可能吗?

构成VcctlWrapper的源代码非常简短,所以我将它全部包含在这里:

package vcctl;

import java.awt.*;
import java.net.*;
import java.nio.file.Path;   
import java.nio.file.Paths;
import java.io.IOException;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.handler.*;
import org.eclipse.jetty.webapp.*;


public class webServiceRunner extends javax.swing.JFrame {

    private URI uri;
    private Desktop desktop;
    private Desktop.Action action = Desktop.Action.OPEN;

    /** Creates new form webServiceRunner */
    public webServiceRunner() {
        // initComponents();

        try {
            startJettyServer();
        } catch (Exception ex) {

        }

        try {
            if (Desktop.isDesktopSupported()) {
                desktop = Desktop.getDesktop();
                try {
                   uri = new URI( "http://localhost:8080/vcctl" );
                } catch (URISyntaxException urisex) {
                }
                desktop.browse( uri );
            }

        } catch (IOException ioex) {
        }

    }


/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(0, 152, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(0, 63, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>                        

protected static void startJettyServer() throws Exception {
    Server server = new Server(8080);
    WebAppContext webapp = new WebAppContext();
    Path currentRelativePath = Paths.get("");
    String s = currentRelativePath.toAbsolutePath().toString();
    System.out.println("Current relative path is: " + s);
    String warPath = s + "/vcctl.war";
    webapp.setContextPath("/vcctl");
    webapp.setWar(warPath);
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[]{ webapp, new DefaultHandler()});
    server.setHandler(handlers);
    server.start();
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new webServiceRunner().setVisible(false);

        }
    });
}

// Variables declaration - do not modify                     
// End of variables declaration                   
}

1 个答案:

答案 0 :(得分:1)

SessionListener如果检测到会话被销毁,它会退出进程(http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpSessionListener.html#sessionDestroyed(javax.servlet.http.HttpSessionEvent)

我知道这不会立竿见影,只要会话在浏览器关闭后就会持续,但这是我能想到的最好的