Class 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 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 with WorkflowMethod. 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 with WorkflowMethod. 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
      • 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 implements
        factory - should create a new instance of the workflow implementation object every time it's called
        options - 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 implements
        factory - 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 class java.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 call start() 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 of WorkflowMethod.name() parameter.
        Parameters:
        workflowInterfaceClass - interface annotated with @WorkflowInterface