Class FixedRateTimer

java.lang.Object
org.pipservices3.commons.run.FixedRateTimer
All Implemented Interfaces:
IClosable

public class FixedRateTimer extends Object implements 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

    Constructors
    Constructor
    Description
    Creates 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 Type
    Method
    Description
    void
    close(String correlationId)
    Closes the timer.
    long
    Gets initial delay before the timer is triggered for the first time.
    long
    Gets periodic timer triggering interval.
    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
    Sets a new INotifiable object to receive notifications from this timer.
    void
    Starts the timer.
    void
    Stops the timer.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • FixedRateTimer

      public FixedRateTimer()
      Creates new instance of the timer.
    • FixedRateTimer

      public FixedRateTimer(INotifiable task, long interval, long delay)
      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

      public INotifiable getTask()
      Gets the INotifiable object that receives notifications from this timer.
      Returns:
      the INotifiable object or null if it is not set.
    • setTask

      public void setTask(INotifiable task)
      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

      public void close(String correlationId) throws ApplicationException
      Closes the timer. This is required by ICloseable interface, but besides that it is identical to stop().
      Specified by:
      close in interface IClosable
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      Throws:
      ApplicationException - when error or null no errors occured.
      See Also: