Class TaskEntity
The Akka runtime registers this entity automatically when a service uses tasks. Application
code normally drives tasks through TaskClient (via
componentClient.forTask(taskId)) rather than by calling this entity directly. Like any entity,
its events can be consumed with a subscribing Consumer, for example to react to task completions.
Command validity is governed by the task's TaskStatus: for example a task can only be
assigned when PENDING, and completion is idempotent once the task is in a terminal state.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordCommand to create a task, capturing the fields of theTaskit was created from.static final recordCommand to reassign an in-progress task, with context for the new assignee.static final recordCommand to reject a completion result, naming theTaskRulethat rejected it.Nested classes/interfaces inherited from class akka.javasdk.eventsourcedentity.EventSourcedEntity
EventSourcedEntity.Effect<T>, EventSourcedEntity.ReadOnlyEffect<T> -
Constructor Summary
ConstructorsConstructorDescriptionTaskEntity(EventSourcedEntityContext context, NotificationPublisher<TaskNotification> notificationPublisher) -
Method Summary
Modifier and TypeMethodDescriptionapplyEvent(TaskEvent event) This is the main event handler method.EventSourcedEntity.Effect<akka.Done> Assign the task to an owner.EventSourcedEntity.Effect<akka.Done> Cancel the task before execution begins and publish aTaskNotification.Cancelled.EventSourcedEntity.Effect<akka.Done> Complete the task with a serialized result and publish aTaskNotification.Completed.EventSourcedEntity.Effect<akka.Done> create(TaskEntity.CreateRequest request) Create the task.Returns the initial empty state object for this entity.EventSourcedEntity.Effect<akka.Done> Fail the task and publish aTaskNotification.Failed.getState()The task's current state.The stream ofTaskNotifications published by this task.EventSourcedEntity.Effect<akka.Done> reassign(TaskEntity.ReassignRequest request) Reassign the task to a new owner.EventSourcedEntity.Effect<akka.Done> Record that aTaskRulerejected the completion result and publish aTaskNotification.ResultRejected.EventSourcedEntity.Effect<akka.Done> start()Mark the task as in progress.Methods inherited from class akka.javasdk.eventsourcedentity.EventSourcedEntity
commandContext, currentState, effects, eventContext, isDeleted
-
Constructor Details
-
TaskEntity
public TaskEntity(EventSourcedEntityContext context, NotificationPublisher<TaskNotification> notificationPublisher)
-
-
Method Details
-
emptyState
Description copied from class:EventSourcedEntityReturns the initial empty state object for this entity. This state is used when the entity is first created and before any events have been persisted and applied.Also known as "zero state" or "neutral state". This method is called when the entity is instantiated for the first time or when recovering from the journal without any persisted events.
The default implementation returns
null. Override this method to provide a more meaningful initial state for your entity.- Overrides:
emptyStatein classEventSourcedEntity<TaskState,TaskEvent> - Returns:
- the initial state object, or
nullif no initial state is needed
-
create
Create the task. Fails if a task with this ID already exists. -
assign
Assign the task to an owner. Only valid whenPENDINGand not already assigned. -
start
Mark the task as in progress. Only valid whenASSIGNED. -
complete
Complete the task with a serialized result and publish aTaskNotification.Completed. Idempotent once the task is in any terminal state. -
rejectResult
Record that aTaskRulerejected the completion result and publish aTaskNotification.ResultRejected. The task moves toRESULT_REJECTEDso the assignee can correct and resubmit. -
fail
Fail the task and publish aTaskNotification.Failed. Idempotent once the task is in any terminal state. -
cancel
Cancel the task before execution begins and publish aTaskNotification.Cancelled. Only valid whenPENDINGorASSIGNED; idempotent once the task is in any terminal state. -
reassign
Reassign the task to a new owner. Only valid whenIN_PROGRESS. -
notifications
The stream ofTaskNotifications published by this task. -
getState
The task's current state. Fails if the task does not exist. -
applyEvent
Description copied from class:EventSourcedEntityThis is the main event handler method. Whenever an event is persisted, this handler will be called. It should return the new state of the entity.Note that this method is called in two situations:
- when one or more events are persisted by the command handler, this method is called to produce the new state of the entity.
- when instantiating an entity from the event journal, this method is called to restore the state of the entity.
Events are required to inherit from a common sealed interface, and it's recommend to implement this method using a switch statement. As such, the compiler can check if all existing events are being handled.
// example of sealed event interface with concrete events implementing it public sealed interface Event { @TypeName("created") public record UserCreated(String name, String email) implements Event {}; @TypeName("email-updated") public record EmailUpdated(String newEmail) implements Event {}; } // example of applyEvent implementation public User applyEvent(Event event) { return switch (event) { case UserCreated userCreated -> new User(userCreated.name, userCreated.email); case EmailUpdated emailUpdated -> this.copy(email = emailUpdated.newEmail); } }- Specified by:
applyEventin classEventSourcedEntity<TaskState,TaskEvent>
-