public class StateHandler<S extends State> extends 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(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,
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) . |
public StateEvent lastStateEvent()
lastStateEvent
in interface Stateful
public void restoreLastJobStateEvent(StateEvent.SerializableNoSource savedEvent)
source
- public void setState(S state, Date date) throws JobDestroyedException
JobDestroyedException
public void setState(S state) throws JobDestroyedException
JobDestroyedException
public void setStateException(S state, Throwable t, Date date) throws JobDestroyedException
JobDestroyedException
public void setStateException(State state, 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, 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, Runnable runnable)
StateLock
waitToWhen
in interface StateLock
when
- The condition.runnable
- The action.public <T> T callLocked(Callable<T> callable)
callable
- The callable.public void sleep(long time) throws InterruptedException
time
- InterruptedException
public void wake()
sleep(long)
.public void addStateListener(StateListener listener) throws JobDestroyedException
addStateListener
in interface Stateful
listener
- The listener.JobDestroyedException
public void removeStateListener(StateListener listener)
removeStateListener
in interface Stateful
listener
- The listener.public int listenerCount()
public void fireEvent()
event
- The event.