org.gcube.common.core.utils.handlers
Class GCUBEScheduledHandler<HANDLED>

java.lang.Object
  extended by org.gcube.common.core.utils.handlers.GCUBEHandler<HANDLED>
      extended by org.gcube.common.core.utils.handlers.GCUBEScheduledHandler<HANDLED>
All Implemented Interfaces:
GCUBEIHandler<HANDLED>, Lifetime<HANDLED>
Direct Known Subclasses:
Scheduler

public class GCUBEScheduledHandler<HANDLED>
extends GCUBEHandler<HANDLED>
implements Lifetime<HANDLED>

An extension of GCUBEComplexHandler which schedules the execution of a single GCUBEHandler, the scheduled handler, at regular time intervals.

The execution of the scheduled handler occurs in accordance with either one of the two modes enumerated by GCUBEScheduledHandler.Mode. In Mode.EAGER, the first execution of the scheduled handler occurs immediately. In Mode.LAZY, it occurs after the first elapse of the time interval.

A GCUBEScheduledHandler may be halted in between iteration of the schedule, either explicitly, by invoking stop(), or implicitly, by overriding repeat(Exception, int) to implement arbitrary halting conditions.

Author:
Fabio Simeoni (University of Strathclyde)

Nested Class Summary
static class GCUBEScheduledHandler.Mode
          Possible modes of execution.
 
Field Summary
protected  Exception exception
          The last exception raised during the schedule.
protected  int exceptionCount
          The number of exceptions raised at any point of the schedule.
protected  long interval
          Time interval.
protected  boolean repeat
          Stop flag.
 
Fields inherited from class org.gcube.common.core.utils.handlers.GCUBEHandler
handled, logger, producer
 
Constructor Summary
GCUBEScheduledHandler()
          Creates an instance.
GCUBEScheduledHandler(long interval, GCUBEScheduledHandler.Mode mode, GCUBEHandler<HANDLED>... handler)
          Creates an instance with a given execution mode, time interval, and scheduled handler.
 
Method Summary
 long getInterval()
          Returns the interval of the schedule.
 GCUBEScheduledHandler.Mode getMode()
          Returns the execution mode of the ScheduledHandler.
 GCUBEHandler<HANDLED> getScheduled()
          Returns the scheduled handler.
protected  boolean onIteration()
          Used internally to perform an iteration of the schedule and to indicate whether it should continue.
protected  boolean repeat(Exception exception, int exceptionCount)
          Indicates whether the scheduling should continue.
 void run()
          Executes the task implemented by the handler.
 void setInterval(long interval)
          Sets the interval of the schedule.
 void setMode(GCUBEScheduledHandler.Mode mode)
          Sets the execution mode of the ScheduledHandler.
 void setScheduled(GCUBEHandler<HANDLED> scheduled)
          Sets the scheduled handler.
 void stop()
          Stops the scheduling.
 
Methods inherited from class org.gcube.common.core.utils.handlers.GCUBEHandler
clearBlackboard, getBlackboard, getHandled, getId, getLogger, getName, getScopeManager, getSecurityManager, getState, notify, setBlackboard, setHandled, setLogger, setName, setScopeManager, setSecurityManager, setState, subscribe, undo, unsubscribe
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.gcube.common.core.utils.handlers.lifetime.Lifetime
getState
 
Methods inherited from interface org.gcube.common.core.utils.handlers.GCUBEIHandler
clearBlackboard, getBlackboard, getHandled, getId, getLogger, getName, getScopeManager, getSecurityManager, setBlackboard, setHandled, setLogger, setName, setScopeManager, setSecurityManager, subscribe, undo, unsubscribe
 

Field Detail

interval

protected long interval
Time interval.


repeat

protected boolean repeat
Stop flag.


exception

protected Exception exception
The last exception raised during the schedule.


exceptionCount

protected int exceptionCount
The number of exceptions raised at any point of the schedule.

Constructor Detail

GCUBEScheduledHandler

public GCUBEScheduledHandler()
Creates an instance.


GCUBEScheduledHandler

public GCUBEScheduledHandler(long interval,
                             GCUBEScheduledHandler.Mode mode,
                             GCUBEHandler<HANDLED>... handler)
Creates an instance with a given execution mode, time interval, and scheduled handler.

Parameters:
interval - the time interval in seconds.
mode - the execution mode.
handler - (optional) the scheduled handler.
Method Detail

getInterval

public long getInterval()
Returns the interval of the schedule.

Returns:
the interval.

setInterval

public void setInterval(long interval)
Sets the interval of the schedule.

Parameters:
the - interval.

getScheduled

public GCUBEHandler<HANDLED> getScheduled()
Returns the scheduled handler.

Returns:
the scheduled handled.

setScheduled

public void setScheduled(GCUBEHandler<HANDLED> scheduled)
Sets the scheduled handler.

Parameters:
scheduled - the scheduled handled.

getMode

public GCUBEScheduledHandler.Mode getMode()
Returns the execution mode of the ScheduledHandler.

Returns:
mode the mode.

setMode

public void setMode(GCUBEScheduledHandler.Mode mode)
Sets the execution mode of the ScheduledHandler.

Parameters:
mode - the mode.

run

public void run()
         throws Exception
Executes the task implemented by the handler.

Specified by:
run in interface GCUBEIHandler<HANDLED>
Specified by:
run in class GCUBEHandler<HANDLED>
Throws:
Exception - if the execution of the task does not complete normally.

onIteration

protected boolean onIteration()
Used internally to perform an iteration of the schedule and to indicate whether it should continue.

Returns:
true if the schedule should continue, false otherwise.

stop

public void stop()
Stops the scheduling.


repeat

protected boolean repeat(Exception exception,
                         int exceptionCount)
Indicates whether the scheduling should continue. It is invoked after each iteration of the schedule as a pre-condition to continue it. By default, it returns true if no exception occurred during the iteration, and false otherwise. Override to implement specific conditions.

Parameters:
exception - any exception which may have occurred during the iteration, or null if the iteration completed successfully.
exceptionCount - the number of exceptions which have occurred so far during the schedule.
Returns:
true if the scheduling should continue, false otherwise.


Copyright © 2013. All Rights Reserved.