Class AbstractWorker

java.lang.Object
io.openems.common.worker.AbstractWorker
Direct Known Subclasses:
AbstractCycleWorker, AbstractImmediateWorker, AppInstallWorker, AppSynchronizeWorker, AppValidateWorker, ClientReconnectorWorker, ComponentManagerWorker, CycleWorker, DiskSpaceWorker, NetworkConfigurationWorker, ReadWorker, ResendHistoricDataWorker, UsbConfigurationWorker

public abstract class AbstractWorker extends Object
Defines a generic Worker Thread.

The business logic of the Worker is inside the forever() method. It is executed by default every getCycleTime() seconds. Additionally execution can be triggered by calling the triggerNextRun() method.

If Cycle-Time is negative (e.g. by using ALWAYS_WAIT_FOR_TRIGGER_NEXT_RUN) the forever() method is called only after triggering it.

If Cycle-Time is zero (e.g. by using DO_NOT_WAIT), the forever() method is always called immediately without any delay.

  • Field Details

    • ALWAYS_WAIT_FOR_TRIGGER_NEXT_RUN

      public static final int ALWAYS_WAIT_FOR_TRIGGER_NEXT_RUN
      See Also:
    • DO_NOT_WAIT

      public static final int DO_NOT_WAIT
      See Also:
    • thread

      protected final Thread thread
  • Constructor Details

    • AbstractWorker

      public AbstractWorker()
  • Method Details

    • activate

      public void activate(String name, boolean initiallyTriggerNextRun)
      Initializes the worker and starts the worker thread.
      Parameters:
      name - the name of the worker thread
      initiallyTriggerNextRun - true if the forever() method should get called immediately; if not false
    • activate

      public void activate(String name)
      Initializes the worker and starts the worker thread.
      Parameters:
      name - the name of the worker thread
    • modified

      public void modified(String name, boolean initiallyTriggerNextRun)
      Modifies the worker thread.
      Parameters:
      name - the name of the worker thread
      initiallyTriggerNextRun - true if the forever() method should get called immediately; if not false
    • modified

      public void modified(String name)
      Modifies the worker thread.
      Parameters:
      name - the name of the worker thread
    • deactivate

      public void deactivate()
      Stops the worker thread.
    • forever

      protected abstract void forever() throws Throwable
      This method is called in a loop forever until the Thread gets interrupted.
      Throws:
      Throwable
    • getCycleTime

      protected abstract int getCycleTime()
      Gets the cycleTime of this worker in [ms].
      • > 0 sets the minimum execution time of one Cycle
      • = 0 never wait between two consecutive executions of forever()
      • < 0 causes the Cycle to sleep forever until 'triggerNextRun()' is called
      Returns:
      the cycleTime
    • triggerNextRun

      public void triggerNextRun()
      Allows the next execution of the forever() method.
    • setPriority

      public final void setPriority(int newPriority)
      Parameters:
      newPriority - priority to set this thread to
      Throws:
      IllegalArgumentException - If the priority is not in the range MIN_PRIORITY to MAX_PRIORITY.
      SecurityException - if the current thread cannot modify this thread.