脚本命令加载外部配置文件夹conf详解编程语言

加载log4j配置文件 Log4iConfigurer类

public class Log4iConfigurer { 
     
    private static boolean INITIALIZED = false; 
     
    public static void initLogger(){ 
        if(!INITIALIZED&&!isConfigured()){ 
            InputStream is =Log4iConfigurer.class.getClassLoader().getResourceAsStream("log4j.properties"); 
            PropertyConfigurator.configure(is); 
            IOUtils.closeQuietly(is); 
        } 
    } 
 
    private static boolean isConfigured() { 
        if(LogManager.getRootLogger().getAllAppenders().hasMoreElements()){ 
            return true; 
        }else{ 
            Enumeration<?> loggers =LogManager.getCurrentLoggers(); 
            while(loggers.hasMoreElements()){ 
                Logger logger= (Logger)loggers.nextElement(); 
                if(logger.getAllAppenders().hasMoreElements()){ 
                    return true; 
                } 
            } 
        } 
        return false; 
    } 
     
}

读取配置文件类Util

public class Util { 
     
    private static final Logger logger = LoggerFactory.getLogger(Util.class); 
     
    /**mapping.properties file name*/ 
    public static final Properties props = new Properties(); 
    public static final String CONF_PROPERTIES_FILE= "mapping.properties"; 
    public static final String CONF = "RUN_CONF"; 
     
    static{ 
        Log4iConfigurer.initLogger(); 
        getProperties(); 
    } 
     
    public static String GetString(String key){ 
        String value = null; 
        value = props.getProperty(key); 
        return value; 
    } 
     
    //读取配置文件 
    private static File getConfProperties(){ 
        String confHome = System.getProperty(CONF); 
        if(!StringUtils.isEmpty(confHome)){ 
            logger.info("Use CONF="+confHome); 
            return getPropertiesFile(confHome); 
        } 
         
        logger.warn("Conf property was not set ,will seek conf env variable"); 
         
        String runHome = getRunHome(); 
        if(StringUtils.isEmpty(runHome)){ 
            throw new RuntimeException("didn't find project runpath,please set"); 
        } 
        String path = runHome+File.separator+"conf"; 
        return getPropertiesFile(path); 
    } 
     
    public static void getProperties(){ 
        File propFile = getConfProperties(); 
        if(propFile == null||!propFile.exists()){ 
            logger.info("fail to load properties"); 
            throw new RuntimeException("fail to load properties"); 
        } 
        FileInputStream fis = null; 
        try { 
            fis = new FileInputStream(propFile); 
            props.load(fis); 
        } catch (IOException e) { 
             throw new RuntimeException(e); 
        } finally { 
             IOUtils.closeQuietly(fis); 
        } 
    } 
     
    private static File getPropertiesFile(String path){ 
        if(path == null){ 
            return null; 
        } 
        return new File(path,CONF_PROPERTIES_FILE); 
    } 
     
    private static String getRunHome(){ 
        String runHome = System.getProperty("runpath"); 
        if(StringUtils.isEmpty(runHome)){ 
            logger.warn("run home was not set"); 
        } 
        return runHome; 
    } 
     
     
    public static void main(String[] args) { 
        System.out.println(System.getenv("PATH")); 
        System.out.println(System.getProperty("conf")); 
        System.setProperty("run.home", "/home/dinpay/mappinghome"); 
        System.out.println(System.getProperty("run.home")); 
    } 
     
}

打包导出

   脚本命令加载外部配置文件夹conf详解编程语言

另外附上window和linux的启动脚本命令

startup.cmd

echo start elasticsearch-mapping 
java -Drunpath=C:/Users/ll-t150/Desktop/esmap -Dlog4j.configuration=conf/log4j.properties -cp lib/* com.dinpay.bdp.rcp.service.Start  
pause 

startup.sh

echo start elasticsearch-mapping 
java -Drunpath=/home/dinpay/esmap -Dlog4j.configuration=conf/log4j.properties -classpath .:lib/* com.dinpay.bdp.rcp.service.Start 
echo "create elasticsearch mapping successfully!"

 

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/11178.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论