org.oddjob.state
Class StateHandler<S extends State>

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

public class StateHandler<S extends State>
extends Object
implements Stateful, StateLock

Helps Jobs handle state change.

Attempted to make waitToWhen(StateCondition, Runnable) and {@link #tryToWhen(StateCondition, Runnable) both use timeouts. This now required interrupt handling and the tryLock was intermittently interrupted. The cause of this could not be found so attempting to implement timeouts was abandoned for the time being.

Author:
Rob Gordon

Constructor Summary
StateHandler(Stateful source, S readyState)
          Constructor.
 
Method Summary
 void addStateListener(StateListener listener)
          Add a job state listener.
 void assertAlive()
          Convenience method to check the job hasn't been destroyed.
 void assertLockHeld()
           
<T> T
callLocked(Callable<T> callable)
          Runs the Callable locked.
 void fireEvent()
          Fire the event, update last event.
 State getState()
          Return the current state of the job.
 StateEvent lastStateEvent()
          Get the last event.
 int listenerCount()
          The number of listeners.
 void removeStateListener(StateListener listener)
          Remove a job state listener.
 void restoreLastJobStateEvent(StateEvent savedEvent)
          Typically only called after restoring a jobstate handler after deserialisation.
 void setState(S state)
           
 void setState(S state, Date date)
           
 void setStateException(State state, Throwable ex)
           
 void setStateException(S state, Throwable t, Date date)
           
 void sleep(long time)
          Sleep.
 String toString()
          Override toString.
 boolean tryToWhen(StateCondition when, Runnable runnable)
          Try to acquire the lock, and then do something when the condition is true.
 boolean waitToWhen(StateCondition when, Runnable runnable)
          Wait to do something when the condition is true.
 void wake()
          Wake any threads that are sleeping via sleep(long).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

StateHandler

public StateHandler(Stateful source,
                    S readyState)
Constructor.

Parameters:
source - The source for events.
readyState - The ready state.
Method Detail

lastStateEvent

public StateEvent lastStateEvent()
Get the last event.

Specified by:
lastStateEvent in interface Stateful
Returns:
The last event.

restoreLastJobStateEvent

public void restoreLastJobStateEvent(StateEvent savedEvent)
Typically only called after restoring a jobstate handler after deserialisation.

Parameters:
source -

setState

public void setState(S state,
                     Date date)
              throws JobDestroyedException
Throws:
JobDestroyedException

setState

public void setState(S state)
              throws JobDestroyedException
Throws:
JobDestroyedException

setStateException

public void setStateException(S state,
                              Throwable t,
                              Date date)
                       throws JobDestroyedException
Throws:
JobDestroyedException

setStateException

public void setStateException(State state,
                              Throwable ex)
                       throws JobDestroyedException
Throws:
JobDestroyedException

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.

callLocked

public <T> T callLocked(Callable<T> callable)
Runs the Callable locked.

Parameters:
callable - The callable.
Returns:
The result of the callable.

sleep

public void sleep(long time)
           throws InterruptedException
Sleep.

Parameters:
time -
Throws:
InterruptedException

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

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.

Parameters:
event - The event.