Interface Worker

All Known Implementing Classes:
AbstractWorker

public interface Worker
A Worker can be constructed in any way as per suits the developer, but should only perform the bare minimum of tasks in the constructor to set itself up to perform the computational work. At some point after construction, the worker-core framework will call through to doWork(), at which point this Worker will be on its own separately managed thread and can start performing useful operations. If the Worker throws an exception from the constructor, this task will be rejected back onto the queue (and eventually it may be dropped, depending upon the WorkerQueue implementation). There are no limits upon time taken for the Worker to perform its task, but it must at some point terminate either via throwing an exception returning from doWork() by returning a WorkerResponse object. The Worker base class has various utility methods for returning a WorkerResponse, such as createSuccessResult, createFailureResult, and createTaskSubmission. Preferably a Worker will always return one of these as opposed to throwing a WorkerException out of the object. Finally, a Worker has methods to classify the type of work it is performing (an "identifier") and another method that returns the integer API version of the task data. These are typically defined in your shareed package that contains the task and result classes, but are used here for constructing a WorkerResponse.
  • Method Summary

    Modifier and Type
    Method
    Description
    Start the work on a task.
    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.
    getPoisonMessageResult(String workerFriendlyName)
    If a message has been identified as a poison message, prepare a WorkerResponse that includes the friendly name of the worker.
    int
    This should return a number that identifies the API version that this worker uses, and should be incremented when the format of the task data (or result data) changes.
     
  • Method Details

    • doWork

      Start the work on a task.
      Returns:
      the result of the worker operation, and appropriate result data
      Throws:
      InterruptedException - indicates that the task is being aborted as requested by the framework
      TaskRejectedException - indicates this Worker wishes to abandon this task and defer its execution
      TaskFailedException - if the Worker fails in an unrecoverable fashion
      InvalidTaskException - if the Worker fails to understand the task to process
    • getWorkerIdentifier

      String getWorkerIdentifier()
      Returns:
      a string to uniquely identify the sort of tasks this worker will do
    • getWorkerApiVersion

      int getWorkerApiVersion()
      This should return a number that identifies the API version that this worker uses, and should be incremented when the format of the task data (or result data) changes. Internal code-logic changes should not affect the API version.
      Returns:
      a numeral that identifies the API version of the worker
    • getGeneralFailureResult

      WorkerResponse getGeneralFailureResult(Throwable t)
      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.
      Parameters:
      t - the throwable that caused the unhandled Worker failure
      Returns:
      a response in case of a general unhandled exception failure scenario
    • getPoisonMessageResult

      default WorkerResponse getPoisonMessageResult(String workerFriendlyName)
      If a message has been identified as a poison message, prepare a WorkerResponse that includes the friendly name of the worker. For compatibility with existing Worker implementations a default implementation has been provided.
      Parameters:
      workerFriendlyName - the worker's friendly name
      Returns:
      a response containing details of the worker that encountered a poison message
      Since:
      6.1.0