【问题标题】:Start, stop, restart Glassfish Server from java desktop application从 Java 桌面应用程序启动、停止、重新启动 Glassfish 服务器
【发布时间】:2014-01-08 11:19:41
【问题描述】:

我正在开发一个需要展示 Web 和桌面应用程序的项目。 Web 应用程序从我的客户那里接收任务并将它们存储(在数据库中)。桌面应用程序(从数据库中)获取任务并一一执行。在我的 Web 应用程序中,我使用 java servlet、Web 服务 ...

有时我的 glassfish 服务器 (v 3.1.2) 会死机,或者他被阻塞,需要重新启动才能继续正常工作。我可以通过监视他来检测这种错误并找出他何时冻结(通过调用引发异常的简单 Web 服务方法、也引发异常的简单 http 请求等)。

我希望我的桌面应用程序获取 Glassfish 服务器状态以及是否

  1. “一切正常”然后“什么都不做”
  2. “服务器已关闭”然后“启动 Glassfish 服务器”
  3. “我检测到错误”然后“重新启动 Glassfish 服务器”
  4. “应用程序退出”然后“关闭 Glassfish 服务器”

有没有人遇到过这个问题并有解决方案。我厌倦了手动重启 glassfish 服务器。

【问题讨论】:

    标签: java glassfish


    【解决方案1】:

    我一次在生产环境中运行 Glassfish 3.1.2 几个月都没有问题。我怀疑您看到的冻结是您部署到它的应用程序的问题。

    我认为最好花时间调查和补救你的悬而未决的问题。发生这种情况时,您是否尝试过对 Glassfish java 进程进行线程转储?

    【讨论】:

    • 问题是我不能调用webservice方法或者http客户端没有收到任何数据。尝试了很多东西,只有重启有效。
    • 这表明您的应用程序正在耗尽 HTTP 线程池。同样,您应该在遇到这种情况时进行线程转储,以便查看发生了什么。
    • 我增加了http线程池,从那以后我就没有问题了,但是如果有时出现,我想重新启动服务器我找出问题后。
    【解决方案2】:

    我找到了自己想要分享的解决方案。

    当我检测到我的 Glassfish 服务器出现问题时,我会重新启动它。此解决方案仅适用于 Linux(如果我发现 Windows 用户类似,我将编辑此答案)。此外,您可能必须在 root 用户下的“/etc/sudoers”中为您的用户添加这一行,adrian 是我的用户名。

    adrian  ALL=(ALL:ALL) ALL
    

    GlassFish 类:(你需要用你的更改 glassfishPath 和 domainName)

        package es.web.glassfish;
    
        import es.os.linux.Konsole;
        import java.io.IOException;
    
        /**
         *
         * @author adrian
         */
        public class Glassfish {
    
        private final static String glassfishPath = "/home/adrian/glassfish-4.0/";
        private final static String domainName = "domain1";
    
        public static String startGlassfishServer() throws IOException, InterruptedException {
            String command = glassfishPath + "bin/asadmin start-domain "+domainName;
            return Konsole.executeCommand(command);
        }
    
        public static String stopGlassfishServer() throws IOException, InterruptedException {
            String command = glassfishPath + "bin/asadmin stop-domain "+domainName;
            return Konsole.executeCommand(command);
        }
    
        public static String restrartGlassfishServer() throws IOException, InterruptedException {
            String command = glassfishPath + "bin/asadmin restart-domain "+domainName;
            return Konsole.executeCommand(command);
        }
    
    }
    

    控制台类:

    package es.os.linux;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    /**
     *
     * @author adrian
     */
    public class Konsole {
    
        static Process process;
        static BufferedReader reader;
    
        public static String executeCommand(String command) throws IOException, InterruptedException {
            String rez = "";
            process = Runtime.getRuntime().exec(command);
            process.waitFor();
            reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                rez += line + "#";
            }
            return rez;
        }
    }
    

    测试类:

    public class test {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args){
            try {
                System.out.println("START");
                System.out.println(Glassfish.startGlassfishServer());
                System.out.println("RESTART");
                System.out.println(Glassfish.restrartGlassfishServer());
                System.out.println("STOP");
                System.out.println(Glassfish.stopGlassfishServer());
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    
    }
    

    测试类输出:

    START
    Waiting for domain1 to start ............#Successfully started the domain : domain1#domain  Location: /home/adrian/glassfish-4.0/glassfish/domains/domain1#Log File: /home/adrian/glassfish-4.0/glassfish/domains/domain1/logs/server.log#Admin Port: 4848#Command start-domain executed successfully.#
    RESTART
    Successfully restarted the domain#Command restart-domain executed successfully.#
    STOP
    Waiting for the domain to stop #Command stop-domain executed successfully.#
    

    【讨论】:

    • 再看我上面的评论。此外,您没有正确使用 Process/Runtime exec API。您正在执行进程的同一线程中读取输入流。这是在看似随机执行时发生线程死锁的好方法。
    • 我知道您的回答很有用,但没有解决我的问题。此解决方案还可以在停止时从桌面应用程序启动 glassfish 服务器。 Restart 命令在服务器关闭时启动服务器,并在启动时重新启动服务器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-08
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多