简体   繁体   English

从Java桌面应用程序启动,停止,重启Glassfish服务器

[英]Start, stop, restart Glassfish Server from java desktop application

I am developing a project that needs booth a web and desktop application. 我正在开发一个需要展示网络和桌面应用程序的项目。 The web app receives the tasks from my clients and stores them (in database). Web应用程序从我的客户端接收任务并存储它们(在数据库中)。 The desktop application gets the tasks (from database) and execute them one by one. 桌面应用程序从数据库中获取任务并逐个执行。 In my web application i am using java servlets, web services ... 在我的Web应用程序中,我正在使用java servlet,Web服务......

Sometimes my glassfish server (v 3.1.2) freezes or he becomes blocked and needs to be restarted so he can continue work properly. 有时我的glassfish服务器(v 3.1.2)会冻结或被阻塞,需要重新启动才能继续正常工作。 I can detect this kind of error by monitoring him and find out when he freezes (by calling simple web service method that throws exception, simple http request that also throws exception etc). 我可以通过监视他并发现他何时冻结(通过调用抛出异常的简单Web服务方法,也引发异常的简单http请求等)来检测这种错误。

I want my desktop application get the Glassfish server status and if 我希望我的桌面应用程序获得Glassfish服务器状态,如果

  1. "Everything is ok" then "Do nothing" “一切都好”然后“什么都不做”
  2. "Server is down" then "Start Glassfish Server" “服务器已关闭”然后“启动Glassfish服务器”
  3. "I detect an error" then "Restart Glassfish Server" “我检测到错误”然后“重启Glassfish服务器”
  4. "Application quit" then "Shutdown Glassfish Server" “应用程序退出”然后“关闭Glassfish服务器”

Does anyone had this problem and has any solution. 有没有人有这个问题,并有任何解决方案。 I am tired of manually restarting the glassfish server. 我厌倦了手动重启glassfish服务器。

I run Glassfish 3.1.2 in production for months at a time without issue. 我在生产中运行Glassfish 3.1.2几个月没有问题。 I would suspect the freezing your are seeing is a problem with the application you have deployed to it. 我怀疑您所看到的冻结是您部署到它的应用程序的问题。

I think you would be best served spending time investigating and remediating your hanging issue. 我认为你最好花时间调查和纠正悬而未决的问题。 Have you tried taking a thread dump of the Glassfish java process when this happens? 当发生这种情况时,您是否尝试过使用Glassfish java进程的线程转储?

I found my own solution that i want to share. 我找到了自己想要分享的解决方案。

When i detect that something is wrong with my Glassfish server, i restart it. 当我检测到Glassfish服务器出现问题时,我重启它。 This solutions only works on Linux (i will edit this answer if i find simular for windows users). 此解决方案仅适用于Linux(如果我找到Windows用户的simular,我将编辑此答案)。 Also u might have to add this line for your user in "/etc/sudoers" under root user, adrian is my username. 此外,您可能需要在root用户下的“/ etc / sudoers”中为您的用户添加此行,adrian是我的用户名。

adrian  ALL=(ALL:ALL) ALL

GlassFish Class: (U will need to change glassfishPath and domainName with yours) 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);
    }

}

Konsole Class: Konsole类:

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;
    }
}

Test Class: 测试类:

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();
        }
    }

}

Test class output: 测试类输出:

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.#

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

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