Java – stop the scheduler after a specific time
•
Java
I'm trying to create a scheduler in akka
>It will start at 5:00 a.m. > it will stop at 10:00 p.m. > it will perform the job every hour (frequency)
I found a solution for point 3 (frequency), but I couldn't find a solution for points 1 and 2
system.scheduler().schedule(Duration.create(10,TimeUnit.SECONDS),Duration.create(1,TimeUnit.HOURS),actorRef,"Hello",system.dispatcher(),null);
Solution
// For 5.00 am time period.
// For 5.00 am time period. int InHrs = 17; int InMinutes = 00; scheduler = Akka.system().scheduler().schedule(Duration.create(nextExecutionInSeconds(InHrs,InMinutes),Duration.create(24,new Runnable() { @Override public void run() { // Call your method System.out.println("EVERY 24:00 Later --- " + System.currentTimeMillis()); } },Akka.system().dispatcher()); public static int nextExecutionInSeconds(int hour,int minute){ return Seconds.secondsBetween( new DateTime(),nextExecution(hour,minute) ).getSeconds(); } public static DateTime nextExecution(int hour,int minute){ DateTime next = new DateTime() .withHourOfDay(hour) .withMinuteOfHour(minute) .withSecondOfMinute(0) .withMillisOfSecond(0); return (next.isBeforeNow()) ? next.plusHours(24) : next; }
Repeat this operation for a 10 am time period, which will be 22 hours I hope it will solve the problem
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码