updating task chaining examples

Signed-off-by: salaboy <Salaboy@gmail.com>
This commit is contained in:
salaboy 2025-06-16 14:23:48 +09:00
parent 1c2662bfd2
commit 7dd79ded06
5 changed files with 33 additions and 19 deletions

View File

@ -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:<DAPR_PORT>/v1.0/workflows/dapr/<INSTANCEID>
curl --request GET --url http://localhost:8080/output
```
Where `<INSTANCEID>` is the workflow instance ID you received in the `Location` header in the previous step.
Where `<DAPR_PORT>` 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`.

View File

@ -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";
}
}

View File

@ -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))))

View File

@ -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<String> {
private static final String UUID_REGEX = "[0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}";

View File

@ -18,7 +18,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TaskChainingApplication {
public class TestTaskChainingApplication {
public static void main(String[] args) {