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.

      • 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.

      • 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 implements
        factory - 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 implements
        factory - 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. 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.

      • 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 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 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