Package com.github.workerframework.caf
Class AbstractWorker<T,V>
java.lang.Object
com.github.workerframework.caf.AbstractWorker<T,V>
- Type Parameters:
T
- the task class for this WorkerV
- the result class for this Worker
- All Implemented Interfaces:
Worker
A partial Worker implementation with utility methods.
-
Constructor Summary
ConstructorsConstructorDescriptionAbstractWorker
(T task, String resultQueue, Codec codec, WorkerTaskData workerTaskData) Create a Worker. -
Method Summary
Modifier and TypeMethodDescriptionprotected String
Builds up a stack trace with one level of cause stack traceprotected final void
Utility method to check the interrupted flag of the current Thread and throw InterruptedException if true.protected final WorkerResponse
createFailureResult
(V result) Utility method for creating a WorkerReponse that represents a failed result.protected final WorkerResponse
createFailureResult
(V result, byte[] context) Utility method for creating a WorkerReponse that represents a failed result with context data.protected final WorkerResponse
createSuccessAndCompleteResponse
(V result) Utility method for creating a WorkerReponse that represents a successful result and reports complete.protected final WorkerResponse
Utility method for creating a WorkerResponse that represents a success, but does not send a message to the worker's output message.protected final WorkerResponse
createSuccessResult
(V result) Utility method for creating a WorkerReponse that represents a successful result.protected final WorkerResponse
createSuccessResult
(V result, byte[] context) Utility method for creating a WorkerReponse that represents a successful result with context data.protected final WorkerResponse
Utility method for creating a WorkerResponse that represents a success, but does not send a message to the worker's output message.protected final WorkerResponse
createTaskSubmission
(String queue, byte[] data, String messageIdentifier, int messageApiVersion) Utility method for creating a new task submission to an arbitrary queue.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.protected final Codec
getCodec()
final WorkerResponse
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.protected final String
protected final T
getTask()
protected final WorkerTaskData
protected String
stackTraceToString
(StackTraceElement[] stackTraceElements) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.github.workerframework.api.Worker
doWork, getPoisonMessageResult, getWorkerApiVersion, getWorkerIdentifier
-
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 onresultQueue
- 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 workercodec
- used to serialising result dataworkerTaskData
- The worker task data to use during operation- Throws:
InvalidTaskException
- if the input task does not validate successfully
-
-
Method Details
-
getGeneralFailureResult
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 interfaceWorker
- Parameters:
t
- the throwable that caused the unhandled Worker failure- Returns:
- a response in case of a general unhandled exception failure scenario
-
getTask
- Returns:
- the task for this Worker to operate on
-
getWorkerTaskData
- Returns:
- the task for this Worker to operate on
-
getResultQueue
- Returns:
- the reference to the queue that should contain the results from this type of Worker
-
getCodec
- Returns:
- the Codec supplied to the Worker from the framework
-
createSuccessResult
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
Utility method for creating a WorkerReponse that represents a successful result with context data.- Parameters:
result
- the result from the Workercontext
- 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
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
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
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
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
Utility method for creating a WorkerReponse that represents a failed result with context data.- Parameters:
result
- the result from the Workercontext
- 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 ondata
- the serialised task-specific message for the Worker to perform the workmessageIdentifier
- the classifier for the task-specific messagemessageApiVersion
- 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 ondata
- the serialised task-specific message for the Worker to perform the workmessageIdentifier
- the classifier for the task-specific messagemessageApiVersion
- the API version for the task-specific messagecontext
- the context entries to add to the published message- Returns:
- a WorkerResponse that represents a new task submission to a specific queue
-
checkIfInterrupted
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
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
-