读书人

ActiveMQ 跟Commons-Daemon整合

发布时间: 2012-11-05 09:35:12 作者: rapoo

ActiveMQ 和Commons-Daemon整合

??????????? 在一般的java项目中,如果在linux中实现进程在后台执行比较简单,可以采用nohup cmd 或者sh cmd &等多种方式实现,进行在后台执行。但是在window操作系统中就比较困难了,需要实现相关的类。如在tomcat,jboss的启动之后,端口一直开放着。

????????? 常用实现方式实现Commons-Daemon中的Daemon接口实现相关的方式,使用相关的bat,命令启动。在Commons-Deamon调用jsvc实现。

?

在activemq.bat中相关bat命令如下:

?

备注:在run.jar包中Main方法调用?ActiveMQLauncher 实现相关的功能。

?

package org.apache.activemq.console;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import org.apache.activemq.console.command.Command;import org.apache.activemq.console.command.ShutdownCommand;import org.apache.activemq.console.command.StartCommand;import org.apache.activemq.console.CommandContext;import org.apache.activemq.console.Main;import org.apache.activemq.console.formatter.CommandShellOutputFormatter;import org.apache.commons.daemon.Daemon;import org.apache.commons.daemon.DaemonContext;/** * This class launches activemq under Apache JSVC {@link http://commons.apache.org/daemon/jsvc.html} *  * @author areese *  */public class ActiveMQLauncher implements Daemon {    private List<String> args;    /**     *      */    public ActiveMQLauncher() {    }    /*     * (non-Javadoc)     *      * @see org.apache.commons.daemon.Daemon#destroy()     */    public void destroy() {    }    /*     * (non-Javadoc)     *      * @see     * org.apache.commons.daemon.Daemon#init(org.apache.commons.daemon.DaemonContext     * )     */    public void init(DaemonContext arg0) throws Exception {        // we need to save the args we started with.        args = Arrays.asList(arg0.getArguments());    }    /*     * (non-Javadoc)     *      * @see org.apache.commons.daemon.Daemon#start()     */    public void start() throws Exception {        CommandContext context = new CommandContext();        context.setFormatter(new CommandShellOutputFormatter(System.out));        Command command = new StartCommand();        command.setCommandContext(context);        command.execute(args);    }    /*     * (non-Javadoc)     *      * @see org.apache.commons.daemon.Daemon#stop()     */    public void stop() throws Exception {        CommandContext context = new CommandContext();        context.setFormatter(new CommandShellOutputFormatter(System.out));        Command command = new ShutdownCommand();        command.setCommandContext(context);        List<String> tokens = new ArrayList<String>(Arrays.asList(new String[] {                "--jmxlocal", "--all", }));        command.execute(tokens);    }}

?

?Daemon that implements the following methods:

void init(String[] arguments): Here open configuration files, create a trace file, create ServerSockets, Threads void start(): Start the Thread, accept incoming connections void stop(): Inform the Thread to terminate the run(), close the ServerSockets void destroy(): Destroy any object created in init()

读书人网 >编程

热点推荐