wildfly 在 jee war 外部写配置文件
有时需要写属性文件,保存配置值,当然也可以写在数据库。这里我们用文件方式。
最简单做法:
写在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 @Startup public 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/