java - schedule management -
finally after trial , errors have managed make work wanted.
but advice make code more readable , simple seems made lot of unnecessary code archive wanted.
what basicly is, if turn on server app @ time schedule task should running, start task , let run time left when should have started otherwise schedule run @ hour supposed run.
so if schedule time 13:00:00 , should run 120 minutes , start app @ 13:30 run 90 minutes. if start after time, schedule next day 13:00:00.
calendar calendar = calendar.getinstance(); calendar.set(calendar.hour_of_day, hour); calendar.set(calendar.minute, 0); calendar.set(calendar.second, 0); long start_time = calendar.gettimeinmillis() - system.currenttimemillis(); if (start_time < 0) { long minutes = (start_time*-1) / (60 * 1000); if (minutes > 0 && minutes < 120) { runtimeleft = 120 - minutes; threadpoolmanager.getinstance().schedule(new runnable() { public void run() { mytask(); } }, 0); } else runtimeleft = 0; calendar calendar = calendar.getinstance(); calendar.set(calendar.hour_of_day, hour+24); calendar.set(calendar.minute, 0); calendar.set(calendar.second, 0); start_time = calendar.gettimeinmillis() - system.currenttimemillis(); } threadpoolmanager.getinstance().scheduleatfixedrate(new runnable() { public void run() { mytask(); } }, start_time, 24 * 60 * 60 * 1000);
so question here improve on above code ?
instead of using java.util.timer
alone, try using timertask
. there article ibm on this.
have @ link: http://www.ibm.com/developerworks/java/library/j-schedule.html
the code shared , seems work trivial routine job.
Comments
Post a Comment