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.声明属 ...
随机推荐
- mutex与semaphore的区别
网摘1:Mutex 的发音是 /mjuteks/ ,其含义为互斥(体),这个词是Mutual Exclude的缩写.Mutex在计算机中是互斥也就是排他持有的一种方式,和信号量-Semaphore有可 ...
- mysql中导入txt文件
1 windows 下 mysql导入txt文件(使用mysql的workbench) load data local infile 'path' into table table_name fiel ...
- Spring Boot 连接MySql数据库
Spring Boot 以后也许会成为入门Spring的首选! 记一下Spring Boot 成功连接Mysql数据库的方法步骤! 一.新建Maven工程,不全Maven所需文件夹,在pom.xml引 ...
- Form表单提交的简要方式
<html> <head> <meta name="viewport" content="width=device-width" ...
- C#中的System.Speech命名空间初探
本程序是口算两位数乘法,随机生成两个两位数,用语音读出来.然后开启语音识别,接受用户输入,知道答案正确关闭语音识别.用户说答案时,可以说“再说一遍”重复题目. 关键是GrammarBuilder和Ch ...
- 在Ubuntu 14.04安装Nginx
###介绍 Nginx是世界上最流行的网络服务器中的一种,负责托管网络上一些流量最高的网站.在多数情况下,Nginx比Apache在资源上更加友好,可以作为网络服务器或反向代理服务器. 这篇教程中,我 ...
- 我的第一个jsp程序-实现注册登录留言功能
1,注册功能,包括两个页面 zhuce.jsp注册页面 <%@ page language="java" contentType="text/html; chars ...
- 一、项目基础架构(附GitHub地址)——以ABP为基础架构的一个中等规模的OA开发日志
前言: 最近园子里ABP炒的火热.看了几篇对于ABP的介绍后,深感其设计精巧,实现优雅.个人感觉,ABP或ABP衍生品的架构设计,未来会成为中型Net项目的首选架构模式.如果您还不了解ABP是什么,有 ...
- 区间DP HDU 4283
t个数据 n个权值 1->n 可以入栈调整顺序 花费 第k个出来 w[i]*(k-1); 求花费最少 #include<stdio.h> #include<string.h&g ...
- js学习笔记1---使用方法
1.获取对象: a) 通过名称来获取元素:document.getElementById() //属于静态方法,方法前面只能用document b) document.getElementsByCla ...