Start and close Java programs using Commons-daemon

Source: Internet
Author: User

System environment:

CentOS 7 X64

JDK1.8

One:

Installing JSVC

Download Commons-daemon's Source package

Http://apache.fayea.com//commons/daemon/source/commons-daemon-1.0.15-src.zip


Unzip after CD src/native/unix/

Perform:

Export cflags=-m64
Export ldflags=-m64

Note: This 2-sentence code is only required for 64-bit systems

./configure
Make

Once completed, the Jsvc file will be generated in the current directory, copied to a directory, this is the/HOME/CLOUDER/VS/JSVC directory

Then, add the Jsvc_home to the environment variable

Vim. BASHRC

Export JSVC_HOME=/HOME/CLOUDER/VS/JSVC


Two: Write Java code

The startup class must implement the Daemon interface, and then override the start () and the Stop () method to

Package Com.lala;import Org.apache.commons.daemon.daemon;import Org.apache.commons.daemon.daemoncontext;import Org.apache.commons.daemon.daemoninitexception;public class Application implements Daemon{myserver server = null; public void init (Daemoncontext context) throws Daemoninitexception,exception {server = new MyServer ();} public void Start () throws Exception {Server.start ();} public void Stop () throws Exception {Server.stop ();} public void Destroy () {system.exit (0);}}


Note: Pom.xml must include the following dependencies:

<dependency><groupid>commons-daemon</groupid><artifactid>commons-daemon</artifactid ><version>1.0.15</version></dependency>



Three: Writing shell scripts

server.sh

#!/bin/bashif ["$JAVA _home" = ""]; Then  echo "Error:java_home are not set."  Exit 1fiif ["$JSVC _home" = ""]; Then  echo "Error:jsvc_home are not set."  Exit 1fibin= ' DirName "$" ' Export myjetty_home= ' CD $bin/... /; PWD ' myjetty_conf_dir= $MYJETTY _home/confmyjetty_work_dir= $MYJETTY _home/workclasspath= "${myjetty_conf_dir}" for F In $MYJETTY _home/lib/*.jar; Do  Classpath=${classpath}: The directory  where the $f;d onelog_dir=${myjetty_home}/logsclass=com.lala.application#jsvc Daemon_home= $JSVC _homepid=${myjetty_work_dir}/myjetty.pid Case  "$" in  start) $DAEMON _home/jsvc- djava.io.tmpdir= $MYJETTY _work_dir-wait 5000-pidfile $PID-outfile log_dir/myjetty.out-errfile ' &1 '-CP $CLASSPATH $CLASS  exit $?  ;;  Stop)  $DAEMON _home/jsvc-stop-pidfile $PID $CLASS exit $?  ;  *)  echo "Usage jsvc start,stop"  exit 1;; Esac  


Finally, use

SH server.sh start Service

SH server.sh stop shutdown Service

Start and close Java programs using Commons-daemon

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.