Package org.oddjob.state
Class IfJob
- All Implemented Interfaces:
Serializable,Runnable,ArooaContextAware,ArooaSessionAware,Forceable,PropertyChangeNotifier,Iconic,LogEnabled,Resettable,Stateful,Stoppable,Structural
public class IfJob
extends StructuralJob<Object>
implements Runnable, Stateful, Resettable, Structural, Stoppable
- Author:
- Rob Gordon
- See Also:
Description
This job implements an if/then/else logic based on job state. This job can contain any number of child jobs. The first provides the state for the condition. If this state matches the given state, the second job is executed. If it doesn't, then the third job is executed, (if it exists).The completion state is that of the then or else job. If either don't exist then the Job is flagged as complete.
If any more than three jobs are provided the extra jobs are ignored.
If the first job enters an ACTIVE state then condition will not be evaluated until the first job leaves the ACTIVE state. This job will not block while this is happening. The thread of execution will pass to its next sibling and this job will also enter the ACTIVE state.
Example
If a file exists.
<oddjob id="this" xmlns:state="http://rgordon.co.uk/oddjob/state">
<job>
<state:if>
<jobs>
<exists name="Check File Exists" file="${this.dir}/data/some.txt"/>
<echo id="then" name="Echo to Console">File Exists</echo>
<echo id="else" name="Echo to Console">File Doesn't Exist</echo>
</jobs>
</state:if>
</job>
</oddjob>
An example showing lots of if's. All these if's go to COMPLETE state
when run.
<oddjob>
<job>
<sequential xmlns:state="http://rgordon.co.uk/oddjob/state">
<jobs>
<state:if>
<jobs>
<echo>Hello</echo>
<echo>Good Bye</echo>
</jobs>
</state:if>
<state:if>
<jobs>
<state:flag name="Exception" state="EXCEPTION"/>
<state:flag name="Unexpected 1" state="EXCEPTION"/>
<echo>No Hello</echo>
</jobs>
</state:if>
<state:if>
<jobs>
<echo>Only Hello</echo>
</jobs>
</state:if>
<state:if state="!COMPLETE">
<jobs>
<state:flag name="Exception" state="EXCEPTION"/>
<echo>No Hello</echo>
</jobs>
</state:if>
<state:if state="!COMPLETE">
<jobs>
<echo>Hello</echo>
<state:flag name="Unexpected 2" state="EXCEPTION"/>
</jobs>
</state:if>
<state:if state="!EXCEPTION">
<jobs>
<echo>Hello</echo>
<echo>Good Bye</echo>
<state:flag name="Unexpected 3" state="EXCEPTION"/>
</jobs>
</state:if>
</jobs>
</sequential>
</job>
</oddjob>
Asynchronous evaluation. Only when the first job moves beyond it's ACTIVE
state will the condition be evaluated and the then job (second job)
be executed. The execution of the second job is also asynchronous.
<oddjob>
<job>
<state:if id="if-job" xmlns:state="http://rgordon.co.uk/oddjob/state">
<jobs>
<parallel>
<jobs>
<state:flag/>
</jobs>
</parallel>
<echo id="then-job">That Worked!</echo>
<echo id="else-job">This should never be shown.</echo>
</jobs>
</state:if>
</job>
</oddjob>
-
Field Summary
Fields inherited from class org.oddjob.framework.extend.StructuralJob
childHelper, destroy, stop, structuralState -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected ExecutorServiceprotected voidexecute()Execute this job.protected StateOperatorSubclasses must provide theStateOperatorthat will decide how to evaluate the children's state.getState()Getter for state.protected voidonStop()Allow sub classes to do something on stop.voidsetArooaContext(ArooaContext context) voidsetExecutorService(ExecutorService executorService) voidvoidsetState(StateCondition state) protected voidStart the child state reflector.Methods inherited from class org.oddjob.framework.extend.StructuralJob
addStructuralListener, fireDestroyedState, force, getStateChanger, hardReset, iconHelper, isStop, onDestroy, onHardReset, onReset, onSoftReset, postStop, removeStructuralListener, run, softReset, stateHandler, stop, stopChildStateReflector, waitForChildrenOnStopMethods inherited from class org.oddjob.framework.extend.BasePrimary
configure, getName, logger, logger, loggerName, save, setName, toStringMethods inherited from class org.oddjob.framework.extend.BaseComponent
addIconListener, addPropertyChangeListener, addStateListener, configure, destroy, firePropertyChange, getArooaSession, iconForId, initialise, lastStateEvent, onConfigured, onInitialised, removeIconListener, removePropertyChangeListener, removeStateListener, save, setArooaSessionMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.oddjob.Resettable
hardReset, softResetMethods inherited from interface org.oddjob.Stateful
addStateListener, lastStateEvent, removeStateListenerMethods inherited from interface org.oddjob.Structural
addStructuralListener, removeStructuralListener
-
Constructor Details
-
IfJob
public IfJob()
-
-
Method Details
-
setArooaContext
- Specified by:
setArooaContextin interfaceArooaContextAware- Overrides:
setArooaContextin classBaseComponent
-
getState
Getter for state.- Returns:
- The state.
-
setState
- Reference Property:
- state
- Required:
- No, defaults to COMPLETE.
Description
The state condition to check against. See the Oddjob User guide for a full list of state conditions. -
setJobs
- Reference Property:
- jobs
- Required:
- At least one.
Description
The child jobs. -
getInitialStateOp
Description copied from class:StructuralJobSubclasses must provide theStateOperatorthat will decide how to evaluate the children's state.- Specified by:
getInitialStateOpin classStructuralJob<Object>- Returns:
- A State Operator. Must not be null.
-
execute
protected void execute()Description copied from class:StructuralJobExecute this job.- Specified by:
executein classStructuralJob<Object>
-
onStop
Description copied from class:StructuralJobAllow sub classes to do something on stop.- Overrides:
onStopin classStructuralJob<Object>- Throws:
FailedToStopException
-
startChildStateReflector
protected void startChildStateReflector()Description copied from class:StructuralJobStart the child state reflector. Sub classes override this if they need to start the child state reflector at a different time.- Overrides:
startChildStateReflectorin classStructuralJob<Object>
-
ensureExecutorService
-
getExecutorService
-
setExecutorService
-