Class StateHandler<S extends State>

java.lang.Object
org.oddjob.state.StateHandler<S>
All Implemented Interfaces:
StateLock, Stateful
Direct Known Subclasses:
EventStateHandler, JobStateHandler, ParentStateHandler, ServiceStateHandler, TimerStateHandler

public class StateHandler<S extends State> extends Object implements Stateful, StateLock
Helps Jobs handle state change.

The state setting operations don't notify the listeners. This must be done using the separate fireEvent() method. This is because jobs that use this need to persist their themselves after setting the state.

Lock must be held during setting and firing operations. Considered using a read/write lock so state could be read while event fired to listeners, however as read write locks are not as performant as a single mutex lock, it was deemed not worth it.

Todo:

  • Constructor Details

    • StateHandler

      public StateHandler(Stateful source, S readyState)
      Constructor.
      Parameters:
      source - The source for events.
      readyState - The ready state.
  • Method Details

    • lastStateEvent

      public StateEvent lastStateEvent()
      Get the last event.
      Specified by:
      lastStateEvent in interface Stateful
      Returns:
      The last event.
    • restoreLastJobStateEvent

      public void restoreLastJobStateEvent(StateDetail savedEvent)
      Typically only called after restoring a jobstate handler after deserialisation.
      Parameters:
      savedEvent - The last serialised event.
    • setState

      @Deprecated(since="1.7", forRemoval=true) public void setState(S state, Date date) throws JobDestroyedException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Throws:
      JobDestroyedException
    • setState

      public void setState(S state, StateInstant instant) throws JobDestroyedException
      Set the state.
      Parameters:
      state - The state.
      instant - The time instant for the event.
      Throws:
      JobDestroyedException - If the current state is DESTROYED.
    • setState

      public void setState(S state) throws JobDestroyedException
      Set the state.
      Parameters:
      state - The state.
      Throws:
      JobDestroyedException - If the current state is DESTROYED.
    • setStateException

      public void setStateException(S state, Throwable t, StateInstant instant) throws JobDestroyedException
      Set the Exception state and time instant of the event.
      Parameters:
      state - The exception state
      t - The exception.
      instant - The time instant for the event.
      Throws:
      JobDestroyedException - If the current state is DESTROYED.
    • setStateException

      public void setStateException(State state, Throwable ex) throws JobDestroyedException
      Set the exception state.
      Parameters:
      state - The exception state.
      ex - The exception.
      Throws:
      JobDestroyedException - If the existing state is DESTROYED.
      See Also:
    • getState

      public State getState()
      Return the current state of the job.
      Returns:
      The current state.
    • assertAlive

      public void assertAlive() throws JobDestroyedException
      Convenience method to check the job hasn't been destroyed.
      Throws:
      JobDestroyedException - If it has.
    • assertLockHeld

      public void assertLockHeld()
    • tryToWhen

      public boolean tryToWhen(StateCondition when, Runnable runnable) throws OddjobLockedException
      Description copied from interface: StateLock
      Try to acquire the lock, and then do something when the condition is true. Both the condition evaluation and the performing of the action are done in the context of the same state lock. If the lock can not be acquired an OddjobLockedException is thrown.
      Specified by:
      tryToWhen in interface StateLock
      Parameters:
      when - The condition.
      runnable - The action.
      Returns:
      true if the condition was true and the action executed. False otherwise.
      Throws:
      OddjobLockedException - If the lock can not be acquired.
    • waitToWhen

      public boolean waitToWhen(StateCondition when, Runnable runnable)
      Description copied from interface: StateLock
      Wait to do something when the condition is true. Both the condition evaluation and the performing of the action are done in the context of the same state lock. If the lock can not be acquired then the lock is waited for.
      Specified by:
      waitToWhen in interface StateLock
      Parameters:
      when - The condition.
      runnable - The action.
      Returns:
      true if the condition was true and the action executed.
    • runLocked

      public void runLocked(Runnable runnable)
      Wait to acquire the lock and execute the Runnable while holding the lock.
      Specified by:
      runLocked in interface StateLock
      Parameters:
      runnable - The Runnable.
    • callLocked

      public <T> T callLocked(Callable<T> callable) throws Exception
      Wait to acquire the lock and execute the Callable while holding the lock.
      Specified by:
      callLocked in interface StateLock
      Parameters:
      callable - The callable.
      Returns:
      The result of the callable.
      Throws:
      Exception - from the callable.
    • supplyLocked

      public <T> T supplyLocked(Supplier<T> supplier)
      Wait to acquire the lock and execute the Supplier while holding the lock.
      Specified by:
      supplyLocked in interface StateLock
      Parameters:
      supplier - The Supplier.
      Returns:
      The result from the Supplier.
    • sleep

      public void sleep(long time) throws InterruptedException
      Sleep.
      Parameters:
      time - The milliseconds to sleep for.
      Throws:
      InterruptedException - If the sleep is interrupted.
    • wake

      public void wake()
      Wake any threads that are sleeping via sleep(long).
    • addStateListener

      public void addStateListener(StateListener listener) throws JobDestroyedException
      Add a job state listener. This method will send the last event to the new listener. It is possible that the listener may get the notification twice.
      Specified by:
      addStateListener in interface Stateful
      Parameters:
      listener - The listener.
      Throws:
      JobDestroyedException - If trying to listen to a destroyed job.
    • removeStateListener

      public void removeStateListener(StateListener listener)
      Remove a job state listener.
      Specified by:
      removeStateListener in interface Stateful
      Parameters:
      listener - The listener.
    • listenerCount

      public int listenerCount()
      The number of listeners.
      Returns:
      The number of listeners.
    • toString

      public String toString()
      Override toString.
      Overrides:
      toString in class Object
    • fireEvent

      public void fireEvent()
      Fire the event, update last event.