我有一个运行我的Java应用程序(Jar)的Linux服务器,我使用命令ps -ef | grep 'myapp.jar'
检查这个并获得2行输出,表明我的应用已启动并运行。
现在我们有一个Web UI应用程序。我想在UI中显示状态消息,例如"我的应用程序是Up / Down"取决于我的jar在linux服务器上运行或停止。
我如何实现这一目标?
答案 0 :(得分:0)
您可以使用jcmd
。
假设myapp.jar引导类是com.myapp.Main
,那么这段代码可以帮助你:
Runtime rt = Runtime.getRuntime();
String[] commands = {"jcmd"};
Process proc = rt.exec(commands);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
// read the output from the command
String s = null;
while ((s = stdInput.readLine()) != null) {
String[] out = s.split("\\s");
if("com.myapp.Main".equals(out[1])) {
System.out.println("is running");
}
}
答案 1 :(得分:0)
由于您没有提到您正在运行的应用程序服务器,我只是继续并假设Apache Tomcat。如果不是这样,这个答案绝对没用。
如果您已启用Tomcat Manager或愿意启用它,则可以list currently running applications从中获取。
运行一个应用程序的Tomcat服务器“myWebApp”以及几个默认应用程序的示例:
OK - Listed applications for virtual host localhost
/:running:0:ROOT
/myWebApp:running:0:myWebApp
/examples:running:0:examples
/host-manager:running:0:host-manager
/jsp:running:0:jsp
/manager:running:1:manager
/docs:running:0:docs
为了使用Tomcat Manager,您还需要在Tomcat中至少启用一个具有适当角色的用户。在Tomcat 7中,所需角色为manager-script
,因此您必须在$CATALINA_HOME/conf/tomcat-users.xml
中添加以下行,以便添加名为“status”且密码为“password”的用户(请将这些更改为更合理的内容) ):
<role rolename="manager"/>
<user username="status" password="password" roles="manager-script"/>
启用了能够查询Tomcat Manager的用户后,您只需要求它为您提供所有正在运行的应用程序的状态。解析文本模式输出非常简单,可以为您提供一个工作示例:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.StringBuilder;
import java.net.URL;
import java.net.URLConnection;
import java.util.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TomcatWebappStatus {
// This is cooked together from examples found in:
// http://stackoverflow.com/questions/4328711
// http://stackoverflow.com/questions/4883100
private String getAppStatus(String url,
String app,
String user,
String pass) throws Exception {
BufferedReader in;
Matcher m;
Pattern p = Pattern.compile("^/\\w+:(\\w+):(\\d+):" + app + "$");
String auth = user + ":" + pass;
String resp = "ERROR (application " + app + " not found)";
URL website = new URL(url);
URLConnection connection;
connection = website.openConnection();
if (user != null && user.length() > 0 &&
pass != null && pass.length() > 0) {
connection.setRequestProperty("Authorization",
"Basic " + Base64.getEncoder().encodeToString(auth. getBytes()));
}
in = new BufferedReader( new InputStreamReader(
connection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
m = p.matcher(inputLine);
if (m.find()) resp = m.group(1);
}
in.close();
return resp.toString();
}
public static void main(String[] argv) throws Exception {
TomcatWebappStatus t = new TomcatWebappStatus();
String tomcatManagerURL = "http://localhost:8080/manager/text/list";
String appName = argv[0];
String tomcatManagerUsername = argv[1];
String tomcatManagerPassword = argv[2];
String s = t.getAppStatus(tomcatManagerURL,
appName,
tomcatManagerUsername,
tomcatManagerPassword);
System.out.println("App \"" + appName + "\" is " + s);
}
}
如果您从上面的示例中取出字节码,您应该能够在本地针对Tomcat 7运行它(至少,也可以与其他版本一起使用)。
如果您的Web UI恰好是使用Java制作的,那么您可以将该示例插入其中。在javascript的情况下,查询/manager/text/list
并解析其输出应该可以解决问题。