简体   繁体   中英

Running Java application as a service

We have a java application that's essentially a long running process. It's deployed on JBoss 6.1. We have to start it by going to the 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?

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.

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

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.

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

java service wrapper

I would recommend using Apache Commons Daemon (or maybe YAJSW) to create your own wrapper controller class. 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 :

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. You could also implement your controller class as a Beanshell script so that it need not be a pre-compiled class to run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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