首页 技术 正文
技术 2022年11月16日
0 收藏 411 点赞 3,258 浏览 2845 个字

最近项目中需要做定时任务,即定时数据库的备份。定时时间用户可以在界面中配置,要求配置修改好立即生效。

想不到什么好办法。下面是一种实现思路

把用户配置的时间存到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;           }

}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905