public class ForEachJob extends StructuralJob<java.lang.Object> implements Stoppable, Loadable, ConfigurationOwner, BeanDirectoryOwner
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.
<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 Green
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.
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>
<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>
Modifier and Type | Class and Description |
---|---|
class |
ForEachJob.LocalBean
This provides a bean for current properties.
|
Modifier and Type | Field and Description |
---|---|
static ArooaElement |
FOREACH_ELEMENT
Root element for configuration.
|
childHelper, destroy, stop, structuralState
Constructor and Description |
---|
ForEachJob()
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
addOwnerStateListener(OwnerStateListener listener)
Add a listener.
|
protected void |
execute()
Execute this job.
|
ArooaConfiguration |
getConfiguration() |
java.lang.Object |
getCurrent()
The current value.
|
java.util.concurrent.ExecutorService |
getExecutorService() |
java.io.File |
getFile() |
int |
getIndex() |
protected StateOperator |
getInitialStateOp()
Subclasses must provide the
StateOperator that will decide
how to evaluate the children's state. |
int |
getPreLoad() |
int |
getPurgeAfter() |
boolean |
hardReset()
Perform a hard reset on the job.
|
boolean |
isLoadable()
Is the component currently loadable.
|
boolean |
isParallel() |
boolean |
isStop()
Expose the internal stop flag as a read only property.
|
void |
load()
Load the component.
|
protected java.lang.Object |
loadConfigFor(java.lang.Object value)
Load a configuration for a single value.
|
protected void |
onDestroy()
Subclasses override this method to clear up resources.
|
protected void |
onStop()
Allow sub classes to do something on stop.
|
protected void |
preLoad()
Setup and load the first jobs.
|
BeanDirectory |
provideBeanDirectory()
Get the
BeanDirectory . |
ConfigurationSession |
provideConfigurationSession()
Provide a
ConfigurationSession . |
void |
removeOwnerStateListener(OwnerStateListener listener)
Remove a listener.
|
SerializableDesignFactory |
rootDesignFactory()
Get the design factory for the configuration.
|
ArooaElement |
rootElement()
Get the root element.
|
void |
setConfiguration(ArooaConfiguration configuration) |
void |
setExecutorService(java.util.concurrent.ExecutorService executorService)
Set the
ExecutorService . |
void |
setFile(java.io.File file) |
void |
setParallel(boolean parallel) |
void |
setPreLoad(int preLoad) |
void |
setPurgeAfter(int purgeAfter) |
void |
setValues(java.lang.Iterable<?> values)
Set the values to iterate over.
|
protected void |
startChildStateReflector()
Start the child state reflector.
|
void |
unload()
Unload the component.
|
addStructuralListener, fireDestroyedState, force, getStateChanger, iconHelper, onHardReset, onReset, onSoftReset, postStop, removeStructuralListener, run, softReset, stateHandler, stop, stopChildStateReflector, waitForChildrenOnStop
configure, getName, logger, logger, loggerName, save, setName, toString
addIconListener, addPropertyChangeListener, addStateListener, configure, destroy, firePropertyChange, getArooaSession, iconForId, initialise, lastStateEvent, onConfigured, onInitialised, removeIconListener, removePropertyChangeListener, removeStateListener, save, setArooaContext, setArooaSession
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
addStateListener, lastStateEvent, removeStateListener
public static final ArooaElement FOREACH_ELEMENT
@Inject public void setExecutorService(java.util.concurrent.ExecutorService executorService)
ExecutorService
.Property: executorService
Description: The ExecutorService to use. This will be automatically set by Oddjob.
Required: No.
executorService
- The Executor Service.public java.util.concurrent.ExecutorService getExecutorService()
public java.lang.Object getCurrent()
public void setValues(java.lang.Iterable<?> values)
values
- The values.protected StateOperator getInitialStateOp()
StructuralJob
StateOperator
that will decide
how to evaluate the children's state.getInitialStateOp
in class StructuralJob<java.lang.Object>
public ConfigurationSession provideConfigurationSession()
ConfigurationOwner
ConfigurationSession
.provideConfigurationSession
in interface ConfigurationOwner
ConfigurationSession
. May be null if no session is available.public void addOwnerStateListener(OwnerStateListener listener)
ConfigurationOwner
addOwnerStateListener
in interface ConfigurationOwner
public void removeOwnerStateListener(OwnerStateListener listener)
ConfigurationOwner
removeOwnerStateListener
in interface ConfigurationOwner
public SerializableDesignFactory rootDesignFactory()
ConfigurationOwner
Note that this is a SerializableDesignFactory
so that
this interface can be represented remotely.
rootDesignFactory
in interface ConfigurationOwner
ConfigurationSession
is available.public ArooaElement rootElement()
ConfigurationOwner
rootElement
in interface ConfigurationOwner
ConfigurationSession
is available.protected java.lang.Object loadConfigFor(java.lang.Object value) throws ArooaParseException
value
- The valueArooaParseException
- If the config can't be parsed.public BeanDirectory provideBeanDirectory()
BeanDirectoryOwner
BeanDirectory
. This method may return null if
the BeanDirectory isn't available.provideBeanDirectory
in interface BeanDirectoryOwner
BeanDirectory
or null.protected void preLoad() throws ArooaParseException
if preLoad
is 0 all will be loaded otherwise up to
that number will be loaded.
ArooaParseException
public void load()
Loadable
public void unload()
Loadable
public boolean isLoadable()
Loadable
isLoadable
in interface Loadable
protected void execute() throws java.lang.Exception
StructuralJob
execute
in class StructuralJob<java.lang.Object>
java.lang.Exception
- If the unexpected occurs.protected void startChildStateReflector()
StructuralJob
startChildStateReflector
in class StructuralJob<java.lang.Object>
protected void onStop() throws FailedToStopException
StructuralJob
onStop
in class StructuralJob<java.lang.Object>
FailedToStopException
public int getIndex()
protected void onDestroy()
BaseComponent
onDestroy
in class StructuralJob<java.lang.Object>
public boolean hardReset()
hardReset
in interface Resettable
hardReset
in class StructuralJob<java.lang.Object>
public boolean isStop()
StructuralJob
isStop
in class StructuralJob<java.lang.Object>
Property: stop
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.
Required: Read Only.
public void setFile(java.io.File file)
Property: file
Description: The name of the configuration file. to use for configuration.
Required: No.
public java.io.File getFile()
public ArooaConfiguration getConfiguration()
public void setConfiguration(ArooaConfiguration configuration)
public int getPreLoad()
public void setPreLoad(int preLoad)
public int getPurgeAfter()
public void setPurgeAfter(int purgeAfter)
public boolean isParallel()
public void setParallel(boolean parallel)