Implements: IClosable
Description
The FixerRateTimer class represents a timer that is triggered in equal time intervals.
Important points
- It has symmetric cross-language implementation and is often used by the Pip.Services toolkit to perform periodic processing and cleanup in microservices.
Constructors
Creates new instance of the timer and sets its values.
public
FixedRateTimer(INotifiable task, long interval, long delay)
- task: INotifiable - (optional) a Notifiable object to call when timer is triggered
- interval: long - (optional) an interval to trigger timer in milliseconds.
- delay: long - (optional) delay before the first triggering in milliseconds.
Instance methods
close
Closes the timer.
This is required by the IClosable interface, but besides that it is identical to stop().
public
void close(IContext context)
- context: IContext - (optional) execution context to trace execution through call chain.
getTask
Gets the INotifiable object that receives notifications from this timer.
public
INotifiable getTask()
- returns: INotifiable - the INotifiable object or null if it is not set.
getDelay
Gets an initial delay before the timer is triggered for the first time.
public
long getDelay()
- returns: long - delay in milliseconds.
getInterval
Gets a periodic timer triggering interval.
public
long getInterval()
- returns: long - interval in milliseconds
setTask
Sets a new INotifiable object to receive notifications from this timer.
public
void setTask(INotifiable task)
- value: INotifiable - INotifiable object to be triggered.
isStarted
Checks if the timer is started.
public
boolean isStarted()
- returns: boolean - true if the timer is started and false if it is stopped.
setDelay
Sets an initial delay before the timer is triggered for the first time.
public
void setDelay(long delay)
- value: long - delay in milliseconds.
setInterval
Sets a periodic timer triggering interval.
public
void setInterval(long interval)
- value: long - interval in milliseconds.
start
Starts the timer.
Initially. the timer is triggered after delay. After that, it is triggered after interval until it is stopped.
public
void start()
stop
Stops the timer.
public
void stop()
Examples
{
class MyComponent {
FixedRateTimer timer = new FixedRateTimer(() -> { this.cleanup }, 60000, 0);
...
public void open(IContext context) {
...
timer.start();
...
}
public void open(IContext context) {
...
timer.stop();
...
}
private void cleanup() {
...
}
...
}
}