【问题标题】:How to terminate javaw.exe when jetty web browser is closed关闭码头网络浏览器时如何终止javaw.exe
【发布时间】:2014-05-14 21:45:20
【问题描述】:

我有一个名为 VcctlWrapper.jar 的 javaw 应用程序,它启动嵌入式码头会话、打开浏览器窗口并允许用户将数据输入到 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                   
}

【问题讨论】:

  • 如果你不想打开任务管理器,你可以从你的控制台调用taskkill /f /im javaw.exe。这不是一个解决方案,但比打开任务管理器要快。
  • 同意,它更快。但是我有这个应用程序的用户,他们不想“在幕后”做任何事情。他们会期望当 Web 浏览器关闭时进程会被终止。

标签: java embedded-jetty


【解决方案1】:

SessionListener 怎么样,当它检测到会话被破坏时,它会退出进程 (http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpSessionListener.html#sessionDestroyed(javax.servlet.http.HttpSessionEvent))

我知道这不会立即发生,只要浏览器关闭后会话持续,它就会持续,但这是我能想到的最好的了

【讨论】:

  • 谢谢。这似乎是一个很好的解决方案,但我不知道如何开始。有没有关于如何做到这一点的分步教程?我可以简单地添加到上面的代码中吗?
  • 好的,在我的脑海中,像这样,在 web.xml 添加 com.mydomain.SessionListener 然后创建实现 javax.servlet.http.HttpSessionListener 的 com.mydomain.SessionListener 类并覆盖 public void sessionDestroyed(HttpSessionEvent httpSessionBindingEvent) 方法,执行 System.exit(0);例如。我还没有测试过,但我想它应该可以工作,请告诉我。
  • 哦,别忘了将 web.xml 中的会话超时设置为合理的值 3
  • 嗨@JeffBullard 有用吗?也许其他人可以从您的反馈中受益
猜你喜欢
  • 2012-10-30
  • 2015-04-23
  • 2015-01-07
  • 1970-01-01
  • 2011-05-22
  • 2012-08-22
  • 2013-09-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多