The recent project needs to do a timed task, that is, a scheduled database backup. Timed time users can be configured in the interface, requiring configuration changes to take effect immediately.
I can't imagine a good idea. Here is a way to realize
The user's configured time is stored in the properties profile, the scheduled task executes every minute, reads the configuration file every time it is executed, and executes the task if the configured time is the same as the current time.
Before doing this, the method to load the configuration file is as follows
ClassLoader ClassLoader = This.getclass (). getClassLoader ();
Properties prop = new properties ();
Prop.load (Classloader.getresourceasstream ("/dbbak.properties"));
found that after modifying. Properties, even after the execution, the read is still the parameter before the modification.
The reason for this problem is that when Classloader.getresourceasstream is read in, the. Properties are saved in the cache, and when they are rerun, they are read from the cache rather than read again. properties files.
The solution is to use the following methods
String filestr = Thread.CurrentThread (). Getcontextclassloader ()
. GetResource (""). toString ();
String file = filestr.substring (5, Filestr.length ())
+ "config.properties";
InputStream in = new FileInputStream (new file);
Properties prop = new properties ();
Prop.load (in);
Here is the complete code in the test
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 { & nbsp; timertask task = new TimerTask () { @ Override public void Run () { try { if (Dbbak.isrun ()) {       SYSTEM.OUT.PRINTLN ("The task begins to execute"); }else{ //system.out.println ("Not Time");    &NBSP}  } catch (Exception e) { E.printstacktrace (); } } }; String filestr = Thread.CurrentThread (). Getcontextclassloader () .getresource (""). Touri (). GetPath (); string file = filestr+ "Dbbak.properties"; inputstream in = new FileInputStream (new file); properties prop = new Properties (); prop.load (in); string time = Prop.get (' time '). toString ();
//Set execution time Calendar calendar Calendar.getinstance (); int year = Calendar.get (calendar.year); int month = Calendar.get (calendar.month); int day = Calendar.get (calendar.day_of_month);//Daily //Custom daily .... Execution, 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; /Daily DateRow task, repeated execution of timer.schedule (task, date, period) every 2 seconds; }
public static void Main (string[] args) throws Exception { new Dbbak (). s Howtimer (); } 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); 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; }
}