From 7dd79ded06dbe38cfe1c5ef60b602921120d4979 Mon Sep 17 00:00:00 2001 From: salaboy Date: Mon, 16 Jun 2025 14:23:48 +0900 Subject: [PATCH] updating task chaining examples Signed-off-by: salaboy --- .../workflow/java/task-chaining/README.md | 14 +++-------- .../examples/TaskChainingRestController.java | 23 ++++++++++++++++--- .../examples/DaprTestContainersConfig.java | 1 - .../examples/TaskChainingAppTests.java | 12 +++++++--- ....java => TestTaskChainingApplication.java} | 2 +- 5 files changed, 33 insertions(+), 19 deletions(-) rename tutorials/workflow/java/task-chaining/src/test/java/io/dapr/springboot/examples/{TaskChainingApplication.java => TestTaskChainingApplication.java} (95%) diff --git a/tutorials/workflow/java/task-chaining/README.md b/tutorials/workflow/java/task-chaining/README.md index e5eca42a..9cb1d19c 100644 --- a/tutorials/workflow/java/task-chaining/README.md +++ b/tutorials/workflow/java/task-chaining/README.md @@ -48,21 +48,13 @@ graph LR 5. Use the GET request in the [`chaining.http`](./chaining.http) file to get the status of the workflow, or use this cURL command: ```bash - curl --request GET --url http://localhost:/v1.0/workflows/dapr/ + curl --request GET --url http://localhost:8080/output ``` - - Where `` is the workflow instance ID you received in the `Location` header in the previous step. - Where `` can be obtained by looking at the port mappings created by Testcontainers when running the application. - You can find this by running `docker ps` and looking at the port-mappings - ```bash - CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES - bc19a46794e1 daprio/daprd:1.15.4 "./daprd --app-id wo…" 48 seconds ago Up 48 seconds 0.0.0.0:61693->3500/tcp, 0.0.0.0:61694->50001/tcp - ``` - For this example: `61693` which was mapped to the Dapr port `3500` (`0.0.0.0:61693->3500/tcp`). + 6. The expected serialized output of the workflow is: ```txt - "\"This is task chaining\"" + This is task chaining ``` 6. Stop the application by pressing `Ctrl+C`. diff --git a/tutorials/workflow/java/task-chaining/src/main/java/io/dapr/springboot/examples/TaskChainingRestController.java b/tutorials/workflow/java/task-chaining/src/main/java/io/dapr/springboot/examples/TaskChainingRestController.java index 59e9efbb..e5fe9e10 100644 --- a/tutorials/workflow/java/task-chaining/src/main/java/io/dapr/springboot/examples/TaskChainingRestController.java +++ b/tutorials/workflow/java/task-chaining/src/main/java/io/dapr/springboot/examples/TaskChainingRestController.java @@ -17,12 +17,15 @@ package io.dapr.springboot.examples; import io.dapr.spring.workflows.config.EnableDaprWorkflows; import io.dapr.springboot.examples.chain.ChainWorkflow; import io.dapr.workflows.client.DaprWorkflowClient; +import io.dapr.workflows.client.WorkflowInstanceStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.Map; import java.util.concurrent.TimeoutException; @RestController @@ -34,18 +37,32 @@ public class TaskChainingRestController { @Autowired private DaprWorkflowClient daprWorkflowClient; + private String instanceId; /** * Run Chain Demo Workflow - * @return the output of the ChainWorkflow execution + * + * @return the instanceId of the ChainWorkflow execution */ @PostMapping("start") public String chain() throws TimeoutException { - String instanceId = daprWorkflowClient.scheduleNewWorkflow(ChainWorkflow.class, "This"); + instanceId = daprWorkflowClient.scheduleNewWorkflow(ChainWorkflow.class, "This"); return instanceId; } - + /** + * Obtain the output of the workflow + * + * @return the output of the ChainWorkflow execution + */ + @GetMapping("output") + public String output() throws TimeoutException { + WorkflowInstanceStatus instanceState = daprWorkflowClient.getInstanceState(instanceId, true); + if (instanceState != null) { + return instanceState.readOutputAs(String.class); + } + return "N/A"; + } } diff --git a/tutorials/workflow/java/task-chaining/src/test/java/io/dapr/springboot/examples/DaprTestContainersConfig.java b/tutorials/workflow/java/task-chaining/src/test/java/io/dapr/springboot/examples/DaprTestContainersConfig.java index 515b8f83..7ba40037 100644 --- a/tutorials/workflow/java/task-chaining/src/test/java/io/dapr/springboot/examples/DaprTestContainersConfig.java +++ b/tutorials/workflow/java/task-chaining/src/test/java/io/dapr/springboot/examples/DaprTestContainersConfig.java @@ -29,7 +29,6 @@ public class DaprTestContainersConfig { @Bean @ServiceConnection public DaprContainer daprContainer() { - return new DaprContainer(DAPR_RUNTIME_IMAGE_TAG) .withAppName("workflow-patterns-app") .withComponent(new Component("kvstore", "state.in-memory", "v1", Collections.singletonMap("actorStateStore", String.valueOf(true)))) diff --git a/tutorials/workflow/java/task-chaining/src/test/java/io/dapr/springboot/examples/TaskChainingAppTests.java b/tutorials/workflow/java/task-chaining/src/test/java/io/dapr/springboot/examples/TaskChainingAppTests.java index aab02732..753cca4f 100644 --- a/tutorials/workflow/java/task-chaining/src/test/java/io/dapr/springboot/examples/TaskChainingAppTests.java +++ b/tutorials/workflow/java/task-chaining/src/test/java/io/dapr/springboot/examples/TaskChainingAppTests.java @@ -29,7 +29,7 @@ import static io.dapr.springboot.examples.StringMatchesUUIDPattern.matchesThePat import static io.restassured.RestAssured.given; import static org.junit.jupiter.api.Assertions.assertEquals; -@SpringBootTest(classes = {TaskChainingApplication.class, DaprTestContainersConfig.class, +@SpringBootTest(classes = {TestTaskChainingApplication.class, DaprTestContainersConfig.class, DaprAutoConfiguration.class, }, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) class TaskChainingAppTests { @@ -52,13 +52,19 @@ class TaskChainingAppTests { .post("/start") .then() .statusCode(200).body(matchesThePatternOfAUUID()); + + String output = given().contentType(ContentType.JSON) + .when() + .get("/output") + .then() + .statusCode(200).extract().asString(); + + assertEquals("This is task chaining", output); } - } - class StringMatchesUUIDPattern extends TypeSafeMatcher { private static final String UUID_REGEX = "[0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}"; diff --git a/tutorials/workflow/java/task-chaining/src/test/java/io/dapr/springboot/examples/TaskChainingApplication.java b/tutorials/workflow/java/task-chaining/src/test/java/io/dapr/springboot/examples/TestTaskChainingApplication.java similarity index 95% rename from tutorials/workflow/java/task-chaining/src/test/java/io/dapr/springboot/examples/TaskChainingApplication.java rename to tutorials/workflow/java/task-chaining/src/test/java/io/dapr/springboot/examples/TestTaskChainingApplication.java index 369c9c83..5771a89d 100644 --- a/tutorials/workflow/java/task-chaining/src/test/java/io/dapr/springboot/examples/TaskChainingApplication.java +++ b/tutorials/workflow/java/task-chaining/src/test/java/io/dapr/springboot/examples/TestTaskChainingApplication.java @@ -18,7 +18,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication -public class TaskChainingApplication { +public class TestTaskChainingApplication { public static void main(String[] args) {