2014-01-08 154 views
1

我正在开发一个项目,需要一个Web和桌面应用程序。 Web应用程序从我的客户端接收任务并将其存储(在数据库中)。桌面应用程序从数据库中获取任务并逐个执行它们。在我的web应用程序中,我正在使用java servlets,web services ...从java桌面应用程序启动,停止,重新启动Glassfish服务器

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

我想我的桌面应用程序中获得GlassFish服务器状态,如果

  1. “一切正常”,那么“什么都不做”
  2. “服务器关闭”,然后“启动GlassFish服务器”
  3. “我发现一个错误”,然后‘重新启动GlassFish服务器’
  4. ‘应用程序退出’,然后‘关闭GlassFish服务器’

是否anyo ne有这个问题,并有任何解决方案。我厌倦了手动重新启动glassfish服务器。

回答

0

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

当我检测到Glassfish服务器出现问题时,我重新启动它。这个解决方案只适用于Linux(如果我发现Windows用户是simular,我会编辑这个答案)。另外你可能需要在root用户下的“/ etc/sudoers”中为你的用户添加这行代码,adrian是我的用户名。

adrian ALL=(ALL:ALL) ALL 

GlassFish的类别:(U需要改变glassfishPath和域名与你)

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类:

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

再次看到我上面的评论。另外,您没有正确使用Process/Runtime exec API。您正在读取与执行过程相同的线程中的输入流。这是在看似随机执行时发生线程死锁的好方法。 – NBW

+0

我知道你的回答很有用,但并没有解决我的问题。该解决方案还可以在停止时从桌面应用程序启动glassfish服务器。重新启动命令在服务器关闭时启动服务器,并在启动时重新启动服务器。 – AdrianES

3

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

我认为你最好花时间调查和纠正你的悬挂问题。您是否尝试过在发生这种情况时使用Glassfish java进程的线程转储?

+0

问题是要么我不能调用webservice方法或http客户端不会收到任何数据。试了很多东西,只重启了作品。 – AdrianES

+0

这表明您的应用程序正在挨饿HTTP线程池。同样,当你遇到这种情况时,你应该进行线程转储,这样你就可以看到发生了什么。 – NBW

+0

我增加了http线程池,从那时起我没有任何问题,但是如果有时会发生,我想重新启动服务器后,我发现问题出在哪里。 – AdrianES

相关问题