Begin executing a task and trigger asynchronous command processing
The StartTaskPreparationUseCase initiates the preparation of a task by updating its status and executing the corresponding preparation command asynchronously using Project Reactor.
public void start() { if (this.status != TaskStatus.PENDING) { throw new IllegalStateException("Task must be in PENDING status to start"); } this.status = TaskStatus.IN_PREPARATION; this.startedAt = LocalDateTime.now();}public void complete() { if (this.status != TaskStatus.IN_PREPARATION) { throw new IllegalStateException("Task must be in IN_PREPARATION status to complete"); } this.status = TaskStatus.COMPLETED; this.completedAt = LocalDateTime.now();}
The use case uses Project Reactor for asynchronous command execution. The caller receives an immediate response with the task in IN_PREPARATION status, while the actual command execution happens in the background.
StartTaskPreparationPort startTask = new StartTaskPreparationUseCase( taskRepository, commandFactory, commandExecutor);// Start task #15Task task = startTask.execute(15L);System.out.println("Task status: " + task.getStatus()); // IN_PREPARATIONSystem.out.println("Started at: " + task.getStartedAt()); // 2026-03-06T10:30:15// Task will complete asynchronously in the background// The command executor simulates preparation time based on station type
TaskNotFoundException: Thrown if the provided taskId doesn’t exist in the repository.IllegalStateException: Thrown by task.start() if the task is not in PENDING status (already started or completed).