com.atlassian.jira.scheduler.cron
Class SimpleToCronTriggerConverter

java.lang.Object
  extended by com.atlassian.jira.scheduler.cron.SimpleToCronTriggerConverter

public class SimpleToCronTriggerConverter
extends Object

From the extranet spec page:

There are a number of things a cronspec cannot handle cleanly when converting a arbitrary fixed millisecond repeat from a specific DateTime into a cronspec. Things such as "every 15 min from 9.53am Jan 23rd 2006" (closest cronspec would be "every 15min").

Basic parsing rules for the algorithm are that the main Unit that will be used is the largest unit it is close to (minutes, hours, days, week, months) and then rounded to the nearest neighbour that is a denominator of the next largest unit. So an unit of 5 hours is specified in hours, but as 5 is not a denominator of 24 it must be rounded to 4 or 6. Similarly 15 hours would be rounded down to 12 and 20 hours would be rounded up to 24 (daily).

Once the time period is specified as a number of days there are some fairly intractable issues. Firstly, the number of days in a week - 7 - is prime and has no denominators apart from 1 and itself, and months may have 28, 29, 30 or 31 days so exact arithmetic is not possible. For these periods we will need to round periods above 7 days and up to 15 days down to 7 days, and of over 15 days to one month. Multi-month periods can be supported in the 2, 3, 4, 6 and 12 month periods. We will not support multi-year periods and any currently specified will be rounded down to a single year period.


Constructor Summary
SimpleToCronTriggerConverter()
           
 
Method Summary
 ConversionResult convertToCronString(Date nextFireDate, long intervalMs)
          Converts trigger data taken from the database entries into a quartz CronTrigger String.
protected  long determineBaseTimeUnit(long intervalMs)
          Return the time unit (SECONDS, MINUTES, MONTHS etc.) that the supplied interval (ms) is closest to.
protected  long getSucceedingTimeUnit(long timeUnit)
          Given a unit of time (e.g.
protected  String makeIncrementalCronElement(int targetTime, long frequency, int base)
          Make a cron element of the form 'START_TIME''/''FREQUENCY'.
protected  long roundInterval(long intervalMs, long baseTimeUnit)
          Round the interval to the nearest time unit multiple that is a denominator of the succeeding time unit.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleToCronTriggerConverter

public SimpleToCronTriggerConverter()
Method Detail

convertToCronString

public ConversionResult convertToCronString(Date nextFireDate,
                                            long intervalMs)
Converts trigger data taken from the database entries into a quartz CronTrigger String. Note that this conversion is sometimes lossy. Not all inputs can result in an equivalent cron string such that the triggers will fire at the same time.

Parameters:
nextFireDate - a time a trigger will next fire
intervalMs - a time delay in milliseconds between trigger firing
Returns:
a ConversionResult which contains composed of the following cron fields:
SECONDS MINUTES HOURS DAY_OF_MONTH MONTH DAY_OF_WEEK

makeIncrementalCronElement

protected String makeIncrementalCronElement(int targetTime,
                                            long frequency,
                                            int base)
Make a cron element of the form 'START_TIME''/''FREQUENCY'.

In order for the trigger to fire the right number of times, we must pick the earliest possible start time where START_TIME + N * FREQUENCY = TARGET_TIME, where N is some positive integer, holds true.

Parameters:
targetTime - The time that the trigger should fire on, parsed from the nextFire quartz trigger field
frequency - The delay between trigger firings
base - The base of the cron timeunit: 0 or 1. Seconds [0-59], minutes [0-59] & hours [0-23] are zero-based. Day of Month [1-31], Months [1-12] and Days of Week [1-7] are one-based.
Returns:
a String of the form "s/f". This represents START_TIME/FREQUENCY.

roundInterval

protected long roundInterval(long intervalMs,
                             long baseTimeUnit)
Round the interval to the nearest time unit multiple that is a denominator of the succeeding time unit. That is, find the set of intervals in milliseconds that satisfies the following: and select the interval length that is nearest the length of the supplied interval.

Parameters:
intervalMs - the original interval in milliseconds
baseTimeUnit - the time unit that is the major component of the cron string (e.g. SECONDS, HOURS, etc.)
Returns:
the rounded interval length

getSucceedingTimeUnit

protected long getSucceedingTimeUnit(long timeUnit)
Given a unit of time (e.g. MINUTES), this method returns the next unit of time from the unit of time sequence (e.g. HOURS).

Parameters:
timeUnit - a unit of time, defined at the top of this class
Returns:
the next unit of time in the sequence, or the last unit of time (YEARS) if we're already at the end of the sequence

determineBaseTimeUnit

protected long determineBaseTimeUnit(long intervalMs)
Return the time unit (SECONDS, MINUTES, MONTHS etc.) that the supplied interval (ms) is closest to.

Parameters:
intervalMs - the supplied interval in milliseconds
Returns:
the unit of time that matches the supplied interval the closest


Copyright © 2002-2010 Atlassian. All Rights Reserved.