日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

java动态加载配置文件

發(fā)布時(shí)間:2023/11/27 生活经验 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java动态加载配置文件 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

最近項(xiàng)目中需要做定時(shí)任務(wù),即定時(shí)數(shù)據(jù)庫(kù)的備份。定時(shí)時(shí)間用戶可以在界面中配置,要求配置修改好立即生效。

?

想不到什么好辦法。下面是一種實(shí)現(xiàn)思路

把用戶配置的時(shí)間存到properties配置文件中,定時(shí)任務(wù)每隔一分鐘執(zhí)行一次,每次執(zhí)行前都會(huì)去讀取配置文件,如果配置的時(shí)間與當(dāng)前時(shí)間一致,則執(zhí)行任務(wù),否則什么也不做。

?

之前做的時(shí)候,加載配置文件的方法如下

ClassLoader classLoader = this.getClass().getClassLoader();Properties prop = new Properties();prop.load(classLoader.getResourceAsStream("/dbbak.properties"));

?

發(fā)現(xiàn)修改了.properties后,即使重新執(zhí)行,讀入的仍為修改前的參數(shù)。
此問(wèn)題的原因在于ClassLoader.getResourceAsStream讀入后,會(huì)將.properties保存在緩存中,重新執(zhí)行時(shí)會(huì)從緩存中讀取,而不是再次讀取.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);

?

?

下面是測(cè)試中完整代碼

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("任務(wù)開(kāi)始執(zhí)行了"); ?????}else{ ??????//System.out.println("時(shí)間未到"); ?????} ????} 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();

??????? //設(shè)置執(zhí)行時(shí)間 ??????? Calendar calendar = Calendar.getInstance(); ??????? int year = calendar.get(Calendar.YEAR); ??????? int month = calendar.get(Calendar.MONTH); ??????? int day = calendar.get(Calendar.DAY_OF_MONTH);//每天 ??????? //定制每天的....執(zhí)行, ??????? 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時(shí)刻執(zhí)行task,每隔2秒重復(fù)執(zhí)行 ??????? 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; ??? ? ??? }

? }

轉(zhuǎn)載于:https://www.cnblogs.com/jiangu66/p/3199016.html

總結(jié)

以上是生活随笔為你收集整理的java动态加载配置文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。