使用daemon直接启动非web项目

1、编写基础服务类,实现Daemon init 、start 、stop

package com.allinfinance.yak.support.service;

import java.io.FileNotFoundException;

import java.util.Iterator;

import java.util.Map;

import org.apache.commons.daemon.Daemon;

import org.apache.commons.daemon.DaemonContext;

import org.apache.commons.daemon.DaemonInitException;

import org.apache.commons.lang.StringUtils;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.util.Log4jConfigurer;

import cn.webank.rmb.api.RMB;

import com.allinfinance.yak.support.future.TaskExecutor;

/**

 * 基础的服务Daemon. 可以带一个参数,指定在classpath中加载的spring配置文件名,程序会自动加上 "-context.xml"后缀

 * 

 * @author licj

 *

 */

public class ServiceDaemon implements Daemon {

private ConfigurableApplicationContext ctx;

@Override

public void init(DaemonContext context) throws DaemonInitException,

Exception {

initLogging();

ctx = new ClassPathXmlApplicationContext(

getContextFilename(context.getArguments()));

ctx.registerShutdownHook();

}

private static void initLogging() throws FileNotFoundException {

//默认调用classpath下的log4j.properties,这样可以有效的避免因为xml优先的原因,被某些jar包里的log4j.xml给覆盖了。

//相当于主动调整默认配置文件加载优先级

String logLocation = System.getProperty("log.config");

if (StringUtils.isNotBlank(logLocation))

Log4jConfigurer.initLogging(logLocation, 1000 * 60);

else

Log4jConfigurer.initLogging("classpath:log4j.properties");

}

@Override

public void start() throws Exception {

}

@Override

public void stop() throws Exception {

RMB.stopWait();

//关闭所有的任务执行器

Map<String,TaskExecutor> taskExecutorMap=ctx.getBeansOfType(TaskExecutor.class);

if(null!=taskExecutorMap){

Iterator<TaskExecutor> taskExecutorIter=taskExecutorMap.values().iterator();

while(taskExecutorIter.hasNext()){

TaskExecutor taskExecutor=taskExecutorIter.next();

if(null==taskExecutor){

continue;

}

taskExecutor.shutdownWait();

}

}

}

@Override

public void destroy() {

ctx.close();

}

/**

* 这样可以直接跑,要关闭就杀掉进程

* @param args

* @throws FileNotFoundException 

*/

public static void main(String[] args) throws FileNotFoundException {

initLogging();

@SuppressWarnings("resource")

ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(

getContextFilename(args));

ctx.registerShutdownHook();

}

public static String getContextFilename(String args[]) {

String filename = "/service-context.xml";

if (args.length >= 1 )

filename = "/" + args[0] + "-context.xml";

return filename;

}

protected ConfigurableApplicationContext getCtx() {

return ctx;

}
 

}

2、在debug/run configuration中配置启动,如图



 

 

猜你喜欢

转载自godsend-jin.iteye.com/blog/2221907