|
| 1 | +# Dapr Spring Boot Workflow Examples |
| 2 | + |
| 3 | +This application allows you to run different workflow patterns including: |
| 4 | +- Chained Activities |
| 5 | +- Parent/Child Workflows |
| 6 | +- Continue workflow by sending External Events |
| 7 | +- Fan Out/In activities for parallel execution |
| 8 | + |
| 9 | +## Running these examples from source code |
| 10 | + |
| 11 | +To run these examples you will need: |
| 12 | +- Java SDK |
| 13 | +- Maven |
| 14 | +- Docker or a container runtime such as Podman |
| 15 | + |
| 16 | +From the `spring-boot-examples/workflows` directory you can start the service using the test configuration that uses |
| 17 | +[Testcontainers](https://testcontainers.com) to boostrap [Dapr](https://dapr.io) by running the following command: |
| 18 | + |
| 19 | +<!-- STEP |
| 20 | +name: Run Demo Workflow Application |
| 21 | +match_order: none |
| 22 | +output_match_mode: substring |
| 23 | +expected_stdout_lines: |
| 24 | +- 'Started WorkflowPatternsApplication' |
| 25 | +background: true |
| 26 | +expected_return_code: 143 |
| 27 | +sleep: 30 |
| 28 | +timeout_seconds: 45 |
| 29 | +--> |
| 30 | +<!-- Timeout for above service must be more than sleep + timeout for the client--> |
| 31 | + |
| 32 | +```sh |
| 33 | +cd workflows/ |
| 34 | +../../mvnw spring-boot:test-run |
| 35 | +``` |
| 36 | + |
| 37 | +<!-- END_STEP --> |
| 38 | + |
| 39 | +Once the application is running you can trigger the different patterns by sending the following requests: |
| 40 | + |
| 41 | +### Chaining Activities Workflow example |
| 42 | + |
| 43 | +The `io.dapr.springboot.examples.wfp.chain.ChainWorkflow` executes three chained activities. For this example the |
| 44 | +`ToUpperCaseActivity.java` is used to transform to upper case three strings from an array. |
| 45 | + |
| 46 | +```mermaid |
| 47 | +graph LR |
| 48 | + SW((Start |
| 49 | + Workflow)) |
| 50 | + A1[Activity1] |
| 51 | + A2[Activity2] |
| 52 | + A3[Activity3] |
| 53 | + EW((End |
| 54 | + Workflow)) |
| 55 | + SW --> A1 |
| 56 | + A1 --> A2 |
| 57 | + A2 --> A3 |
| 58 | + A3 --> EW |
| 59 | +``` |
| 60 | + |
| 61 | +<!-- STEP |
| 62 | +name: Start Customer Workflow |
| 63 | +match_order: none |
| 64 | +output_match_mode: substring |
| 65 | +expected_stdout_lines: |
| 66 | +- 'New Workflow Instance created for Customer' |
| 67 | +background: true |
| 68 | +sleep: 1 |
| 69 | +timeout_seconds: 2 |
| 70 | +--> |
| 71 | +<!-- Timeout for above service must be more than sleep + timeout for the client--> |
| 72 | + |
| 73 | +```sh |
| 74 | +curl -X POST localhost:8080/wfp/chain -H 'Content-Type: application/json' |
| 75 | +``` |
| 76 | + |
| 77 | +<!-- END_STEP --> |
| 78 | + |
| 79 | + |
| 80 | +As result from executing the request you should see: |
| 81 | + |
| 82 | +```bash |
| 83 | +TOKYO, LONDON, SEATTLE |
| 84 | +``` |
| 85 | + |
| 86 | +In the application output you should see the workflow activities being executed. |
| 87 | + |
| 88 | +```bash |
| 89 | +2025-05-16T09:59:22.176+01:00 INFO 8360 --- [pool-3-thread-1] io.dapr.workflows.WorkflowContext : Starting Workflow: io.dapr.springboot.examples.wfp.chain.ChainWorkflow |
| 90 | +2025-05-16T09:59:22.194+01:00 INFO 8360 --- [nio-8080-exec-2] i.d.s.e.w.WorkflowPatternsRestController : Workflow instance 7625b4af-8c04-408a-93dc-bad753466e43 started |
| 91 | +2025-05-16T09:59:22.195+01:00 INFO 8360 --- [pool-3-thread-2] i.d.s.e.wfp.chain.ToUpperCaseActivity : Starting Activity: io.dapr.springboot.examples.wfp.chain.ToUpperCaseActivity |
| 92 | +2025-05-16T09:59:22.196+01:00 INFO 8360 --- [pool-3-thread-2] i.d.s.e.wfp.chain.ToUpperCaseActivity : Message Received from input: Tokyo |
| 93 | +2025-05-16T09:59:22.197+01:00 INFO 8360 --- [pool-3-thread-2] i.d.s.e.wfp.chain.ToUpperCaseActivity : Sending message to output: TOKYO |
| 94 | +2025-05-16T09:59:22.210+01:00 INFO 8360 --- [pool-3-thread-1] i.d.s.e.wfp.chain.ToUpperCaseActivity : Starting Activity: io.dapr.springboot.examples.wfp.chain.ToUpperCaseActivity |
| 95 | +2025-05-16T09:59:22.210+01:00 INFO 8360 --- [pool-3-thread-1] i.d.s.e.wfp.chain.ToUpperCaseActivity : Message Received from input: London |
| 96 | +2025-05-16T09:59:22.210+01:00 INFO 8360 --- [pool-3-thread-1] i.d.s.e.wfp.chain.ToUpperCaseActivity : Sending message to output: LONDON |
| 97 | +2025-05-16T09:59:22.216+01:00 INFO 8360 --- [pool-3-thread-3] i.d.s.e.wfp.chain.ToUpperCaseActivity : Starting Activity: io.dapr.springboot.examples.wfp.chain.ToUpperCaseActivity |
| 98 | +2025-05-16T09:59:22.216+01:00 INFO 8360 --- [pool-3-thread-3] i.d.s.e.wfp.chain.ToUpperCaseActivity : Message Received from input: Seattle |
| 99 | +2025-05-16T09:59:22.216+01:00 INFO 8360 --- [pool-3-thread-3] i.d.s.e.wfp.chain.ToUpperCaseActivity : Sending message to output: SEATTLE |
| 100 | +2025-05-16T09:59:22.219+01:00 INFO 8360 --- [pool-3-thread-1] io.dapr.workflows.WorkflowContext : Workflow finished with result: TOKYO, LONDON, SEATTLE |
| 101 | +``` |
| 102 | + |
| 103 | +### Parent / Child Workflows example |
| 104 | + |
| 105 | + |
| 106 | +```mermaid |
| 107 | +graph LR |
| 108 | + SW((Start |
| 109 | + Workflow)) |
| 110 | + subgraph for each word in the input |
| 111 | + GWL[Call child workflow] |
| 112 | + end |
| 113 | + ALL[Wait until all tasks |
| 114 | + are completed] |
| 115 | + EW((End |
| 116 | + Workflow)) |
| 117 | + SW --> GWL |
| 118 | + GWL --> ALL |
| 119 | + ALL --> EW |
| 120 | +``` |
| 121 | + |
| 122 | + |
| 123 | +## Testing workflow executions |
| 124 | + |
| 125 | +Workflow execution can be tested using Testcontainers and you can find all the tests for the patterns covered in this |
| 126 | +application [here](test/java/io/dapr/springboot/examples/wfp/TestWorkflowPatternsApplication.java). |
0 commit comments