com.emarsys.ecommon.time.schedule
Class DelayedLinearQueueSchedulingStrategy<T>

java.lang.Object
  extended by com.emarsys.ecommon.time.schedule.DelayedLinearQueueSchedulingStrategy<T>
All Implemented Interfaces:
QueueSchedulingStrategy<T>

public class DelayedLinearQueueSchedulingStrategy<T>
extends java.lang.Object
implements QueueSchedulingStrategy<T>

The DelayedLinearQueueSchedulingStrategy represents a two phase scheduling algorithm that reschedules queue elements in their first phase in constant intervals and scales them linearly with number of processings in their second.

See calculateNextProcessing(QueueScheduler, SchedulableQueueElement) for the detailed scheduling alorithm specification and DelayedLinearQueueSchedulingStrategy(int, int) for the specification of its parameters.

Under the hood the DelayedLinearQueueSchedulingStrategy is powered by QueueSchedulingStrategyChain that consists of a ConstantQueueSchedulingStrategy and a LinearQueueSchedulingStrategy as well as the corresponding predicates that decide which concrete strategy to use.

Author:
Michael "kULO" Kulovits


Field Summary
protected  QueueSchedulingStrategyChain<T> alice
          the QueueSchedulingStrategyChain that acutally performs
protected  int baseIntLen
           
protected static java.lang.String CONST_PHASE_ITERATIONS_KEY
           
protected  int delayIntLen
           
 
Constructor Summary
DelayedLinearQueueSchedulingStrategy(int baseIntLen, int delayIntLen)
           Creates a new DelayedLinearQueueSchedulingStrategy instance with the passed parameters.
 
Method Summary
 java.util.Calendar calculateNextProcessing(QueueScheduler<T> scheduler, SchedulableQueueElement<T> schedulable)
           If the passed schedulable has been queued (according to SchedulableQueueElement.getQueueingDate()) before the delay threshold (== queueing date + delayIntLen) it will be constantly rescheduled in baseIntLen milliseconds (see ConstantQueueSchedulingStrategy).
Else if it has been queued after said threshold it will be linearly rescheduled in SchedulableQueueElement.getScheduledCount() * delayIntLen milliseconds (see LinearQueueSchedulingStrategy).
protected  void checkInvariants()
          Checks the following invariants which have to be fullfilled at any time: 0 < baseIntLen < delayIntLen
protected  int getConstPhaseIterationCount(QueueSchedulingContext<T> context)
           
protected  QueueSchedulingStrategy<T> getConstStrategy()
          Creates a new wrapped ConstantQueueSchedulingStrategy instance that will increment the number of the context param CONST_PHASE_ITERATIONS_KEY on every call of calculateNextProcessing(QueueScheduler, SchedulableQueueElement).
protected  org.apache.commons.collections.Predicate getIsBeforeDelayThresholdPredicate()
          Returns a Predicate that evaluates to true if the passed SchedulableQueueElement's (the one associated with the passed ChainElement) queueing date + delayIntLen is before the current time.
protected  QueueSchedulingStrategy<T> getLinearStrategy()
           Creates a LinearQueueSchedulingStrategy that will start to scale the scheduling interval after the constant scheduling phase.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CONST_PHASE_ITERATIONS_KEY

protected static final java.lang.String CONST_PHASE_ITERATIONS_KEY

baseIntLen

protected final int baseIntLen

delayIntLen

protected final int delayIntLen

alice

protected QueueSchedulingStrategyChain<T> alice
the QueueSchedulingStrategyChain that acutally performs

Constructor Detail

DelayedLinearQueueSchedulingStrategy

public DelayedLinearQueueSchedulingStrategy(int baseIntLen,
                                            int delayIntLen)
                                     throws java.lang.IllegalArgumentException

Creates a new DelayedLinearQueueSchedulingStrategy instance with the passed parameters.

See checkInvariants() for the restritions of the params.

Parameters:
baseIntLen - - the base interval lenth for the constant rescheduling phase.
delayIntLen - - both the delay after which the linear rescheduling phase will start and the to be scaled interval lenghte in said phase..
Throws:
java.lang.IllegalArgumentException - - if an invariant is violated through the params

Method Detail

checkInvariants

protected void checkInvariants()
Checks the following invariants which have to be fullfilled at any time:


getConstStrategy

protected QueueSchedulingStrategy<T> getConstStrategy()
Creates a new wrapped ConstantQueueSchedulingStrategy instance that will increment the number of the context param CONST_PHASE_ITERATIONS_KEY on every call of calculateNextProcessing(QueueScheduler, SchedulableQueueElement).

Returns:
a valid QueueSchedulingStrategy instance, never null.

getConstPhaseIterationCount

protected int getConstPhaseIterationCount(QueueSchedulingContext<T> context)
Parameters:
context -
Returns:

getLinearStrategy

protected QueueSchedulingStrategy<T> getLinearStrategy()

Creates a LinearQueueSchedulingStrategy that will start to scale the scheduling interval after the constant scheduling phase.

In order to get the right number of reschedulings in the constant phase the QueueSchedulingContext param CONST_PHASE_ITERATIONS_KEY is queried.
The retrieved number will be used to create a dynamical negative offset for the scheduled count of a proxy for the schedulable, see SchedulableQueueElementProxy.setScheduledCountOffset(int) for details.

Returns:
a valid new QueueSchedulingStrategy instance, never null.

getIsBeforeDelayThresholdPredicate

protected org.apache.commons.collections.Predicate getIsBeforeDelayThresholdPredicate()
Returns a Predicate that evaluates to true if the passed SchedulableQueueElement's (the one associated with the passed ChainElement) queueing date + delayIntLen is before the current time.

Returns:
always a valid Predicate instance.

calculateNextProcessing

public java.util.Calendar calculateNextProcessing(QueueScheduler<T> scheduler,
                                                  SchedulableQueueElement<T> schedulable)

If the passed schedulable has been queued (according to SchedulableQueueElement.getQueueingDate()) before the delay threshold (== queueing date + delayIntLen) it will be constantly rescheduled in baseIntLen milliseconds (see ConstantQueueSchedulingStrategy).
Else if it has been queued after said threshold it will be linearly rescheduled in SchedulableQueueElement.getScheduledCount() * delayIntLen milliseconds (see LinearQueueSchedulingStrategy).

Specified by:
calculateNextProcessing in interface QueueSchedulingStrategy<T>
Parameters:
scheduler - - the QueueScheduler that is associated with this strategy instance
schedulable - - the SchedulableQueueElement that will be (re)scheduled
Returns:
never null because every of the chained strategies returns a valid Calendar instance the predicates for executing them are exclusive or joint.
See Also:
QueueSchedulingStrategy.calculateNextProcessing(com.emarsys.ecommon.time.schedule.QueueScheduler, com.emarsys.ecommon.time.schedule.SchedulableQueueElement)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
See Also:
Object.toString()


Copyright © 2010 emarsys AG. All Rights Reserved.