public class StateHandler<S extends State> extends java.lang.Object implements Stateful, StateLock
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:
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)
Wait to acquire the lock and execute the Callable while holding the lock.
|
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 |
runLocked(java.lang.Runnable runnable)
Wait to acquire the lock and execute the Runnable while holding the lock.
|
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.
|
<T> T |
supplyLocked(java.util.function.Supplier<T> supplier)
Wait to acquire the lock and execute the Supplier while holding the lock.
|
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 Statefulpublic void restoreLastJobStateEvent(StateEvent.SerializableNoSource savedEvent)
savedEvent - The last serialised event.public void setState(S state, java.util.Date date) throws JobDestroyedException
JobDestroyedExceptionpublic void setState(S state) throws JobDestroyedException
JobDestroyedExceptionpublic void setStateException(S state, java.lang.Throwable t, java.util.Date date) throws JobDestroyedException
JobDestroyedExceptionpublic void setStateException(State state, java.lang.Throwable ex) throws JobDestroyedException
JobDestroyedExceptionpublic 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
StateLockOddjobLockedException
is thrown.tryToWhen in interface StateLockwhen - The condition.runnable - The action.OddjobLockedException - If the lock can not be acquired.public boolean waitToWhen(StateCondition when, java.lang.Runnable runnable)
StateLockwaitToWhen in interface StateLockwhen - The condition.runnable - The action.public void runLocked(java.lang.Runnable runnable)
public <T> T callLocked(java.util.concurrent.Callable<T> callable)
throws java.lang.Exception
callLocked in interface StateLockcallable - The callable.java.lang.Exception - from the callable.public <T> T supplyLocked(java.util.function.Supplier<T> supplier)
supplyLocked in interface StateLocksupplier - The Supplier.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 Statefullistener - The listener.JobDestroyedException - If trying to listen to a destroyed job.public void removeStateListener(StateListener listener)
removeStateListener in interface Statefullistener - The listener.public int listenerCount()
public java.lang.String toString()
toString in class java.lang.Objectpublic void fireEvent()