0

I am executing a job every 16 minutes within an hour (3 Runs). The job will run at 16 past, 32 past and 48 past the hour. It will not spill over into the next hour.

Here is a sample of the runs for a two hour window:

Run 1:
Thu Jul 04 06:16:00 EDT 2013
Run 2:
Thu Jul 04 06:32:00 EDT 2013
Run 3:
Thu Jul 04 06:48:00 EDT 2013
Run 4:
Thu Jul 04 07:16:00 EDT 2013
Run 5:
Thu Jul 04 07:32:00 EDT 2013
Run 6:
Thu Jul 04 07:48:00 EDT 2013

My problem is that I need to build some targets for each job (not important), but ultimately I decided upon determining how many jobs would run for the day and then setting a target for each job.

I am working on the logic to determine how many times the job will run per day given a variable start time. I tried doing this with some calculations, but then ultimately relied on the this piece of code that uses a calendar to figure out how many runs are left in the day:

public int getRunsLeftInDay(int runIntervalMinutes, int offset){
    /*  
        Offset is when the job starts
        Currently runIntervalMinutes = 16 && offset = 16
        So start 16 minutes after the hour and run on 16 minute interval
    */

    /*
       This method is tested returns proper next run date, basically gives
       the next 16 past || 32 past || 48 past time that will occur
    */
    Date nextRunDt = this.getNextRun(runIntervalMinutes, offset);

    //Determine last run per hour   
    int lastRunMinute = (60/runIntervalMinutes) * runIntervalMinutes;
    int runCount = 0;

    Calendar cal = Calendar.getInstance();
    cal.setTime(nextRunDt);
    int currentDay = cal.get(Calendar.DAY_OF_YEAR);

    while(cal.get(Calendar.DAY_OF_YEAR) == currentDay){
        runCount++; //count first run since calendar is set to nextRunDt

        if(cal.get(Calendar.MINUTE) == lastRunMinute){
            cal.add(Calendar.HOUR_OF_DAY, 1);
            cal.set(Calendar.MINUTE, offset);
        }else{
            cal.add(Calendar.MINUTE, runIntervalMinutes);
        }
    }
    return runCount;
}

Currently my code is returning the correct amount of runs, but I would like to explore if there is a better way of determining this value (through some Date calculations).

So basically I have two questions:

1. Is this method of finding the amount of runs per day sufficient?

2. How would I do this using a date/time calculations?

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189

2 Answers2

0

The number of runs left in the day is the number of runs left in the current hour plus the number of hours left in the day times three. Or, did I misunderstand the question?

Bob Dalgleish
  • 8,167
  • 4
  • 32
  • 42
0

You should avoid the while loop, otherwise I don't think efficiency matters. In my experience Date objects are more difficult to deal with. (not tested yet)

Calendar now = Calendar.getInstance(),
    soon = Calendar.getInstance(),
    midnight = Calendar.getInstance();
soon.add(Calendar.HOUR, 1);
soon.set(Calendar.MINUTE, 0);
soon.set(Calendar.SECOND, 0);
midnight.add(Calendar.DAY, 1);
midnight.set(Calendar.HOUR, 0);
midnight.set(Calendar.MINUTE, 0);
midnight.set(Calendar.SECOND, 0);

int times_left_this_hour = (soon.getTimeInMillis() - now.getTimeInMillis()) / 1000 / 60 / 16;
int times_left_this_day = (midnight.getTimeInMillis() - soon.getTimeInMillis()) / 1000 / 60 / 60 * 3;
David
  • 943
  • 1
  • 10
  • 26