Package org.copperengine.core
Class Workflow<D>
- java.lang.Object
-
- org.copperengine.core.Workflow<D>
-
- Type Parameters:
D
- workflow'sdata
class
- All Implemented Interfaces:
java.io.Serializable
- Direct Known Subclasses:
PersistentWorkflow
public abstract class Workflow<D> extends java.lang.Object implements java.io.Serializable
Abstract COPPER workflow base class. Workflows must inherit from this class. If your workflow requires (or maybe requires) persistence, then it has to inherit fromPersistentWorkflow
.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Stack<StackEntry>
__stack
for internal use onlyprotected int
__stackPosition
for internal use onlystatic int
NO_TIMEOUT
Constant value forwait(WaitMode, int, Callback...)
andwait(WaitMode, int, String...)
indicating that there is no timeout
-
Constructor Summary
Constructors Modifier Constructor Description protected
Workflow()
Creates a new instance
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
__beforeProcess()
protected <E> Callback<E>
createCallback()
Creates aCallback
object used for the asynchronous wait for some reponse.protected Acknowledge
createCheckpointAcknowledge()
We create a quasi empty acknowledge here.java.util.List<StackEntry>
get__stack()
For internal usage onlyprotected <T> Response<T>
getAndRemoveResponse(java.lang.String correlationId)
Gets and removes a response for the specified correlation id.protected <T> java.util.List<Response<T>>
getAndRemoveResponses(java.lang.String correlationId)
Gets and removes all responses for the specified correlation idprotected <T> Response<T>
getAnyNonTimedOutAndRemoveResponse(java.lang.Class<? extends T> type)
This method is similar to getAndRemoveResponse but a convenient extension for some usage scenarios.java.util.Date
getCreationTS()
D
getData()
ProcessingEngine
getEngine()
java.lang.String
getId()
java.util.Date
getLastActivityTS()
java.lang.String
getLastWaitStackTrace()
int
getPriority()
ProcessingState
getProcessingState()
java.lang.String
getProcessorPoolId()
java.util.Date
getTimeoutTS()
abstract void
main()
Entry point for this workflowprotected <T> void
notify(Response<T> response)
java.lang.String
prettyPrintData()
void
putResponse(Response<?> r)
Internal use only - called by the processing engineprotected void
registerCheckpointAcknowledge(Acknowledge ack)
protected void
registerSavepointAware(SavepointAware sa)
protected void
resubmit()
Causes the engine to stop processing of this workflow instance and to enqueue it again.protected void
savepoint()
Causes the engine to stop processing of this workflow instance and to enqueue it again.void
setData(D data)
Sets the data for this workflow instance.void
setEngine(ProcessingEngine engine)
For internal use onlyvoid
setId(java.lang.String id)
void
setPriority(int priority)
Sets the priority for this workflow instance.void
setProcessorPoolId(java.lang.String processorPoolId)
Sets the processor pool id for this workflow instance.java.lang.String
toString()
protected void
wait(WaitMode mode, int timeoutMsec, java.lang.String... correlationIds)
Generic wait/sleep.protected void
wait(WaitMode mode, int timeoutMsec, Callback<?>... callbacks)
protected void
wait(WaitMode mode, long timeout, java.util.concurrent.TimeUnit timeUnit, java.lang.String... correlationIds)
Generic wait/sleep.protected void
wait(WaitMode mode, long timeout, java.util.concurrent.TimeUnit timeUnit, Callback<?>... callbacks)
Generic wait/sleep.protected void
waitForAll(java.lang.String... correlationIds)
waits/sleeps until a response for every correlation id occursprotected void
waitForAll(Callback<?>... callbacks)
waits/sleeps until a response for every callback occurs
-
-
-
Field Detail
-
NO_TIMEOUT
public static final int NO_TIMEOUT
Constant value forwait(WaitMode, int, Callback...)
andwait(WaitMode, int, String...)
indicating that there is no timeout- See Also:
- Constant Field Values
-
__stack
protected java.util.Stack<StackEntry> __stack
for internal use only
-
__stackPosition
protected transient int __stackPosition
for internal use only
-
-
Method Detail
-
getProcessingState
public ProcessingState getProcessingState()
- Returns:
- the processing state
-
getPriority
public int getPriority()
- Returns:
- the workflow instance priority
-
setPriority
public void setPriority(int priority)
Sets the priority for this workflow instance. A smaller value is a higher priority, e.g. a priority of 1 is higher than 2.- Parameters:
priority
- new priority (Takes effect after next checkpoint (wait/resubmit/savepoint) for prioritizing waiting..
-
setEngine
public void setEngine(ProcessingEngine engine)
For internal use only- Parameters:
engine
- engine which (currently) owns this workflow. [For a distributed setup it might happen that another engine takes over the workflow for computation. But the workflow can always callgetEngine()
and receives the current owning engine.]
-
setId
public void setId(java.lang.String id)
-
getEngine
public ProcessingEngine getEngine()
- Returns:
- the processing engine currently executing this workflow instance
-
getId
public java.lang.String getId()
- Returns:
- the id of this workflow instance. The id of a workflow instance is at least unique within one processing engine.
-
createCallback
protected <E> Callback<E> createCallback()
Creates aCallback
object used for the asynchronous wait for some reponse.- Type Parameters:
E
- type of response for the new callback- Returns:
- the created callback
-
waitForAll
protected final void waitForAll(java.lang.String... correlationIds) throws Interrupt
waits/sleeps until a response for every correlation id occurs- Parameters:
correlationIds
- one or more correlation ids- Throws:
Interrupt
- for internal use of COPPER. When wait is called, an Interrupt is thrown, caught by COPPER itself for taking back control over the executed workflow.
-
waitForAll
protected final void waitForAll(Callback<?>... callbacks) throws Interrupt
waits/sleeps until a response for every callback occurs- Parameters:
callbacks
- one or more callback objects- Throws:
Interrupt
- for internal use of COPPER. When wait is called, an Interrupt is thrown, caught by COPPER itself for taking back control over the executed workflow.
-
wait
protected final void wait(WaitMode mode, int timeoutMsec, java.lang.String... correlationIds) throws Interrupt
Generic wait/sleep. In case of WaitMode FIRST, it waits until at least one response for the specified correlation ids occurs. In case of WaitMode ALL, it waits until a response for every specified correlation id occurs.- Parameters:
mode
- WaitModetimeoutMsec
- timeout in milliseconds orNO_TIMEOUT
(or any value ≤ 0) to wait for evercorrelationIds
- one ore more correlation ids- Throws:
Interrupt
- for internal use of COPPER. When wait is called, an Interrupt is thrown, caught by COPPER itself for taking back control over the executed workflow.
-
wait
protected final void wait(WaitMode mode, int timeoutMsec, Callback<?>... callbacks) throws Interrupt
- Throws:
Interrupt
-
wait
protected final void wait(WaitMode mode, long timeout, java.util.concurrent.TimeUnit timeUnit, java.lang.String... correlationIds) throws Interrupt
Generic wait/sleep. In case of WaitMode FIRST, it waits until at least one response for the specified correlation ids occurs. In case of WaitMode ALL, it waits until a response for every specified correlation id occurs.- Parameters:
mode
- WaitModetimeout
- timeout orNO_TIMEOUT
(or any value ≤ 0) to wait for evertimeUnit
- unit of the timeout; ignored, if a negative timeout is specifiedcorrelationIds
- one ore more correlation ids- Throws:
Interrupt
- for internal use of COPPER. When wait is called, an Interrupt is thrown, caught by COPPER itself for taking back control over the executed workflow.
-
wait
protected final void wait(WaitMode mode, long timeout, java.util.concurrent.TimeUnit timeUnit, Callback<?>... callbacks) throws Interrupt
Generic wait/sleep. In case of WaitMode FIRST, it waits until at least one response for the specified correlation ids occurs. In case of WaitMode ALL, it waits until a response for every specified correlation id occurs.- Parameters:
mode
- WaitModetimeout
- timeout orNO_TIMEOUT
(or any value ≤ 0) to wait for evertimeUnit
- unit of the timeout; ignored, if a negative timeout is specifiedcallbacks
- one ore more callbacks- Throws:
Interrupt
- for internal use of COPPER. When wait is called, an Interrupt is thrown, caught by COPPER itself for taking back control over the executed workflow.
-
putResponse
public void putResponse(Response<?> r)
Internal use only - called by the processing engine- Parameters:
r
- response to be put into the response map.
-
getAndRemoveResponse
protected <T> Response<T> getAndRemoveResponse(java.lang.String correlationId)
Gets and removes a response for the specified correlation id.Attention: When a workflow instance is resumed after a
Workflow.wait
call, all corresponding responses for this workflow instance in the storage are read. Each of these responses that you do NOT retrieve usinggetAndRemoveResponse(String)
orgetAndRemoveResponses(String)
are discarded when calling the nextWorkflow.wait
- Type Parameters:
T
- type of data holt in the response- Parameters:
correlationId
- correlation id for which response shall be get- Returns:
- the response or null, if no response for the specified correlation id is found
-
getAndRemoveResponses
protected <T> java.util.List<Response<T>> getAndRemoveResponses(java.lang.String correlationId)
Gets and removes all responses for the specified correlation idAttention: When a workflow instance is resumed after a
Workflow.wait
call, all corresponding responses for this workflow instance in the storage are read. Each of these responses that you do NOT retrieve usinggetAndRemoveResponse(String)
orgetAndRemoveResponses(String)
are discarded when calling the nextWorkflow.wait
- Type Parameters:
T
- type of data holt in the response- Parameters:
correlationId
- correlation id for which responses shall be get- Returns:
- the list of responses or an empty list, if no response for the specified correlation id is found
-
getAnyNonTimedOutAndRemoveResponse
protected <T> Response<T> getAnyNonTimedOutAndRemoveResponse(java.lang.Class<? extends T> type)
This method is similar to getAndRemoveResponse but a convenient extension for some usage scenarios. Sometimes, we don't bother which correlation ID is notificated upon but just want one of the notifications. (e.g. we send the same request to multiple services and are happy if one of those answers.) We could also run wait in a timed out loop and handle always those responses only which already got answered (for the next loop iteration we could remove the to be waited correlation ids from the wait list by querying the correlation id via response.getCorrelationID).) In the method, we look up the list of responses and return any of those which didn't run into a timeout (and with the per parameter given type). If multiple correlations didn't run into a timeout, a random one of those is returned and removed. If all given correlation IDs with the given type ran into the timeout, null is returned! This is due to the fact, that we can't find the type of an empty response on runtime and thus not filter accordingly. So if null is returned, you know that the workflow was waken up by timeout. (If all correlation IDs were of the given type) Responses with different types than the specified one are just ignored. Call it likeResponse<MyResponseType> resp = getAnyNonTimedOutAndRemoveResponse(MyResponseType.class);
To get any non timed out response, you can just call the method with Object.class as parameter.- Type Parameters:
T
- type of data holt in the response- Parameters:
type
- type class for which responses shall be get. (e.g. for getting only Response<String>). Might as well beObject.class
to get valid Responses with all types of data.- Returns:
- the response or null, if no fitting response is found
-
main
public abstract void main() throws Interrupt
Entry point for this workflow- Throws:
Interrupt
- for internal use of COPPER. When main is called, an Interrupt may be thrown, caught by COPPER itself for taking back control over the executed workflow.
-
resubmit
protected final void resubmit() throws Interrupt
Causes the engine to stop processing of this workflow instance and to enqueue it again. May be used in case of processor pool change or to create a 'savepoint'.- Throws:
Interrupt
- for internal use of COPPER. After resubmit is called, an Interrupt is thrown, caught by COPPER itself for taking back control over the executed workflow.
-
savepoint
protected final void savepoint() throws Interrupt
Causes the engine to stop processing of this workflow instance and to enqueue it again. May be used in case of processor pool change or to create a 'savepoint' in a persistent engine.Same as
resubmit()
- Throws:
Interrupt
- for internal use of COPPER. After savepoint is called, an Interrupt is thrown, caught by COPPER itself for taking back control over the executed workflow.
-
notify
protected final <T> void notify(Response<T> response)
-
setProcessorPoolId
public void setProcessorPoolId(java.lang.String processorPoolId)
Sets the processor pool id for this workflow instance. Changes get active at the next enqueue of this workflow instance. You may initiate an enqueue usingresubmit()
- Parameters:
processorPoolId
- id of the proccesor pool as specified in the configuration
-
getProcessorPoolId
public java.lang.String getProcessorPoolId()
- Returns:
- the processor pool id
-
get__stack
public java.util.List<StackEntry> get__stack()
For internal usage only- Returns:
- the internally used stack representing the call stack altogether with used local variables.
-
getData
public D getData()
- Returns:
- the data attached to this workflow instance
-
setData
public void setData(D data)
Sets the data for this workflow instance. Typically invoked at construction time of a workflow instance- Parameters:
data
- the new data object for this workflow
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
__beforeProcess
public void __beforeProcess()
-
getCreationTS
public java.util.Date getCreationTS()
- Returns:
- the creation timestamp for this workflow instance.
-
createCheckpointAcknowledge
protected Acknowledge createCheckpointAcknowledge()
We create a quasi empty acknowledge here. It doesn't block at all and has empty callbacks for success and errors.- Returns:
- a BestEffortAcknowledge
-
registerCheckpointAcknowledge
protected void registerCheckpointAcknowledge(Acknowledge ack)
-
registerSavepointAware
protected void registerSavepointAware(SavepointAware sa)
-
prettyPrintData
public java.lang.String prettyPrintData()
-
getLastWaitStackTrace
public java.lang.String getLastWaitStackTrace()
-
getLastActivityTS
public java.util.Date getLastActivityTS()
-
getTimeoutTS
public java.util.Date getTimeoutTS()
-
-