Class EfsActivator
EfsActivator is responsible for starting, enabling,
disabling, and stopping agents
according to a given Workflow.
An activator may be created either programmatically using
EfsActivator.Builder or by
loading a typesafe
configuration from a file.
Please see org.efs.activator package documentation for
a detailed description on how to use EfsActivator.
EfsActivator is thread-safe but can only process a
single workflow at a time. That workflow must be either
run to completion or terminated
prior to initiating
another workflow.
That said, this API does not
preclude the user from building multiple EfsActivator
instances and processing multiple workflows in parallel -
which is thread unsafe. It is recommended that an application
create a single activator and use that to process all
workflows.
- Author:
- Charles W. Rapp
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder class used to create an activator. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringIllegalArgumentExceptionmessage when an invalid agent name is provided is "agent name is either null, an empty string, or blank".static final StringIllegalArgumentExceptionmessage when an invalid workflow name is provided is "workflowName is either null or an empty string".static final StringIllegalStateExceptionmessage when execution is called but there is not workflow in progress (""no workflow in-progress"").static final StringNullPointerExceptionmessage for a null agent is "agent is null".static final StringNullPointerExceptionmessage for a null callback is "callback is null".static final StringStand-alone executed steps are named "standalone-step".static final StringIllegalStateExceptionmessage for an agent not registered with a dispatcher is "agent not registered with dispatcher". -
Method Summary
Modifier and TypeMethodDescriptionagentState(String agentName) Returns named agent's current state.voidagentState(String agentName, EfsAgentState state) Forcibly sets named agent's state.static EfsActivator.Builderbuilder()Returns a newBuilderinstance.voidderegisterListener(IEfsAgent agent) Retracts registered agent from activator state listening.voidexecute(String agentName, EfsAgentState beginState, EfsAgentState endState, Duration transitionTime) Executes a single explicit step on a given agent.booleanExecutes next stage in workflow.booleanExecutes next step in workflow.booleanExecutes all remaining stages and steps in this workflow.voidinitializeWorkflow(String workflowName) Sets workflow to be executed.booleanReturnstrueif there is a workflow activation in progress andfalseotherwise.booleanisRegisteredListener(IEfsAgent agent) Returnstrueif agent is a registered activator listener andfalseotherwise.static EfsActivatorloadActivator(String fileName) Returns activator based on configuration loaded from given file name.voidregisterListener(Consumer<ActivatorEvent> callback, IEfsAgent agent) Registers agent for listening to activator state changes.voidsetWorkflowStage(int stageIndex, int stepIndex) Sets workflow position to given stage and step index.voidTerminates an in-progress workflow.workflow()Returns current, in-progress workflow name.
-
Field Details
-
INVALID_WORKFLOW_NAME
IllegalArgumentExceptionmessage when an invalid workflow name is provided is "workflowName is either null or an empty string".- See Also:
-
NO_WORKFLOW_IN_PROGRESS
IllegalStateExceptionmessage when execution is called but there is not workflow in progress (""no workflow in-progress"").- See Also:
-
NULL_AGENT
NullPointerExceptionmessage for a null agent is "agent is null".- See Also:
-
UNREGISTERED_AGENT
IllegalStateExceptionmessage for an agent not registered with a dispatcher is "agent not registered with dispatcher".- See Also:
-
NULL_CALLBACK
NullPointerExceptionmessage for a null callback is "callback is null".- See Also:
-
STAND_ALONE_STEP
Stand-alone executed steps are named "standalone-step".- See Also:
-
INVALID_AGENT_NAME
IllegalArgumentExceptionmessage when an invalid agent name is provided is "agent name is either null, an empty string, or blank".- See Also:
-
-
Method Details
-
workflow
Returns current, in-progress workflow name. If there is no workflow in progress, then returnsNO_WORKFLOW_IN_PROGRESS.- Returns:
- name of workflow currently in-progress.
-
isInProgress
public boolean isInProgress()Returnstrueif there is a workflow activation in progress andfalseotherwise.- Returns:
trueif a workflow activation is in progress.
-
agentState
Returns named agent's current state.- Parameters:
agentName- agent's unique name.- Returns:
- agent's current state or
nullif there is no activation agent with given agent name. - Throws:
IllegalArgumentException- ifagentNameis eithernull, an empty string, or blank.
-
isRegisteredListener
Returnstrueif agent is a registered activator listener andfalseotherwise.- Parameters:
agent- check if this agent is registered as an activator listener.- Returns:
trueifagentis a registered activator listener.
-
initializeWorkflow
Sets workflow to be executed. This method must be successfully called prior to calling the following workflow execution methods:This activator may not have an in-progress workflow when attempting initialize a workflow. A workflow remains in-progress until either the workflow reaches completion or is
terminated.Note:
execute(String, EfsAgentState, EfsAgentState, Duration)may always be called whether a workflow is in progress or not.- Parameters:
workflowName- set in place this activator workflow.- Throws:
IllegalArgumentException- ifworkflowNameis eithernull, an empty string, or does not reference a known workflow.IllegalStateException- if this activator already has an in-progress workflow.- See Also:
-
setWorkflowStage
public void setWorkflowStage(int stageIndex, int stepIndex) Sets workflow position to given stage and step index.This method forcibly sets workflow to a particular stage, step pair independently of workflow's current position. As such, this method should be used with caution since workflow may be set to an invalid state.
- Parameters:
stageIndex- set current stage to this index.stepIndex- set current step within stage to this index.- Throws:
IllegalStateException- if there is no workflow in-progress.IndexOutOfBoundsException- if eitherstageIndexorstepIndexis out-of-bounds.
-
agentState
Forcibly sets named agent's state. Use this method with care! This method should be used only when an activation step fails but it is determined that named agent is actually in the target state. Using this method indiscriminately will result in activator no longer able to correctly change an agent's state.- Parameters:
agentName- set this agent's state.state- agent's current state.- Throws:
NullPointerException- ifstateisnull.IllegalArgumentException- ifagentNameis eithernull, an empty string, or does not reference a known agent.
-
terminateWorkflow
public void terminateWorkflow()Terminates an in-progress workflow. No further executions are possible exceptexecute(String, EfsAgentState, EfsAgentState, Duration).- See Also:
-
registerListener
Registers agent for listening to activator state changes.ActivatorEvents are delivered to agent via the given callback.Please note that an agent may only have one current registration. If you wish to change the activation event callback for a currently registered listener, then you may register again with a new callback and the previous callback will be quietly replaced. It is not necessary to
de-registerprior to registering the new callback.- Parameters:
callback- agent callback lambda.agent- agent listening to activator changes.- Throws:
NullPointerException- if eithercallbackoragentisnull.IllegalStateException- ifagentis not registered with anEfsDispatcher.- See Also:
-
deregisterListener
Retracts registered agent from activator state listening. Does nothing if agent is not currently registered.- Parameters:
agent- agent listening to activator changes.- Throws:
NullPointerException- ifagentisnull.- See Also:
-
executeNextStep
public boolean executeNextStep()Executes next step in workflow. Returnstrueif workflow is completed after successfully completing step.- Returns:
trueif workflow is completed.- Throws:
IllegalStateException- if there is no activation in progress or the activation fails. Note: if activation step fails, agent remains in its initial state and the step is marked asActivatorEvent.StepState.COMPLETED_FAILED. User may recover the workflow by either:-
judiciously calling
execute(String, EfsAgentState, EfsAgentState, Duration)to put the agent into the proper state, -
calling
setWorkflowStage(int, int)to put the workflow back on track, or -
terminatingworkflow with the understanding that the agent is left in an invalid state.
-
judiciously calling
-
executeNextStage
public boolean executeNextStage()Executes next stage in workflow. Returnstrueif workflow is completed after successfully completing stage.- Returns:
trueif workflow is completed.- Throws:
IllegalStateException- if there is no activation in progress or the activation fails. Note: if activation step fails, agent remains in its initial state and the step is marked asActivatorEvent.StepState.COMPLETED_FAILED. User may recover the workflow by either:-
judiciously calling
execute(String, EfsAgentState, EfsAgentState, Duration)to put the agent into the proper state, -
calling
setWorkflowStage(int, int)to put the workflow back on track, or -
terminatingworkflow with the understanding that the agent is left in an invalid state.
-
judiciously calling
-
executeWorkflow
public boolean executeWorkflow()Executes all remaining stages and steps in this workflow.- Returns:
true.- Throws:
IllegalStateException- if there is no activation in progress or the activation fails. Note: if activation step fails, agent remains in its initial state and the step is marked asActivatorEvent.StepState.COMPLETED_FAILED. User may recover the workflow by either:-
judiciously calling
execute(String, EfsAgentState, EfsAgentState, Duration)to put the agent into the proper state, -
calling
setWorkflowStage(int, int)to put the workflow back on track, or -
terminatingworkflow with the understanding that the agent is left in an invalid state.
-
judiciously calling
-
execute
public void execute(@Nonnull String agentName, @Nonnull EfsAgentState beginState, @Nonnull EfsAgentState endState, @Nonnull Duration transitionTime) Executes a single explicit step on a given agent.This method may throw any number of
RuntimeExceptions either due to the given parameters being invalid (name does not reference a knownIEfsActivateAgent, begin and end states not being adjacent) or the step execution failing. You are advised to use a generalExceptioncatch when calling this method.- Parameters:
agentName- unique agent namebeginState- expected agent current state.endState- target agent state.transitionTime- time limit for state transition.- Throws:
IllegalStateException- if activation fails.
-
builder
Returns a newBuilderinstance.- Returns:
- new
Builderinstance.
-
loadActivator
Returns activator based on configuration loaded from given file name.- Parameters:
fileName- name of file containing efs activator definition.- Returns:
- activator loaded from configuration file.
- Throws:
IllegalArgumentException- if:-
fileNameis a non-existent file, -
fileNameis not a regular file, -
fileNamecannot be read.
-
com.typesafe.config.ConfigException- iffileNamecontain an invalid efs activator configuration.
-