Package org.oddjob.events
Class When<T>
- All Implemented Interfaces:
Serializable,Runnable,Consumer<T>,ArooaContextAware,ArooaSessionAware,Outbound<T>,Forceable,PropertyChangeNotifier,Iconic,LogEnabled,Resettable,Stateful,Stoppable,Structural
- Author:
- Rob Gordon.
- See Also:
Description
Runs a job when triggered by the arrival of an event. The job will be re-run every time the event arrives. If the job is still running when a new event arrives, the job will attempt to be stopped and rerun. A typical use case would be processing a file when it arrives, but which may be re-sent with more up-to-date information.Example
Evaluating greengrocer portfolios of fruit when data arrives.
<oddjob id="oddjob">
<job>
<sequential>
<jobs>
<properties name="Properties">
<values>
<file file="${oddjob.dir}/data" key="data.dir"/>
</values>
</properties>
<bean class="org.oddjob.events.example.FileFactStore" id="factStore" rootDir="${data.dir}"/>
<events:when id="whenBookList" name="When BookList Available" xmlns:events="oddjob:events">
<jobs>
<bean class="org.oddjob.events.example.FactSubscriber" factStore="${factStore}" name="Subscribe to BookList" query="BookList:GREENGROCERS"/>
<foreach id="forEachBook" name="For Each Book">
<values>
<value value="${whenBookList.trigger.of.books}"/>
</values>
<configuration>
<inline>
<foreach id="bookName">
<job>
<events:when id="whenBook" name="When ${bookName.current}">
<jobs>
<bean class="org.oddjob.events.example.FactSubscriber" factStore="${factStore}" name="Subscribe to Book ${bookName.current}" query="Book:${bookName.current}"/>
<events:when id="priceMatch" name="When Prices for ${bookName.current}">
<jobs>
<events:for name="For Each Trade">
<configuration>
<inline>
<events id="trade">
<job>
<bean class="org.oddjob.events.example.FactSubscriber" factStore="${factStore}" name="Subscribe to Price for ${trade.current.product}" query="Price:${trade.current.product}"/>
</job>
</events>
</inline>
</configuration>
<values>
<value value="${whenBook.trigger.of.trades}"/>
</values>
</events:for>
<sequential name="Run Calculation">
<jobs>
<bean class="org.oddjob.events.example.ValueCalculator" id="calculate">
<trades>
<value value="${whenBook.trigger.of.trades}"/>
</trades>
<prices>
<value value="${priceMatch.trigger.ofs}"/>
</prices>
</bean>
<echo>Value of ${bookName.current} is ${calculate.value}</echo>
</jobs>
</sequential>
</jobs>
</events:when>
</jobs>
</events:when>
</job>
</foreach>
</inline>
</configuration>
</foreach>
</jobs>
</events:when>
</jobs>
</sequential>
</job>
</oddjob>
Being a destination in a pipeline.
<oddjob>
<job>
<sequential>
<jobs>
<bus:bus xmlns:bus="oddjob:beanbus">
<of>
<bean class="org.oddjob.events.WhenTest$PretendFileWatcher" id="fileWatcher"/>
<bus:filter id="filter">
<predicate>
<bean class="org.oddjob.events.WhenTest$OnlyTxtFiles"/>
</predicate>
</bus:filter>
<events:when beDestination="true" id="when" xmlns:events="oddjob:events">
<jobs>
<echo id="result">
Result: ${when.trigger}
</echo>
</jobs>
</events:when>
</of>
</bus:bus>
<folder>
<jobs>
<set id="set1" name="Set File Name 1">
<values>
<value key="fileWatcher.someFileName" value="Fruit.txt"/>
</values>
</set>
<set id="set2" name="Set File Name 2">
<values>
<value key="fileWatcher.someFileName" value="Names.doc"/>
</values>
</set>
<set id="set3" name="Set File Name 3">
<values>
<value key="fileWatcher.someFileName" value="Prices.txt"/>
</values>
</set>
</jobs>
</folder>
</jobs>
</sequential>
</job>
</oddjob>
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThe Conversion from Stringstatic interfacestatic enumstatic interface -
Field Summary
Fields inherited from class org.oddjob.framework.extend.StructuralJob
childHelper, destroy, stop, structuralState -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidonImmediateEvent(T event) Do something with an event received immediately before the subscribe method has returned.protected voidonLaterEvent(T event, Object job, Executor executor) Called when an event is received after subscription.protected voidonSubscriptionStarted(Object job, Executor executor) Called once the subscription has started.voidsetHaltOn(StateCondition haltOn) voidsetTriggerStrategy(When.TriggerStrategy triggerStrategy) protected StateListenerUsed by subclasses to provide a state listener to be added to the child job.Methods inherited from class org.oddjob.events.EventJobBase
accept, execute, getEventSource, getInitialStateOp, getTo, getTrigger, isBeDestination, onStop, postStop, setBeDestination, setEventSource, setExecutorService, setJobs, setTo, setTrigger, startChildStateReflector, switchToChildStateReflector, unsubscribeMethods inherited from class org.oddjob.framework.extend.StructuralJob
addStructuralListener, fireDestroyedState, force, getStateChanger, hardReset, iconHelper, isStop, onDestroy, onHardReset, onReset, onSoftReset, 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
-
Constructor Details
-
When
public When()
-
-
Method Details
-
onImmediateEvent
Description copied from class:EventJobBaseDo something with an event received immediately before the subscribe method has returned.- Specified by:
onImmediateEventin classEventJobBase<T>- Parameters:
event- The event.
-
onSubscriptionStarted
Description copied from class:EventJobBaseCalled once the subscription has started.- Specified by:
onSubscriptionStartedin classEventJobBase<T>- Parameters:
job- The job to execute if any.executor- The executor to use to execute the job.
-
onLaterEvent
Description copied from class:EventJobBaseCalled when an event is received after subscription. There is no guarantee that this will be called after theEventJobBase.onSubscriptionStarted(Object, Executor)method, implementors are responsible for their own synchronisation if required.- Specified by:
onLaterEventin classEventJobBase<T>- Parameters:
event- The event.
-
stateOnChildComplete
Description copied from class:EventJobBaseUsed by subclasses to provide a state listener to be added to the child job.- Specified by:
stateOnChildCompletein classEventJobBase<T>- Returns:
- A State Listener. Must not be null.
-
getTriggerStrategy
-
setTriggerStrategy
-
getHaltOn
-
setHaltOn
-