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/
wildfly 在 jee war 外部写配置文件的更多相关文章
- JBoss 系列十四:JBoss7/WildFly如何加载外部的文件或properties文件
http://www.tuicool.com/articles/M7ZR3y 原文 http://blog.csdn.net/kylinsoong/article/details/12623997 主 ...
- Spring使用外部的配置文件
在使用Spring做web项目的时候,通常会使用到数据库的连接信息 jdbcUrl driverClass username password 那么应该如何使用这些属性呢? 如在Spring中使用数据 ...
- Maven将依赖包、jar/war包及配置文件输出到指定目录
使用Maven插件将依赖包 jar包 war包及配置文件输出到指定目录 写在前面 最近遇到一个朋友遇到一个项目需要将 maven 的依赖包和配置文件分开打包然后用脚本执行程序.这样的好处在于可以随 ...
- vue-cli3.0读取外部化配置文件来修改公共路径
之前我写过一篇通过nginx配置代理转发的博客,正常来说也是正确的,但不足之处在了甲方还用了F5负载均衡和gateway来代理转发.所以之前我认为的请求->nginx转发代理->后端服务, ...
- $Django importlib与dir知识,手写配置文件, 配置查找顺序 drf分页器&drf版本控制
1 importlib与dir知识 # importlib简介动态导入字符串模块 # 常规导入 from ss.aa import b from ss import a print(b,type(b ...
- jar包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法
jar包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法 用系统属性System.getProperty("user.dir")获得执行命令的目录(网上 ...
- PHP正则配合写配置文件导致Getshell
PHP正则配合写配置文件导致Getshell,偶然间看到的一个题目, p 牛的小密圈的一个问题. 分析一下,漏洞代码: index.php <?php $str=addslashes($_GET ...
- Maven War包 POM配置文件
如何为你的Web程序(war包设定配置文件) 约定 http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering ...
- 【Java Web开发学习】Spring加载外部properties配置文件
[Java Web开发学习]Spring加载外部properties配置文件 转载:https://www.cnblogs.com/yangchongxing/p/9136505.html 1.声明属 ...
随机推荐
- Android之捕获TextView超链接
应该是好久没有写有关技术类的文章了,今天分享一篇捕获TextView超链接的文章,希望对大家有所帮助,我终于在歪路上回归正途了.这个捕获TextView超链接应该算是比较常用吧,如果你会了,就不用看了 ...
- SQL-Server下载地址
有同学费尽心思的找SQL server数据库各版本的下载地址,看到别人的求助贴就不自觉的想去帮助他们,但是一个一个去帮助又不太现实,毕竟个人精力有限,既然大家有需求,那么艾薇百科今天就本着乐于分享和奉 ...
- win7 IIS 部署-vs2012开发网站-全是问题啊。。。
1.文件夹权限everyone2.aspnet_regiis.exe -i 表现为:
- 3-cd 命令总结
- Linux下解决用户不能执行sudo的方法
报错: xxx is not in the sudoers file. This incident will be reported. Linux默认没有为当前用户开启sudo权限! $ su $ ...
- MyBatis学习--延迟加载
简介 在resultMap可以实现高级映射(使用association.collection实现一对一及一对多映射),association.collection具备延迟加载功能.例如:我们查询订单并 ...
- AsyncTask异步任务类使用学习
new MyAsyncTask() .execute("http://pic.baike.soso.com/p/20120716/bki-20120716095331-640956396.j ...
- leetcode(一)Word Pattern
题目描述: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...
- 一起学HTML基础-CSS样式表-基本概念、分类、选择器
一.基本概念: CSS (Cascading Style Sheets)层叠样式表,是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言. ...
- selenium 3 对我们的影响
The major change in Selenium 3.0 is we're removing the original Selenium Core implementation and rep ...