Class ForEachJob
- All Implemented Interfaces:
Serializable,Runnable,ArooaContextAware,ArooaSessionAware,ConfigurationOwner,BeanDirectoryOwner,Forceable,PropertyChangeNotifier,Iconic,Loadable,LogEnabled,Resettable,Stateful,Stoppable,Structural
- See Also:
Description
A job which executes its child jobs for each of the provided values. The child job can access the current value using the pseudo property 'current' to gain access to the current value. The pseudo property 'index' provides a 0 based number for the instance.
The return state of this job depends on the return state
of the children (like SequentialJob). Hard resetting this job
will cause the children to be destroyed and recreated on the next run
(with possibly new values). Soft resetting this job will reset the
children but when re-run will not reconfigure the values.
As yet There is no persistence for child jobs.
It is not possible to reference the internal jobs via their id from outside the foreach job, but within the foreach internal configuration they can reference each other and themselves via their ids.
Example
For each of 3 values.
<oddjob id="this" xmlns:arooa="http://rgordon.co.uk/oddjob/arooa">
<job>
<foreach id="foreach">
<values>
<list>
<values>
<value value="Red"/>
<value value="Blue"/>
<value value="Green"/>
</values>
</list>
</values>
<configuration>
<arooa:configuration resource="org/oddjob/jobs/structural/ForEachEchoColour.xml"/>
</configuration>
</foreach>
</job>
</oddjob>
The internal configuration is:
<foreach id="colours">
<job>
<echo id="echo-colour" name="${colours.current}">I'm number ${colours.index} and my name is ${echo-colour.name}</echo>
</job>
</foreach>
Unlike other jobs, a job in a for each has it's name configured when it is loaded, before it is run. The job references its self using its id.
This example will display the following on the console:
I'm number 0 and my name is Red I'm number 1 and my name is Blue I'm number 2 and my name is GreenFor each of 3 files. The 3 files
test1.txt,
test2.txt and test3.txt are
copied to the work/foreach directory. The oddjob argument
${this.args[0]} is so that a base directory can be passed
in as part of the unit test for this example.
<oddjob id="this" xmlns:arooa="http://rgordon.co.uk/oddjob/arooa">
<job>
<foreach>
<values>
<files files="${base.dir}/test/io/reference/test?.txt"/>
</values>
<configuration>
<arooa:configuration>
<xml>
<xml>
<foreach id="copy-files">
<job>
<copy to="${some.dir}">
<from>
<value value="${copy-files.current}"/>
</from>
</copy>
</job>
</foreach>
</xml>
</xml>
</arooa:configuration>
</configuration>
</foreach>
</job>
</oddjob>
Also ExistsJob has a similar example.Executing children in parallel. This example uses a
ExecutorThrottleType to limit the number of parallel
executions to three.
<oddjob>
<job>
<foreach parallel="true">
<values>
<tokenizer text="1,2,3,4,5,6,7,8,9"/>
</values>
<configuration>
<xml>
<foreach id="loop">
<job>
<wait name="Wait ${loop.current}"/>
</job>
</foreach>
</xml>
</configuration>
<executorService>
<throttle limit="3"/>
</executorService>
</foreach>
</job>
</oddjob>
Using an execution window. Only the configuration for two jobs will be
pre-loaded, and only the last three complete jobs will remain loaded.
<oddjob id="this">
<job>
<foreach preLoad="2" purgeAfter="3">
<values>
<tokenizer text="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15"/>
</values>
<configuration>
<xml>
<foreach id="loop">
<job>
<wait name="Wait ${loop.current} " pause="1"/>
</job>
</foreach>
</xml>
</configuration>
</foreach>
</job>
</oddjob>
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassThis provides a bean for current properties. -
Field Summary
FieldsFields inherited from class org.oddjob.framework.extend.StructuralJob
childHelper, destroy, stop, structuralState -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddOwnerStateListener(OwnerStateListener listener) Add a listener.protected voidexecute()Execute this job.The current value.getFile()intgetIndex()protected StateOperatorSubclasses must provide theStateOperatorthat will decide how to evaluate the children's state.intintbooleanPerform a hard reset on the job.booleanIs the component currently loadable.booleanbooleanisStop()Expose the internal stop flag as a read only property.voidload()Load the component.protected ObjectloadConfigFor(Object value) Load a configuration for a single value.protected voidSubclasses override this method to clear up resources.protected voidonStop()Allow sub classes to do something on stop.protected voidpreLoad()Setup and load the first jobs.Get theBeanDirectory.Provide aConfigurationSession.voidremoveOwnerStateListener(OwnerStateListener listener) Remove a listener.Get the design factory for the configuration.Get the root element.voidsetConfiguration(ArooaConfiguration configuration) voidsetExecutorService(ExecutorService executorService) Set theExecutorService.voidvoidsetParallel(boolean parallel) voidsetPreLoad(int preLoad) voidsetPurgeAfter(int purgeAfter) voidSet the values to iterate over.protected voidStart the child state reflector.voidunload()Unload the component.Methods inherited from class org.oddjob.framework.extend.StructuralJob
addStructuralListener, fireDestroyedState, force, getStateChanger, iconHelper, 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, setArooaContext, setArooaSessionMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.oddjob.Stateful
addStateListener, lastStateEvent, removeStateListener
-
Field Details
-
FOREACH_ELEMENT
Root element for configuration.
-
-
Constructor Details
-
ForEachJob
public ForEachJob()Constructor.
-
-
Method Details
-
setExecutorService
Set theExecutorService.- Parameters:
executorService- The Executor Service.- Reference Property:
- executorService
- Required:
- No.
Description
The ExecutorService to use. This will be automatically set by Oddjob. -
getExecutorService
-
getCurrent
The current value.- Returns:
- The current value.
-
setValues
Set the values to iterate over.- Parameters:
values- The values.
-
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.
-
provideConfigurationSession
Description copied from interface:ConfigurationOwnerProvide aConfigurationSession.- Specified by:
provideConfigurationSessionin interfaceConfigurationOwner- Returns:
- A
ConfigurationSession. May be null if no session is available.
-
addOwnerStateListener
Description copied from interface:ConfigurationOwnerAdd a listener.- Specified by:
addOwnerStateListenerin interfaceConfigurationOwner- Parameters:
listener-
-
removeOwnerStateListener
Description copied from interface:ConfigurationOwnerRemove a listener.- Specified by:
removeOwnerStateListenerin interfaceConfigurationOwner- Parameters:
listener-
-
rootDesignFactory
Description copied from interface:ConfigurationOwnerGet the design factory for the configuration. If this is null the Oddjob Explorer won't show a DesignInside action.Note that this is a
SerializableDesignFactoryso that this interface can be represented remotely.- Specified by:
rootDesignFactoryin interfaceConfigurationOwner- Returns:
- A DesignFactory. Must not be null if a
ConfigurationSessionis available.
-
rootElement
Description copied from interface:ConfigurationOwnerGet the root element.- Specified by:
rootElementin interfaceConfigurationOwner- Returns:
- The root element of the configuration. Must not be null if
a
ConfigurationSessionis available.
-
loadConfigFor
Load a configuration for a single value.- Parameters:
value- The value- Returns:
- The root component in the child configuration.
- Throws:
ArooaParseException- If the config can't be parsed.
-
provideBeanDirectory
Description copied from interface:BeanDirectoryOwnerGet theBeanDirectory. This method may return null if the BeanDirectory isn't available.- Specified by:
provideBeanDirectoryin interfaceBeanDirectoryOwner- Returns:
- The
BeanDirectoryor null.
-
preLoad
Setup and load the first jobs.if
preLoadis 0 all will be loaded otherwise up to that number will be loaded.- Throws:
ArooaParseException
-
load
public void load()Description copied from interface:LoadableLoad the component. -
unload
public void unload()Description copied from interface:LoadableUnload the component. -
isLoadable
public boolean isLoadable()Description copied from interface:LoadableIs the component currently loadable.- Specified by:
isLoadablein interfaceLoadable- Returns:
- true if the component can be loaded, false otherwise.
-
execute
Description copied from class:StructuralJobExecute this job.- Specified by:
executein classStructuralJob<Object>- Throws:
Exception- If the unexpected occurs.
-
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>
-
onStop
Description copied from class:StructuralJobAllow sub classes to do something on stop.- Overrides:
onStopin classStructuralJob<Object>- Throws:
FailedToStopException
-
getIndex
public int getIndex()- Returns:
- Returns the index.
-
onDestroy
protected void onDestroy()Description copied from class:BaseComponentSubclasses override this method to clear up resources. This is called by the framework before child elements have been destroyed.- Overrides:
onDestroyin classStructuralJob<Object>
-
hardReset
public boolean hardReset()Perform a hard reset on the job.- Specified by:
hardResetin interfaceResettable- Overrides:
hardResetin classStructuralJob<Object>- Returns:
- true if successful.
-
isStop
public boolean isStop()Description copied from class:StructuralJobExpose the internal stop flag as a read only property.- Overrides:
isStopin classStructuralJob<Object>- Returns:
- The file name.
- Reference Property:
- stop
- Required:
- Read Only.
Description
The stop flag. This is an internal read only property that is exposed for diagnostic reasons. If a child job does not support stopping then the request to stop may time out but it is useful to know that the stop flag is still set so this job will still stop eventually. -
setFile
- Reference Property:
- file
- Required:
- No.
Description
The name of the configuration file. to use for configuration. -
getFile
-
getConfiguration
-
setConfiguration
-
getPreLoad
public int getPreLoad() -
setPreLoad
public void setPreLoad(int preLoad) -
getPurgeAfter
public int getPurgeAfter() -
setPurgeAfter
public void setPurgeAfter(int purgeAfter) -
isParallel
public boolean isParallel() -
setParallel
public void setParallel(boolean parallel)
-