public class StateHandler<S extends State> extends java.lang.Object implements Stateful, StateLock
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.
Constructor and Description |
---|
StateHandler(Stateful source,
S readyState)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
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(java.util.concurrent.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.SerializableNoSource savedEvent)
Typically only called after restoring a jobstate handler after deserialisation.
|
void |
setState(S state) |
void |
setState(S state,
java.util.Date date) |
void |
setStateException(State state,
java.lang.Throwable ex) |
void |
setStateException(S state,
java.lang.Throwable t,
java.util.Date date) |
void |
sleep(long time)
Sleep.
|
java.lang.String |
toString()
Override toString.
|
boolean |
tryToWhen(StateCondition when,
java.lang.Runnable runnable)
Try to acquire the lock, and then do something when the
condition is true.
|
boolean |
waitToWhen(StateCondition when,
java.lang.Runnable runnable)
Wait to do something when the condition is true.
|
void |
wake()
Wake any threads that are sleeping via
sleep(long) . |
public StateEvent lastStateEvent()
lastStateEvent
in interface Stateful
public void restoreLastJobStateEvent(StateEvent.SerializableNoSource savedEvent)
savedEvent
- The last serialised event.public void setState(S state, java.util.Date date) throws JobDestroyedException
JobDestroyedException
public void setState(S state) throws JobDestroyedException
JobDestroyedException
public void setStateException(S state, java.lang.Throwable t, java.util.Date date) throws JobDestroyedException
JobDestroyedException
public void setStateException(State state, java.lang.Throwable ex) throws JobDestroyedException
JobDestroyedException
public State getState()
public void assertAlive() throws JobDestroyedException
JobDestroyedException
- If it has.public void assertLockHeld()
public boolean tryToWhen(StateCondition when, java.lang.Runnable runnable) throws OddjobLockedException
StateLock
OddjobLockedException
is thrown.tryToWhen
in interface StateLock
when
- The condition.runnable
- The action.OddjobLockedException
- If the lock can not be acquired.public boolean waitToWhen(StateCondition when, java.lang.Runnable runnable)
StateLock
waitToWhen
in interface StateLock
when
- The condition.runnable
- The action.public <T> T callLocked(java.util.concurrent.Callable<T> callable)
callable
- The callable.public void sleep(long time) throws java.lang.InterruptedException
time
- The milliseconds to sleep for.java.lang.InterruptedException
- If the sleep is interrupted.public void wake()
sleep(long)
.public void addStateListener(StateListener listener) throws JobDestroyedException
addStateListener
in interface Stateful
listener
- The listener.JobDestroyedException
- If trying to listen to a destroyed job.public void removeStateListener(StateListener listener)
removeStateListener
in interface Stateful
listener
- The listener.public int listenerCount()
public java.lang.String toString()
toString
in class java.lang.Object
public void fireEvent()