Class 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 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 with WorkflowMethod. 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 with WorkflowMethod. 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 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. 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 class java.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 call start() 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 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
      • 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 interface Suspendable
      • 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