博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wildfly 在 jee war 外部写配置文件
阅读量:4953 次
发布时间:2019-06-12

本文共 4226 字,大约阅读时间需要 14 分钟。

有时需要写属性文件,保存配置值,当然也可以写在数据库。这里我们用文件方式。

最简单做法:

写在wildfly的配置目录里面:

File confDir = new File(System.getProperty("jboss.server.config.dir"));        logger.info("jboss.server.config.dir:" + confDir);        fileProp = new File(confDir, "stats.properties");
package com.italktv.colnv.stat.task;import java.io.File;import java.io.FileInputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.util.Properties;import java.util.logging.Logger;import javax.annotation.PostConstruct;import javax.annotation.Resource;import javax.ejb.ScheduleExpression;import javax.ejb.Singleton;import javax.ejb.Startup;import javax.ejb.Timeout;import javax.ejb.Timer;import javax.ejb.TimerConfig;import javax.ejb.TimerService;import javax.inject.Inject;@Singleton@Startuppublic class CustomTimerService {    @Inject    private Logger logger;    @Resource    private TimerService timerService;    @Inject    private JobStarter job;    File fileProp;    Properties prop;    @PostConstruct    public void initTimer() {        File confDir = new File(System.getProperty("jboss.server.config.dir"));        logger.info("jboss.server.config.dir:" + confDir);        fileProp = new File(confDir, "stats.properties");        prop = read();        setTimber(prop.getProperty("hour", "6"), prop.getProperty("minute", "0"), prop.getProperty("second", "0"));    }    public boolean setTimber(String hour, String minute, String second) {        boolean suc = true;        hour = hour.trim();        minute = minute.trim();        if (minute.equals("*") && hour.equals("*"))            return false;        if (timerService.getTimers() != null) {            for (Timer timer : timerService.getTimers()) {                logger.info("Cancel timer:" + timer.getInfo().toString() + timer.getSchedule().toString());                timer.cancel();            }        }        try {            timerService.createCalendarTimer(new ScheduleExpression().hour(hour).minute(minute).second(second), new TimerConfig("定时统计任务",                    false));        } catch (Exception e) {            e.printStackTrace();            suc = false;        }        if (suc) {            save(hour, minute, second);        }        return suc;    }    private void cancelTimers() {        for (Timer timer : timerService.getTimers()) {            // timer.cancel();        }    }    public String getTimerInfo() {        StringBuffer sb = new StringBuffer();        if (timerService.getTimers() != null) {            for (Timer timer : timerService.getTimers()) {                sb.append("任务每天执行时间:");                sb.append(timer.getSchedule().getHour() + "点" + timer.getSchedule().getMinute() + "分");            }        }        return sb.toString();    }    private void save(String hour, String minute, String second) {        prop.setProperty("hour", hour);        prop.setProperty("minute", minute);        prop.setProperty("second", second);        try {            FileWriter f = new FileWriter(fileProp);            prop.store(f, prop.toString());            f.close();        } catch (IOException e) {            e.printStackTrace();        }    }    private Properties read() {        InputStream in = null;        Properties properties = new Properties();        if (fileProp.exists()) {            try {                in = new FileInputStream(fileProp);                properties.load(in);                logger.info("properties:" + properties.toString());            } catch (IOException e) {                e.printStackTrace();            } finally {                try {                    in.close();                } catch (Exception ignored) {                }            }        } else {            logger.info("not exist:" + fileProp.getAbsolutePath());        }        return properties;    }    @Timeout    public void timeout(Timer timer) {        logger.info("=== job " + " started ====");        String id = job.start();        logger.info("=== job id: " + id);    }}

用cdi的复杂做法:

http://piotrnowicki.com/2012/06/inject-java-properties-in-java-ee-using-cdi/

https://blog.jyore.com/2013/05/jboss-eap6as7wildfly-how-to-use-properties-files-outside-your-archive/

转载于:https://www.cnblogs.com/bigben0123/p/5715443.html

你可能感兴趣的文章
Fireworks基本使用
查看>>
c#线程学习笔记一---基本概念
查看>>
2018-4-13
查看>>
两台电脑间的消息传输
查看>>
Linux 标准 I/O 库
查看>>
Spring Data JPA教程, 第八部分:Adding Functionality to a Repository (未翻译)
查看>>
教练技术的小应用
查看>>
.net Tuple特性
查看>>
Java基础常见英语词汇
查看>>
iOS并发编程笔记【转】
查看>>
泛型 T的定义<1>
查看>>
thinkphp dispaly和fetch的区别
查看>>
08号团队-团队任务5:项目总结会
查看>>
mybatis 插入数据 在没有commit时 获取主键id
查看>>
SQL2005 删除空白行null
查看>>
lightoj 1030 概率dp
查看>>
重新注册.NET
查看>>
Java 内存溢出(java.lang.OutOfMemoryError)的常见情况和处理方式总结
查看>>
Vagrant入门
查看>>
python and 我爱自然语言处理
查看>>