[英]Running Java application as a service
We have a java application that's essentially a long running process. 我们有一个Java应用程序,它实际上是一个长期运行的过程。 It's deployed on JBoss 6.1. 它已部署在JBoss 6.1上。 We have to start it by going to the url http://ip.ip.ip.ip:8080/MyApp/Monitor
我们必须通过转到URL http://ip.ip.ip.ip:8080/MyApp/Monitor
来启动它
Is there a way we could run it as a service via the cli with some ability to start / stop / restart as needed? 有没有一种方法可以通过cli将它作为服务运行,并具有根据需要启动/停止/重新启动的功能?
If it is the only application running on the JBoss instance, you could configure your JBoss to automatically start you application when it starts and then follow the steps in http://community.jboss.org/wiki/JBossSystemService to run JBoss as a service. 如果它是在JBoss实例上运行的唯一应用程序,则可以将JBoss配置为在启动时自动启动应用程序,然后按照http://community.jboss.org/wiki/JBossSystemService中的步骤运行JBoss即服务。
If you have other applications on the JBoss instance or you want to start/stop only the application and not the whole server, you could write a Java application that connects to your JBoss instance remotely using Java Management Extensions and use the JMX Beans provided by JBoss to start/stop your application. 如果您在JBoss实例上有其他应用程序,或者只想启动/停止该应用程序而不是整个服务器,则可以编写一个Java应用程序,该Java应用程序使用Java Management Extensions远程连接到您的JBoss实例,并使用JBoss提供的JMX Bean。启动/停止您的应用程序。 More about JBoss JMX interface can be found in http://docs.jboss.org/jbossas/jboss4guide/r2/html/ch2.chapter.html I know this is of JBoss 4 but I don't think they dropped JMX support in newer versions. 有关JBoss JMX接口的更多信息,请参见http://docs.jboss.org/jbossas/jboss4guide/r2/html/ch2.chapter.html。我知道这是JBoss 4的,但我不认为他们放弃了JBoss 4的支持。较新的版本。
Make it a console application? 使其成为控制台应用程序吗?
public static void main(String [] args)
{
doStuffThatMyAppMonitorNormallyDoes();
}
Fire a call to your application directly from the console using the java
command. 使用java
命令直接从控制台向应用程序调用。
Example : 范例 :
public class MonitorService{
..
public static void main(String[] args) {
if(args[0].equalsIgnoreCase("-start")
{
//Do start routine
}
else if(args[0].equalsIgnoreCase("-stop")
{
//Do stop routine
}
}
...
}
Run the program as the follows. 如下运行程序。
Java MonitorService -start
You can turn it to windows/unix service using 您可以使用以下方法将其转换为Windows / unix服务
I would recommend using Apache Commons Daemon (or maybe YAJSW) to create your own wrapper controller class. 我建议使用Apache Commons Daemon(或也许是YAJSW)来创建自己的包装控制器类。 An example of this is shown here . 这里显示了一个示例。 The wrapper controller extends the Daemon class of Commons-Daemon and it could be ran on the command line in the form of : 包装器控制器扩展了Commons-Daemon的Daemon类,可以在命令行上以以下形式运行:
java -cp . WrapperController.class -windowsStop
java -cp . WrapperController.class -windowsStart
java -cp . WrapperController.class -verify
You could also of course, extend the example to have its own SysTray object. 当然,您也可以将示例扩展为具有自己的SysTray对象。 You could also implement your controller class as a Beanshell script so that it need not be a pre-compiled class to run. 您还可以将您的控制器类实现为Beanshell脚本,这样它就不必是预编译的类即可运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.