Package org.oddjob.state
Class StateHandler<S extends State>
java.lang.Object
org.oddjob.state.StateHandler<S>
- Direct Known Subclasses:
EventStateHandler,JobStateHandler,ParentStateHandler,ServiceStateHandler,TimerStateHandler
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:
- Attempted to make
waitToWhen(StateCondition, Runnable)andinvalid input: '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.</li> </ul> </p> @author Rob Gordon'
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddStateListener(StateListener listener) Add a job state listener.voidConvenience method to check the job hasn't been destroyed.void<T> TcallLocked(Callable<T> callable) Wait to acquire the lock and execute the Callable while holding the lock.voidFire the event, update last event.getState()Return the current state of the job.Get the last event.intThe number of listeners.voidremoveStateListener(StateListener listener) Remove a job state listener.voidrestoreLastJobStateEvent(StateDetail savedEvent) Typically only called after restoring a jobstate handler after deserialisation.voidWait to acquire the lock and execute the Runnable while holding the lock.voidSet the state.voidDeprecated, for removal: This API element is subject to removal in a future version.voidsetState(S state, StateInstant instant) Set the state.voidsetStateException(State state, Throwable ex) Set the exception state.voidsetStateException(S state, Throwable t, StateInstant instant) Set the Exception state and time instant of the event.voidsleep(long time) Sleep.<T> TsupplyLocked(Supplier<T> supplier) Wait to acquire the lock and execute the Supplier while holding the lock.toString()Override toString.booleantryToWhen(StateCondition when, Runnable runnable) Try to acquire the lock, and then do something when the condition is true.booleanwaitToWhen(StateCondition when, Runnable runnable) Wait to do something when the condition is true.voidwake()Wake any threads that are sleeping viasleep(long).
-
Constructor Details
-
StateHandler
Constructor.- Parameters:
source- The source for events.readyState- The ready state.
-
-
Method Details
-
lastStateEvent
Get the last event.- Specified by:
lastStateEventin interfaceStateful- Returns:
- The last event.
-
restoreLastJobStateEvent
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.Deprecated - usesetState(State, StateInstant).- Throws:
JobDestroyedException
-
setState
Set the state.- Parameters:
state- The state.instant- The time instant for the event.- Throws:
JobDestroyedException- If the current state is DESTROYED.
-
setState
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 statet- The exception.instant- The time instant for the event.- Throws:
JobDestroyedException- If the current state is DESTROYED.
-
setStateException
Set the exception state.- Parameters:
state- The exception state.ex- The exception.- Throws:
JobDestroyedException- If the existing state is DESTROYED.- See Also:
-
getState
Return the current state of the job.- Returns:
- The current state.
-
assertAlive
Convenience method to check the job hasn't been destroyed.- Throws:
JobDestroyedException- If it has.
-
assertLockHeld
public void assertLockHeld() -
tryToWhen
Description copied from interface:StateLockTry 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 anOddjobLockedExceptionis thrown.- Specified by:
tryToWhenin interfaceStateLock- 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
Description copied from interface:StateLockWait 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:
waitToWhenin interfaceStateLock- Parameters:
when- The condition.runnable- The action.- Returns:
- true if the condition was true and the action executed.
-
runLocked
Wait to acquire the lock and execute the Runnable while holding the lock. -
callLocked
Wait to acquire the lock and execute the Callable while holding the lock.- Specified by:
callLockedin interfaceStateLock- Parameters:
callable- The callable.- Returns:
- The result of the callable.
- Throws:
Exception- from the callable.
-
supplyLocked
Wait to acquire the lock and execute the Supplier while holding the lock.- Specified by:
supplyLockedin interfaceStateLock- Parameters:
supplier- The Supplier.- Returns:
- The result from the Supplier.
-
sleep
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 viasleep(long). -
addStateListener
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:
addStateListenerin interfaceStateful- Parameters:
listener- The listener.- Throws:
JobDestroyedException- If trying to listen to a destroyed job.
-
removeStateListener
Remove a job state listener.- Specified by:
removeStateListenerin interfaceStateful- Parameters:
listener- The listener.
-
listenerCount
public int listenerCount()The number of listeners.- Returns:
- The number of listeners.
-
toString
Override toString. -
fireEvent
public void fireEvent()Fire the event, update last event.
-