Package org.pipservices3.commons.run
Class FixedRateTimer
java.lang.Object
org.pipservices3.commons.run.FixedRateTimer
- All Implemented Interfaces:
IClosable
Timer that is triggered in equal time intervals.
It has summetric cross-language implementation and is often used by Pip.Services toolkit to perform periodic processing and cleanup in microservices.
### Example ###
class MyComponent {
FixedRateTimer timer = new FixedRateTimer(() -> { this.cleanup }, 60000, 0);
...
public void open(String correlationId) {
...
timer.start();
...
}
public void open(String correlationId) {
...
timer.stop();
...
}
private void cleanup() {
...
}
...
}
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates new instance of the timer.FixedRateTimer
(INotifiable task, long interval, long delay) Creates new instance of the timer and sets its values. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Closes the timer.long
getDelay()
Gets initial delay before the timer is triggered for the first time.long
Gets periodic timer triggering interval.getTask()
Gets the INotifiable object that receives notifications from this timer.boolean
Checks if the timer is started.void
setDelay
(long delay) Sets initial delay before the timer is triggered for the first time.void
setInterval
(long interval) Sets periodic timer triggering interval.void
setTask
(INotifiable task) Sets a new INotifiable object to receive notifications from this timer.void
start()
Starts the timer.void
stop()
Stops the timer.
-
Constructor Details
-
FixedRateTimer
public FixedRateTimer()Creates new instance of the timer. -
FixedRateTimer
Creates new instance of the timer and sets its values.- Parameters:
task
- (optional) a Notifiable object to call when timer is triggered.interval
- (optional) an interval to trigger timer in milliseconds.delay
- (optional) a delay before the first triggering in milliseconds.- See Also:
-
-
Method Details
-
getTask
Gets the INotifiable object that receives notifications from this timer.- Returns:
- the INotifiable object or null if it is not set.
-
setTask
Sets a new INotifiable object to receive notifications from this timer.- Parameters:
task
- a INotifiable object to be triggered.
-
getDelay
public long getDelay()Gets initial delay before the timer is triggered for the first time.- Returns:
- the delay in milliseconds.
-
setDelay
public void setDelay(long delay) Sets initial delay before the timer is triggered for the first time.- Parameters:
delay
- a delay in milliseconds.
-
getInterval
public long getInterval()Gets periodic timer triggering interval.- Returns:
- the interval in milliseconds
-
setInterval
public void setInterval(long interval) Sets periodic timer triggering interval.- Parameters:
interval
- an interval in milliseconds.
-
isStarted
public boolean isStarted()Checks if the timer is started.- Returns:
- true if the timer is started and false if it is stopped.
-
start
public void start()Starts the timer. Initially the timer is triggered after delay. After that it is triggered after interval until it is stopped.- See Also:
-
stop
public void stop()Stops the timer.- See Also:
-
close
Closes the timer. This is required by ICloseable interface, but besides that it is identical to stop().- Specified by:
close
in interfaceIClosable
- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.- Throws:
ApplicationException
- when error or null no errors occured.- See Also:
-