linux系统服务的脚本文件都存放在/etc/init.d目录中,要把hornetq以系统服务方式运行,需要在/etc/init.d目录新建脚本hornetq
cd /etc/init.d vi hornetq -------------------- #!/bin/bash # chkconfig: 2345 10 90 # description: Starts and Stops the Hornetq daemon. HORNETQ_HOME=/usr/local/hornetq-2.1.0.Final HORNETQ_START=$HORNETQ_HOME/bin/run.sh HORNETQ_STOP=$HORNETQ_HOME/bin/stop.sh # necessary environment variables export export JAVA_HOME=/usr/java/latest # check for hornetq script if [ ! -f $HORNETQ_HOME/bin/run.sh ] then echo "Hornetq not valilable..." exit fi start(){ echo "Starting Hornetq... " cd $HORNETQ_HOME/bin nohup $HORNETQ_START & sleep 5 echo "Hornetq Started. " echo } stop(){ echo "Shutting down Hornetq... " cd $HORNETQ_HOME/bin $HORNETQ_STOP sleep 1 echo "Hornetq Stoped." echo } restart(){ stop sleep 1 start } status(){ ps ax | grep "[o]rg.hornetq.integration.bootstrap.HornetQBootstrapServer hornetq-beans.xml" | awk '{printf $1 " "}' | wc | awk '{print $2}' > /tmp/hornetq_process_count.txt read line < /tmp/hornetq_process_count.txt if [ $line -gt 0 ]; then echo -n "Hornetq ( pid " ps ax | grep "[o]rg.hornetq.integration.bootstrap.HornetQBootstrapServer hornetq-beans.xml" | awk '{printf $1 " "}' echo -n ") is running..." echo else echo "Hornetq is stopped" fi } case "$1" in start) start ;; stop) stop ;; restart) restart ;; status) status ;; *) echo "Usage: hornetq {start|stop|restart|status}" exit 1 esac exit 0 -------------------- chmod 755 hornetq
执行service hornetq start/stop/restart/status就可以对hornetq进行[启动/结束/重启/查看状态]操作。
注意:在RedHat中出现
*********************************************************************************** java -XX:+UseParallelGC -XX:+AggressiveOpts -XX:+UseFastAccessorMethods -Xms512M -Xmx1024M -Dhornetq.config.dir=../config/stand-alone/non-clustered -Djava.util.logging.config.file=../config/stand-alone/non-clustered/logging.properties -Djava.library.path=. -classpath ../lib/netty.jar:../lib/jnpserver.jar:../lib/jnp-client.jar:../lib/jboss-mc.jar:../lib/jboss-jms-api.jar:../lib/hornetq-logging.jar:../lib/hornetq-jms.jar:../lib/hornetq-jms-client.jar:../lib/hornetq-jboss-as-integration.jar:../lib/hornetq-core.jar:../lib/hornetq-core-client.jar:../lib/hornetq-bootstrap.jar:../config/stand-alone/non-clustered:../schemas/ org.hornetq.integration.bootstrap.HornetQBootstrapServer hornetq-beans.xml *********************************************************************************** Warning: -XX:+UseParallelGC not understood. Ignoring. Warning: -XX:+AggressiveOpts not understood. Ignoring. Warning: -XX:+UseFastAccessorMethods not understood. Ignoring. Warning: -Xms512M not understood. Ignoring. Warning: -Xmx1024M not understood. Ignoring. Exception in thread "main" java.lang.ClassFormatError: org.hornetq.integration.bootstrap.HornetQBootstrapServer (erroneous method access flags) at _Jv_ClassReader.throw_class_format_error(byte) (/usr/lib/libgcj.so.5.0.0) at _Jv_ClassReader.handleMethod(int, int, int, int) (/usr/lib/libgcj.so.5.0.0) at _Jv_ClassReader.read_methods() (/usr/lib/libgcj.so.5.0.0) at _Jv_ClassReader.parse() (/usr/lib/libgcj.so.5.0.0) at _Jv_DefineClass(java.lang.Class, byte[], int, int) (/usr/lib/libgcj.so.5.0.0) at java.lang.VMClassLoader.defineClass(java.lang.ClassLoader, java.lang.String, byte[], int, int, java.security.ProtectionDomain) (/usr/lib/libgcj.so.5.0.0) at java.lang.ClassLoader.defineClass(java.lang.String, byte[], int, int, java.security.ProtectionDomain) (/usr/lib/libgcj.so.5.0.0) at java.security.SecureClassLoader.defineClass(java.lang.String, byte[], int, int, java.security.CodeSource) (/usr/lib/libgcj.so.5.0.0) at java.net.URLClassLoader.findClass(java.lang.String) (/usr/lib/libgcj.so.5.0.0) at gnu.gcj.runtime.VMClassLoader.findClass(java.lang.String) (/usr/lib/libgcj.so.5.0.0) at java.lang.ClassLoader.loadClass(java.lang.String, boolean) (/usr/lib/libgcj.so.5.0.0) at _Jv_FindClass(_Jv_Utf8Const, java.lang.ClassLoader) (/usr/lib/libgcj.so.5.0.0) at java.lang.Class.forName(java.lang.String, boolean, java.lang.ClassLoader) (/usr/lib/libgcj.so.5.0.0) at java.lang.Class.forName(java.lang.String) (/usr/lib/libgcj.so.5.0.0) at gnu.gcj.runtime.FirstThread.run() (/usr/lib/libgcj.so.5.0.0) at _Jv_ThreadRun(java.lang.Thread) (/usr/lib/libgcj.so.5.0.0) at _Jv_RunMain(java.lang.Class, byte const, int, byte const, boolean) (/usr/lib/libgcj.so.5.0.0) at __gcj_personality_v0 (/root/webx/hornetq/hornetq-2.1.0.Final/bin/java.version=1.4.2) at __libc_start_main (/lib/tls/libc-2.3.4.so) at _Jv_RegisterClasses (/root/webx/hornetq/hornetq-2.1.0.Final/bin/java.version=1.4.2)
原因是RedHat中默认的jdk为1.4.2,通过服务方式启动hornetq使用的是默认的jdk,而我们配置的JAVA_HOME=/usr/java/latest并没有起作用,因此需要在$HORNETQ_HOME/bin/run.sh中显式得使用/usr/java/latest/bin/java命令来代替java命令,更改后run.sh内容如下:
#!/bin/sh
JAVAHOME=/usr/java/latest
export HORNETQ_HOME=..
mkdir -p ../logs
# By default, the server is started in the non-clustered standalone configuration
if [ a"$1" = a ]; then CONFIG_DIR=$HORNETQ_HOME/config/stand-alone/non-clustered; else CONFIG_DIR="$1"; fi
if [ a"$2" = a ]; then FILENAME=hornetq-beans.xml; else FILENAME="$2"; fi
export CLASSPATH=$CONFIG_DIR:$HORNETQ_HOME/schemas/
#you can use the following line if you want to run with different ports
#export CLUSTER_PROPS="-Djnp.port=1099 -Djnp.rmiPort=1098 -Djnp.host=localhost -Dhornetq.remoting.netty.host=localhost -Dhornetq.remoting.netty.port=5445"
export JVM_ARGS="$CLUSTER_PROPS -XX:+UseParallelGC -XX:+AggressiveOpts -XX:+UseFastAccessorMethods -Xms512M -Xmx1024M -Dhornetq.config.dir=$CONFIG_DIR -Djava.util.logging.config.file=$CONFIG_DIR/logging.properties -Djava.library.path=."
#export JVM_ARGS="-Xmx512M -Djava.util.logging.config.file=$CONFIG_DIR/logging.properties -Dhornetq.config.dir=$CONFIG_DIR -Djava.library.path=. -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
for i in `ls $HORNETQ_HOME/lib/*.jar`; do
CLASSPATH=$i:$CLASSPATH
done
echo "***********************************************************************************"
echo "java $JVM_ARGS -classpath $CLASSPATH org.hornetq.integration.bootstrap.HornetQBootstrapServer $FILENAME"
echo "***********************************************************************************"
$JAVAHOME/bin/java $JVM_ARGS -classpath $CLASSPATH -Dcom.sun.management.jmxremote org.hornetq.integration.bootstrap.HornetQBootstrapServer $FILENAME