Java Worker Class
What is a Worker?
A service which executes Work - flow and Activities is referred to as a Worker. On user-controlled hosts, workers are defined and put into action. The Worker Factory class enables you to generate and execute as many Workers across as many hosts as your use case requires.
Workers check Task Queues periodically for new Tasks, run sections of code in response to all those Tasks, and then report their findings to the Temporal Server.
Because all communication in between Worker as well as the Temporal Server is handled by the Java SDK behind the scenes, running Workers is a reasonably straightforward process for developers.
How to Start a Worker class?
- Make a new WorkflowClient instance.
- Create WorkerOptions if desired.
- Establish a WorkerFactory instance.
- Use the newWorker method of the newly formed WorkerFactory to generate a Worker.
- Register the workflows and tasks this worker needs to complete.
public static class WorkerClass implements Workflow {
private final EmployeeActivities exp =
Workflow.newActivityStub ( EmployeeActivities.class,
ActivityOptions.newBuilder().setStartToCloseTimeout(Duration.ofSeconds(2)).build() ) ;
@Override
public Employee getEmployee ( String id ) {
return activities . getEmployeeById ( id ) ;
}
}
Our Workflow invokes EmployeeActivities Activities. We can register our Workflow with our Worker:
WorkflowServiceStubs service = WorkflowServiceStubs . newLocalServiceStubs ( ) ;
WorkflowClient client = WorkflowClient . newInstance ( service ) ;
WorkerFactory factory = WorkerFactory . newInstance ( client ) ;
Worker worker = factory . newWorker ( TASK_QUEUE_NAME ) ;
Worker . registerWorkflowImplementationTypes ( EmployeeWorkflowImpl . class ) ;
Remember that no Activities need to be registered in order to run our EmployeeWorkflowImpl Workflow implementation. Only if the Activity implementations are also hosted by the Worker we constructed should we register them as well, for instance by adding:
String URL =
" jdbc:sqlserver://localhost:1433;databaseName=EmployeesDb;user=user;password=pass " ;
w.registerActivitiesImplementations ( new EmployeeActivitiesImpl ( Url ) ) ;
The Workflow Type is registered for workflows. We need to register an Activity instance since Activities are stateless and thread-safe.
A new task is added to the Workflows / Activity Task Queue by the Temporal Server whenever a Workflow is started or when a Workflow needs to execute an Activity. Any Worker polling that Task Queue who is registered for that Workflow or Activity is able to pick up the new task and complete it.
Worker Class
A worker is an object that executes some code in one or more background threads while maintaining access to its state from the main JavaFX application thread and being observable by JavaFX apps. Since Task and Service are the main implementers of this interface, both classes share a common API , which makes it simpler for libraries and frameworks to supply workers that function well when creating user interfaces.
Depending on the implementation, a Worker might or might not be reusable. For instance, a Service can be reused while a Task cannot.
A Worker's life cycle is clearly defined. Each Worker starts out in the Worker. State is ready. It changes to Worker after someone has been scheduled to report for duty. State.SCHEDULED. Even workers who were begun right away and weren't officially scheduled will move through the worker. On its way to the Worker, State.SCHEDULED. RUNNING state is used.
The state will have changed to Worker.State once the Worker has started doing its work.RUNNING. The Worker will end in the Worker.State if it runs smoothly. SUCCEEDED state; the Worker's output is set as the value property. However, if an Exception arises while the Worker is being executed, the state will be changed to Worker.State. FAILED, with the Exception that occurred being set as the exception property.
Modifier | Type | Explanation |
boolean | cancel() | brings this Worker's execution to a close. |
ReadOnlyObjectProperty < Throwable > | exceptionProperty( ) | Retrieves the ReadOnlyObjectProperty for every thrown exception. |
Thowable() | getException ( ) | Indicates, if any, the exception that occurred while the Worker was active. |
String | getMessage ( ) | receives a message related to this Worker's current status. |
double | getProgress ( ) | returns the percent completion status of this Worker's current progress. |
Worker.State getState ( ) | getState ( ) | The current state of this Worker is specified by state getState (). |
String | getTitle ( ) | An optional name for this Worker that should be attached to it. |
double | getTotalWork ( ) | gives the workDoneProperty() property a maximum value. |