Class Worker
- java.lang.Object
-
- com.uber.cadence.worker.Worker
-
- All Implemented Interfaces:
Suspendable
public final class Worker extends java.lang.Object implements Suspendable
Hosts activity and workflow implementations. Uses long poll to receive activity and decision tasks and processes them in a correspondent thread pool.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <R> void
addWorkflowImplementationFactory(WorkflowImplementationOptions options, java.lang.Class<R> workflowInterface, Functions.Func<R> factory)
Configures a factory to use when an instance of a workflow implementation is created.<R> void
addWorkflowImplementationFactory(java.lang.Class<R> workflowInterface, Functions.Func<R> factory)
Configures a factory to use when an instance of a workflow implementation is created.java.lang.String
getTaskList()
java.util.concurrent.CompletableFuture<java.lang.Boolean>
isHealthy()
Checks if we have a valid connection to the Cadence cluster, and potentially resets the peer listboolean
isSuspended()
void
registerActivitiesImplementations(java.lang.Object... activityImplementations)
Register activity implementation objects with a worker.void
registerWorkflowImplementationTypes(WorkflowImplementationOptions options, java.lang.Class<?>... workflowImplementationClasses)
Register workflow implementation classes with a worker.void
registerWorkflowImplementationTypes(java.lang.Class<?>... workflowImplementationClasses)
Register workflow implementation classes with a worker.void
replayWorkflowExecution(WorkflowExecutionHistory history)
This is an utility method to replay a workflow execution using this particular instance of a worker.void
replayWorkflowExecution(java.lang.String jsonSerializedHistory)
This is an utility method to replay a workflow execution using this particular instance of a worker.void
resumePolling()
Allow new poll requests.void
suspendPolling()
Do not make new poll requests.java.lang.String
toString()
-
-
-
Method Detail
-
registerWorkflowImplementationTypes
public void registerWorkflowImplementationTypes(java.lang.Class<?>... workflowImplementationClasses)
Register workflow implementation classes with a worker. Overwrites previously registered types. A workflow implementation class must implement at least one interface with a method annotated withWorkflowMethod
. That method becomes a workflow type that this worker supports.Implementations that share a worker must implement different interfaces as a workflow type is identified by the workflow interface, not by the implementation.
The reason for registration accepting workflow class, but not the workflow instance is that workflows are stateful and a new instance is created for each workflow execution.
-
registerWorkflowImplementationTypes
public void registerWorkflowImplementationTypes(WorkflowImplementationOptions options, java.lang.Class<?>... workflowImplementationClasses)
Register workflow implementation classes with a worker. Overwrites previously registered types. A workflow implementation class must implement at least one interface with a method annotated withWorkflowMethod
. That method becomes a workflow type that this worker supports.Implementations that share a worker must implement different interfaces as a workflow type is identified by the workflow interface, not by the implementation.
The reason for registration accepting workflow class, but not the workflow instance is that workflows are stateful and a new instance is created for each workflow execution.
-
addWorkflowImplementationFactory
public <R> void addWorkflowImplementationFactory(WorkflowImplementationOptions options, java.lang.Class<R> workflowInterface, Functions.Func<R> factory)
Configures a factory to use when an instance of a workflow implementation is created. !IMPORTANT to provide newly created instances, each time factory is applied.Unless mocking a workflow execution use
registerWorkflowImplementationTypes(Class[])
.- Type Parameters:
R
- type of the workflow object to create.- Parameters:
workflowInterface
- Workflow interface that this factory implementsfactory
- factory that when called creates a new instance of the workflow implementation object.
-
addWorkflowImplementationFactory
public <R> void addWorkflowImplementationFactory(java.lang.Class<R> workflowInterface, Functions.Func<R> factory)
Configures a factory to use when an instance of a workflow implementation is created. The only valid use for this method is unit testing, specifically to instantiate mocks that implement child workflows. An example of mocking a child workflow:worker.addWorkflowImplementationFactory(ChildWorkflow.class, () -> { ChildWorkflow child = mock(ChildWorkflow.class); when(child.workflow(anyString(), anyString())).thenReturn("result1"); return child; });
Unless mocking a workflow execution use
registerWorkflowImplementationTypes(Class[])
.- Type Parameters:
R
- type of the workflow object to create.- Parameters:
workflowInterface
- Workflow interface that this factory implementsfactory
- factory that when called creates a new instance of the workflow implementation object.
-
registerActivitiesImplementations
public void registerActivitiesImplementations(java.lang.Object... activityImplementations)
Register activity implementation objects with a worker. Overwrites previously registered objects. As activities are reentrant and stateless only one instance per activity type is registered.Implementations that share a worker must implement different interfaces as an activity type is identified by the activity interface, not by the implementation.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
replayWorkflowExecution
public void replayWorkflowExecution(WorkflowExecutionHistory history) throws java.lang.Exception
This is an utility method to replay a workflow execution using this particular instance of a worker. This method is useful to troubleshoot workflows by running them in a debugger. To work the workflow implementation type must be registered with this worker. There is no need to callstart()
to be able to call this method.- Parameters:
history
- workflow execution history to replay- Throws:
java.lang.Exception
- if replay failed for any reason
-
replayWorkflowExecution
public void replayWorkflowExecution(java.lang.String jsonSerializedHistory) throws java.lang.Exception
This is an utility method to replay a workflow execution using this particular instance of a worker. This method is useful to troubleshoot workflows by running them in a debugger. To work the workflow implementation type must be registered with this worker. There is no need to callstart()
to be able to call this method.- Parameters:
jsonSerializedHistory
- workflow execution history in JSON format to replay- Throws:
java.lang.Exception
- if replay failed for any reason
-
getTaskList
public java.lang.String getTaskList()
-
suspendPolling
public void suspendPolling()
Description copied from interface:Suspendable
Do not make new poll requests. Outstanding long polls still can return tasks after this method was called.- Specified by:
suspendPolling
in interfaceSuspendable
-
resumePolling
public void resumePolling()
Description copied from interface:Suspendable
Allow new poll requests.- Specified by:
resumePolling
in interfaceSuspendable
-
isSuspended
public boolean isSuspended()
- Specified by:
isSuspended
in interfaceSuspendable
-
isHealthy
public java.util.concurrent.CompletableFuture<java.lang.Boolean> isHealthy()
Checks if we have a valid connection to the Cadence cluster, and potentially resets the peer list
-
-