java动态加载配置文件
最近项目中需要做定时任务,即定时数据库的备份。定时时间用户可以在界面中配置,要求配置修改好立即生效。
想不到什么好办法。下面是一种实现思路
把用户配置的时间存到properties配置文件中,定时任务每隔一分钟执行一次,每次执行前都会去读取配置文件,如果配置的时间与当前时间一致,则执行任务,否则什么也不做。
之前做的时候,加载配置文件的方法如下
ClassLoader classLoader = this.getClass().getClassLoader();
Properties prop = new Properties();
prop.load(classLoader.getResourceAsStream("/dbbak.properties"));
发现修改了.properties后,即使重新执行,读入的仍为修改前的参数。
此问题的原因在于ClassLoader.getResourceAsStream读入后,会将.properties保存在缓存中,重新执行时会从缓存中读取,而不是再次读取.properties文件。
解决办法就是用如下方法
String fileStr = Thread.currentThread().getContextClassLoader()
.getResource("").toString();
String file = fileStr.substring(5, fileStr.length())
+ "Config.properties";
InputStream in = new FileInputStream(new File(file));
Properties prop = new Properties();
prop.load(in);
下面是测试中完整代码
package org.lkm.db.time;
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Properties; import java.util.Timer; import java.util.TimerTask;
public class Dbbak {
public static void showTimer() throws Exception { TimerTask task = new TimerTask() { @Override public void run() { try { if(Dbbak.isRun()){ System.out.println("任务开始执行了"); }else{ //System.out.println("时间未到"); } } catch (Exception e) { e.printStackTrace(); } } }; String fileStr = Thread.currentThread().getContextClassLoader() .getResource("").toURI().getPath(); String file = fileStr+ "dbbak.properties"; InputStream in = new FileInputStream(new File(file)); Properties prop = new Properties(); prop.load(in); String time = prop.get("time").toString();
//设置执行时间 Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH);//每天 //定制每天的....执行, String t[] = time.split(":"); calendar.set(year, month, day, Integer.parseInt(t[0]), Integer.parseInt(t[1]), 00); Date date = calendar.getTime(); Timer timer = new Timer(); int period = 60 * 1000; //每天的date时刻执行task,每隔2秒重复执行 timer.schedule(task, date, period); }
public static void main(String[] args) throws Exception { new Dbbak().showTimer(); } public static boolean isRun() throws Exception{ String fileStr = Thread.currentThread().getContextClassLoader() .getResource("").toURI().getPath(); System.out.println(fileStr); String file = fileStr + "dbbak.properties"; InputStream in = new FileInputStream(new File(file)); Properties prop = new Properties(); prop.load(in); String time = prop.get("time").toString(); System.out.println(time); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm-ss"); String str = sdf.format(new Date()); if(str.split("-")[0].equals(time)){ return true; } return false; }
}
java动态加载配置文件的更多相关文章
- java动态加载配置文件(申明:来源于网络)
java动态加载配置文件 地址:http://blog.csdn.net/longvs/article/details/9361449
- log4j和log4j2怎么动态加载配置文件
应用场景与问题 当项目在运行时,我们如果需要修改log4j 1.X或者log4j2的配置文件,一般来说我们是不能直接将项目停止运行再来修改文件重新部署的.于是就有这样一个问题:如何在不停止当前项目的运 ...
- NGINX的启停命令、以及动态加载配置文件的命令
-- 启动(不推荐):在nginx目录下有一个sbin目录,sbin目录下有一个nginx可执行程序../nginx -- 启动(指定配置文件,推荐)/usr/local/nginx/sbin/ngi ...
- java动态加载jar文件
public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, Invo ...
- java动态加载类和静态加载类笔记
JAVA中的静态加载类是编译时刻加载类 动态加载类指的是运行时刻加载类 二者有什么区别呢 举一个例子 现在我创建了一个类 实现的功能假设为通过传入的参数调用具体的类和方法 class offic ...
- Java动态加载类在功能模块开发中的作用
Java中我们一般会使用new关键字实例化对象然后调用该对象所属类提供的方法来实现相应的功能,比如我们现在有个主类叫Web类这个类中能实现各种方法,比如用户注册.发送邮件等功能,代码如下: /* * ...
- JAVA动态加载JAR包的实现
如何动态的加载这些驱动!不可能把所有的数据库驱动都集成到JAR包中吧?!于是动态加载驱动的JAR包就产生了!其实这些在做系统基础代码时,经常用到,只是一般我们没有机会去搞而已. 动态加载JAR包,使用 ...
- Java动态加载jar及class文件
经常碰到需要动态加载jar及class文件的场景.Java类由于需要加载和编译字节码,动态加载class文件较为麻烦,但JDK仍提供了一整套方法来动态加载jar文件和class文件. 一.动态加载ja ...
- java动态加载jar包,并运行其中的类和方法
动态加载jar包,在实际开发中经常会需要用到,尤其涉及平台和业务的关系的时候,业务逻辑部分可以独立出去交给业务方管理,业务方只需要提供jar包,就能在平台上运行. 下面通过一个实例来直观演示: 第一: ...
随机推荐
- 怎样选择PHP的版本
原文:怎样选择PHP的版本 IIS 如果想使用IIS配置PHP的话,那么需要选择Non-Thread Safe(NTS)版本的PHP Apache 如果你是用的Apache的版本来自Apache Lo ...
- 安卓MonkeyRunner源码分析之工作原理架构图及系列集合
花了点时间整理了下MonkeyRunner的工作原理图,请配合本人博客里面MonkeyRunner其他源码分析文章进行阅读.下面整理成相应系列列表方便大家阅读: MonkeyRunner源码分析之-谁 ...
- Arraylist、Linkedlist遍历方式性能分析
本文主要介绍ArrayList和LinkedList这两种list的常用循环遍历方式,各种方式的性能分析.熟悉java的知道,常用的list的遍历方式有以下几种: 1.for-each List< ...
- Huffman 压缩和解压缩java实现
附上完整的代码 http://download.csdn.net/download/u010485034/7847447 Huffman编码原理这里就不说了,是.这里来讲讲利用Huffman编码来进行 ...
- 安装SQL Server 2005 - 初学者系列 - 学习者系列文章
初学者阶段,建议从数据库为基础入手进行学习. 下面介绍微软的SQL Server 2005数据库的安装. 首先,从下列地址获取SQL Server 2005的安装程序. ed2k://|file|cs ...
- 苹果公司的新的编程语言 Swift 高级语言(十五)--协议
协议定义了适合某个特定任务或功能须要的方法.属性和其他需求的一个蓝图.协议本身不提供这些需求的实现,它仅仅是描写叙述了一个任务或功能实现的蓝图. 协议与java 语言中的接口定义类似,都是描写叙述了一 ...
- 使用pager进行分页
pager jar网址:http://java2s.com/Code/Jar/t/Downloadtaglibspagejar.htm package com.binary.entity; impor ...
- FileWriter字符输出流和FileReader字符输出流
//FileWriter public class FileWriterDemo { //字符流:适用于文本文件,以字符为单位进行操作,经常和缓冲流一起使用 /** * 字符流操作步骤: * 1. ...
- 为ASP.NET MVC应用程序使用高级功能
为ASP.NET MVC应用程序使用高级功能 这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译, ...
- MINIGUI 编译 helloworld
MiniGui 编译hello.c 文件成功!记载一下! MiniGui 版本v3.0 和 2 编译 差异 是极其的大! 源文件代码 : #include <stdio.h>#in ...