Class Worker
- java.lang.Object
-
- io.temporal.worker.Worker
-
public final class Worker extends java.lang.Object
Hosts activity and workflow implementations. Uses long poll to receive activity and workflow tasks and processes them in a correspondent thread pool.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description <R> void
addWorkflowImplementationFactory(WorkflowImplementationOptions options, java.lang.Class<R> workflowInterface, Functions.Func<R> factory)
Deprecated.<R> void
addWorkflowImplementationFactory(java.lang.Class<R> workflowInterface, Functions.Func<R> factory)
Deprecated.UseregisterWorkflowImplementationFactory(Class, Func, WorkflowImplementationOptions)
withWorkflowImplementationOptions.newBuilder().setFailWorkflowExceptionTypes(Throwable.class).build()
as a 3rd parameter to preserve the unexpected behavior of this method.java.lang.String
getTaskQueue()
static java.lang.String
getWorkflowType(java.lang.Class<?> workflowInterfaceClass)
Name of the workflow type the interface defines.boolean
isSuspended()
void
registerActivitiesImplementations(java.lang.Object... activityImplementations)
Register activity implementation objects with a worker.<R> void
registerWorkflowImplementationFactory(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
registerWorkflowImplementationFactory(java.lang.Class<R> workflowInterface, Functions.Func<R> factory, WorkflowImplementationOptions options)
Configures a factory to use when an instance of a workflow implementation is created.void
registerWorkflowImplementationTypes(WorkflowImplementationOptions options, java.lang.Class<?>... workflowImplementationClasses)
Registers workflow implementation classes with a worker.void
registerWorkflowImplementationTypes(java.lang.Class<?>... workflowImplementationClasses)
Registers workflow implementation classes with a worker.void
replayWorkflowExecution(WorkflowExecutionHistory history)
This is a utility method to replay a workflow execution using this particular instance of a worker.void
replayWorkflowExecution(java.lang.String jsonSerializedHistory)
This is a utility method to replay a workflow execution using this particular instance of a worker.void
resumePolling()
void
suspendPolling()
java.lang.String
toString()
-
-
-
Method Detail
-
registerWorkflowImplementationTypes
public void registerWorkflowImplementationTypes(java.lang.Class<?>... workflowImplementationClasses)
Registers workflow implementation classes with a worker. Can be called multiple times to add more types. A workflow implementation class must implement at least one interface with a method annotated withWorkflowMethod
. By default, the short name of the interface is used as 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.
Use
DynamicWorkflow
implementation to implement many workflow types dynamically. It can be useful for implementing DSL based workflows. Only a single type that implements DynamicWorkflow can be registered per worker.- Throws:
TypeAlreadyRegisteredException
- if one of the workflow types is already registered
-
registerWorkflowImplementationTypes
public void registerWorkflowImplementationTypes(WorkflowImplementationOptions options, java.lang.Class<?>... workflowImplementationClasses)
Registers workflow implementation classes with a worker. Can be called multiple times to add more types. A workflow implementation class must implement at least one interface with a method annotated withWorkflowMethod
. By default, the short name of the interface is used as 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.
Use
DynamicWorkflow
implementation to implement many workflow types dynamically. It can be useful for implementing DSL based workflows. Only a single type that implements DynamicWorkflow can be registered per worker.- Throws:
TypeAlreadyRegisteredException
- if one of the workflow types is already registered
-
addWorkflowImplementationFactory
@Deprecated public <R> void addWorkflowImplementationFactory(WorkflowImplementationOptions options, java.lang.Class<R> workflowInterface, Functions.Func<R> factory)
Deprecated.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.- Throws:
TypeAlreadyRegisteredException
- if one of the workflow types is already registered
-
addWorkflowImplementationFactory
@Deprecated public <R> void addWorkflowImplementationFactory(java.lang.Class<R> workflowInterface, Functions.Func<R> factory)
Deprecated.UseregisterWorkflowImplementationFactory(Class, Func, WorkflowImplementationOptions)
withWorkflowImplementationOptions.newBuilder().setFailWorkflowExceptionTypes(Throwable.class).build()
as a 3rd parameter to preserve the unexpected behavior of this method.
Or useregisterWorkflowImplementationFactory(Class, Func)
with an expected behavior - Workflow Execution is failed only when aTemporalFailure
subtype is thrown.This method may behave differently from your expectations! This method makes anyThrowable
thrown from a Workflow code to fail the Workflow Execution.By default, only throwing
TemporalFailure
or an exception of class explicitly specified onWorkflowImplementationOptions.Builder.setFailWorkflowExceptionTypes(java.lang.Class<? extends java.lang.Throwable>...)
fails Workflow Execution. Other exceptions fail Workflow Task instead of the whole Workflow Execution. It is designed so that an exception which is not expected by the user, doesn't fail the Workflow Execution. Which allows the user to fix Workflow implementation and then continue the execution from the point it got stuck.This method is misaligned with other workflow implementation registration methods in this aspect.
-
registerWorkflowImplementationFactory
public <R> void registerWorkflowImplementationFactory(java.lang.Class<R> workflowInterface, Functions.Func<R> factory, WorkflowImplementationOptions options)
Configures a factory to use when an instance of a workflow implementation is created.The only valid use for this method is unit testing and mocking.
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[])
.Workflow instantiation and Dependency Injection
This method may look convenient to integrate with dependency injection frameworks and inject into workflow instances. Please note that Dependency Injection into Workflow instances is strongly discouraged. Dependency Injection into Workflow Instances is a direct way to cause changes that are incompatible with the persisted histories and cause NonDeterministicException.To provide an external configuration to a workflow in a deterministic way, use a Local Activity that returns configuration to the workflow. Dependency Injection into Activity instances is allowed. This way, the configuration is persisted into the history and maintained same during replay.
- Type Parameters:
R
- type of the workflow object to create- Parameters:
workflowInterface
- Workflow interface that this factory implementsfactory
- should create a new instance of the workflow implementation object every time it's calledoptions
- custom workflow implementation options for a worker- Throws:
TypeAlreadyRegisteredException
- if one of the workflow types is already registered
-
registerWorkflowImplementationFactory
public <R> void registerWorkflowImplementationFactory(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 and mocking.
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[])
.Workflow instantiation and Dependency Injection
This method may look convenient to integrate with dependency injection frameworks and inject into workflow instances. Please note that Dependency Injection into Workflow instances is strongly discouraged. Dependency Injection into Workflow Instances is a direct way to cause changes that are incompatible with the persisted histories and cause NonDeterministicException.To provide an external configuration to a workflow in a deterministic way, use a Local Activity that returns configuration to the workflow. Dependency Injection into Activity instances is allowed. This way, the configuration is persisted into the history and maintained same during replay.
- Type Parameters:
R
- type of the workflow object to create- Parameters:
workflowInterface
- Workflow interface that this factory implementsfactory
- should create a new instance of the workflow implementation object every time it's called- Throws:
TypeAlreadyRegisteredException
- if one of the workflow types is already registered
-
registerActivitiesImplementations
public void registerActivitiesImplementations(java.lang.Object... activityImplementations)
Register activity implementation objects with a worker. An implementation object can implement one or more activity types.An activity implementation object must implement at least one interface annotated with
ActivityInterface
. Each method of the annotated interface becomes an activity type.Implementations that share a worker must implement different interfaces as an activity type is identified by the activity interface, not by the implementation.
Use an implementation of
DynamicActivity
to register an object that can implement activity types dynamically. A single registration of DynamicActivity implementation per worker is allowed.- Throws:
TypeAlreadyRegisteredException
- if one of the activity types is already registered
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
replayWorkflowExecution
public void replayWorkflowExecution(WorkflowExecutionHistory history) throws java.lang.Exception
This is a utility method to replay a workflow execution using this particular instance of a worker. This method is useful for troubleshooting workflows by running them in a debugger. The workflow implementation type must be already registered with this worker for this method to work.There is no need to call
start()
to be able to call this method
The worker doesn't have to be registered on the same task queue as the execution in the history.
This method shouldn't be a part of normal production usage. It's intended for testing and debugging only.- 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 a 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
-
getTaskQueue
public java.lang.String getTaskQueue()
-
suspendPolling
public void suspendPolling()
-
resumePolling
public void resumePolling()
-
isSuspended
public boolean isSuspended()
-
getWorkflowType
public static java.lang.String getWorkflowType(java.lang.Class<?> workflowInterfaceClass)
Name of the workflow type the interface defines. It is either the interface short name or value ofWorkflowMethod.name()
parameter.- Parameters:
workflowInterfaceClass
- interface annotated with @WorkflowInterface
-
-