Class Lifecycle

org.elasticsearch.common.component.Lifecycle

public class Lifecycle extends
Lifecycle state. Allows the following transitions:
  • INITIALIZED -> STARTED, STOPPED, CLOSED
  • STARTED -> STOPPED
  • STOPPED -> STARTED, CLOSED
  • CLOSED ->

Also allows to stay in the same state. For example, when calling stop on a component, the following logic can be applied:

 public void stop() {
  if (lifecycleState.moveToStopped() == false) {
      return;
  }
 // continue with stop logic
 }
 

NOTE: The Lifecycle class is thread-safe. It is also possible to prevent concurrent state transitions by locking on the Lifecycle object itself. This is typically useful when chaining multiple transitions.

Note, closed is only allowed to be called when stopped, so make sure to stop the component first. Here is how the logic can be applied. A lock of the lifecycleState object is taken so that another thread cannot move the state from STOPPED to STARTED before it has moved to CLOSED.

 public void close() {
  synchronized (lifecycleState) {
      if (lifecycleState.started()) {
          stop();
      }
      if (lifecycleState.moveToClosed() == false) {
          return;
      }
  }
  // perform close logic here
 }
 
  • Constructor Details

    • Lifecycle

      public Lifecycle()
  • Method Details

    • state

      public Lifecycle.State state()
    • initialized

      public boolean initialized()
      Returns true if the state is initialized.
    • started

      public boolean started()
      Returns true if the state is started.
    • stopped

      public boolean stopped()
      Returns true if the state is stopped.
    • closed

      public boolean closed()
      Returns true if the state is closed.
    • stoppedOrClosed

      public boolean stoppedOrClosed()
    • canMoveToStarted

      public boolean canMoveToStarted() throws
      Throws:
    • moveToStarted

      public boolean moveToStarted() throws
      Throws:
    • canMoveToStopped

      public boolean canMoveToStopped() throws
      Throws:
    • moveToStopped

      public boolean moveToStopped() throws
      Throws:
    • canMoveToClosed

      public boolean canMoveToClosed() throws
      Throws:
    • moveToClosed

      public boolean moveToClosed() throws
      Throws:
    • toString

      public  toString()
      Overrides:
       in class