Class JpaDatabaseTaskStore

java.lang.Object
org.a2aproject.sdk.extras.taskstore.database.jpa.JpaDatabaseTaskStore
All Implemented Interfaces:
TaskStateProvider, TaskStore

@ApplicationScoped @Alternative @Priority(50) public class JpaDatabaseTaskStore extends Object implements TaskStore, TaskStateProvider
  • Constructor Details

    • JpaDatabaseTaskStore

      public JpaDatabaseTaskStore()
    • JpaDatabaseTaskStore

      @Inject public JpaDatabaseTaskStore(@Any jakarta.enterprise.inject.Instance<TaskAuthorizationProvider> authorizationProviderInstance)
  • Method Details

    • save

      public void save(Task task, boolean isReplicated)
      Description copied from interface: TaskStore
      Saves or updates a task.
      Specified by:
      save in interface TaskStore
      Parameters:
      task - the task to save
      isReplicated - true if this task update came from a replicated event, false if it originated locally. Used to prevent feedback loops in replicated scenarios (e.g., don't fire TaskFinalizedEvent for replicated updates)
    • get

      public Task get(String taskId)
      Description copied from interface: TaskStore
      Retrieves a task by its ID.
      Specified by:
      get in interface TaskStore
      Parameters:
      taskId - the task identifier
      Returns:
      the task if found, null otherwise
    • delete

      public void delete(String taskId)
      Description copied from interface: TaskStore
      Deletes a task by its ID.
      Specified by:
      delete in interface TaskStore
      Parameters:
      taskId - the task identifier
    • isTaskActive

      public boolean isTaskActive(String taskId)
      Determines if a task is considered active for queue management purposes.

      A task is active if:

      • Its state is not final, OR
      • Its state is final but it was finalized within the grace period

      The grace period handles the race condition where events are published to Kafka while a task is active, but consumed on a replica node after the task is finalized.

      Specified by:
      isTaskActive in interface TaskStateProvider
      Parameters:
      taskId - the task ID to check
      Returns:
      true if the task is active (or recently finalized within grace period), false otherwise
    • isTaskFinalized

      public boolean isTaskFinalized(String taskId)
      Determines if a task is in a final state, ignoring the grace period.

      This method performs an immediate check: returns true only if the task is in a final state (COMPLETED, CANCELED, FAILED, etc.), regardless of when it was finalized.

      This method is used by the MainQueue.onClose callback to decide whether to publish the QueueClosedEvent "poison pill". By ignoring the grace period, it ensures that subscribers are terminated immediately when the task is done, providing responsive UX.

      Specified by:
      isTaskFinalized in interface TaskStateProvider
      Parameters:
      taskId - the task ID to check
      Returns:
      true if the task is in a final state (ignoring grace period), false otherwise
    • list

      public ListTasksResult list(ListTasksParams params, @Nullable ServerCallContext context)
      Description copied from interface: TaskStore
      List tasks with optional filtering and pagination.

      Authorization filtering: When a TaskAuthorizationProvider bean is present, implementations must call checkRead for each candidate task and exclude tasks for which the check returns false. The filtering should be applied before pagination so that page sizes are correct from the caller's perspective. If no provider is present, all tasks are returned.

      ⚠ Custom implementation warning: Returning unfiltered results bypasses the authorization model and can leak tasks belonging to other users. Custom implementations must apply per-task checkRead filtering before pagination. The TaskAuthorizationProvider should be declared as a CDI dependency and injected via the constructor or @Inject.

      Specified by:
      list in interface TaskStore
      Parameters:
      params - the filtering and pagination parameters
      context - the server call context (used for authorization filtering)
      Returns:
      the list of tasks matching the criteria with pagination info