Class AbstractWorker<T,V>

java.lang.Object
com.github.workerframework.caf.AbstractWorker<T,V>
Type Parameters:
T - the task class for this Worker
V - the result class for this Worker
All Implemented Interfaces:
Worker

public abstract class AbstractWorker<T,V> extends Object implements Worker
A partial Worker implementation with utility methods.
  • Constructor Details

    • AbstractWorker

      public AbstractWorker(T task, String resultQueue, Codec codec, WorkerTaskData workerTaskData) throws InvalidTaskException
      Create a Worker. The input task will be validated.
      Parameters:
      task - the input task for this Worker to operate on
      resultQueue - the reference to the queue that should take results from this type of Worker. This can be null if no resultQueue is provided for this type of worker
      codec - used to serialising result data
      workerTaskData - The worker task data to use during operation
      Throws:
      InvalidTaskException - if the input task does not validate successfully
  • Method Details

    • getGeneralFailureResult

      public final WorkerResponse getGeneralFailureResult(Throwable t)
      Description copied from interface: Worker
      In case of a Worker's doWork() method failing with an unhandled exception, it is expected a Worker should be able to return a general result.
      Specified by:
      getGeneralFailureResult in interface Worker
      Parameters:
      t - the throwable that caused the unhandled Worker failure
      Returns:
      a response in case of a general unhandled exception failure scenario
    • getTask

      protected final T getTask()
      Returns:
      the task for this Worker to operate on
    • getWorkerTaskData

      protected final WorkerTaskData getWorkerTaskData()
      Returns:
      the task for this Worker to operate on
    • getResultQueue

      protected final String getResultQueue()
      Returns:
      the reference to the queue that should contain the results from this type of Worker
    • getCodec

      protected final Codec getCodec()
      Returns:
      the Codec supplied to the Worker from the framework
    • createSuccessResult

      protected final WorkerResponse createSuccessResult(V result)
      Utility method for creating a WorkerReponse that represents a successful result.
      Parameters:
      result - the result from the Worker
      Returns:
      a WorkerResponse that represents a successful result containing the specified task-specific serialised message
    • createSuccessResult

      protected final WorkerResponse createSuccessResult(V result, byte[] context)
      Utility method for creating a WorkerReponse that represents a successful result with context data.
      Parameters:
      result - the result from the Worker
      context - the context entries to add to the published message
      Returns:
      a WorkerResponse that represents a successful result containing the specified task-specific serialised message
    • createSuccessAndCompleteResponse

      protected final WorkerResponse createSuccessAndCompleteResponse(V result)
      Utility method for creating a WorkerReponse that represents a successful result and reports complete.
      Parameters:
      result - the result from the Worker
      Returns:
      a WorkerResponse that represents a successful result containing the specified task-specific serialised message
    • createSuccessNoOutputToQueue

      protected final WorkerResponse createSuccessNoOutputToQueue()
      Utility method for creating a WorkerResponse that represents a success, but does not send a message to the worker's output message.
      Returns:
      a WorkerResponse that represents a success
    • createTaskCompleteResponse

      protected final WorkerResponse createTaskCompleteResponse()
      Utility method for creating a WorkerResponse that represents a success, but does not send a message to the worker's output message. This method would be used to set target queue and final tracking queue to null.
      Returns:
      a WorkerResponse that represents a success
    • createFailureResult

      protected final WorkerResponse createFailureResult(V result)
      Utility method for creating a WorkerReponse that represents a failed result.
      Parameters:
      result - the result from the Worker
      Returns:
      a WorkerResponse that represents a failed result containing the specified task-specific serialised message
    • createFailureResult

      protected final WorkerResponse createFailureResult(V result, byte[] context)
      Utility method for creating a WorkerReponse that represents a failed result with context data.
      Parameters:
      result - the result from the Worker
      context - the context entries to add to the published message
      Returns:
      a WorkerResponse that represents a failed result containing the specified task-specific serialised message
    • createTaskSubmission

      protected final WorkerResponse createTaskSubmission(String queue, byte[] data, String messageIdentifier, int messageApiVersion)
      Utility method for creating a new task submission to an arbitrary queue. This is for Workers that are chaining jobs to other Workers.
      Parameters:
      queue - the reference of the queue to put the message on
      data - the serialised task-specific message for the Worker to perform the work
      messageIdentifier - the classifier for the task-specific message
      messageApiVersion - the API version for the task-specific message
      Returns:
      a WorkerResponse that represents a new task submission to a specific queue
    • createTaskSubmission

      protected final WorkerResponse createTaskSubmission(String queue, byte[] data, String messageIdentifier, int messageApiVersion, byte[] context)
      Utility method for creating a new task submission to an arbitrary queue with context data. This is for Workers that are chaining jobs to other Workers.
      Parameters:
      queue - the reference of the queue to put the message on
      data - the serialised task-specific message for the Worker to perform the work
      messageIdentifier - the classifier for the task-specific message
      messageApiVersion - the API version for the task-specific message
      context - the context entries to add to the published message
      Returns:
      a WorkerResponse that represents a new task submission to a specific queue
    • checkIfInterrupted

      protected final void checkIfInterrupted() throws InterruptedException
      Utility method to check the interrupted flag of the current Thread and throw InterruptedException if true.
      Throws:
      InterruptedException - if the current Thread is interrupted
    • buildExceptionStackTrace

      protected String buildExceptionStackTrace(Throwable e)
      Builds up a stack trace with one level of cause stack trace
      Parameters:
      e - The exception to build a stack trace from
      Returns:
      Stack trace constructed from exception
    • stackTraceToString

      protected String stackTraceToString(StackTraceElement[] stackTraceElements)